Skip to content

Commit

Permalink
Fix: paymentRecord rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
nampongo committed Apr 16, 2024
1 parent fd5c2a2 commit 4651a30
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<PaymentRecord, Long> {
@Query("SELECT p FROM PaymentRecord p WHERE p.id.memberId = :memberId")
Page<PaymentRecord> getPaymentRecordByMemberId(@Param("memberId")Long memberId, Pageable pageable);
Page<PaymentRecord> getPaymentRecordByMemberId(Long memberId, Pageable pageable);

Optional<PaymentRecord> findByMemberIdAndMyChallengeIdAndPaymentStatus(Long memberId, Long myChallengeId, PaymentStatus paymentStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
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;
import com.siot.IamportRestClient.request.CancelData;
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
Expand Down Expand Up @@ -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<PaymentRecord> paymentRecords = paymentRecordRepository.findByMemberIdAndMyChallengeIdAndPaymentStatus(memberId, myChallengeId, paymentStatus);
if (paymentRecords.isPresent()) {
throw new PaymentRecordExistException("이미 존재하는 결제 내역입니다.");
}
}

Expand Down

0 comments on commit 4651a30

Please sign in to comment.