Search in sources :

Example 1 with PersistableShoppingCartItem

use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.

the class ServicesTestSupport method sampleCart.

protected ReadableShoppingCart sampleCart() {
    ReadableProduct product = sampleProduct("sampleCart");
    assertNotNull(product);
    PersistableShoppingCartItem cartItem = new PersistableShoppingCartItem();
    cartItem.setProduct(product.getId());
    cartItem.setQuantity(1);
    final HttpEntity<PersistableShoppingCartItem> cartEntity = new HttpEntity<>(cartItem, getHeader());
    final ResponseEntity<ReadableShoppingCart> response = testRestTemplate.postForEntity(String.format("/api/v1/cart/"), cartEntity, ReadableShoppingCart.class);
    assertNotNull(response);
    assertThat(response.getStatusCode(), is(CREATED));
    return response.getBody();
}
Also used : HttpEntity(org.springframework.http.HttpEntity) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct)

Example 2 with PersistableShoppingCartItem

use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.

the class ShoppingCartAPIIntegrationTest method updateMultiWCartId.

/**
 * Update cart items with qty 2 (1) on existing items & adding new item with qty 1 which gives result 2x2+1 = 5
 *
 * @throws Exception
 */
@Test
@Order(4)
public void updateMultiWCartId() throws Exception {
    PersistableShoppingCartItem cartItem1 = new PersistableShoppingCartItem();
    cartItem1.setProduct(data.getProducts().get(0).getId());
    cartItem1.setQuantity(2);
    PersistableShoppingCartItem cartItem2 = new PersistableShoppingCartItem();
    cartItem2.setProduct(data.getProducts().get(1).getId());
    cartItem2.setQuantity(2);
    PersistableShoppingCartItem cartItem3 = new PersistableShoppingCartItem();
    cartItem3.setProduct(data.getProducts().get(2).getId());
    cartItem3.setQuantity(1);
    PersistableShoppingCartItem[] productsQtyUpdates = { cartItem1, cartItem2, cartItem3 };
    final HttpEntity<PersistableShoppingCartItem[]> cartEntity = new HttpEntity<>(productsQtyUpdates, getHeader());
    final ResponseEntity<ReadableShoppingCart> response = testRestTemplate.exchange(String.format("/api/v1/cart/" + data.getCartId() + "/multi"), HttpMethod.POST, cartEntity, ReadableShoppingCart.class);
    assertNotNull(response);
    assertThat(response.getStatusCode(), is(CREATED));
    assertEquals(5, response.getBody().getQuantity());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with PersistableShoppingCartItem

use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.

the class ShoppingCartAPIIntegrationTest method addToCart.

/**
 * Add an Item & Create cart, whould give HTTP 201 & 1 qty
 *
 * @throws Exception
 */
@Test
@Order(1)
public void addToCart() throws Exception {
    ReadableProduct product = sampleProduct("addToCart");
    assertNotNull(product);
    data.getProducts().add(product);
    PersistableShoppingCartItem cartItem = new PersistableShoppingCartItem();
    cartItem.setProduct(product.getId());
    cartItem.setQuantity(1);
    final HttpEntity<PersistableShoppingCartItem> cartEntity = new HttpEntity<>(cartItem, getHeader());
    final ResponseEntity<ReadableShoppingCart> response = testRestTemplate.postForEntity(String.format("/api/v1/cart/"), cartEntity, ReadableShoppingCart.class);
    data.setCartId(response.getBody().getCode());
    assertNotNull(response);
    assertThat(response.getStatusCode(), is(CREATED));
    assertEquals(response.getBody().getQuantity(), 1);
}
Also used : HttpEntity(org.springframework.http.HttpEntity) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with PersistableShoppingCartItem

use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method createCartItems.

// used for api
private List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> createCartItems(ShoppingCart cartModel, List<PersistableShoppingCartItem> shoppingCartItems, MerchantStore store) throws Exception {
    List<Long> productIds = shoppingCartItems.stream().map(s -> Long.valueOf(s.getProduct())).collect(Collectors.toList());
    List<Product> products = productService.getProductsByIds(productIds);
    if (products == null || products.size() != shoppingCartItems.size()) {
        LOG.warn("----------------------- Items with in id-list " + productIds + " does not exist");
        throw new ResourceNotFoundException("Item with id " + productIds + " does not exist");
    }
    List<Product> wrongStoreProducts = products.stream().filter(p -> p.getMerchantStore().getId() != store.getId()).collect(Collectors.toList());
    if (wrongStoreProducts.size() > 0) {
        throw new ResourceNotFoundException("One or more of the items with id's " + wrongStoreProducts.stream().map(s -> Long.valueOf(s.getId())).collect(Collectors.toList()) + " does not belong to merchant " + store.getId());
    }
    List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new ArrayList<>();
    for (Product p : products) {
        com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(p);
        Optional<PersistableShoppingCartItem> oShoppingCartItem = shoppingCartItems.stream().filter(i -> i.getProduct() == p.getId()).findFirst();
        if (!oShoppingCartItem.isPresent()) {
            // Should never happen if not something is updated in realtime or user has item in local storage and add it long time after to cart!
            LOG.warn("Missing shoppingCartItem for product " + p.getSku() + " ( " + p.getId() + " )");
            continue;
        }
        PersistableShoppingCartItem shoppingCartItem = oShoppingCartItem.get();
        item.setQuantity(shoppingCartItem.getQuantity());
        item.setShoppingCart(cartModel);
        /**
         * Check if product is available
         * Check if product quantity is 0
         * Check if date available <= now
         */
        if (!p.isAvailable()) {
            throw new Exception("Item with id " + p.getId() + " is not available");
        }
        Set<ProductAvailability> availabilities = p.getAvailabilities();
        if (availabilities == null) {
            throw new Exception("Item with id " + p.getId() + " is not properly configured");
        }
        for (ProductAvailability availability : availabilities) {
            if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
                throw new Exception("Item with id " + p.getId() + " is not available");
            }
        }
        if (!DateUtil.dateBeforeEqualsDate(p.getDateAvailable(), new Date())) {
            throw new Exception("Item with id " + p.getId() + " is not available");
        }
        // end qty & availablility checks
        // set attributes
        List<com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute> attributes = shoppingCartItem.getAttributes();
        if (!CollectionUtils.isEmpty(attributes)) {
            for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attribute : attributes) {
                ProductAttribute productAttribute = productAttributeService.getById(attribute.getId());
                if (productAttribute != null && productAttribute.getProduct().getId().longValue() == p.getId().longValue()) {
                    com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
                    item.addAttributes(attributeItem);
                }
            }
        }
        items.add(item);
    }
    return items;
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) NoResultException(javax.persistence.NoResultException) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ServiceException(com.salesmanager.core.business.exception.ServiceException) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) ShoppingCartDataPopulator(com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator) PricingService(com.salesmanager.core.business.services.catalog.product.PricingService) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) Set(java.util.Set) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) ShoppingCartCalculationService(com.salesmanager.core.business.services.shoppingcart.ShoppingCartCalculationService) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) LocalDate(java.time.LocalDate) Optional(java.util.Optional) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ProductPriceUtils(com.salesmanager.core.business.utils.ProductPriceUtils) ProductService(com.salesmanager.core.business.services.catalog.product.ProductService) Constants(com.salesmanager.shop.constants.Constants) ReadableShoppingCartMapper(com.salesmanager.shop.mapper.cart.ReadableShoppingCartMapper) DateUtil(com.salesmanager.shop.utils.DateUtil) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Language(com.salesmanager.core.model.reference.language.Language) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Nullable(org.springframework.lang.Nullable) ShoppingCartAttribute(com.salesmanager.shop.model.shoppingcart.ShoppingCartAttribute) ProductAttributeService(com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService) ShoppingCartService(com.salesmanager.core.business.services.shoppingcart.ShoppingCartService) Product(com.salesmanager.core.model.catalog.product.Product) Logger(org.slf4j.Logger) Customer(com.salesmanager.core.model.customer.Customer) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ImageFilePath(com.salesmanager.shop.utils.ImageFilePath) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) Validate(org.apache.commons.lang3.Validate) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ArrayList(java.util.ArrayList) Product(com.salesmanager.core.model.catalog.product.Product) ProductAttribute(com.salesmanager.core.model.catalog.product.attribute.ProductAttribute) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) NoResultException(javax.persistence.NoResultException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) CartModificationException(com.salesmanager.shop.model.shoppingcart.CartModificationException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) LocalDate(java.time.LocalDate) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)

Example 5 with PersistableShoppingCartItem

use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.

the class ShoppingCartFacadeImpl method modifyCart.

private ReadableShoppingCart modifyCart(ShoppingCart cartModel, PersistableShoppingCartItem item, MerchantStore store, Language language) throws Exception {
    com.salesmanager.core.model.shoppingcart.ShoppingCartItem itemModel = createCartItem(cartModel, item, store);
    boolean itemModified = false;
    // check if existing product
    Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = cartModel.getLineItems();
    if (!CollectionUtils.isEmpty(items)) {
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> newItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
        Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> removeItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
        for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem anItem : items) {
            // take care of existing product
            if (itemModel.getProduct().getId().longValue() == anItem.getProduct().getId()) {
                if (item.getQuantity() == 0) {
                    // left aside item to be removed
                    // don't add it to new list of item
                    removeItems.add(anItem);
                } else {
                    // new quantity
                    anItem.setQuantity(item.getQuantity());
                    newItems.add(anItem);
                }
                itemModified = true;
            } else {
                newItems.add(anItem);
            }
        }
        if (!removeItems.isEmpty()) {
            for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem emptyItem : removeItems) {
                shoppingCartService.deleteShoppingCartItem(emptyItem.getId());
            }
        }
        if (!itemModified) {
            newItems.add(itemModel);
        }
        if (newItems.isEmpty()) {
            newItems = null;
        }
        cartModel.setLineItems(newItems);
    } else {
        // new item
        if (item.getQuantity() > 0) {
            cartModel.getLineItems().add(itemModel);
        }
    }
    // promo code added to the cart but no promo cart exists
    if (!StringUtils.isBlank(item.getPromoCode()) && StringUtils.isBlank(cartModel.getPromoCode())) {
        cartModel.setPromoCode(item.getPromoCode());
        cartModel.setPromoAdded(new Date());
    }
    saveShoppingCart(cartModel);
    // refresh cart
    cartModel = shoppingCartService.getById(cartModel.getId(), store);
    if (cartModel == null) {
        return null;
    }
    shoppingCartCalculationService.calculate(cartModel, store, language);
    ReadableShoppingCart readableCart = new ReadableShoppingCart();
    readableCart = readableShoppingCartMapper.convert(cartModel, store, language);
    return readableCart;
}
Also used : PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) Date(java.util.Date) LocalDate(java.time.LocalDate) HashSet(java.util.HashSet)

Aggregations

PersistableShoppingCartItem (com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem)8 ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)8 HttpEntity (org.springframework.http.HttpEntity)6 Order (org.junit.jupiter.api.Order)5 Test (org.junit.jupiter.api.Test)5 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)4 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 PricingService (com.salesmanager.core.business.services.catalog.product.PricingService)1 ProductService (com.salesmanager.core.business.services.catalog.product.ProductService)1 ProductAttributeService (com.salesmanager.core.business.services.catalog.product.attribute.ProductAttributeService)1 ShoppingCartCalculationService (com.salesmanager.core.business.services.shoppingcart.ShoppingCartCalculationService)1 ShoppingCartService (com.salesmanager.core.business.services.shoppingcart.ShoppingCartService)1 ProductPriceUtils (com.salesmanager.core.business.utils.ProductPriceUtils)1 Product (com.salesmanager.core.model.catalog.product.Product)1