From 4651a3029fc727bc1e5e9081302f4af68b53ffcd Mon Sep 17 00:00:00 2001 From: sdw504512 Date: Tue, 16 Apr 2024 18:08:23 +0900 Subject: [PATCH] Fix: paymentRecord rollback --- .../response/PaymentRecordResponseDto.java | 5 +--- .../paymentRecord/entity/PaymentRecord.java | 29 ++++++++++--------- .../paymentRecord/entity/PaymentRecordId.java | 27 ----------------- .../repository/PaymentRecordRepository.java | 10 ++++--- .../service/PaymentRecordServiceImpl.java | 19 ++++++++---- 5 files changed, 36 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecordId.java diff --git a/src/main/java/com/senity/waved/domain/paymentRecord/dto/response/PaymentRecordResponseDto.java b/src/main/java/com/senity/waved/domain/paymentRecord/dto/response/PaymentRecordResponseDto.java index 9962758..f73c2ce 100644 --- a/src/main/java/com/senity/waved/domain/paymentRecord/dto/response/PaymentRecordResponseDto.java +++ b/src/main/java/com/senity/waved/domain/paymentRecord/dto/response/PaymentRecordResponseDto.java @@ -1,7 +1,6 @@ package com.senity.waved.domain.paymentRecord.dto.response; import com.senity.waved.domain.paymentRecord.entity.PaymentRecord; -import com.senity.waved.domain.paymentRecord.entity.PaymentRecordId; import com.senity.waved.domain.paymentRecord.entity.PaymentStatus; import lombok.AccessLevel; import lombok.Getter; @@ -21,11 +20,9 @@ public class PaymentRecordResponseDto { private ZonedDateTime createDate; public static PaymentRecordResponseDto from(PaymentRecord paymentRecord) { - PaymentRecordId paymentId = paymentRecord.getId(); - return PaymentRecordResponseDto.builder() .groupTitle(paymentRecord.getGroupTitle()) - .status(paymentId.getPaymentStatus()) + .status(paymentRecord.getPaymentStatus()) .deposit(paymentRecord.getDeposit()) .createDate(paymentRecord.getCreateDate()) .build(); diff --git a/src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecord.java b/src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecord.java index ee26c22..a37c600 100644 --- a/src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecord.java +++ b/src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecord.java @@ -1,43 +1,46 @@ package com.senity.waved.domain.paymentRecord.entity; +import com.senity.waved.common.BaseEntity; import com.senity.waved.domain.myChallenge.entity.MyChallenge; import jakarta.persistence.Column; -import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; -import org.hibernate.annotations.CreationTimestamp; - -import java.time.ZonedDateTime; @Entity @Getter @NoArgsConstructor @SuperBuilder(toBuilder = true) -public class PaymentRecord { - - @EmbeddedId - private PaymentRecordId id; +public class PaymentRecord extends BaseEntity { - @CreationTimestamp - @Column(name = "create_date", updatable = false) - private ZonedDateTime createDate; + @Column(name="payment_status") + @Enumerated(EnumType.STRING) + private PaymentStatus paymentStatus; @Column(name = "deposit") private Long deposit; + @Column(name = "member_id") + private Long memberId; + @Column(name = "group_title") private String groupTitle; + @Column(name = "my_challenge_id") + private Long myChallengeId; + public static PaymentRecord of(PaymentStatus status, Long memberId, MyChallenge myChallenge, String groupTitle) { Long deposit = status.equals(PaymentStatus.APPLIED) ? myChallenge.getDeposit() * (-1) : status.equals(PaymentStatus.FAIL) ? 0 : myChallenge.getDeposit(); - PaymentRecordId paymentId = new PaymentRecordId(memberId, myChallenge.getId(), status); return PaymentRecord.builder() .deposit(deposit) - .id(paymentId) + .paymentStatus(status) + .memberId(memberId) + .myChallengeId(myChallenge.getId()) .groupTitle(groupTitle) .build(); } diff --git a/src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecordId.java b/src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecordId.java deleted file mode 100644 index 6bc275c..0000000 --- a/src/main/java/com/senity/waved/domain/paymentRecord/entity/PaymentRecordId.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.senity.waved.domain.paymentRecord.entity; - -import jakarta.persistence.Embeddable; -import jakarta.persistence.EnumType; -import jakarta.persistence.Enumerated; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.io.Serializable; - - -@Embeddable -@Data -@AllArgsConstructor -@NoArgsConstructor -public class PaymentRecordId implements Serializable { - - private Long memberId; - private Long myChallengeId; - @Enumerated(EnumType.STRING) - private PaymentStatus paymentStatus; - - public Long getMemberId() { - return memberId; - } -} diff --git a/src/main/java/com/senity/waved/domain/paymentRecord/repository/PaymentRecordRepository.java b/src/main/java/com/senity/waved/domain/paymentRecord/repository/PaymentRecordRepository.java index 039a3cf..5e6ea78 100644 --- a/src/main/java/com/senity/waved/domain/paymentRecord/repository/PaymentRecordRepository.java +++ b/src/main/java/com/senity/waved/domain/paymentRecord/repository/PaymentRecordRepository.java @@ -1,13 +1,15 @@ package com.senity.waved.domain.paymentRecord.repository; import com.senity.waved.domain.paymentRecord.entity.PaymentRecord; +import com.senity.waved.domain.paymentRecord.entity.PaymentStatus; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; + +import java.util.Optional; public interface PaymentRecordRepository extends JpaRepository { - @Query("SELECT p FROM PaymentRecord p WHERE p.id.memberId = :memberId") - Page getPaymentRecordByMemberId(@Param("memberId")Long memberId, Pageable pageable); + Page getPaymentRecordByMemberId(Long memberId, Pageable pageable); + + Optional findByMemberIdAndMyChallengeIdAndPaymentStatus(Long memberId, Long myChallengeId, PaymentStatus paymentStatus); } diff --git a/src/main/java/com/senity/waved/domain/paymentRecord/service/PaymentRecordServiceImpl.java b/src/main/java/com/senity/waved/domain/paymentRecord/service/PaymentRecordServiceImpl.java index 7e69440..57e6569 100644 --- a/src/main/java/com/senity/waved/domain/paymentRecord/service/PaymentRecordServiceImpl.java +++ b/src/main/java/com/senity/waved/domain/paymentRecord/service/PaymentRecordServiceImpl.java @@ -13,6 +13,7 @@ import com.senity.waved.domain.paymentRecord.entity.PaymentStatus; import com.senity.waved.domain.paymentRecord.exception.DepositAmountNotMatchException; import com.senity.waved.domain.paymentRecord.exception.MemberAndMyChallengeNotMatchException; +import com.senity.waved.domain.paymentRecord.exception.PaymentRecordExistException; import com.senity.waved.domain.paymentRecord.repository.PaymentRecordRepository; import com.siot.IamportRestClient.IamportClient; import com.siot.IamportRestClient.exception.IamportResponseException; @@ -20,10 +21,12 @@ import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.time.temporal.ChronoUnit; +import java.util.Optional; @Slf4j @@ -93,17 +96,21 @@ public String checkDepositRefundedOrNot(String email, Long myChallengeId) { return message; } + @Transactional(isolation = Isolation.SERIALIZABLE) public void savePaymentRecord(MyChallenge myChallenge, Long memberId, PaymentStatus status) { + checkIfPaymentRecordExist(memberId, myChallenge.getId(), status); ChallengeGroup group = getGroupById(myChallenge.getChallengeGroupId()); String groupTitle = group.getGroupTitle(); updateGroupParticipantCount(group, status); - try { - PaymentRecord paymentRecord = PaymentRecord.of(status, memberId, myChallenge, groupTitle); - // paymentRecordRepository.save(paymentRecord); - } catch (Exception e) { - // throw new PaymentRecordExistException("이미 존재하는 예치금 내역입니다."); - log.error("------------------- sql exception"); + PaymentRecord paymentRecord = PaymentRecord.of(status, memberId, myChallenge, groupTitle); + paymentRecordRepository.save(paymentRecord); + } + + private void checkIfPaymentRecordExist(Long memberId, Long myChallengeId, PaymentStatus paymentStatus) { + Optional paymentRecords = paymentRecordRepository.findByMemberIdAndMyChallengeIdAndPaymentStatus(memberId, myChallengeId, paymentStatus); + if (paymentRecords.isPresent()) { + throw new PaymentRecordExistException("이미 존재하는 결제 내역입니다."); } }