diff --git a/CHANGELOG.md b/CHANGELOG.md index 685be22b6683f69dba64608eaa63c3ff59e74162..5f4b8d9b980165d3b60c01525c5938a387c245c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Added - MYD-739/Fix List with sale sessions stats - Add delete item functionality in back office and display sale session ID +- prevent duplicate open carts for the same client ### Changed diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/CartRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/CartRepository.java index 259a5b78ab501779f275dc55aab8f12e3c45b8bb..ce6b5141806e4dbe9a58400ba4511022699efa22 100644 --- a/src/main/java/com/marketingconfort/mydressin/repositories/CartRepository.java +++ b/src/main/java/com/marketingconfort/mydressin/repositories/CartRepository.java @@ -97,4 +97,7 @@ public interface CartRepository extends JpaRepository<Cart, Long>, JpaSpecificat @Param("clientId") Long clientId, @Param("status") Status status); + @Lock(LockModeType.PESSIMISTIC_WRITE) + List<Cart> findAllByClientIdAndStatus(Long clientId, Status status); + } diff --git a/src/main/java/com/marketingconfort/mydressin/services/impl/CartServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/impl/CartServiceImp.java index 7ad05378ae8939f663c8aa1c23d57e522dd35213..605612961aee832affb43fcf2509f0c69336c1ca 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/impl/CartServiceImp.java +++ b/src/main/java/com/marketingconfort/mydressin/services/impl/CartServiceImp.java @@ -43,6 +43,7 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; @@ -82,13 +83,72 @@ public class CartServiceImp implements CartService { } - @Transactional + @Transactional(isolation = Isolation.SERIALIZABLE) @Override public Cart findOrCreateCartByClientId(Long clientId) { + List<Cart> existingCarts = cartRepository.findAllByClientIdAndStatus(clientId, Status.open); + + if (existingCarts.size() > 1) { + Cart latestCart = existingCarts.stream() + .sorted(Comparator.comparing(Cart::getCreatedDate).reversed()) + .findFirst() + .orElse(null); + + for (Cart cart : existingCarts) { + if (!cart.getId().equals(latestCart.getId())) { + try { + mergeCartItems(cart, latestCart); + cartRepository.delete(cart); + logger.warn("Duplicate cart deleted: ID={} for clientId={}", cart.getId(), clientId); + } catch (Exception e) { + logger.error("Error while deleting a duplicate cart: {}", e.getMessage()); + } + } + } + + return latestCart; + } + Optional<Cart> optionalCart = cartRepository.findByClientIdAndStatus(clientId, Status.open); return optionalCart.orElseGet(() -> createNewCartClient(clientId)); } + // Ajouter cette méthode pour fusionner les éléments de panier + private void mergeCartItems(Cart sourceCart, Cart targetCart) { + if (sourceCart.getItems() != null && !sourceCart.getItems().isEmpty()) { + sourceCart.getItems().forEach(item -> { + if (!item.isExpired() && !item.isPayed() && + item.getStatus() != ItemCartStatus.DELETED_BO && + item.getStatus() != ItemCartStatus.DELETED_SITE) { + + // Vérifier si l'élément existe déjà dans le panier cible + Optional<ItemCart> existingItem = targetCart.getItems().stream() + .filter(targetItem -> targetItem.getProductId().equals(item.getProductId()) && + targetItem.getSource() == item.getSource()) + .findFirst(); + + if (existingItem.isPresent()) { + // Mettre à jour la quantité + existingItem.get().setQuantity(existingItem.get().getQuantity() + item.getQuantity()); + } else { + // Transférer l'élément + item.setCart(targetCart); + targetCart.getItems().add(item); + } + } + }); + } + + // Fusionner les codes promo et cartes cadeaux si nécessaire + if (sourceCart.getPromoCodeIds() != null) { + targetCart.getPromoCodeIds().addAll(sourceCart.getPromoCodeIds()); + } + + if (sourceCart.getGiftCardIds() != null) { + targetCart.getGiftCardIds().addAll(sourceCart.getGiftCardIds()); + } + } + @Override public Cart createNewCartClient(Long clientId) { Cart cart = new Cart();