Skip to content

Commit

Permalink
Merge pull request #177 from 9oormthon-univ/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
HyunWoo9930 authored Nov 28, 2024
2 parents febeff0 + 50abb14 commit 0638733
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
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
@RequiredArgsConstructor
@RequestMapping("/prepay")
public class PrepayController {

private final PrepayService prepayService;
private final PrepayService prepayService;

@Operation(summary = "์„ ๊ฒฐ์ œ", description = "ํŒ€๊ณผ ๋งค์žฅ ์„ ๊ฒฐ์ œ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.")
@PostMapping
public ResponseCustom<Message> prepay(Authentication authentication,
@RequestBody PrepayRequest prepayRequest) {
return ResponseCustom.OK(prepayService.prepay(AuthenticationParser.parseUserId(authentication), prepayRequest));
}

@Operation(summary = "์„ ๊ฒฐ์ œ", description = "ํŒ€๊ณผ ๋งค์žฅ ์„ ๊ฒฐ์ œ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.")
@PostMapping
public ResponseCustom<Message> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,7 +107,7 @@ public ResponseCustom<StoreGetResponseDTO> getStoreInfo(Authentication authentic

@Operation(summary = "๊ฒฐ์ œ ๊ทธ๋ฃน ์กฐํšŒ", description = "์žฅ๋ถ€ ๊ฒฐ์ œ ๊ทธ๋ฃน์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
@GetMapping("/payment_group")
public ResponseCustom<List<com.jangburich.domain.store.dto.StoreTeamResponse>> getPaymentGroup(Authentication authentication) {
public ResponseCustom<List<StoreTeamResponse>> getPaymentGroup(Authentication authentication) {
return ResponseCustom.OK(
storeService.getPaymentGroup(AuthenticationParser.parseUserId(authentication)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
159 changes: 98 additions & 61 deletions src/main/java/com/jangburich/domain/store/service/PrepayService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<StoreTeam> 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<StoreTeam> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -215,7 +215,7 @@ public StoreGetResponseDTO getStoreInfo(String authentication) {
return new StoreGetResponseDTO().of(store, menus);
}

public List<com.jangburich.domain.store.dto.StoreTeamResponse> getPaymentGroup(String userId) {
public List<StoreTeamResponse> getPaymentGroup(String userId) {
User user = userRepository.findByProviderId(userId)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

Expand Down

0 comments on commit 0638733

Please sign in to comment.