public MergeCartResponse mergeCart(Customer customer, Order anonymousCart, boolean priceOrder) throws PricingException { try { return mergeCartService.mergeCart(customer, anonymousCart, priceOrder); } catch (RemoveFromCartException e) { // This should not happen as this service should be configured to use the LegacyMergeCartService, which will // not throw this exception throw new PricingException(e); } }
public ReconstructCartResponse reconstructCart(Customer customer, boolean priceOrder) throws PricingException { try { return mergeCartService.reconstructCart(customer, priceOrder); } catch (RemoveFromCartException e) { // This should not happen as this service should be configured to use the LegacyMergeCartService, which will // not throw this exception throw new PricingException(e); } }
@Override public void execute(WebRequest request, Authentication authResult) { Customer loggedInCustomer = customerService.readCustomerByUsername(authResult.getName()); Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request); Order cart = null; if (anonymousCustomer != null) { cart = orderService.findCartForCustomer(anonymousCustomer); } MergeCartResponse mergeCartResponse; try { mergeCartResponse = mergeCartService.mergeCart(loggedInCustomer, cart); } catch (PricingException e) { throw new RuntimeException(e); } catch (RemoveFromCartException e) { throw new RuntimeException(e); } if (BLCRequestUtils.isOKtoUseSession(request)) { request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION); } }
/** * Looks up the anonymous customer and merges that cart with the cart from the given logged in <b>customer</b>. This * will also remove the customer from session after it has finished since it is no longer needed */ public Order mergeCart(Customer customer, WebRequest request) { Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request); MergeCartResponse mergeCartResponse; try { Order cart = orderService.findCartForCustomer(anonymousCustomer); mergeCartResponse = mergeCartService.mergeCart(customer, cart); } catch (PricingException e) { throw new RuntimeException(e); } catch (RemoveFromCartException e) { throw new RuntimeException(e); } if (BLCRequestUtils.isOKtoUseSession(request)) { // The anonymous customer from session is no longer needed; it can be safely removed request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION); request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerIdSessionAttributeName(), WebRequest.SCOPE_GLOBAL_SESSION); request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION); } return mergeCartResponse.getOrder(); }
@Transactional @Test(groups = { "testMergeCart" }) public void testMergeWithNoAnonymousCart() throws PricingException, RemoveFromCartException, AddToCartException { Order anonymousCart = null; Order customerCart = setUpCartWithActiveSku(); Customer customer = customerCart.getCustomer(); MergeCartResponse response = mergeCartService.mergeCart(customer, anonymousCart); assert response.getOrder().getOrderItems().size() == 1; assert response.getOrder().getId().equals(customerCart.getId()); assert response.isMerged() == false; }
@Transactional @Test(groups = { "testMergeCart" }) public void testMergeWithInactiveAnonymousCart() throws PricingException, RemoveFromCartException, AddToCartException { Order anonymousCart = null; Order customerCart = setUpCartWithInactiveSku(); Customer customer = customerCart.getCustomer(); MergeCartResponse response = mergeCartService.mergeCart(customer, anonymousCart); assert response.getOrder().getOrderItems().size() == 0; assert response.getOrder().getId().equals(customerCart.getId()); assert response.isMerged() == false; }
@Transactional @Test(groups = { "testMergeCart" }) public void testMergeWithNoCustomerCart() throws PricingException, RemoveFromCartException, AddToCartException { Order anonymousCart = setUpCartWithActiveSku(); Order customerCart = null; Customer customer = customerService.saveCustomer(createNamedCustomer()); MergeCartResponse response = mergeCartService.mergeCart(customer, anonymousCart); assert response.getOrder().getOrderItems().size() == 1; assert response.getOrder().getId().equals(anonymousCart.getId()); assert response.isMerged() == false; }
@Transactional @Test(groups = { "testMergeCart" }) public void testMergeWithBothCarts() throws PricingException, RemoveFromCartException, AddToCartException { Order anonymousCart = setUpCartWithActiveSku(); Order customerCart = setUpCartWithActiveSku(); Customer customer = customerCart.getCustomer(); MergeCartResponse response = mergeCartService.mergeCart(customer, anonymousCart); assert response.getOrder().getOrderItems().size() == 1; assert response.getOrder().getId().equals(anonymousCart.getId()); assert response.isMerged() == false; }
@Transactional @RequestMapping(value = "/merge", method = RequestMethod.POST) @ApiOperation( value = "Merge carts", notes = "Merges anonymous cart with a logged in customer's cart", response = Void.class ) @ApiResponses(value = { @ApiResponse(code = 200, message = "Carts have been successfully merged", response = Void.class) }) public void mergeWithAnonymous( @ApiIgnore @AuthenticationPrincipal final CustomerUserDetails userDetails, @RequestBody final String anonymousToken) throws RemoveFromCartException, PricingException { final Customer loggedUser = customerService.readCustomerById(userDetails.getId()); final CustomerUserDetails anonymousUserDetails = (CustomerUserDetails) tokenServices.loadAuthentication (anonymousToken).getPrincipal(); final Customer anonymousUser = customerService.readCustomerById(anonymousUserDetails.getId()); final Order anonymousCart = orderService.findCartForCustomer(anonymousUser); mergeCartService.mergeCart(loggedUser, anonymousCart); }