Skip to content

Commit

Permalink
feat: 선결제 시 기존 결제 내역과 멱등성 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 committed Nov 23, 2024
1 parent 83c7f69 commit 25be727
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public static StoreTeam create(Team team, Store store, Integer point) {
storeTeam.setPersonalAllocatedPoint(0);
return storeTeam;
}

public void recharge(int prepayAmount) {
this.point += prepayAmount;
this.remainPoint += prepayAmount;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jangburich.domain.store.repository;

import com.jangburich.domain.team.domain.Team;
import java.util.Optional;

import org.springframework.data.domain.Page;
Expand All @@ -14,4 +15,6 @@ public interface StoreTeamRepository extends JpaRepository<StoreTeam, Long> {
Optional<StoreTeam> findByStoreIdAndTeamId(Long store_id, Long team_id);

Page<StoreTeamResponseDTO> findAllByStore(Store store, Pageable pageable);

Optional<StoreTeam> findByStoreAndTeam(Store store, Team team);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.jangburich.global.error.DefaultNullPointerException;
import com.jangburich.global.payload.ErrorCode;
import com.jangburich.global.payload.Message;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -64,7 +65,7 @@ public Message prepay(String userId, PrepayRequest prepayRequest) {

pointTransactionRepository.save(pointTransaction);

StoreTeam storeTeam = StoreTeam
StoreTeam buildedStoreTeam = StoreTeam
.builder()
.team(team)
.store(store)
Expand All @@ -73,7 +74,14 @@ public Message prepay(String userId, PrepayRequest prepayRequest) {
.remainPoint(prepayRequest.prepayAmount())
.build();

storeTeamRepository.save(storeTeam);
Optional<StoreTeam> storeAndTeam = storeTeamRepository.findByStoreAndTeam(store, team);

if (storeAndTeam.isEmpty()) {
storeTeamRepository.save(buildedStoreTeam);
}

StoreTeam storeTeam = storeAndTeam.get();
storeTeam.recharge(prepayRequest.prepayAmount());

return Message.builder()
.message("매장 선결제가 완료되었습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,27 @@ public MyTeamDetailsResponse findMyTeamDetailsAsMember(Long userId, Long teamId)
TransactionType.FOOD_PURCHASE))
.fetch();


return queryFactory
.selectDistinct(new QMyTeamDetailsResponse(
team.id,
team.name,
team.description,
Expressions.constant(30000),
storeTeam.team.id,
storeTeam.team.name,
storeTeam.team.description,
Expressions.constant(-1),
Expressions.constant(30000),
Expressions.constant(30000),
storeTeam.remainPoint,
storeTeam.personalAllocatedPoint,
pointTransaction.transactionedPoint.sum(),
Expressions.constant(prepayedStores),
Expressions.constant(images),
Expressions.constant(images.size()),
Expressions.constant(todayPayments),
Expressions.constant(todayPayments.size())
))
.from(team)
.leftJoin(storeTeam).on(storeTeam.team.id.eq(teamId))
.leftJoin(userTeam).on(userTeam.team.id.eq(teamId))
.where(team.id.eq(teamId))
.from(storeTeam)
.leftJoin(team).on(storeTeam.team.id.eq(teamId))
.leftJoin(userTeam).on(userTeam.team.id.eq(storeTeam.team.id))
.leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE),
pointTransaction.user.userId.eq(userId))
.where(storeTeam.team.id.eq(teamId))
.fetchOne();
}

Expand Down Expand Up @@ -141,23 +142,25 @@ public MyTeamDetailsResponse findMyTeamDetailsAsLeader(Long userId, Long teamId)

return queryFactory
.selectDistinct(new QMyTeamDetailsResponse(
team.id,
team.name,
team.description,
Expressions.constant(30000),
storeTeam.team.id,
storeTeam.team.name,
storeTeam.team.description,
storeTeam.point,
storeTeam.remainPoint,
Expressions.constant(-1),
Expressions.constant(30000),
Expressions.constant(30000),
pointTransaction.transactionedPoint.sum(),
Expressions.constant(prepayedStores),
Expressions.constant(images),
Expressions.constant(images.size()),
Expressions.constant(todayPayments),
Expressions.constant(todayPayments.size())
))
.from(team)
.leftJoin(storeTeam).on(storeTeam.team.id.eq(teamId))
.leftJoin(userTeam).on(userTeam.team.id.eq(teamId))
.where(team.id.eq(teamId))
.from(storeTeam)
.leftJoin(team).on(storeTeam.team.id.eq(teamId))
.leftJoin(userTeam).on(userTeam.team.id.eq(storeTeam.team.id))
.leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE),
pointTransaction.user.userId.eq(userId))
.where(storeTeam.team.id.eq(teamId))
.fetchOne();
}
}

0 comments on commit 25be727

Please sign in to comment.