diff --git a/src/main/java/com/jangburich/domain/store/controller/PrepayController.java b/src/main/java/com/jangburich/domain/store/controller/PrepayController.java index 1cf7787..80248d9 100644 --- a/src/main/java/com/jangburich/domain/store/controller/PrepayController.java +++ b/src/main/java/com/jangburich/domain/store/controller/PrepayController.java @@ -1,19 +1,22 @@ package com.jangburich.domain.store.controller; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + import com.jangburich.domain.store.dto.request.PrepayRequest; import com.jangburich.domain.store.service.PrepayService; import com.jangburich.global.payload.Message; import com.jangburich.global.payload.ResponseCustom; import com.jangburich.utils.parser.AuthenticationParser; + import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; -import org.springframework.security.core.Authentication; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; @Tag(name = "Prepay", description = "Prepay API") @RestController @@ -21,12 +24,22 @@ @RequestMapping("/prepay") public class PrepayController { - private final PrepayService prepayService; + private final PrepayService prepayService; + + @Operation(summary = "선결제", description = "팀과 매장 선결제를 진행합니다.") + @PostMapping + public ResponseCustom prepay(Authentication authentication, + @RequestBody PrepayRequest prepayRequest) { + return ResponseCustom.OK(prepayService.prepay(AuthenticationParser.parseUserId(authentication), prepayRequest)); + } - @Operation(summary = "선결제", description = "팀과 매장 선결제를 진행합니다.") - @PostMapping - public ResponseCustom prepay(Authentication authentication, - @RequestBody PrepayRequest prepayRequest) { - return ResponseCustom.OK(prepayService.prepay(AuthenticationParser.parseUserId(authentication), prepayRequest)); - } + @Operation(summary = "선결제 정보 조회", description = "선결제 진행하기 위한 정보를 조회합니다.") + @GetMapping("") + public ResponseCustom getPrepayInfo(Authentication authentication, + @RequestParam(value = "storeId") Long storeId, + @RequestParam(value = "teamId") Long teamId + ) { + return ResponseCustom.OK( + prepayService.getPrepayInfo(AuthenticationParser.parseUserId(authentication), storeId, teamId)); + } } diff --git a/src/main/java/com/jangburich/domain/store/controller/StoreController.java b/src/main/java/com/jangburich/domain/store/controller/StoreController.java index a8cf894..cc145d1 100644 --- a/src/main/java/com/jangburich/domain/store/controller/StoreController.java +++ b/src/main/java/com/jangburich/domain/store/controller/StoreController.java @@ -21,13 +21,13 @@ import com.jangburich.domain.store.domain.Category; import com.jangburich.domain.store.domain.StoreCreateRequestDTO; import com.jangburich.domain.store.domain.StoreGetResponseDTO; -import com.jangburich.domain.store.domain.StoreTeamResponseDTO; import com.jangburich.domain.store.domain.StoreUpdateRequestDTO; import com.jangburich.domain.store.dto.response.OrdersDetailResponse; import com.jangburich.domain.store.dto.response.OrdersGetResponse; import com.jangburich.domain.store.dto.response.OrdersTodayResponse; import com.jangburich.domain.store.dto.response.PaymentGroupDetailResponse; import com.jangburich.domain.store.dto.response.SearchStoresResponse; +import com.jangburich.domain.store.dto.response.StoreTeamResponse; import com.jangburich.domain.store.service.StoreService; import com.jangburich.global.payload.Message; import com.jangburich.global.payload.ResponseCustom; @@ -107,7 +107,7 @@ public ResponseCustom getStoreInfo(Authentication authentic @Operation(summary = "결제 그룹 조회", description = "장부 결제 그룹을 조회합니다.") @GetMapping("/payment_group") - public ResponseCustom> getPaymentGroup(Authentication authentication) { + public ResponseCustom> getPaymentGroup(Authentication authentication) { return ResponseCustom.OK( storeService.getPaymentGroup(AuthenticationParser.parseUserId(authentication))); } diff --git a/src/main/java/com/jangburich/domain/store/dto/response/PrepaymentInfoResponse.java b/src/main/java/com/jangburich/domain/store/dto/response/PrepaymentInfoResponse.java new file mode 100644 index 0000000..1d16a48 --- /dev/null +++ b/src/main/java/com/jangburich/domain/store/dto/response/PrepaymentInfoResponse.java @@ -0,0 +1,14 @@ +package com.jangburich.domain.store.dto.response; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@RequiredArgsConstructor +public class PrepaymentInfoResponse { + private Long minPrepayAmount; + private Integer wallet; + private Integer remainPrepay; +} diff --git a/src/main/java/com/jangburich/domain/store/dto/StoreTeamResponse.java b/src/main/java/com/jangburich/domain/store/dto/response/StoreTeamResponse.java similarity index 96% rename from src/main/java/com/jangburich/domain/store/dto/StoreTeamResponse.java rename to src/main/java/com/jangburich/domain/store/dto/response/StoreTeamResponse.java index 3fb6ecf..ae711b5 100644 --- a/src/main/java/com/jangburich/domain/store/dto/StoreTeamResponse.java +++ b/src/main/java/com/jangburich/domain/store/dto/response/StoreTeamResponse.java @@ -1,4 +1,4 @@ -package com.jangburich.domain.store.dto; +package com.jangburich.domain.store.dto.response; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; diff --git a/src/main/java/com/jangburich/domain/store/service/PrepayService.java b/src/main/java/com/jangburich/domain/store/service/PrepayService.java index 87e7cbe..f75a0ae 100644 --- a/src/main/java/com/jangburich/domain/store/service/PrepayService.java +++ b/src/main/java/com/jangburich/domain/store/service/PrepayService.java @@ -1,11 +1,18 @@ package com.jangburich.domain.store.service; +import java.time.LocalDate; +import java.util.Optional; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import com.jangburich.domain.point.domain.PointTransaction; import com.jangburich.domain.point.domain.TransactionType; import com.jangburich.domain.point.domain.repository.PointTransactionRepository; import com.jangburich.domain.store.domain.Store; import com.jangburich.domain.store.domain.StoreTeam; import com.jangburich.domain.store.dto.request.PrepayRequest; +import com.jangburich.domain.store.dto.response.PrepaymentInfoResponse; import com.jangburich.domain.store.repository.StoreRepository; import com.jangburich.domain.store.repository.StoreTeamRepository; import com.jangburich.domain.team.domain.Team; @@ -15,72 +22,102 @@ import com.jangburich.global.error.DefaultNullPointerException; import com.jangburich.global.payload.ErrorCode; import com.jangburich.global.payload.Message; -import java.time.LocalDate; -import java.util.Optional; + import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; @Service @Transactional(readOnly = true) @RequiredArgsConstructor public class PrepayService { - private final UserRepository userRepository; - private final StoreRepository storeRepository; - private final TeamRepository teamRepository; - private final StoreTeamRepository storeTeamRepository; - private final PointTransactionRepository pointTransactionRepository; - - @Transactional - public Message prepay(String userId, PrepayRequest prepayRequest) { - User user = userRepository.findByProviderId(userId) - .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); - - Team team = teamRepository.findById(prepayRequest.teamId()) - .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 팀 id 입니다.")); - - Store store = storeRepository.findById(prepayRequest.storeId()) - .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다.")); - - team.validateIsTeamLeader(team.getTeamLeader().getUser_id(), user.getUserId()); - - user.validateHasPointWithPrepayAmount(prepayRequest.prepayAmount(), user.getPoint()); - - user.usePoint(prepayRequest.prepayAmount()); - PointTransaction pointTransaction = PointTransaction - .builder() - .transactionType(TransactionType.PREPAY) - .transactionedPoint(prepayRequest.prepayAmount()) - .user(user) - .store(store) - .build(); - - pointTransactionRepository.save(pointTransaction); - - LocalDate expirationDate = LocalDate.now().plusDays(store.getPrepaymentDuration()); - - StoreTeam buildedStoreTeam = StoreTeam - .builder() - .team(team) - .store(store) - .point(prepayRequest.prepayAmount()) - .personalAllocatedPoint(prepayRequest.personalAllocatedAmount()) - .remainPoint(prepayRequest.prepayAmount()) - .prepaidExpirationDate(expirationDate) - .build(); - - Optional storeAndTeam = storeTeamRepository.findByStoreAndTeam(store, team); - - if (storeAndTeam.isEmpty()) { - storeAndTeam = Optional.of(storeTeamRepository.save(buildedStoreTeam)); - } - - StoreTeam storeTeam = storeAndTeam.get(); - storeTeam.recharge(prepayRequest.prepayAmount()); - - return Message.builder() - .message("매장 선결제가 완료되었습니다.") - .build(); - } + private final UserRepository userRepository; + private final StoreRepository storeRepository; + private final TeamRepository teamRepository; + private final StoreTeamRepository storeTeamRepository; + private final PointTransactionRepository pointTransactionRepository; + + @Transactional + public Message prepay(String userId, PrepayRequest prepayRequest) { + User user = userRepository.findByProviderId(userId) + .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); + + Team team = teamRepository.findById(prepayRequest.teamId()) + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 팀 id 입니다.")); + + Store store = storeRepository.findById(prepayRequest.storeId()) + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다.")); + + team.validateIsTeamLeader(team.getTeamLeader().getUser_id(), user.getUserId()); + + user.validateHasPointWithPrepayAmount(prepayRequest.prepayAmount(), user.getPoint()); + if (!team.getTeamLeader().getUser_id().equals(user.getUserId())) { + return Message.builder() + .message("팀의 리더가 아닌 사람은 선결제를 할 수 없습니다.") + .build(); + } + + if (prepayRequest.prepayAmount() > user.getPoint()) { + return Message.builder() + .message("보유하고 있는 금액이 선결제 하려는 금액보다 적습니다.") + .build(); + } + + user.usePoint(prepayRequest.prepayAmount()); + PointTransaction pointTransaction = PointTransaction + .builder() + .transactionType(TransactionType.PREPAY) + .transactionedPoint(prepayRequest.prepayAmount()) + .user(user) + .store(store) + .build(); + + pointTransactionRepository.save(pointTransaction); + + LocalDate expirationDate = LocalDate.now().plusDays(store.getPrepaymentDuration()); + + StoreTeam buildedStoreTeam = StoreTeam + .builder() + .team(team) + .store(store) + .point(prepayRequest.prepayAmount()) + .personalAllocatedPoint(prepayRequest.personalAllocatedAmount()) + .remainPoint(prepayRequest.prepayAmount()) + .prepaidExpirationDate(expirationDate) + .build(); + + Optional storeAndTeam = storeTeamRepository.findByStoreAndTeam(store, team); + + if (storeAndTeam.isEmpty()) { + storeAndTeam = Optional.of(storeTeamRepository.save(buildedStoreTeam)); + } + + StoreTeam storeTeam = storeAndTeam.get(); + storeTeam.recharge(prepayRequest.prepayAmount()); + + return Message.builder() + .message("매장 선결제가 완료되었습니다.") + .build(); + } + + @Transactional + public PrepaymentInfoResponse getPrepayInfo(String userId, Long storeId, Long teamId) { + User user = userRepository.findByProviderId(userId) + .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION)); + + StoreTeam storeTeam = storeTeamRepository.findByStoreIdAndTeamId(storeId, teamId) + .orElse(null); + + Store store = storeRepository.findById(storeId) + .orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다.")); + + PrepaymentInfoResponse prepaymentInfoResponse = new PrepaymentInfoResponse(); + if (storeTeam == null) { + prepaymentInfoResponse.setRemainPrepay(0); + } else { + prepaymentInfoResponse.setRemainPrepay(storeTeam.getRemainPoint()); + } + prepaymentInfoResponse.setMinPrepayAmount(store.getMinPrepayment()); + prepaymentInfoResponse.setWallet(user.getPoint()); + return prepaymentInfoResponse; + } } diff --git a/src/main/java/com/jangburich/domain/store/service/StoreService.java b/src/main/java/com/jangburich/domain/store/service/StoreService.java index 42273af..cec9028 100644 --- a/src/main/java/com/jangburich/domain/store/service/StoreService.java +++ b/src/main/java/com/jangburich/domain/store/service/StoreService.java @@ -34,7 +34,7 @@ import com.jangburich.domain.store.domain.StoreGetResponseDTO; import com.jangburich.domain.store.domain.StoreTeam; import com.jangburich.domain.store.domain.StoreUpdateRequestDTO; -import com.jangburich.domain.store.dto.StoreTeamResponse; +import com.jangburich.domain.store.dto.response.StoreTeamResponse; import com.jangburich.domain.store.dto.response.OrdersDetailResponse; import com.jangburich.domain.store.dto.response.OrdersGetResponse; import com.jangburich.domain.store.dto.response.OrdersTodayResponse; @@ -215,7 +215,7 @@ public StoreGetResponseDTO getStoreInfo(String authentication) { return new StoreGetResponseDTO().of(store, menus); } - public List getPaymentGroup(String userId) { + public List getPaymentGroup(String userId) { User user = userRepository.findByProviderId(userId) .orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));