Search in sources :

Example 61 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class OrderTotalApi method payment.

/**
 * This service calculates order total for a given shopping cart This method takes in
 * consideration any applicable sales tax An optional request parameter accepts a quote id that
 * was received using shipping api
 *
 * @param quote
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/auth/cart/{id}/total" }, method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableOrderTotalSummary payment(@PathVariable final Long id, @RequestParam(value = "quote", required = false) Long quote, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) {
    try {
        Principal principal = request.getUserPrincipal();
        String userName = principal.getName();
        Customer customer = customerService.getByNick(userName);
        if (customer == null) {
            response.sendError(503, "Error while getting user details to calculate shipping quote");
        }
        ShoppingCart shoppingCart = shoppingCartFacade.getShoppingCartModel(id, merchantStore);
        if (shoppingCart == null) {
            response.sendError(404, "Cart id " + id + " does not exist");
            return null;
        }
        if (shoppingCart.getCustomerId() == null) {
            response.sendError(404, "Cart id " + id + " does not exist for exist for user " + userName);
            return null;
        }
        if (shoppingCart.getCustomerId().longValue() != customer.getId().longValue()) {
            response.sendError(404, "Cart id " + id + " does not exist for exist for user " + userName);
            return null;
        }
        ShippingSummary shippingSummary = null;
        // get shipping quote if asked for
        if (quote != null) {
            shippingSummary = shippingQuoteService.getShippingSummary(quote, merchantStore);
        }
        OrderTotalSummary orderTotalSummary = null;
        OrderSummary orderSummary = new OrderSummary();
        orderSummary.setShippingSummary(shippingSummary);
        List<ShoppingCartItem> itemsSet = new ArrayList<ShoppingCartItem>(shoppingCart.getLineItems());
        orderSummary.setProducts(itemsSet);
        orderTotalSummary = orderService.caculateOrderTotal(orderSummary, customer, merchantStore, language);
        ReadableOrderTotalSummary returnSummary = new ReadableOrderTotalSummary();
        ReadableOrderSummaryPopulator populator = new ReadableOrderSummaryPopulator();
        populator.setMessages(messages);
        populator.setPricingService(pricingService);
        populator.populate(orderTotalSummary, returnSummary, merchantStore, language);
        return returnSummary;
    } catch (Exception e) {
        LOGGER.error("Error while calculating order summary", e);
        try {
            response.sendError(503, "Error while calculating order summary " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : ReadableOrderSummaryPopulator(com.salesmanager.shop.populator.order.ReadableOrderSummaryPopulator) Customer(com.salesmanager.core.model.customer.Customer) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) OrderSummary(com.salesmanager.core.model.order.OrderSummary) ArrayList(java.util.ArrayList) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) Principal(java.security.Principal) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 62 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class AuthenticateCustomerApi method changePassword.

@RequestMapping(value = "/auth/customer/password", method = RequestMethod.POST, produces = { "application/json" })
@ApiOperation(httpMethod = "POST", value = "Sends a request to reset password", notes = "Password reset request is {\"username\":\"test@email.com\"}", response = ResponseEntity.class)
public ResponseEntity<?> changePassword(@RequestBody @Valid PasswordRequest passwordRequest, HttpServletRequest request) {
    try {
        MerchantStore merchantStore = storeFacade.getByCode(request);
        Customer customer = customerFacade.getCustomerByUserName(passwordRequest.getUsername(), merchantStore);
        if (customer == null) {
            return ResponseEntity.notFound().build();
        }
        // need to validate if password matches
        if (!customerFacade.passwordMatch(passwordRequest.getCurrent(), customer)) {
            throw new ResourceNotFoundException("Username or password does not match");
        }
        if (!passwordRequest.getPassword().equals(passwordRequest.getRepeatPassword())) {
            throw new ResourceNotFoundException("Both passwords do not match");
        }
        customerFacade.changePassword(customer, passwordRequest.getPassword());
        return ResponseEntity.ok(Void.class);
    } catch (Exception e) {
        return ResponseEntity.badRequest().body("Exception when reseting password " + e.getMessage());
    }
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) AuthenticationException(org.apache.http.auth.AuthenticationException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 63 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class AuthenticateCustomerApi method setPassword.

@RequestMapping(value = "/private/customer/password", method = RequestMethod.PUT, produces = { "application/json" })
@ApiOperation(httpMethod = "PUT", value = "Change customer password", notes = "Change password request object is {\"username\":\"test@email.com\"}", response = ResponseEntity.class)
public ResponseEntity<?> setPassword(@RequestBody @Valid AuthenticationRequest authenticationRequest, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {
    String authenticatedUser = userFacade.authenticatedUser();
    if (authenticatedUser == null) {
        throw new UnauthorizedException();
    }
    userFacade.authorizedGroup(authenticatedUser, Stream.of(Constants.GROUP_SUPERADMIN, Constants.GROUP_ADMIN, Constants.GROUP_ADMIN_RETAIL).collect(Collectors.toList()));
    Customer customer = customerFacade.getCustomerByUserName(authenticationRequest.getUsername(), merchantStore);
    if (customer == null) {
        return ResponseEntity.notFound().build();
    }
    customerFacade.changePassword(customer, authenticationRequest.getPassword());
    return ResponseEntity.ok(Void.class);
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 64 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerApi method delete.

@DeleteMapping("/auth/customer/")
@ApiOperation(httpMethod = "DELETE", value = "Deletes a loged in customer profile", notes = "Requires authentication", produces = "application/json", response = Void.class)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT") })
public void delete(@ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request) {
    Principal principal = request.getUserPrincipal();
    String userName = principal.getName();
    Customer customer;
    try {
        customer = customerFacade.getCustomerByUserName(userName, merchantStore);
        if (customer == null) {
            throw new ResourceNotFoundException("Customer [" + userName + "] not found");
        }
        customerFacade.delete(customer);
    } catch (Exception e) {
        throw new ServiceRuntimeException("An error occured while deleting customer [" + userName + "]");
    }
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Principal(java.security.Principal) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 65 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class OrderApi method getOrder.

/**
 * Get a given order by id
 *
 * @param id
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/auth/orders/{id}" }, method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
public ReadableOrder getOrder(@PathVariable final Long id, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Principal principal = request.getUserPrincipal();
    String userName = principal.getName();
    Customer customer = customerService.getByNick(userName);
    if (customer == null) {
        response.sendError(401, "Error while performing checkout customer not authorized");
        return null;
    }
    ReadableOrder order = orderFacade.getReadableOrder(id, merchantStore, language);
    if (order == null) {
        LOGGER.error("Order is null for id " + id);
        response.sendError(404, "Order is null for id " + id);
        return null;
    }
    if (order.getCustomer() == null) {
        LOGGER.error("Order is null for customer " + principal);
        response.sendError(404, "Order is null for customer " + principal);
        return null;
    }
    if (order.getCustomer().getId() != null && order.getCustomer().getId().longValue() != customer.getId().longValue()) {
        LOGGER.error("Order is null for customer " + principal);
        response.sendError(404, "Order is null for customer " + principal);
        return null;
    }
    return order;
}
Also used : ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) Principal(java.security.Principal) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Customer (com.salesmanager.core.model.customer.Customer)71 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)33 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)32 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)31 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 Language (com.salesmanager.core.model.reference.language.Language)26 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)17 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)17 ConversionException (com.salesmanager.core.business.exception.ConversionException)16 ServiceException (com.salesmanager.core.business.exception.ServiceException)16 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)16 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)12 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)12 Authentication (org.springframework.security.core.Authentication)12 Date (java.util.Date)11 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)10 ArrayList (java.util.ArrayList)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 Product (com.salesmanager.core.model.catalog.product.Product)9 Country (com.salesmanager.core.model.reference.country.Country)9