Skip to content

Commit

Permalink
fix: Product에 대한 배송비를 주문의 배송비로 저장되도록 수정 (#504)
Browse files Browse the repository at this point in the history
Signed-off-by: hseungho <[email protected]>
  • Loading branch information
hseungho authored Dec 3, 2023
1 parent b96291b commit 6df21fd
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 112 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.liberty52.main.global.data;

import com.liberty52.main.global.adapter.portone.dto.PortOnePaymentInfo;
import com.liberty52.main.global.constants.PriceConstants;
import com.liberty52.main.global.constants.ProductConstants;
import com.liberty52.main.global.constants.VBankConstants;
import com.liberty52.main.service.applicationservice.OrderCreateService;
Expand Down Expand Up @@ -161,10 +160,9 @@ public void init() {

// Add Order
Orders order = ordersRepository.save(
Orders.create(AUTH_ID, PriceConstants.DEFAULT_DELIVERY_PRICE,
OrderDestination.create("receiver", "email", "01012341234",
"경기도 어딘가",
"101동 101호", "12345")));
Orders.create(AUTH_ID, OrderDestination.create(
"receiver", "email", "01012341234",
"경기도 어딘가", "101동 101호", "12345")));
DBInitService.order = order;

customProduct0 = CustomProduct.create(imageUrl, 1, AUTH_ID);
Expand All @@ -191,10 +189,9 @@ public void init() {
// Add Order
Orders orderSub
= ordersRepository.save(
Orders.create(AUTH_ID, PriceConstants.DEFAULT_DELIVERY_PRICE,
OrderDestination.create("receiver", "email", "01012341234",
"경기도 어딘가",
"101동 101호", "12345")));
Orders.create(AUTH_ID, OrderDestination.create(
"receiver", "email", "01012341234",
"경기도 어딘가", "101동 101호", "12345")));
DBInitService.order = order;

CustomProduct customProduct = CustomProduct.create(imageUrl, 1, AUTH_ID);
Expand Down Expand Up @@ -226,9 +223,9 @@ public void init() {

for (int i = 0; i < 10; i++) {
Orders guestOrder = Orders.create("GUEST-00" + i,
PriceConstants.DEFAULT_DELIVERY_PRICE,
OrderDestination.create("receiver", "email", "01012341234", "경기도 어딘가",
"101동 101호", "12345"));
guestOrder.changeOrderStatusToOrdered();

Field guestOrderId = guestOrder.getClass().getDeclaredField("id");
guestOrderId.setAccessible(true);
Expand Down Expand Up @@ -261,7 +258,6 @@ public void init() {

for (int i = 11; i < 15; i++) {
Orders guestOrder = Orders.create("GUEST-00" + i,
PriceConstants.DEFAULT_DELIVERY_PRICE,
OrderDestination.create("receiver", "email", "01012341234", "경기도 어딘가",
"101동 101호", "12345"));
guestOrder.changeOrderStatusToWaitingDeposit();
Expand Down Expand Up @@ -297,7 +293,6 @@ public void init() {
// 카드 취소
for (; c < 5; c++) {
Orders c_order = Orders.create("CANCELER-00" + c,
PriceConstants.DEFAULT_DELIVERY_PRICE,
OrderDestination.create("receiver", "email", "01012341234", "경기도 어딘가",
"101동 101호", "12345")
);
Expand Down Expand Up @@ -333,7 +328,6 @@ public void init() {

for (; c < 10; c++) {
Orders c_order = Orders.create("CANCELER-00" + c,
PriceConstants.DEFAULT_DELIVERY_PRICE,
OrderDestination.create("receiver", "email", "01012341234", "경기도 어딘가",
"101동 101호", "12345")
);
Expand Down Expand Up @@ -366,7 +360,6 @@ public void init() {

for (; c < 15; c++) {
Orders c_order = Orders.create("CANCELER-00" + c,
PriceConstants.DEFAULT_DELIVERY_PRICE,
OrderDestination.create("receiver", "email", "01012341234", "경기도 어딘가",
"101동 101호", "12345")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,8 @@ public void modifyQuantity(int quantity) {
this.quantity = quantity;
}

public int getDeliveryFee() {
return this.product.getDeliveryFee();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.liberty52.main.service.entity;

import com.liberty52.main.global.constants.PriceConstants;
import com.liberty52.main.global.util.Utils;
import com.liberty52.main.service.entity.payment.Payment;
import jakarta.persistence.*;
Expand All @@ -26,12 +25,14 @@ public class Orders {
@Id
private final String id = UUID.randomUUID().toString();
private final LocalDateTime orderedAt = LocalDateTime.now(ZoneId.of("Asia/Seoul"));

@Column(updatable = false, nullable = false)
private String authId;

@Enumerated(EnumType.STRING)
private OrderStatus orderStatus;

private int deliveryPrice = PriceConstants.DEFAULT_DELIVERY_PRICE;
private int deliveryPrice = 0;

private Long amount = 0L;

Expand All @@ -56,27 +57,13 @@ public class Orders {
@OneToOne(cascade = CascadeType.ALL, mappedBy = "order")
private OrderDelivery orderDelivery;

@Deprecated
private Orders(String authId, int deliveryPrice, OrderDestination orderDestination) {
this.authId = authId;
orderStatus = OrderStatus.ORDERED;
this.orderNum = Utils.OrderNumberBuilder.createOrderNum();
this.deliveryPrice = deliveryPrice;
this.orderDestination = orderDestination;
}

private Orders(String authId, OrderDestination orderDestination) {
this.authId = authId;
this.orderStatus = OrderStatus.READY;
this.orderNum = Utils.OrderNumberBuilder.createOrderNum();
this.orderDestination = orderDestination;
}

@Deprecated
public static Orders create(String authId, int deliveryPrice, OrderDestination orderDestination) {
return new Orders(authId, deliveryPrice, orderDestination);
}

public static Orders create(String authId, OrderDestination orderDestination) {
return new Orders(authId, orderDestination);
}
Expand Down Expand Up @@ -131,7 +118,11 @@ private void calcTotalAmountAndSet() {
// 수량
totalAmount.getAndUpdate(x -> customProduct.getQuantity() * x);
});

// 배송비
this.deliveryPrice = this.customProducts.stream()
.mapToInt(CustomProduct::getDeliveryFee)
.sum();
totalAmount.getAndAdd(this.deliveryPrice);

this.amount = totalAmount.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@ public float getRate(List<Review> productReviewList) {
public void updateProductOrder(int productOrder) {
this.productOrder = productOrder;
}

public int getDeliveryFee() {
if (this.deliveryOption == null) {
return 0;
}
return this.deliveryOption.getFee();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private PaymentCardResponseDto executeCardPaymentOrders(String authId, List<Stri
void createCardPaymentOrder() {
// given
var product = MockFactory.createProduct("pd");
MockFactory.createProductDeliveryOption("cj", 10000, product);
given(productRepository.findByName(anyString()))
.willReturn(Optional.of(product));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,94 +1,121 @@
package com.liberty52.main.service.entity;

import com.liberty52.main.MockS3Test;
import com.liberty52.main.global.adapter.s3.S3UploaderApi;
import com.liberty52.main.global.constants.PriceConstants;
import com.liberty52.main.service.applicationservice.OrderCreateService;
import com.liberty52.main.service.controller.dto.OrderCreateRequestDto;
import com.liberty52.main.service.controller.dto.PaymentCardResponseDto;
import com.liberty52.main.service.repository.OptionDetailRepository;
import com.liberty52.main.service.repository.OrdersRepository;
import com.liberty52.main.service.repository.ProductRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import com.liberty52.main.service.utils.MockFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.IOException;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@SpringBootTest
@Transactional
@AutoConfigureMockMvc
class OrdersEntityTest extends MockS3Test {

private static final String LIBERTY = "Liberty 52_Frame";
private static final String OPTION_1 = "OPT-001";
private static final String OPTION_2 = "OPT-003";
private static final String OPTION_3 = "OPT-004";
private static final int QUANTITY = 2;
private static final int DELIVERY_PRICE = PriceConstants.DEFAULT_DELIVERY_PRICE;
String authId = UUID.randomUUID().toString();
MockMultipartFile imageFile = new MockMultipartFile("image", "test.png", "image/jpeg", new FileInputStream("src/test/resources/static/test.jpg"));
@Autowired
private OrderCreateService orderCreateService;
@Autowired
private OrdersRepository ordersRepository;
@Autowired
private ProductRepository productRepository;
@Autowired
private OptionDetailRepository optionDetailRepository;
@Autowired
private S3UploaderApi s3Uploader;
private String orderId;

OrdersEntityTest() throws IOException {

import static org.junit.jupiter.api.Assertions.assertEquals;

class OrdersEntityTest {

@Test
void calculateTotalValueAndSet_givenOneCustomProduct_success() {
// given
var authId = "users";
var product = givenProduct(10000L, 1000);
var customProduct = givenCustomProduct(
authId,
product,
product.getProductOptions().get(0).getOptionDetails().get(0), // 1000
product.getProductOptions().get(1).getOptionDetails().get(1) // 10000
);

var order = givenOrder(authId, customProduct);

// when
order.calculateTotalValueAndSet();

// then
var expected = 10000 + 1000 + 10000 + 1000;
assertEquals(expected, order.getAmount());
}

@Test
void test_getTotalAmount() {
OrderCreateRequestDto requestDto = OrderCreateRequestDto.forTestCard(
LIBERTY, List.of(OPTION_1, OPTION_2, OPTION_3), QUANTITY, List.of(),"",
"receiverName", "receiverEmail", "receiverPhoneNumber", "address1", "address2", "zipCode"
void calculateTotalValueAndSet_givenTwoCustomProductOfAnotherProduct_success() {
// given
var authId = "user";
var product_1 = givenProduct(10000L, 100);
var product_2 = givenProduct(100_000L, 10000);

var customProducts = List.of(
givenCustomProduct(
authId,
product_1,
product_1.getProductOptions().get(0).getOptionDetails().get(1), // 100
product_1.getProductOptions().get(1).getOptionDetails().get(0) // 1000
),
givenCustomProduct(
authId,
product_2,
product_2.getProductOptions().get(0).getOptionDetails().get(0), // 1000
product_2.getProductOptions().get(1).getOptionDetails().get(1) // 10000
)
);

PaymentCardResponseDto save = orderCreateService.createCardPaymentOrders(authId, requestDto, imageFile);
orderId = save.getMerchantId();
var order = givenOrder(authId, customProducts);

Orders orders = ordersRepository.findById(orderId).get();
// when
order.calculateTotalValueAndSet();

long expected = getExpectedPrice();
orders.calculateTotalValueAndSet();
long actual = orders.getAmount();
Assertions.assertNotNull(orders);
Assertions.assertEquals(expected, actual);
// then
var expected = 10000 + 100 + 1000 + 100 + 100_000 + 1000 + 10000 + 10000;
assertEquals(expected, order.getAmount());
}

private Long getExpectedPrice() {
long expected = 0;
expected += productRepository.findByName(LIBERTY).get().getPrice();
expected += optionDetailRepository.findById(OPTION_1).get().getPrice();
expected += optionDetailRepository.findById(OPTION_2).get().getPrice();
expected += optionDetailRepository.findById(OPTION_3).get().getPrice();
expected *= QUANTITY;
expected += DELIVERY_PRICE;
return expected;
private Orders givenOrder(
String authId,
List<CustomProduct> customProducts
) {
var order = MockFactory.createOrder(authId);
customProducts.forEach(it -> it.associateWithOrder(order));
return order;
}

@AfterEach
public void deleteS3Image() {
Orders orders = ordersRepository.findById(orderId).get();
orders.getCustomProducts().forEach(customProduct -> {
String imageUrl = customProduct.getUserCustomPictureUrl();
s3Uploader.delete(imageUrl);
});
private Orders givenOrder(
String authId,
CustomProduct customProduct
) {
return givenOrder(authId, List.of(customProduct));
}

private CustomProduct givenCustomProduct(
String authId,
Product product,
OptionDetail... selectedOptions
) {
var customProduct = MockFactory.createCustomProduct("image", 1, authId, product);

Arrays.stream(selectedOptions).forEach(it ->
MockFactory.createCustomProductOption(customProduct, it));

return customProduct;
}

private Product givenProduct(
Long productPrice,
Integer deliveryFee
) {
var product = MockFactory.createProduct("product", productPrice);

var productOption1 = MockFactory.createProductOption("product-option-1", true);
var optionDetail1 = MockFactory.createOptionDetail("detail-1", 1000);
var optionDetail2 = MockFactory.createOptionDetail("detail-2", 100);
optionDetail1.associate(productOption1);
optionDetail2.associate(productOption1);

var productOption2 = MockFactory.createProductOption("product-option-2", true);
var optionDetail3 = MockFactory.createOptionDetail("detail-3", 1000);
var optionDetail4 = MockFactory.createOptionDetail("detail-4", 10000);
optionDetail3.associate(productOption2);
optionDetail4.associate(productOption2);

product.addOption(productOption1);
product.addOption(productOption2);

MockFactory.createProductDeliveryOption("cj", deliveryFee, product);

return product;
}
}

0 comments on commit 6df21fd

Please sign in to comment.