Skip to content
Extraits de code Groupes Projets
Valider 84253d61 rédigé par salaheddine zidani's avatar salaheddine zidani
Parcourir les fichiers

terminate changes for apply promo code to cart and refactoring Add, delete and...

terminate changes for apply promo code to cart and refactoring Add, delete and calculate total discount of gift card and voucher
parent 5977ca70
Branches
3 requêtes de fusion!82add gift card to cart delete and duplicate gift card in cart,!72MYD-467/ Apply promo code to cart and refactoring some existing features.,!71Apply promo code to cart and refactoring some existing features.
Affichage de
avec 314 ajouts et 236 suppressions
......@@ -18,7 +18,7 @@
<dependency>
<groupId>com.marketingconfort</groupId>
<artifactId>mydressin-common</artifactId>
<version>1.0.136-RELEASE</version>
<version>1.0.137-SNAPSHOT-5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
......
......@@ -15,6 +15,6 @@ public class Paths {
public static final String API_CART_PROMO_CODE = "/api/cart/promoCode";
public static final String CHECK_APPLY_PROMO_CODE = "/checkApplyPromoCode";
public static final String REMOVE_PROMO_CODE = "/delete";
public static final String CHECK_APPLY_PROMO_CODE = "/check-apply-promoCode";
public static final String REMOVE_PROMO_CODE = "/delete-promoCode";
}
......@@ -5,7 +5,6 @@ import com.marketingconfort.mydressin.dtos.CartDTO;
import com.marketingconfort.mydressin.exceptions.PromoCodeExpiredException;
import com.marketingconfort.mydressin.exceptions.PromoCodeNotFoundException;
import com.marketingconfort.mydressin.services.CartService;
import com.marketingconfort.mydressin.services.PromoCodeUtilService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
......@@ -13,17 +12,17 @@ import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(Paths.API_CART_PROMO_CODE)
public class CodePromoController {
public class PromoCodeController {
private final CartService cartService;
public CodePromoController(CartService cartService) {
public PromoCodeController(CartService cartService) {
this.cartService = cartService;
}
@PostMapping(Paths.CHECK_APPLY_PROMO_CODE)
public ResponseEntity<?> checkPromoCode(@RequestParam String Code, @RequestParam Long ClientId) {
public ResponseEntity<?> checkPromoCode(@RequestParam String code, @RequestParam Long clientId) {
try {
CartDTO cartDto = cartService.checkPromoCodeInCart(Code, ClientId);
CartDTO cartDto = cartService.checkPromoCodeInCart(code, clientId);
return ResponseEntity.ok(cartDto);
} catch (PromoCodeNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
......
......@@ -7,6 +7,7 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
......@@ -17,7 +18,7 @@ public class CartDTO {
private Long id;
private Long clientId;
private double totalPrice;
private double totalPriceDiscounted;
private BigDecimal totalPriceDiscounted;
private String typeCountDown;
private List<String> errorsMessages = new ArrayList<>();
......
......@@ -9,6 +9,7 @@ import com.marketingconfort.mydressin.common.stockmanagement.enumurations.Produc
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
......@@ -20,7 +21,7 @@ public class ItemCartDTO {
private ProductType productType;
private Long productId;
private double totalPrice;
private double totalPriceDiscounted;
private BigDecimal totalPriceDiscounted;
private ProductCartDTO product;
private ItemSource source;
private ItemCartStatus status;
......
package com.marketingconfort.mydressin.mappers;
import com.marketingconfort.mydressin.common.cart.dtos.GiftCardDTO;
import com.marketingconfort.mydressin.common.cart.enumurations.ItemCartStatus;
import com.marketingconfort.mydressin.common.cart.models.Cart;
import com.marketingconfort.mydressin.common.cart.models.ItemCart;
import com.marketingconfort.mydressin.common.stockmanagement.enumurations.ProductType;
import com.marketingconfort.mydressin.dtos.CartDTO;
import com.marketingconfort.mydressin.services.ExternalApiService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
import java.math.BigDecimal;
import java.util.stream.Collectors;
@Service
......@@ -34,6 +31,7 @@ public class CartMapper {
.map(itemCartMapper::toDTO)
.collect(Collectors.toList()));
cartDTO.setTotalPrice(cart.getTotalPrice());
cartDTO.setTotalPriceDiscounted(BigDecimal.valueOf(-1));
cartDTO.setInfos(cart.getInfos());
cartDTO.setErrorsMessages(cart.getErrorsMessages());
cartDTO.setPromoCodeIds(cart.getPromoCodeIds());
......@@ -50,44 +48,4 @@ public class CartMapper {
return cartDTO;
}
public double calculateCartTotalPrice(Cart cart) {
if (cart == null || cart.getItems() == null || cart.getItems().isEmpty()) {
return 0.0;
}
return calculateCartBasePrice(cart);
}
public double calculateCartBasePriceFromItems(List<ItemCart> items) {
if (items == null || items.isEmpty()) {
return 0.0;
}
return items.stream()
.mapToDouble(this::calculateRegularPrice)
.sum();
}
public double calculateCartBasePrice(Cart cart) {
if (cart == null || cart.getItems() == null) {
return 0.0;
}
return cart.getItems().stream()
.mapToDouble(item -> calculateRegularPrice(item))
.sum();
}
public double calculateRegularPrice(ItemCart itemCart) {
if (itemCart == null) {
return 0.0;
}
if (itemCart.getProductType() == ProductType.GIFT_CARD && itemCart.getGiftCards() != null) {
return itemCart.getGiftCards().stream()
.mapToDouble(GiftCardDTO::getSolde)
.sum();
} else if (itemCart.getProduct() != null) {
double promoPrice = itemCart.getProduct().getPromoPrice();
double regularPrice = itemCart.getProduct().getRegularPrice();
return (promoPrice > 0 ? promoPrice : regularPrice) * itemCart.getQuantity();
} else {
return 0.0;
}
}
}
......@@ -6,6 +6,7 @@ import com.marketingconfort.mydressin.common.stockmanagement.enumurations.Produc
import com.marketingconfort.mydressin.dtos.ItemCartDTO;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.ArrayList;
@Component
......@@ -42,6 +43,7 @@ public class ItemCartMapper {
itemCartDTO.setGiftCardIds(itemCart.getGiftCardIds());
calculateItemCartTotalPrice(itemCartDTO);
itemCartDTO.setTotalPriceDiscounted(BigDecimal.valueOf(-1));
return itemCartDTO;
}
......
package com.marketingconfort.mydressin.services;
import com.marketingconfort.mydressin.common.Addons.dtos.PromoCodeDTO;
import com.marketingconfort.mydressin.common.cart.models.Cart;
import com.marketingconfort.mydressin.common.cart.models.ItemCart;
import com.marketingconfort.mydressin.dtos.CartDTO;
import com.marketingconfort.mydressin.dtos.ItemCartDTO;
import java.util.List;
public interface PromoCodeUtilService {
CartDTO applyDiscountsToCart(CartDTO cartDTO, List<PromoCodeDTO> promoCodeDTOs);
CartDTO calculatePromoCodeDiscountsToCart(CartDTO cartDTO);
boolean validateCartItemsForPromoCode(CartDTO cartDTO, PromoCodeDTO promoCodeDTO);
boolean isItemIncludedOrExcluded(ItemCartDTO item, PromoCodeDTO promoCodeDTO);
boolean isPromoCodeReducingCartTotal(CartDTO cart, PromoCodeDTO promoCodeDTO);
double applyDiscountPerProduct(PromoCodeDTO promoCodeDTO, ItemCartDTO item);
double applyDiscountPerCart(PromoCodeDTO promoCodeDTO, CartDTO cart);
double applyPercentagePerCart(PromoCodeDTO promoCodeDTO, CartDTO cart);
......
......@@ -5,6 +5,7 @@ import com.marketingconfort.mydressin.common.Addons.enumerations.PromoCodeType;
import com.marketingconfort.mydressin.common.stockmanagement.enumurations.ProductType;
import com.marketingconfort.mydressin.dtos.CartDTO;
import com.marketingconfort.mydressin.dtos.ItemCartDTO;
import com.marketingconfort.mydressin.services.ExternalApiService;
import com.marketingconfort.mydressin.services.PromoCodeUtilService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -18,17 +19,26 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
private static final Logger logger = LoggerFactory.getLogger(PromoCodeUtilServiceImpl.class);
private final ExternalApiService externalApiService;
public PromoCodeUtilServiceImpl(ExternalApiService externalApiService) {
this.externalApiService = externalApiService;
}
@Override
public CartDTO applyDiscountsToCart(CartDTO cart, List<PromoCodeDTO> promoCodeDTOs) {
if (cart == null || promoCodeDTOs == null || promoCodeDTOs.isEmpty()) {
public CartDTO calculatePromoCodeDiscountsToCart(CartDTO cart) {
if (cart == null || cart.getPromoCodeIds() == null || cart.getPromoCodeIds().isEmpty()) {
logger.warn("Cart is null or no promo codes provided.");
return cart;
}
List<PromoCodeDTO> promoCodeDTOList = externalApiService.getPromoCodesByIds(cart.getPromoCodeIds());
double finalTotalCartDiscount = 0.0;
for (PromoCodeDTO promoCodeDTO : promoCodeDTOs) {
for (PromoCodeDTO promoCodeDTO : promoCodeDTOList) {
double promoCodeTotalCartDiscount = 0.0;
switch (promoCodeDTO.getPromoCodeType()) {
......@@ -40,10 +50,10 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
}
case DISCOUNT_PER_PRODUCT -> {
for (ItemCartDTO item : cart.getItems()) {
double itemDiscount = applyDiscountPerProduct(promoCodeDTO, item);
if (itemDiscount > 0) {
item.setTotalPriceDiscounted(item.getTotalPrice() - itemDiscount);
promoCodeTotalCartDiscount += itemDiscount;
BigDecimal itemDiscount = BigDecimal.valueOf(applyDiscountPerProduct(promoCodeDTO, item));
if (itemDiscount.compareTo(BigDecimal.ZERO) > 0) {
item.setTotalPriceDiscounted(BigDecimal.valueOf(item.getTotalPrice()).subtract(itemDiscount));
promoCodeTotalCartDiscount += itemDiscount.doubleValue();
}
}
}
......@@ -62,8 +72,9 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
}
}
double updatedTotalPrice = cart.getTotalPrice() - finalTotalCartDiscount;
cart.setTotalPriceDiscounted(Math.max(updatedTotalPrice, 0));
BigDecimal updatedTotalPrice = BigDecimal.valueOf(cart.getTotalPrice()).subtract(BigDecimal.valueOf(finalTotalCartDiscount));
cart.setTotalPriceDiscounted(updatedTotalPrice.max(BigDecimal.ZERO));
logger.info("Total discount applied: {}. Updated cart total: {}", finalTotalCartDiscount, cart.getTotalPrice());
return cart;
......@@ -153,6 +164,55 @@ public class PromoCodeUtilServiceImpl implements PromoCodeUtilService {
return false;
}
@Override
public boolean isPromoCodeReducingCartTotal(CartDTO cart, PromoCodeDTO promoCodeDTO) {
double totalPriceBeforeDiscount;
if (cart.getTotalPriceDiscounted().compareTo(BigDecimal.ZERO) >= 0) {
totalPriceBeforeDiscount = cart.getTotalPriceDiscounted().doubleValue();
} else {
totalPriceBeforeDiscount = cart.getTotalPrice();
}
double totalPriceAfterDiscount;
switch (promoCodeDTO.getPromoCodeType()) {
case DISCOUNT_PER_CART -> {
// Application de la remise fixe par panier
double discount = applyDiscountPerCart(promoCodeDTO, cart);
totalPriceAfterDiscount = totalPriceBeforeDiscount - discount;
}
case PERCENTAGE_PER_CART -> {
// Application de la remise en pourcentage sur le panier
double discount = applyPercentagePerCart(promoCodeDTO, cart);
totalPriceAfterDiscount = totalPriceBeforeDiscount - discount;
}
case DISCOUNT_PER_PRODUCT -> {
// Calculer le total après réduction sur les produits éligibles
totalPriceAfterDiscount = cart.getItems().stream()
.mapToDouble(item -> {
if (isItemIncludedOrExcluded(item, promoCodeDTO)) {
double discount = applyDiscountPerProduct(promoCodeDTO, item);
return item.getTotalPrice() - discount; // Prix après réduction
} else {
return item.getTotalPrice();
}
})
.sum();
}
default -> {
// Si le type de code promo n'est pas supporté, retourner false
logger.warn("Type de code promo non supporté : {}", promoCodeDTO.getPromoCodeType());
return false;
}
}
// Vérifier si le nouveau total est bien inférieur au total actuel
boolean isReducingTotal = totalPriceAfterDiscount < totalPriceBeforeDiscount;
logger.info("Validation du code promo (Type: {}): Total avant réduction = {}, Total après réduction = {}, Réduction appliquée = {}",
promoCodeDTO.getPromoCodeType(), totalPriceBeforeDiscount, totalPriceAfterDiscount, totalPriceBeforeDiscount - totalPriceAfterDiscount);
return isReducingTotal;
}
@Override
public double applyDiscountPerProduct(PromoCodeDTO promoCodeDTO, ItemCartDTO item) {
// Vérification si le produit est éligible à la remise
......
0% ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter