Skip to content

Commit

Permalink
[refactor] 일간 쿠폰 리포트 상태 값 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
KwonJuHwan committed Jan 25, 2024
1 parent d8848a0 commit f949b6b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public CouponDailyResponse dailyReport(String jwtPrincipal, Long accommodationId
Long memberId = Long.valueOf(jwtPrincipal);

if (Boolean.TRUE.equals(couponRepository.noRegister(memberId, accommodationId))) {
return CouponDailyResponse.from(1,CouponDailyCondition.NO_REGISTER);
return CouponDailyResponse.from(3,CouponDailyCondition.NO_REGISTER);
}

List<Coupon> expiration3daysCoupons = couponRepository.expiration3days(memberId,
accommodationId);
if (!expiration3daysCoupons.isEmpty()) {
return CouponDailyResponse.from(3, CouponDailyCondition.EXPIRATION_3DAYS,
return CouponDailyResponse.from(1, CouponDailyCondition.EXPIRATION_3DAYS,
expiration3daysCoupons.stream().map(Coupon::getCouponTitle).toList());
}

Expand Down
156 changes: 85 additions & 71 deletions src/main/java/com/coolpeace/domain/coupon/service/CouponService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ public class CouponService {
private final RoomRepository roomRepository;

@Transactional(readOnly = true)
public Page<CouponResponse> searchCoupons(Long memberId, Long accommodationId, SearchCouponParams searchCouponParams, Pageable pageable) {
public Page<CouponResponse> searchCoupons(Long memberId, Long accommodationId,
SearchCouponParams searchCouponParams, Pageable pageable) {
return couponRepository.findAllCoupons(memberId, accommodationId, searchCouponParams,
PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSortOr(Sort.by(Sort.Direction.DESC, "createdAt"))))
.map(CouponResponse::from);
PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),
pageable.getSortOr(Sort.by(Sort.Direction.DESC, "createdAt"))))
.map(CouponResponse::from);
}

@Transactional(readOnly = true)
public CouponCategoryResponse getCouponCategories(Long memberId, Long accommodationId) {
Map<CouponStatusType, Long> counts = couponRepository.countCouponsByCouponStatus(memberId, accommodationId);
Map<CouponStatusType, Long> counts = couponRepository.countCouponsByCouponStatus(memberId,
accommodationId);

long all = counts.values().stream().mapToLong(Long::longValue).sum()
- counts.getOrDefault(CouponStatusType.DELETED, 0L);
- counts.getOrDefault(CouponStatusType.DELETED, 0L);
long exposureOn = counts.getOrDefault(CouponStatusType.EXPOSURE_ON, 0L);
long exposureOff = counts.getOrDefault(CouponStatusType.EXPOSURE_OFF, 0L)
+ counts.getOrDefault(CouponStatusType.EXPOSURE_WAIT, 0L);
+ counts.getOrDefault(CouponStatusType.EXPOSURE_WAIT, 0L);
long expired = counts.getOrDefault(CouponStatusType.EXPOSURE_END, 0L);

return CouponCategoryResponse.from(all, exposureOn, exposureOff, expired);
Expand All @@ -75,83 +77,88 @@ public CouponCategoryResponse getCouponCategories(Long memberId, Long accommodat
public Coupon getCouponByCouponNumber(Long memberId, String couponNumber) {
validateMemberHasCoupon(memberId, couponNumber);
return couponRepository.findByCouponNumber(couponNumber)
.orElseThrow(CouponNotFoundException::new);
.orElseThrow(CouponNotFoundException::new);
}

@Transactional(readOnly = true)
public List<CouponResponse> getRecentHistory(Long memberId) {
return couponRepository.findRecentCouponByMemberId(memberId)
.stream().map(CouponResponse::from).toList();
.stream().map(CouponResponse::from).toList();
}

@Transactional
@CacheEvict(value = "dailyReport", key = "#couponRegisterRequest.accommodationId()", cacheManager = "contentCacheManager")
public void register(Long memberId, CouponRegisterRequest couponRegisterRequest) {
Member storedMember = memberRepository.findById(memberId).orElseThrow(MemberNotFoundException::new);
Accommodation accommodation = accommodationRepository.findById(couponRegisterRequest.accommodationId())
.orElseThrow(AccommodationNotFoundException::new);
Member storedMember = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);
Accommodation accommodation = accommodationRepository.findById(
couponRegisterRequest.accommodationId())
.orElseThrow(AccommodationNotFoundException::new);

List<Room> rooms;
if (!couponRegisterRequest.registerAllRoom()) {
if (CollectionUtils.isEmpty(couponRegisterRequest.registerRooms())) {
throw new RegisterRoomsEmptyException();
}
rooms = couponRegisterRequest.registerRooms()
.stream().map(roomNumber -> roomRepository.findByRoomNumber(roomNumber)
.orElseThrow(RoomNotFoundException::new))
.toList();
.stream().map(roomNumber -> roomRepository.findByRoomNumber(roomNumber)
.orElseThrow(RoomNotFoundException::new))
.toList();
} else {
rooms = Collections.emptyList();
}

DiscountType discountType = ValuedEnum.of(DiscountType.class, couponRegisterRequest.discountType());
DiscountType discountType = ValuedEnum.of(DiscountType.class,
couponRegisterRequest.discountType());
Integer discountValue = switch (discountType) {
case FIXED_RATE -> couponRegisterRequest.discountFlatRate();
case FIXED_PRICE -> couponRegisterRequest.discountFlatValue();
};

CouponRoomType couponRoomTypeValue = getCouponRoomType(
couponRegisterRequest.couponRoomTypes(), CouponRoomType.RENTAL);
CouponRoomType couponRoomStayTypeValue = getCouponRoomStayTypeValue(couponRegisterRequest.couponRoomTypes(),
couponRoomTypeValue, couponRegisterRequest.couponRoomStayMore());
couponRegisterRequest.couponRoomTypes(), CouponRoomType.RENTAL);
CouponRoomType couponRoomStayTypeValue = getCouponRoomStayTypeValue(
couponRegisterRequest.couponRoomTypes(),
couponRoomTypeValue, couponRegisterRequest.couponRoomStayMore());

Coupon savedCoupon = couponRepository.save(Coupon.from(
couponRegisterRequest.title(),
discountType,
discountValue,
couponRegisterRequest.maximumDiscountPrice(),
ValuedEnum.of(CustomerType.class, couponRegisterRequest.customerType()),
couponRoomTypeValue,
couponRoomStayTypeValue,
couponRegisterRequest.minimumReservationPrice(),
ValuedEnum.of(CouponUseDaysType.class, couponRegisterRequest.couponUseConditionDays()),
couponRegisterRequest.exposureStartDate(),
couponRegisterRequest.exposureEndDate(),
accommodation,
rooms,
storedMember
couponRegisterRequest.title(),
discountType,
discountValue,
couponRegisterRequest.maximumDiscountPrice(),
ValuedEnum.of(CustomerType.class, couponRegisterRequest.customerType()),
couponRoomTypeValue,
couponRoomStayTypeValue,
couponRegisterRequest.minimumReservationPrice(),
ValuedEnum.of(CouponUseDaysType.class, couponRegisterRequest.couponUseConditionDays()),
couponRegisterRequest.exposureStartDate(),
couponRegisterRequest.exposureEndDate(),
accommodation,
rooms,
storedMember
));
savedCoupon.generateCouponNumber(CouponIssuerType.OWNER, savedCoupon.getId());
}

@Transactional
public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest couponUpdateRequest) {
public void updateCoupon(Long memberId, String couponNumber,
CouponUpdateRequest couponUpdateRequest) {
validateMemberHasCoupon(memberId, couponNumber);
Coupon storedCoupon = couponRepository.findByCouponNumber(couponNumber)
.orElseThrow(CouponNotFoundException::new);
.orElseThrow(CouponNotFoundException::new);
List<Room> rooms;
if (couponUpdateRequest.registerRooms() != null) {
rooms = couponUpdateRequest.registerRooms()
.stream().map(roomNumber -> roomRepository.findByRoomNumber(roomNumber)
.orElseThrow(RoomNotFoundException::new))
.toList();
.stream().map(roomNumber -> roomRepository.findByRoomNumber(roomNumber)
.orElseThrow(RoomNotFoundException::new))
.toList();
} else {
rooms = Collections.emptyList();
}

DiscountType discountType = Optional.ofNullable(couponUpdateRequest.discountType())
.map(str -> ValuedEnum.of(DiscountType.class, str))
.orElse(null);
.map(str -> ValuedEnum.of(DiscountType.class, str))
.orElse(null);

Integer discountValue = null;
if (discountType != null) {
Expand All @@ -162,58 +169,64 @@ public void updateCoupon(Long memberId, String couponNumber, CouponUpdateRequest
}

CustomerType customerType = Optional.ofNullable(couponUpdateRequest.customerType())
.map(str -> ValuedEnum.of(CustomerType.class, str))
.orElse(null);
.map(str -> ValuedEnum.of(CustomerType.class, str))
.orElse(null);

CouponRoomType couponRoomTypeValue = getCouponRoomType(
couponUpdateRequest.couponRoomTypes(), CouponRoomType.RENTAL);
CouponRoomType couponRoomStayTypeValue = getCouponRoomStayTypeValue(couponUpdateRequest.couponRoomTypes(),
couponRoomTypeValue, couponUpdateRequest.couponRoomStayMore());
couponUpdateRequest.couponRoomTypes(), CouponRoomType.RENTAL);
CouponRoomType couponRoomStayTypeValue = getCouponRoomStayTypeValue(
couponUpdateRequest.couponRoomTypes(),
couponRoomTypeValue, couponUpdateRequest.couponRoomStayMore());

CouponUseDaysType couponUseDays = Optional.ofNullable(couponUpdateRequest.couponUseConditionDays())
.map(str -> ValuedEnum.of(CouponUseDaysType.class, str))
.orElse(null);
CouponUseDaysType couponUseDays = Optional.ofNullable(
couponUpdateRequest.couponUseConditionDays())
.map(str -> ValuedEnum.of(CouponUseDaysType.class, str))
.orElse(null);

storedCoupon.updateCoupon(
discountType,
discountValue,
couponUpdateRequest.maximumDiscountPrice(),
customerType,
couponRoomTypeValue,
couponRoomStayTypeValue,
couponUpdateRequest.minimumReservationPrice(),
couponUseDays,
rooms,
couponUpdateRequest.exposureStartDate(),
couponUpdateRequest.exposureEndDate()
discountType,
discountValue,
couponUpdateRequest.maximumDiscountPrice(),
customerType,
couponRoomTypeValue,
couponRoomStayTypeValue,
couponUpdateRequest.minimumReservationPrice(),
couponUseDays,
rooms,
couponUpdateRequest.exposureStartDate(),
couponUpdateRequest.exposureEndDate()
);
}

private CouponRoomType getCouponRoomStayTypeValue(List<String> couponRoomTypeStrings,
CouponRoomType couponRoomTypeValue,
boolean CouponRoomStayMore) {
if (CouponRoomStayMore && couponRoomTypeValue != null && couponRoomTypeValue.equals(CouponRoomType.LODGE)) {
CouponRoomType couponRoomTypeValue,
boolean CouponRoomStayMore) {
if (CouponRoomStayMore && couponRoomTypeValue != null && couponRoomTypeValue.equals(
CouponRoomType.LODGE)) {
return CouponRoomType.TWO_NIGHT;
}
return getCouponRoomType(couponRoomTypeStrings, CouponRoomType.LODGE);
}

private CouponRoomType getCouponRoomType(List<String> request, CouponRoomType couponRoomType) {
return request.stream()
.map(str -> ValuedEnum.of(CouponRoomType.class, str))
.filter(roomType -> roomType.equals(couponRoomType))
.findFirst().orElse(null);
.map(str -> ValuedEnum.of(CouponRoomType.class, str))
.filter(roomType -> roomType.equals(couponRoomType))
.findFirst().orElse(null);
}

@Transactional
public void exposeCoupon(Long memberId, String couponNumber, CouponExposeRequest couponExposeRequest) {
public void exposeCoupon(Long memberId, String couponNumber,
CouponExposeRequest couponExposeRequest) {
validateMemberHasCoupon(memberId, couponNumber);
Coupon storedCoupon = couponRepository.findByCouponNumber(couponNumber)
.orElseThrow(CouponNotFoundException::new);
.orElseThrow(CouponNotFoundException::new);

// 노출 ON/노출 OFF 외의 요청을 한 경우 예외처리
boolean isONOFFRequest = couponExposeRequest.couponStatus().equals(CouponStatusType.EXPOSURE_ON.getValue())
|| couponExposeRequest.couponStatus().equals(CouponStatusType.EXPOSURE_OFF.getValue());
boolean isONOFFRequest =
couponExposeRequest.couponStatus().equals(CouponStatusType.EXPOSURE_ON.getValue())
|| couponExposeRequest.couponStatus()
.equals(CouponStatusType.EXPOSURE_OFF.getValue());
if (!isONOFFRequest) {
throw new CouponUpdateLimitExposureStateException();
}
Expand All @@ -223,14 +236,15 @@ public void exposeCoupon(Long memberId, String couponNumber, CouponExposeRequest
throw new InvalidCouponStateOutsideExposureDateException();
}

storedCoupon.changeCouponStatus(ValuedEnum.of(CouponStatusType.class, couponExposeRequest.couponStatus()));
storedCoupon.changeCouponStatus(
ValuedEnum.of(CouponStatusType.class, couponExposeRequest.couponStatus()));
}

@Transactional
public void deleteCoupon(Long memberId, String couponNumber) {
validateMemberHasCoupon(memberId, couponNumber);
Coupon storedCoupon = couponRepository.findByCouponNumber(couponNumber)
.orElseThrow(CouponNotFoundException::new);
.orElseThrow(CouponNotFoundException::new);
storedCoupon.changeCouponStatus(CouponStatusType.DELETED);
}

Expand Down

0 comments on commit f949b6b

Please sign in to comment.