Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 쿠폰 도메인에 메타데이터 필드 추가 #847

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.gdschongik.gdsc.domain.coupon.dao.CouponRepository;
import com.gdschongik.gdsc.domain.coupon.dao.IssuedCouponRepository;
import com.gdschongik.gdsc.domain.coupon.domain.Coupon;
import com.gdschongik.gdsc.domain.coupon.domain.CouponType;
import com.gdschongik.gdsc.domain.coupon.domain.IssuedCoupon;
import com.gdschongik.gdsc.domain.coupon.dto.request.CouponCreateRequest;
import com.gdschongik.gdsc.domain.coupon.dto.request.CouponIssueRequest;
Expand All @@ -16,10 +17,13 @@
import com.gdschongik.gdsc.domain.member.dao.MemberRepository;
import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.study.dao.StudyHistoryRepository;
import com.gdschongik.gdsc.domain.study.dao.StudyRepository;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.domain.study.domain.StudyHistory;
import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.util.MemberUtil;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -36,13 +40,16 @@ public class CouponService {
private final CouponNameUtil couponNameUtil;
private final MemberUtil memberUtil;
private final StudyHistoryRepository studyHistoryRepository;
private final StudyRepository studyRepository;
private final CouponRepository couponRepository;
private final IssuedCouponRepository issuedCouponRepository;
private final MemberRepository memberRepository;

@Transactional
public void createCoupon(CouponCreateRequest request) {
Coupon coupon = Coupon.create(request.name(), Money.from(request.discountAmount()));
Optional<Study> study = Optional.ofNullable(request.studyId()).flatMap(studyRepository::findById);
Coupon coupon = Coupon.createManual(
request.name(), Money.from(request.discountAmount()), request.couponType(), study.orElse(null));
couponRepository.save(coupon);
log.info("[CouponService] 쿠폰 생성: name={}, discountAmount={}", request.name(), request.discountAmount());
}
Expand Down Expand Up @@ -100,11 +107,11 @@ public void createAndIssueCouponByStudyHistories(List<Long> studyHistoryIds) {
.map(studyHistory -> studyHistory.getStudent().getId())
.toList();
List<Member> students = memberRepository.findAllById(studentIds);
Study study = studyHistories.get(0).getStudy();

Comment on lines +110 to 111
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

스터디 히스토리의 일관성 검증이 필요합니다.

코드베이스 분석 결과, 현재 구현에서는 모든 히스토리가 동일한 스터디에서 왔다고 가정하고 있으며, 이를 검증하는 로직이 없습니다. 이는 잘못된 쿠폰 발급으로 이어질 수 있습니다.

다음과 같은 검증 로직 추가를 제안합니다:

Set<Study> uniqueStudies = studyHistories.stream()
    .map(StudyHistory::getStudy)
    .collect(Collectors.toSet());
if (uniqueStudies.size() > 1) {
    throw new CustomException(MULTIPLE_STUDIES_NOT_ALLOWED);
}
Study study = studyHistories.get(0).getStudy();
🔗 Analysis chain

스터디 히스토리의 일관성 검증이 필요합니다.

첫 번째 히스토리에서 스터디 정보를 가져오는 방식은 모든 히스토리가 동일한 스터디에서 왔다고 가정합니다. 이 가정이 깨질 경우 잘못된 쿠폰이 발급될 수 있습니다.

다음과 같은 검증 로직 추가를 제안합니다:

+Set<Study> uniqueStudies = studyHistories.stream()
+    .map(StudyHistory::getStudy)
+    .collect(Collectors.toSet());
+if (uniqueStudies.size() > 1) {
+    throw new CustomException(MULTIPLE_STUDIES_NOT_ALLOWED);
+}
 Study study = studyHistories.get(0).getStudy();
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Search for StudyHistory related patterns
ast-grep --pattern 'List<StudyHistory> $_ = $_'
ast-grep --pattern 'studyHistories.stream().$$$'

# Search for validation patterns
rg "throw\s+new\s+CustomException" -A 2

# Search for Study related validations
rg "Study.*validate" -A 2

Length of output: 35307


Script:

#!/bin/bash
# Check StudyHistoryValidator implementation
ast-grep --pattern 'class StudyHistoryValidator {
  $$$
}'

# Search for any study-related consistency checks
rg "studyHistories.*stream\(\).*collect" -A 2

Length of output: 108


Script:

#!/bin/bash
# Search for any stream operations on studyHistories
rg "studyHistories\." -A 3

# Look for study validation patterns
ast-grep --pattern 'validateStudy($$$)'
ast-grep --pattern 'validate($$$) {
  $$$
  study
  $$$
}'

Length of output: 7962

String couponName = couponNameUtil.generateStudyCompletionCouponName(
studyHistories.get(0).getStudy());
String couponName = couponNameUtil.generateStudyCompletionCouponName(study);
// TODO: 요청할 때마다 새로운 쿠폰 생성되는 문제 수정: 스터디마다 하나의 쿠폰만 존재하도록 쿠폰 타입 및 참조 식별자 추가
Coupon coupon = Coupon.create(couponName, Money.from(5000L));
Coupon coupon = Coupon.createAutomatic(couponName, Money.from(5000L), CouponType.STUDY_COMPLETION, study);
couponRepository.save(coupon);

List<IssuedCoupon> issuedCoupons = students.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

import com.gdschongik.gdsc.domain.common.model.BaseEntity;
import com.gdschongik.gdsc.domain.common.vo.Money;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.global.exception.CustomException;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -31,15 +37,45 @@ public class Coupon extends BaseEntity {
@Embedded
private Money discountAmount;

@Enumerated(EnumType.STRING)
private CouponType couponType;

@Enumerated(EnumType.STRING)
private IssuanceType issuanceType;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "study_id")
private Study study;

@Builder(access = AccessLevel.PRIVATE)
private Coupon(String name, Money discountAmount) {
private Coupon(String name, Money discountAmount, CouponType couponType, IssuanceType issuanceType, Study study) {
this.name = name;
this.discountAmount = discountAmount;
this.couponType = couponType;
this.issuanceType = issuanceType;
this.study = study;
}
Comment on lines +51 to +57
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

도메인 무결성 검증이 필요합니다.

생성자에서 couponTypestudy 간의 관계에 대한 도메인 규칙 검증이 누락되었습니다.

다음과 같은 검증 메소드를 추가해주세요:

private static void validateStudyRequirement(CouponType couponType, Study study) {
    if (couponType == CouponType.STUDY_COMPLETION && study == null) {
        throw new CustomException(STUDY_REQUIRED_FOR_STUDY_COMPLETION);
    }
    if (couponType == CouponType.ADMIN && study != null) {
        throw new CustomException(STUDY_NOT_ALLOWED_FOR_ADMIN);
    }
}


public static Coupon createAutomatic(String name, Money discountAmount, CouponType couponType, Study study) {
validateDiscountAmountPositive(discountAmount);
return Coupon.builder()
.name(name)
.discountAmount(discountAmount)
.couponType(couponType)
.issuanceType(IssuanceType.AUTOMATIC)
.study(study)
.build();
}

public static Coupon create(String name, Money discountAmount) {
public static Coupon createManual(String name, Money discountAmount, CouponType couponType, Study study) {
validateDiscountAmountPositive(discountAmount);
return Coupon.builder().name(name).discountAmount(discountAmount).build();
return Coupon.builder()
.name(name)
.discountAmount(discountAmount)
.couponType(couponType)
.issuanceType(IssuanceType.MANUAL)
.study(study)
.build();
}

// 검증 로직
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.gdschongik.gdsc.domain.coupon.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum CouponType {
ADMIN("어드민"),
STUDY_COMPLETION("스터디 수료");

private final String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.gdschongik.gdsc.domain.coupon.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum IssuanceType {
AUTOMATIC("자동 발급"),
MANUAL("수동 발급");

private final String value;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.gdschongik.gdsc.domain.coupon.dto.request;

import com.gdschongik.gdsc.domain.coupon.domain.CouponType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import java.math.BigDecimal;

public record CouponCreateRequest(@NotBlank String name, @Positive BigDecimal discountAmount) {}
public record CouponCreateRequest(
@NotBlank String name,
@Positive BigDecimal discountAmount,
@NotNull(message = "쿠폰 타입은 null이 될 수 없습니다.") CouponType couponType,
@Nullable @Schema(description = "스터디 관련 쿠폰이 아니라면 null을 가집니다.") Long studyId) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdschongik.gdsc.domain.coupon.application;

import static com.gdschongik.gdsc.domain.coupon.domain.CouponType.*;
import static com.gdschongik.gdsc.global.common.constant.CouponConstant.*;
import static com.gdschongik.gdsc.global.exception.ErrorCode.*;
import static java.math.BigDecimal.*;
Expand Down Expand Up @@ -34,7 +35,7 @@ class 쿠폰_생성할때 {
@Test
void 성공한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);

// when
couponService.createCoupon(request);
Expand All @@ -46,7 +47,7 @@ class 쿠폰_생성할때 {
@Test
void 할인금액이_양수가_아니라면_실패한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ZERO);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ZERO, ADMIN, null);

// when & then
assertThatThrownBy(() -> couponService.createCoupon(request))
Expand All @@ -61,7 +62,7 @@ class 쿠폰_발급할때 {
@Test
void 성공한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);
couponService.createCoupon(request);

createMember();
Expand All @@ -79,7 +80,7 @@ class 쿠폰_발급할때 {
@Test
void 존재하지_않는_유저이면_제외하고_성공한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);
couponService.createCoupon(request);

createMember();
Expand All @@ -97,7 +98,7 @@ class 쿠폰_발급할때 {
@Test
void 존재하지_않는_쿠폰이면_실패한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);
couponService.createCoupon(request);

createMember();
Expand All @@ -118,7 +119,7 @@ class 쿠폰_회수할때 {
@Test
void 성공한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);
couponService.createCoupon(request);

createMember();
Expand All @@ -137,7 +138,7 @@ class 쿠폰_회수할때 {
@Test
void 존재하지_않는_발급쿠폰이면_실패한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);
couponService.createCoupon(request);

createMember();
Expand All @@ -153,7 +154,7 @@ class 쿠폰_회수할때 {
@Test
void 이미_회수한_발급쿠폰이면_실패한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);
couponService.createCoupon(request);

createMember();
Expand All @@ -174,7 +175,7 @@ class 쿠폰_회수할때 {
@Test
void 이미_사용한_발급쿠폰이면_실패한다() {
// given
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE);
CouponCreateRequest request = new CouponCreateRequest(COUPON_NAME, ONE, ADMIN, null);
couponService.createCoupon(request);

createMember();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdschongik.gdsc.domain.coupon.domain;

import static com.gdschongik.gdsc.domain.coupon.domain.CouponType.*;
import static com.gdschongik.gdsc.global.common.constant.CouponConstant.*;
import static com.gdschongik.gdsc.global.exception.ErrorCode.*;
import static java.math.BigDecimal.*;
Expand All @@ -18,7 +19,7 @@ class 쿠폰_생성할때 {
@Test
void 성공한다() {
// when
Coupon coupon = Coupon.create(COUPON_NAME, Money.from(ONE));
Coupon coupon = Coupon.createAutomatic(COUPON_NAME, Money.from(ONE), ADMIN, null);

// then
assertThat(coupon).isNotNull();
Expand All @@ -30,7 +31,7 @@ class 쿠폰_생성할때 {
Money discountAmount = Money.from(ZERO);

// when & then
assertThatThrownBy(() -> Coupon.create(COUPON_NAME, discountAmount))
assertThatThrownBy(() -> Coupon.createAutomatic(COUPON_NAME, discountAmount, ADMIN, null))
.isInstanceOf(CustomException.class)
.hasMessageContaining(COUPON_DISCOUNT_AMOUNT_NOT_POSITIVE.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdschongik.gdsc.domain.coupon.domain;

import static com.gdschongik.gdsc.domain.coupon.domain.CouponType.*;
import static com.gdschongik.gdsc.global.common.constant.CouponConstant.*;
import static com.gdschongik.gdsc.global.common.constant.MemberConstant.*;
import static com.gdschongik.gdsc.global.exception.ErrorCode.*;
Expand All @@ -21,7 +22,7 @@ class 발급쿠폰_사용할때 {
@Test
void 성공하면_사용여부는_true이다() {
// given
Coupon coupon = Coupon.create(COUPON_NAME, Money.from(ONE));
Coupon coupon = Coupon.createAutomatic(COUPON_NAME, Money.from(ONE), ADMIN, null);
Member member = Member.createGuest(OAUTH_ID);
IssuedCoupon issuedCoupon = IssuedCoupon.create(coupon, member);
LocalDateTime now = LocalDateTime.now();
Expand All @@ -36,7 +37,7 @@ class 발급쿠폰_사용할때 {
@Test
void 이미_사용한_쿠폰이면_실패한다() {
// given
Coupon coupon = Coupon.create(COUPON_NAME, Money.from(ONE));
Coupon coupon = Coupon.createAutomatic(COUPON_NAME, Money.from(ONE), ADMIN, null);
Member member = Member.createGuest(OAUTH_ID);
IssuedCoupon issuedCoupon = IssuedCoupon.create(coupon, member);
LocalDateTime now = LocalDateTime.now();
Expand All @@ -51,7 +52,7 @@ class 발급쿠폰_사용할때 {
@Test
void 이미_회수한_쿠폰이면_실패한다() {
// given
Coupon coupon = Coupon.create(COUPON_NAME, Money.from(ONE));
Coupon coupon = Coupon.createAutomatic(COUPON_NAME, Money.from(ONE), ADMIN, null);
Member member = Member.createGuest(OAUTH_ID);
IssuedCoupon issuedCoupon = IssuedCoupon.create(coupon, member);
issuedCoupon.revoke();
Expand All @@ -70,7 +71,7 @@ class 발급쿠폰_회수할때 {
@Test
void 성공하면_회수여부는_true이다() {
// given
Coupon coupon = Coupon.create(COUPON_NAME, Money.from(ONE));
Coupon coupon = Coupon.createAutomatic(COUPON_NAME, Money.from(ONE), ADMIN, null);
Member member = Member.createGuest(OAUTH_ID);
IssuedCoupon issuedCoupon = IssuedCoupon.create(coupon, member);

Expand All @@ -84,7 +85,7 @@ class 발급쿠폰_회수할때 {
@Test
void 이미_회수한_발급쿠폰이면_실패한다() {
// given
Coupon coupon = Coupon.create(COUPON_NAME, Money.from(ONE));
Coupon coupon = Coupon.createAutomatic(COUPON_NAME, Money.from(ONE), ADMIN, null);
Member member = Member.createGuest(OAUTH_ID);
IssuedCoupon issuedCoupon = IssuedCoupon.create(coupon, member);
issuedCoupon.revoke();
Expand All @@ -98,7 +99,7 @@ class 발급쿠폰_회수할때 {
@Test
void 이미_사용한_발급쿠폰이면_실패한다() {
// given
Coupon coupon = Coupon.create(COUPON_NAME, Money.from(ONE));
Coupon coupon = Coupon.createAutomatic(COUPON_NAME, Money.from(ONE), ADMIN, null);
Member member = Member.createGuest(OAUTH_ID);
IssuedCoupon issuedCoupon = IssuedCoupon.create(coupon, member);
issuedCoupon.use(LocalDateTime.now());
Expand Down
Loading
Loading