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

#1174: Apply promotion code when checkout #1247

Merged
merged 10 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions order/src/it/java/com/yas/order/service/OrderServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class OrderServiceIT {
@MockBean
private ProductService productService;

@MockBean
private PromotionService promotionService;

@MockBean
private CartService cartService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

@ConfigurationProperties(prefix = "yas.services")
public record ServiceUrlConfig(
String cart, String customer, String product, String tax) {
String cart, String customer, String product, String tax, String promotion) {
}
11 changes: 11 additions & 0 deletions order/src/main/java/com/yas/order/mapper/CheckoutMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import com.yas.order.viewmodel.checkout.CheckoutItemVm;
import com.yas.order.viewmodel.checkout.CheckoutPostVm;
import com.yas.order.viewmodel.checkout.CheckoutVm;

import java.math.BigDecimal;

Check warning on line 10 in order/src/main/java/com/yas/order/mapper/CheckoutMapper.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.math.BigDecimal'

import org.mapstruct.Mapper;

Check warning on line 12 in order/src/main/java/com/yas/order/mapper/CheckoutMapper.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'org.mapstruct.Mapper'
import org.mapstruct.Mapping;


import org.springframework.stereotype.Component;

Check warning on line 16 in order/src/main/java/com/yas/order/mapper/CheckoutMapper.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'org.springframework.stereotype.Component'

@Mapper(componentModel = "spring")
@Component
Expand All @@ -19,10 +24,16 @@

@Mapping(target = "id", ignore = true)
@Mapping(target = "checkoutState", ignore = true)
@Mapping(target = "totalAmount", source = "totalAmount") // Ánh xạ tường minh cho totalAmount
@Mapping(target = "totalDiscountAmount", source = "totalDiscountAmount")
Checkout toModel(CheckoutPostVm checkoutPostVm);

CheckoutItemVm toVm(CheckoutItem checkoutItem);

@Mapping(target = "checkoutItemVms", ignore = true)
CheckoutVm toVm(Checkout checkout);

default BigDecimal map(BigDecimal value) {
return value != null ? value : BigDecimal.ZERO;
}
}
25 changes: 21 additions & 4 deletions order/src/main/java/com/yas/order/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import com.yas.order.viewmodel.order.PaymentOrderStatusVm;
import com.yas.order.viewmodel.orderaddress.OrderAddressPostVm;
import com.yas.order.viewmodel.product.ProductVariationVm;
import com.yas.order.viewmodel.promotion.PromotionUsageVm;

import java.io.IOException;

Check warning on line 32 in order/src/main/java/com/yas/order/service/OrderService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.io.IOException'
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -36,7 +38,9 @@
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;


import lombok.RequiredArgsConstructor;

Check warning on line 43 in order/src/main/java/com/yas/order/service/OrderService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'lombok.RequiredArgsConstructor'
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -55,6 +59,7 @@
private final ProductService productService;
private final CartService cartService;
private final OrderMapper orderMapper;
private final PromotionService promotionService;

public OrderVm createOrder(OrderPostVm orderPostVm) {

Expand Down Expand Up @@ -128,6 +133,18 @@
productService.subtractProductStockQuantity(orderVm);
cartService.deleteCartItems(orderVm);
acceptOrder(orderVm.id());

// update promotion
List<PromotionUsageVm> promotionUsageVms = new ArrayList<>();
orderItems.forEach(item -> {
PromotionUsageVm promotionUsageVm = PromotionUsageVm.builder()
.productId(item.getProductId())
.orderId(order.getId())
.promotionCode(order.getCouponCode())
.build();
promotionUsageVms.add(promotionUsageVm);
});
promotionService.updateUsagePromotion(promotionUsageVms);
return orderVm;
}

Expand Down Expand Up @@ -179,7 +196,7 @@
}

Pageable pageable = PageRequest.of(0, count);
List<Order> orders = orderRepository.getLatestOrders(pageable);
List<Order> orders = orderRepository.getLatestOrders(pageable);

if (CollectionUtils.isEmpty(orders)) {
return List.of();
Expand Down Expand Up @@ -272,14 +289,14 @@
int pageSize = orderRequest.getPageSize();

OrderListVm orderListVm = getAllOrder(createdFrom, createdTo,
warehouse, productName,
orderStatus, billingCountry, billingPhoneNumber, email, pageNo, pageSize);
warehouse, productName,
orderStatus, billingCountry, billingPhoneNumber, email, pageNo, pageSize);
if (Objects.isNull(orderListVm.orderList())) {
return CsvExporter.exportToCsv(List.of(), OrderItemCsv.class);
}

List<BaseCsv> orders = orderListVm.orderList().stream().map(orderMapper::toCsv).collect(
Collectors.toUnmodifiableList());
Collectors.toUnmodifiableList());
return CsvExporter.exportToCsv(orders, OrderItemCsv.class);
}
}
42 changes: 42 additions & 0 deletions order/src/main/java/com/yas/order/service/PromotionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.yas.order.service;

import com.yas.order.config.ServiceUrlConfig;
import com.yas.order.viewmodel.promotion.PromotionUsageVm;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;

import java.net.URI;

Check warning on line 13 in order/src/main/java/com/yas/order/service/PromotionService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.net.URI'

Check warning on line 13 in order/src/main/java/com/yas/order/service/PromotionService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.net.URI' import. Should be before 'org.springframework.web.client.RestClient'.
import java.util.List;

Check warning on line 14 in order/src/main/java/com/yas/order/service/PromotionService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Wrong lexicographical order for 'java.util.List' import. Should be before 'org.springframework.web.client.RestClient'.

import org.springframework.web.util.UriComponentsBuilder;

Check warning on line 16 in order/src/main/java/com/yas/order/service/PromotionService.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'org.springframework.web.util.UriComponentsBuilder'


@Service
@RequiredArgsConstructor
public class PromotionService extends AbstractCircuitBreakFallbackHandler {
private final RestClient restClient;
private final ServiceUrlConfig serviceUrlConfig;

@Retry(name = "restApi")
@CircuitBreaker(name = "restCircuitBreaker", fallbackMethod = "handleBodilessFallback")
public void updateUsagePromotion(List<PromotionUsageVm> promotionUsageVms) {
final String jwt = ((Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getTokenValue();
final URI url = UriComponentsBuilder
.fromHttpUrl(serviceUrlConfig.promotion())
.path("/backoffice/promotions/updateUsage")
.buildAndExpand()
.toUri();

restClient.post()
.uri(url)
.headers(h -> h.setBearerAuth(jwt))
.body(promotionUsageVms)
.retrieve();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.yas.order.viewmodel.checkout;

import jakarta.validation.constraints.NotNull;

import java.math.BigDecimal;

Check warning on line 5 in order/src/main/java/com/yas/order/viewmodel/checkout/CheckoutPostVm.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.math.BigDecimal'


import java.util.List;

Check warning on line 8 in order/src/main/java/com/yas/order/viewmodel/checkout/CheckoutPostVm.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck

Extra separation in import group before 'java.util.List'

public record CheckoutPostVm(
String email,
String note,
String couponCode,
BigDecimal totalAmount,
BigDecimal totalDiscountAmount,
@NotNull
List<CheckoutItemPostVm> checkoutItemPostVms
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yas.order.viewmodel.checkout;

import java.math.BigDecimal;
import java.util.Set;
import lombok.Builder;

Expand All @@ -9,6 +10,8 @@ public record CheckoutVm(
String email,
String note,
String couponCode,
BigDecimal totalAmount,
BigDecimal totalDiscountAmount,
Set<CheckoutItemVm> checkoutItemVms
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ public record OrderItemGetVm(
Long productId,
String productName,
Integer quantity,
BigDecimal productPrice
BigDecimal productPrice,
BigDecimal discountAmount,
BigDecimal taxAmount
) {
public static OrderItemGetVm fromModel(OrderItem orderItem) {
return new OrderItemGetVm(
orderItem.getId(),
orderItem.getProductId(),
orderItem.getProductName(),
orderItem.getQuantity(),
orderItem.getProductPrice());
orderItem.getProductPrice(),
orderItem.getDiscountAmount(),
orderItem.getTaxAmount());
}

public static List<OrderItemGetVm> fromModels(Collection<OrderItem> orderItems) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.yas.order.viewmodel.promotion;

import lombok.Builder;

@Builder
public record PromotionUsageVm(
String promotionCode,
Long productId,
String userId,
Long orderId
) {
}
1 change: 1 addition & 0 deletions order/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ yas.services.cart=http://api.yas.local/cart
yas.services.customer=http://api.yas.local/customer
yas.services.product=http://api.yas.local/product
yas.services.tax=http://api.yas.local/tax
yas.services.promotion=http://api.yas.local/promotion

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/order
Expand Down
Loading
Loading