Skip to content

Commit

Permalink
Add promotion apply for specific product
Browse files Browse the repository at this point in the history
  • Loading branch information
Patpum Hakaew authored and Patpum Hakaew committed Mar 31, 2024
1 parent 57078a5 commit 9016007
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kampus.kbazaar.cart;

import com.kampus.kbazaar.promotion.PromotionApplyCartRequest;
import com.kampus.kbazaar.promotion.Promotion;
import com.kampus.kbazaar.promotion.PromotionService;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -11,8 +12,11 @@ public class CartController {

private final CartService cartService;

public CartController(CartService cartService) {
private final PromotionService promotionService;

public CartController(CartService cartService, PromotionService promotionService) {
this.cartService = cartService;
this.promotionService = promotionService;
}

@GetMapping("/carts")
Expand All @@ -22,8 +26,7 @@ public ResponseEntity<List<CartResponse>> getCart() {

@PostMapping("/carts/{username}/promotions")
public CartResponse applyCartPromotion(
@PathVariable String username,
@RequestBody PromotionApplyCartRequest promotionApplyCartRequest) {
return cartService.applyCartPromotion(username, promotionApplyCartRequest);
@PathVariable String username, @RequestBody Promotion promotion) {
return promotionService.applyPromotion(username, promotion);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.kampus.kbazaar.promotion;

public record PromotionApplyCartRequest(String code) {}
public record PromotionApplyCartRequest(String code, String productSkus) {}
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
package com.kampus.kbazaar.promotion;

import com.kampus.kbazaar.cart.CartRepository;
import com.kampus.kbazaar.cart.CartResponse;
import com.kampus.kbazaar.cartitem.CartItem;
import com.kampus.kbazaar.cartitem.CartItemRepository;
import com.kampus.kbazaar.cartitem.CartItemService;
import com.kampus.kbazaar.exceptions.NotFoundException;
import io.jsonwebtoken.lang.Strings;
import java.util.List;
import java.util.Set;
import org.springframework.stereotype.Service;

@Service
public class PromotionService {
private PromotionRepository promotionRepository;
private final PromotionRepository promotionRepository;

public PromotionService(PromotionRepository promotionRepository) {
private final CartItemRepository cartItemRepository;

private final CartItemService cartItemService;

private final CartRepository cartRepository;

public PromotionService(
PromotionRepository promotionRepository,
CartItemRepository cartItemRepository,
CartRepository cartRepository,
CartItemService cartItemService) {
this.promotionRepository = promotionRepository;
this.cartItemRepository = cartItemRepository;
this.cartRepository = cartRepository;
this.cartItemService = cartItemService;
}

public List<PromotionResponse> getAll() {
Expand All @@ -22,4 +42,23 @@ public PromotionResponse getPromotionByCode(String code) {
.map(Promotion::toResponse)
.orElseThrow(() -> new NotFoundException("Promotion not found"));
}

public CartResponse applyPromotion(String username, Promotion promotion) {
List<CartItem> cartItems = cartItemRepository.findByUsername(username);

String[] productSkus = Strings.split(promotion.getProductSkus(), ",");
Set<String> productSkuSet = Set.of(productSkus);

for (CartItem item : cartItems) {
if (productSkuSet.contains(item.getSku())) {
item.setDiscount(promotion.getDiscountAmount().doubleValue());
item.setPromotionCodes(promotion.getCode());
cartItemRepository.save(item);
}
}

CartResponse response = cartItemService.getCartByUsername(username);

return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.kampus.kbazaar.cart;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.kampus.kbazaar.promotion.PromotionService;
import com.kampus.kbazaar.security.JwtAuthFilter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -32,6 +35,8 @@ public class CartControllerTest {

@MockBean private CartService cartService;

@MockBean private PromotionService promotionService;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
Expand All @@ -42,4 +47,17 @@ public void getCart_ReturnsOk() throws Exception {
mockMvc.perform(get("/api/v1/carts").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

// @Test
// public void getCards_ReturnsCorrectResponse() throws Exception {
// mockMvc.perform(get("/api/v1/carts").contentType(MediaType.APPLICATION_JSON))
// .andDo(print())
// .andExpect(jsonPath("$.username").value("TechNinja"))
// .andExpect(jsonPath("$.items").isArray())
// .andExpect(jsonPath("$.discount").value(0))
// .andExpect(jsonPath("$.totalDiscount").value(0))
// .andExpect(jsonPath("$.subtotal").value(1))
// .andExpect(jsonPath("$.grandTotal").value(1))
// .andExpect(status().isOk());
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import com.kampus.kbazaar.cart.CartRepository;
import com.kampus.kbazaar.cart.CartResponse;
import com.kampus.kbazaar.cartitem.CartItem;
import com.kampus.kbazaar.cartitem.CartItemRepository;
import com.kampus.kbazaar.cartitem.CartItemResponse;
import com.kampus.kbazaar.cartitem.CartItemService;
import com.kampus.kbazaar.exceptions.NotFoundException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand All @@ -18,6 +26,12 @@ class PromotionServiceTest {

@Mock private PromotionRepository promotionRepository;

@Mock private CartItemRepository mockCartItemRepository;

@Mock private CartRepository mockCartRepository;

@Mock private CartItemService mockCartItemService;

@InjectMocks private PromotionService promotionService;

@BeforeEach
Expand Down Expand Up @@ -67,4 +81,89 @@ void getPromotionByCode_NonExistingCode_ShouldThrowNotFoundException() {
// Act & Assert
assertThrows(NotFoundException.class, () -> promotionService.getPromotionByCode(code));
}

@Test
@DisplayName("should Appply Promotion To The RightProduct")
void shouldAppplyPromotionToTheRightProduct() {

// Mock repository method returning empty optional
PromotionService productService =
new PromotionService(
promotionRepository,
mockCartItemRepository,
mockCartRepository,
mockCartItemService);

Long promotionId = 1L;
String code = "SUMMER50";
String name = "Summer Sale";
String description = "Get 50% off on summer collection";
LocalDateTime startDate =
LocalDateTime.of(2024, 6, 1, 0, 0); // Start date: 2024-06-01 00:00
LocalDateTime endDate = LocalDateTime.of(2024, 6, 30, 23, 59); // End date: 2024-06-30 23:59
String discountType = "Percentage";
BigDecimal discountAmount = BigDecimal.valueOf(50); // 50% discount
BigDecimal maxDiscountAmount = BigDecimal.valueOf(100); // Maximum discount amount
String applicableTo = "Summer Collection";
String productSkus = "SKU001,SKU002,SKU003";
Integer minQuantity = 1;
Integer freeQuantity = 0;

Promotion promotion =
new Promotion(
promotionId,
code,
name,
description,
startDate,
endDate,
discountType,
discountAmount,
maxDiscountAmount,
applicableTo,
productSkus,
minQuantity,
freeQuantity);

Long id = 1L;
String username = "john_doe";
String sku = "SKU001";
String nameCartItem = "Sample Product";
double price = 50.99;
Integer quantity = 1;
double discount = 50.00;
String promotionCodes = "SUMMER50";

CartItemResponse cartItemResponseExpected =
new CartItemResponse(
id, username, sku, nameCartItem, price, quantity, discount, promotionCodes);

List<CartItem> cartItems =
List.of(
CartItem.builder()
.id(id)
.username(username)
.sku(sku)
.name(nameCartItem)
.price(price)
.quantity(quantity)
.discount(0)
.promotionCodes("")
.build());

when(mockCartItemRepository.findByUsername(username)).thenReturn(cartItems);
when(mockCartItemService.getCartByUsername(username))
.thenReturn(
CartResponse.builder()
.items(List.of(cartItemResponseExpected))
.username(username)
.build());

CartResponse cartResponseActual = promotionService.applyPromotion(username, promotion);

assertEquals(cartItemResponseExpected.username(), cartResponseActual.getUsername());
assertEquals(1, cartResponseActual.getItems().size());
assertEquals(promotionCodes, cartResponseActual.getItems().get(0).promotionCodes());
assertEquals(discount, cartResponseActual.getItems().get(0).discount());
}
}

0 comments on commit 9016007

Please sign in to comment.