Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

토큰 시간 변경, 데이터베이스 조회 메서드 분리 #191

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
@Component
public class JWTProvider {
// access-token expire time = 2 min
chaee813 marked this conversation as resolved.
Show resolved Hide resolved
public final Long ACCESS_TOKEN_EXP = 1000L * 60 * 5;
public final Long ACCESS_TOKEN_EXP = 1000L * 60 * 15;
// refresh-token expire time = 3 min
public final Long REFRESH_TOKEN_EXP = 1000L * 60 * 60 * 24 * 1;
public final Long REFRESH_TOKEN_EXP = 1000L * 60 * 60 * 24 * 3;
public final String TOKEN_PREFIX = "Bearer ";
public final String AUTHORIZATION_HEADER = "Authorization";
public final String REFRESH_HEADER = "Refresh";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public ChatResponse.ChatDTO addChat(User user, ChatRequest.AddChatDTO requestDTO
Long coupleId = user.getId();
Long plannerId = requestDTO.plannerId();

Couple couple = coupleJPARepository.findById(coupleId).orElseThrow(
() -> new NotFoundException(BaseException.USER_NOT_FOUND));
Planner planner = plannerJPARepository.findById(plannerId).orElseThrow(
() -> new NotFoundException(BaseException.PLANNER_NOT_FOUND));
Couple couple = findCoupleById(coupleId);
Planner planner = findPlannerById(plannerId);

Chat chat = chatJPARepository.save(Chat.builder().build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public void likePortfolio(User user, Long portfolioId){
Optional<Favorite> favoriteOptional = favoriteJPARepository.findByUserAndPortfolio(user1.getId(), portfolio.getId());

// 이전에 좋아요 누른 적 있으면 에러 반환
if (favoriteOptional.isPresent()){
throw new BadRequestException(BaseException.FAVORITE_ALREADY_EXISTS);
}
checkFavoritePermission(favoriteOptional);

// 없을 경우 새로운 좋아요 저장
favoriteJPARepository.save(
Expand All @@ -52,9 +50,7 @@ public void likePortfolio(User user, Long portfolioId){
@Transactional
public void unlikePortfolio(User user, Long portfolioId){
// 이전에 좋아요 누른 적 없으면 에러 반환
Favorite favorite = favoriteJPARepository.findByUserAndPortfolio(user.getId(), portfolioId).orElseThrow(
() -> new NotFoundException(BaseException.FAVORITE_NOT_FOUND)
);
Favorite favorite = findFavoriteByUserIdAndPortfolioId(user, portfolioId);
favoriteJPARepository.delete(favorite);
}

Expand Down Expand Up @@ -89,4 +85,16 @@ private User findByUserId(Long userId){
() -> new NotFoundException(BaseException.USER_NOT_FOUND)
);
}

private Favorite findFavoriteByUserIdAndPortfolioId(User user, Long portfolioId) {
return favoriteJPARepository.findByUserAndPortfolio(user.getId(), portfolioId).orElseThrow(
() -> new NotFoundException(BaseException.FAVORITE_NOT_FOUND)
);
}

private static void checkFavoritePermission(Optional<Favorite> favoriteOptional) {
if (favoriteOptional.isPresent()){
throw new BadRequestException(BaseException.FAVORITE_ALREADY_EXISTS);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public Pair<Boolean, Long> addMatch(Couple couple, Planner planner, Chat chat) {

public MatchResponse.FindAllWithNoReviewDTO findMatchesWithNoReview(User user) {

Couple couple = coupleJPARepository.findById(user.getId()).orElseThrow(
() -> new NotFoundException(BaseException.USER_NOT_FOUND)
);
Couple couple = findCoupleByUser(user);

List<Match> matches = matchJPARepository.findAllByCouple(couple);
List<Match> confirmedMatches = findConfirmedMatches(matches);
Expand All @@ -70,19 +68,15 @@ public MatchResponse.FindAllWithNoReviewDTO findMatchesWithNoReview(User user) {
// Match Update : 확정 상태, 가격, 확정 날짜
@Transactional
public void confirm(User user, Long chatId) {
Match match = matchJPARepository.findByChatId(chatId).orElseThrow(
() -> new NotFoundException(BaseException.MATCHING_NOT_FOUND));
Match match = findMatchByChatId(chatId);

// 유저 본인의 채팅방이 맞는지 확인
permissionCheck(user.getDtype(), user.getId(), match);

// 플래너가 1개씩 전부 확정한 후에 예비 부부가 전체 확정 가능
List<Quotation> quotations = quotationJPARepository.findAllByMatch(match);

if (!isAllConfirmed(quotations)) {
// 확정되지 않은 견적서가 있을 때 에러 반환
throw new BadRequestException(BaseException.QUOTATIONS_NOT_ALL_CONFIRMED);
}
checkAllQuotationConfirmed(quotations);

match.updateStatusConfirmed();
match.updateConfirmedPrice(match.getPrice());
Expand All @@ -92,6 +86,13 @@ public void confirm(User user, Long chatId) {
portfolioServiceImpl.updateConfirmedPrices(match.getPlanner());
}

private void checkAllQuotationConfirmed(List<Quotation> quotations) {
if (!isAllConfirmed(quotations)) {
// 확정되지 않은 견적서가 있을 때 에러 반환
throw new BadRequestException(BaseException.QUOTATIONS_NOT_ALL_CONFIRMED);
}
}


private Boolean isAllConfirmed(List<Quotation> quotations) {
if (quotations.isEmpty()) {
Expand All @@ -112,6 +113,17 @@ private void permissionCheck(String role, Long userId, Match match) {
}
}

private Couple findCoupleByUser(User user) {
return coupleJPARepository.findById(user.getId()).orElseThrow(
() -> new NotFoundException(BaseException.USER_NOT_FOUND)
);
}

private Match findMatchByChatId(Long chatId) {
return matchJPARepository.findByChatId(chatId).orElseThrow(
() -> new NotFoundException(BaseException.MATCHING_NOT_FOUND));
}

private List<Match> findConfirmedMatches(List<Match> matches) {
return matches.stream()
.filter(match -> match.getStatus().equals(MatchStatus.CONFIRMED))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public QuotationResponse.FindByUserDTO findQuotationsByUser(User user, int page)
public void confirm(User user, Long chatId, Long quotationId) {
Match match = findMatchByChatIdAndPlannerId(user, chatId);

if (match.getStatus().equals(MatchStatus.CONFIRMED)) {
throw new BadRequestException(BaseException.QUOTATION_ALREADY_CONFIRMED);
}
checkQuotationConfirmed(match);

List<Quotation> quotations = quotationJPARepository.findAllByMatch(match);
Quotation quotation = findQuotationById(quotationId, quotations);
Expand All @@ -108,9 +106,7 @@ public void confirm(User user, Long chatId, Long quotationId) {
public void updateQuotation(User user, Long chatId, Long quotationId, QuotationRequest.Update request) {
Match match = findMatchByChatIdAndPlannerId(user, chatId);

if (match.getStatus().equals(MatchStatus.CONFIRMED)) {
throw new BadRequestException(BaseException.QUOTATION_ALREADY_CONFIRMED);
}
checkQuotationConfirmed(match);

List<Quotation> quotations = quotationJPARepository.findAllByMatch(match);
Quotation quotation = findQuotationById(quotationId, quotations);
Expand Down Expand Up @@ -155,9 +151,7 @@ private Match findMatchByChatIdAndPlannerId(User user, Long chatId) {
// 매칭 ID 로만 조회 후 권한 체크
Match match = findMatchByChatId(chatId);

if (!match.getPlanner().getId().equals(user.getId())) {
throw new ForbiddenException(BaseException.QUOTATION_ACCESS_DENIED);
}
checkQuotationAccessPermission(user, match);

return match;
}
Expand All @@ -169,9 +163,7 @@ private Quotation findQuotationById(Long quotationId, List<Quotation> quotations
.findFirst()
.orElseThrow(() -> new NotFoundException(BaseException.QUOTATION_NOT_FOUND));

if (quotation.getStatus().equals(QuotationStatus.CONFIRMED)) {
throw new BadRequestException(BaseException.QUOTATION_ALREADY_CONFIRMED);
}
checkQuotationConfirmed(quotation);

return quotation;
}
Expand All @@ -193,4 +185,22 @@ private static void checkCoupleExist(Match match) {
throw new ForbiddenException(BaseException.MATCHING_USER_NOT_FOUND);
}
}

private static void checkQuotationConfirmed(Match match) {
if (match.getStatus().equals(MatchStatus.CONFIRMED)) {
throw new BadRequestException(BaseException.QUOTATION_ALREADY_CONFIRMED);
}
}

private static void checkQuotationConfirmed(Quotation quotation) {
if (quotation.getStatus().equals(QuotationStatus.CONFIRMED)) {
throw new BadRequestException(BaseException.QUOTATION_ALREADY_CONFIRMED);
}
}

private static void checkQuotationAccessPermission(User user, Match match) {
if (!match.getPlanner().getId().equals(user.getId())) {
throw new ForbiddenException(BaseException.QUOTATION_ACCESS_DENIED);
}
}
}
Loading