Skip to content

Commit

Permalink
Merge pull request #225 from readyvery/test
Browse files Browse the repository at this point in the history
3/25 배포 최종
  • Loading branch information
marinesnow34 authored Mar 25, 2024
2 parents b802174 + e3dfc82 commit 3f1555c
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class JwtConfig {
private final String refreshTokenName;
private final String userFrontendUrl;
private final String guestFrontendUrl;
private final String cookieDomain;
private final Algorithm algorithm;

public static final String ACCESS_TOKEN_SUBJECT = "AccessToken";
Expand All @@ -35,8 +34,8 @@ public JwtConfig(
@Value("${jwt.access.cookie}") String accessTokenName,
@Value("${jwt.refresh.cookie}") String refreshTokenName,
@Value("${jwt.redirect-uri-user}") String userFrontendUrl,
@Value("${jwt.redirect-uri-guest}") String guestFrontendUrl,
@Value("${jwt.cookie.domain}") String cookieDomain
@Value("${jwt.redirect-uri-guest}") String guestFrontendUrl

) {
this.secretKey = secretKey;
this.accessTokenExpirationPeriod = accessTokenExpirationPeriod;
Expand All @@ -45,7 +44,6 @@ public JwtConfig(
this.refreshTokenName = refreshTokenName;
this.userFrontendUrl = userFrontendUrl;
this.guestFrontendUrl = guestFrontendUrl;
this.cookieDomain = cookieDomain;
this.algorithm = initializeAlgorithm(secretKey);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/readyvery/readyverydemo/domain/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@Builder
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.readyvery.readyverydemo.domain.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.readyvery.readyverydemo.domain.Order;
import com.readyvery.readyverydemo.domain.Point;
import com.readyvery.readyverydemo.domain.UserInfo;

public interface PointRepository extends JpaRepository<Point, Long> {
List<Point> findAllByUserInfo(UserInfo userInfo);

Optional<Point> findByOrder(Order order);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;

import com.readyvery.readyverydemo.domain.SocialType;
import com.readyvery.readyverydemo.domain.UserInfo;

import jakarta.persistence.LockModeType;

public interface UserRepository extends JpaRepository<UserInfo, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<UserInfo> findUserInfoById(Long id);

Optional<UserInfo> findByEmail(String email);

Optional<UserInfo> findByRefreshToken(String refreshToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum ExceptionCode {
POINT_NOT_ENOUGH(400, "Point is not enough."),
INVALID_INPUT(400, "Invalid input."),
UNAUTHORIZED(400, "Already User"),
AUTH_ERROR(403, "Auth Error");
AUTH_ERROR(403, "Auth Error"),
POINT_NOT_FOUND(403, "Point not found");

private int status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public void addTokenCookie(HttpServletResponse response, String name, String val
Cookie cookie = new Cookie(name, value);
cookie.setHttpOnly(httpOnly);
cookie.setPath(path);
//cookie.setDomain(jwtConfig.getCookieDomain());
cookie.setMaxAge(maxAge);
response.addCookie(cookie);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public BoardRes toBoardRes(List<Store> stores) {
throw new BusinessLogicException(ExceptionCode.STORE_NOT_FOUND);
}
return BoardRes.builder()
.stores(stores.stream().map(this::toStoreDto).toList())
.stores(stores.stream().filter(store -> !store.isDeleted()).map(this::toStoreDto).toList())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
import com.readyvery.readyverydemo.src.order.dto.TossCancelReq;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentDto;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentMakeRes;
import com.readyvery.readyverydemo.src.point.PointServiceFacade;
import com.readyvery.readyverydemo.src.user.UserServiceFacade;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand All @@ -82,13 +84,15 @@ public class OrderServiceImpl implements OrderService {
private final FoodieRepository foodieRepository;
private final FoodieOptionRepository foodieOptionRepository;
private final UserRepository userRepository;
private final UserServiceFacade userServiceFacade;
private final StoreRepository storeRepository;
private final OrderMapper orderMapper;
private final TossPaymentConfig tosspaymentConfig;
private final OrdersRepository ordersRepository;
private final ReceiptRepository receiptRepository;
private final CouponRepository couponRepository;
private final PointRepository pointRepository;
private final PointServiceFacade pointServiceFacade;

@Override
public FoodyDetailRes getFoody(Long storeId, Long foodyId, Long inout) {
Expand Down Expand Up @@ -307,7 +311,9 @@ private void cartOrder(Cart cart) {
@Transactional
public PaySuccess tossPaymentSuccess(String paymentKey, String orderId, Long amount) {
Order order = getOrder(orderId);
UserInfo user = userServiceFacade.getUserInfoWithPessimisticLock(order.getUserInfo().getId());
verifyOrder(order, amount);
verifyUsePoint(order, user);

TosspaymentDto tosspaymentDto;
if (amount > 0) {
Expand All @@ -328,10 +334,23 @@ public PaySuccess tossPaymentSuccess(String paymentKey, String orderId, Long amo
if (order.getPoint() < 0L) {
Point point = orderMapper.orderToPoint(order);
pointRepository.save(point);
userServiceFacade.saveUserPoint(user, user.getPoint() + order.getPoint());
}
return orderMapper.tosspaymentDtoToPaySuccess(TOSSPAYMENT_SUCCESS_MESSAGE);
}

private void verifyUsePoint(Order order, UserInfo user) {
// 포인트 사용 X
if (order.getPoint() >= 0L) {
return;
}
// 유저 포인트 충분
if (user.getPoint() + order.getPoint() >= 0L) {
return;
}
throw new BusinessLogicException(ExceptionCode.POINT_NOT_ENOUGH);
}

private TosspaymentDto makeZeroPaymentDto(String paymentKey) {
return TosspaymentDto.builder()
.paymentKey(paymentKey)
Expand Down Expand Up @@ -365,18 +384,33 @@ public CurrentRes getCurrent(String orderId) {
@Override
@Transactional
public Object cancelTossPayment(CustomUserDetails userDetails, TossCancelReq tossCancelReq) {
UserInfo user = getUserInfo(userDetails);
UserInfo user = userServiceFacade.getUserInfoWithPessimisticLock(userDetails.getId());
Order order = getOrder(tossCancelReq.getOrderId());
verifyCancel(order, user);
Point point = pointServiceFacade.getPointByOrder(order);

TosspaymentDto tosspaymentDto = requestTossPaymentCancel(order.getPaymentKey());
verifyCancel(order, user);
TosspaymentDto tosspaymentDto = null;
if (order.getAmount() > 0L) {
tosspaymentDto = requestTossPaymentCancel(order.getPaymentKey());
} else {
tosspaymentDto = makeZeroPaymentCancelDto();
}

applyCancelTosspaymentDto(order, tosspaymentDto);
userServiceFacade.saveUserPoint(user, user.getPoint() - order.getPoint());
pointServiceFacade.cancelPoint(point);

ordersRepository.save(order);
return orderMapper.tosspaymentDtoToCancelRes();
}

private TosspaymentDto makeZeroPaymentCancelDto() {
return TosspaymentDto.builder()
.cancels(",=단순 변심")
.status("CANCELED")
.build();
}

@Override
@Transactional
public HistoryDetailRes getReceipt(CustomUserDetails userDetails, String orderId) {
Expand Down Expand Up @@ -430,7 +464,6 @@ private void applyCancelTosspaymentDto(Order order, TosspaymentDto tosspaymentDt
order.setPayStatus(false);
order.getReceipt().setCancels(tosspaymentDto.getCancels().toString());
order.getReceipt().setStatus(tosspaymentDto.getStatus());
order.getUserInfo().setPoint(order.getUserInfo().getPoint() - order.getPoint());
if (order.getCoupon() != null) {
order.getCoupon().setUseCount(order.getCoupon().getUseCount() - 1);
}
Expand Down Expand Up @@ -508,7 +541,6 @@ private void applyTosspaymentDto(Order order, TosspaymentDto tosspaymentDto) {
order.setPayStatus(true);
order.getCart().setIsOrdered(true);
order.setMessage(TOSSPAYMENT_SUCCESS_MESSAGE);
order.getUserInfo().setPoint(order.getUserInfo().getPoint() + order.getPoint());
if (order.getCoupon() != null) {
order.getCoupon().setUseCount(order.getCoupon().getUseCount() + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ public HistoryRes ordersToHistoryRes(List<Order> orders) {
.receipts(
orders
.stream()
.filter(order -> !order.getStore().isDeleted())
.filter(order -> order.getProgress() == PICKUP
|| order.getProgress() == COMPLETE
|| order.getProgress() == FAIL
|| order.getProgress() == CANCEL)
.map(this::orderToReceiptHistoryDto)
Expand All @@ -249,6 +251,7 @@ public HistoryRes ordersToFastOrderRes(List<Order> orders) {
.receipts(
orders
.stream()
.filter(order -> !order.getStore().isDeleted())
.filter(order -> order.getCart().getCreatedAt()
// 데이터 변경 시점 이후의 데이터만 가져옴
.isAfter(LocalDateTime.of(2023, 12, 6, 0, 0, 0)))
Expand All @@ -264,9 +267,9 @@ public HistoryRes ordersToNewHistoryRes(List<Order> orders) {
.receipts(
orders
.stream()
.filter(order -> !order.getStore().isDeleted())
.filter(order -> order.getProgress() == ORDER
|| order.getProgress() == MAKE
|| order.getProgress() == COMPLETE)
|| order.getProgress() == MAKE)
.map(this::orderToReceiptHistoryDto)
.toList())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.readyvery.readyverydemo.src.point;

import java.util.List;

import org.springframework.stereotype.Service;

import com.readyvery.readyverydemo.domain.Order;
import com.readyvery.readyverydemo.domain.Point;
import com.readyvery.readyverydemo.domain.UserInfo;
import com.readyvery.readyverydemo.domain.repository.PointRepository;
import com.readyvery.readyverydemo.global.exception.BusinessLogicException;
import com.readyvery.readyverydemo.global.exception.ExceptionCode;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class PointServiceFacade {
private final PointRepository pointRepository;

public List<Point> findAllByUserInfo(UserInfo userInfo) {
return pointRepository.findAllByUserInfo(userInfo);
}

public Point getPointByOrder(Order order) {
return pointRepository.findByOrder(order)
.orElseThrow(() -> new BusinessLogicException(ExceptionCode.POINT_NOT_FOUND));
}

public void cancelPoint(Point point) {
point.setIsDeleted(true);
pointRepository.save(point);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,30 @@
import org.springframework.stereotype.Service;

import com.readyvery.readyverydemo.domain.UserInfo;
import com.readyvery.readyverydemo.domain.repository.PointRepository;
import com.readyvery.readyverydemo.domain.repository.UserRepository;
import com.readyvery.readyverydemo.global.exception.BusinessLogicException;
import com.readyvery.readyverydemo.global.exception.ExceptionCode;
import com.readyvery.readyverydemo.security.jwt.dto.CustomUserDetails;
import com.readyvery.readyverydemo.src.point.dto.GetPointHistoryRes;
import com.readyvery.readyverydemo.src.point.dto.GetPointRes;
import com.readyvery.readyverydemo.src.point.dto.PointMapper;
import com.readyvery.readyverydemo.src.user.UserServiceFacade;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class PointServiceImpl implements PointService {
private final UserRepository userRepository;
private final PointRepository pointRepository;
private final UserServiceFacade userServiceFacade;
private final PointServiceFacade pointServiceFacade;
private final PointMapper pointMapper;

@Override
public GetPointRes getPoint(CustomUserDetails userDetails) {
UserInfo userInfo = getUserInfo(userDetails.getId());
UserInfo userInfo = userServiceFacade.getUserInfo(userDetails.getId());
return pointMapper.toGetPointRes(userInfo.getPoint());
}

@Override
public GetPointHistoryRes getPointHistory(CustomUserDetails userDetails) {
UserInfo userInfo = getUserInfo(userDetails.getId());
return pointMapper.toGetPointHistoryRes(pointRepository.findAllByUserInfo(userInfo));
}

private UserInfo getUserInfo(Long id) {
return userRepository.findById(id).orElseThrow(
() -> new BusinessLogicException(ExceptionCode.USER_NOT_FOUND)
);
UserInfo userInfo = userServiceFacade.getUserInfo(userDetails.getId());
return pointMapper.toGetPointHistoryRes(pointServiceFacade.findAllByUserInfo(userInfo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public GetPointRes toGetPointRes(Long point) {
public GetPointHistoryRes toGetPointHistoryRes(List<Point> points) {
return GetPointHistoryRes
.builder()
.history(points.stream().map(this::pointToPointDto).toList())
.history(points.stream().filter(point -> !point.getIsDeleted()).map(this::pointToPointDto).toList())
.build();
}

private PointDto pointToPointDto(Point point) {
return PointDto.builder()
.point((point.getPoint() > 0 ? "+" : "") + point.getPoint()) // +1 or -1
.point((point.getPoint() >= 0 ? "+" : "") + point.getPoint()) // +1 or -1
.date(point.getCreatedAt().format(DateTimeFormatter.ofPattern(DATE_FORMAT)))
.status(point.getIsDeleted())
.store(point.getOrder().getStore().getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public UserInfo getUserInfo(Long id) {
);
}

public UserInfo getUserInfoWithPessimisticLock(Long id) {
return userRepository.findUserInfoById(id).orElseThrow(
() -> new BusinessLogicException(ExceptionCode.USER_NOT_FOUND)
);
}

public UserInfo getUserInfoByEmail(String email) {
return userRepository.findByEmail(email).orElseThrow(
() -> new BusinessLogicException(ExceptionCode.USER_NOT_FOUND)
Expand All @@ -32,4 +38,9 @@ public void updateUserPhone(UserInfo userInfo, String phoneNumber) {
userInfo.updatePhone(phoneNumber);
userRepository.save(userInfo);
}

public void saveUserPoint(UserInfo user, long point) {
user.setPoint(point);
userRepository.save(user);
}
}

0 comments on commit 3f1555c

Please sign in to comment.