Search in sources :

Example 1 with GenericRuntimeException

use of com.salesmanager.shop.store.api.exception.GenericRuntimeException in project shopizer by shopizer-ecommerce.

the class OrderApi method updateOrderStatus.

@RequestMapping(value = { "/private/orders/{id}/status" }, method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
public void updateOrderStatus(@PathVariable final Long id, @Valid @RequestBody String status, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {
    String user = authorizationUtils.authenticatedUser();
    authorizationUtils.authorizeUser(user, Stream.of(Constants.GROUP_SUPERADMIN, Constants.GROUP_ADMIN, Constants.GROUP_ADMIN_ORDER, Constants.GROUP_ADMIN_RETAIL).collect(Collectors.toList()), merchantStore);
    Order order = orderService.getOrder(id, merchantStore);
    if (order == null) {
        throw new GenericRuntimeException("412", "Order not found [" + id + "]");
    }
    OrderStatus statusEnum = OrderStatus.valueOf(status);
    orderFacade.updateOrderStatus(order, statusEnum, merchantStore);
    return;
}
Also used : PersistableAnonymousOrder(com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder) PersistableOrder(com.salesmanager.shop.model.order.v1.PersistableOrder) Order(com.salesmanager.core.model.order.Order) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) 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)

Example 2 with GenericRuntimeException

use of com.salesmanager.shop.store.api.exception.GenericRuntimeException in project shopizer by shopizer-ecommerce.

the class CustomerFacadeImpl method verifyCustomerLink.

private Customer verifyCustomerLink(String token, String store) {
    Customer customer = null;
    try {
        customer = customerService.getByPasswordResetToken(store, token);
        if (customer == null) {
            throw new ResourceNotFoundException("Customer not fount for store [" + store + "] and token [" + token + "]");
        }
    } catch (Exception e) {
        throw new ServiceRuntimeException("Cannot verify customer token", e);
    }
    Date tokenExpiry = customer.getCredentialsResetRequest().getCredentialsRequestExpiry();
    if (tokenExpiry == null) {
        throw new GenericRuntimeException("No expiry date configured for token [" + token + "]");
    }
    if (!DateUtil.dateBeforeEqualsDate(new Date(), tokenExpiry)) {
        throw new GenericRuntimeException("Ttoken [" + token + "] has expired");
    }
    return customer;
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) Date(java.util.Date) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 3 with GenericRuntimeException

use of com.salesmanager.shop.store.api.exception.GenericRuntimeException in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method verifyUserLink.

private User verifyUserLink(String token, String store) {
    User user = null;
    try {
        user = userService.getByPasswordResetToken(store, token);
        if (user == null) {
            throw new ResourceNotFoundException("Customer not fount for store [" + store + "] and token [" + token + "]");
        }
    } catch (Exception e) {
        throw new ServiceRuntimeException("Cannot verify customer token", e);
    }
    Date tokenExpiry = user.getCredentialsResetRequest().getCredentialsRequestExpiry();
    if (tokenExpiry == null) {
        throw new GenericRuntimeException("No expiry date configured for token [" + token + "]");
    }
    if (!DateUtil.dateBeforeEqualsDate(new Date(), tokenExpiry)) {
        throw new GenericRuntimeException("Ttoken [" + token + "] has expired");
    }
    return user;
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) Date(java.util.Date) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 4 with GenericRuntimeException

use of com.salesmanager.shop.store.api.exception.GenericRuntimeException in project shopizer by shopizer-ecommerce.

the class OrderApi method checkout.

/**
 * Main checkout resource that will complete the order flow
 * @param code
 * @param order
 * @param merchantStore
 * @param language
 * @return
 */
@RequestMapping(value = { "/cart/{code}/checkout" }, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
public ReadableOrderConfirmation checkout(// shopping cart
@PathVariable final String code, // order
@Valid @RequestBody PersistableAnonymousOrder order, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {
    Validate.notNull(order.getCustomer(), "Customer must not be null");
    ShoppingCart cart;
    try {
        cart = shoppingCartService.getByCode(code, merchantStore);
        if (cart == null) {
            throw new ResourceNotFoundException("Cart code " + code + " does not exist");
        }
        // security password validation
        PersistableCustomer presistableCustomer = order.getCustomer();
        if (!StringUtils.isBlank(presistableCustomer.getPassword())) {
            // validate customer password
            credentialsService.validateCredentials(presistableCustomer.getPassword(), presistableCustomer.getRepeatPassword(), merchantStore, language);
        }
        Customer customer = new Customer();
        customer = customerFacade.populateCustomerModel(customer, order.getCustomer(), merchantStore, language);
        if (!StringUtils.isBlank(presistableCustomer.getPassword())) {
            // check if customer already exist
            customer.setAnonymous(false);
            // username
            customer.setNick(customer.getEmailAddress());
            if (customerFacadev1.checkIfUserExists(customer.getNick(), merchantStore)) {
                // 409 Conflict
                throw new GenericRuntimeException("409", "Customer with email [" + customer.getEmailAddress() + "] is already registered");
            }
        }
        order.setShoppingCartId(cart.getId());
        Order modelOrder = orderFacade.processOrder(order, customer, merchantStore, language, LocaleUtils.getLocale(language));
        Long orderId = modelOrder.getId();
        // populate order confirmation
        order.setId(orderId);
        // set customer id
        order.getCustomer().setId(modelOrder.getCustomerId());
        return orderFacadeV1.orderConfirmation(modelOrder, customer, merchantStore, language);
    } catch (Exception e) {
        if (e instanceof CredentialsException) {
            throw new GenericRuntimeException("412", "Credentials creation Failed [" + e.getMessage() + "]");
        }
        String message = e.getMessage();
        if (StringUtils.isBlank(message)) {
            // exception type
            message = "APP-BACKEND";
            if (e.getCause() instanceof com.salesmanager.core.modules.integration.IntegrationException) {
                message = "Integration problen occured to complete order";
            }
        }
        throw new ServiceRuntimeException("Error during checkout [" + message + "]", e);
    }
}
Also used : PersistableAnonymousOrder(com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder) PersistableOrder(com.salesmanager.shop.model.order.v1.PersistableOrder) Order(com.salesmanager.core.model.order.Order) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) CredentialsException(com.salesmanager.shop.store.security.services.CredentialsException) CredentialsException(com.salesmanager.shop.store.security.services.CredentialsException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) 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)

Example 5 with GenericRuntimeException

use of com.salesmanager.shop.store.api.exception.GenericRuntimeException in project shopizer by shopizer-ecommerce.

the class AuthenticateCustomerApi method register.

/**
 * Create new customer for a given MerchantStore, then authenticate that customer
 */
@RequestMapping(value = { "/customer/register" }, method = RequestMethod.POST, produces = { "application/json" })
@ResponseStatus(HttpStatus.CREATED)
@ApiOperation(httpMethod = "POST", value = "Registers a customer to the application", notes = "Used as self-served operation", response = AuthenticationResponse.class)
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
@ResponseBody
public ResponseEntity<?> register(@Valid @RequestBody PersistableCustomer customer, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws Exception {
    customer.setUserName(customer.getEmailAddress());
    if (customerFacade.checkIfUserExists(customer.getUserName(), merchantStore)) {
        // 409 Conflict
        throw new GenericRuntimeException("409", "Customer with email [" + customer.getEmailAddress() + "] is already registered");
    }
    Validate.notNull(customer.getUserName(), "Username cannot be null");
    Validate.notNull(customer.getBilling(), "Requires customer Country code");
    Validate.notNull(customer.getBilling().getCountry(), "Requires customer Country code");
    customerFacade.registerCustomer(customer, merchantStore, language);
    // Perform the security
    Authentication authentication = null;
    try {
        authentication = jwtCustomerAuthenticationManager.authenticate(new UsernamePasswordAuthenticationToken(customer.getUserName(), customer.getPassword()));
    } catch (Exception e) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    if (authentication == null) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    SecurityContextHolder.getContext().setAuthentication(authentication);
    // Reload password post-security so we can generate token
    final JWTUser userDetails = (JWTUser) jwtCustomerDetailsService.loadUserByUsername(customer.getUserName());
    final String token = jwtTokenUtil.generateToken(userDetails);
    // Return the token
    return ResponseEntity.ok(new AuthenticationResponse(customer.getId(), token));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Authentication(org.springframework.security.core.Authentication) JWTUser(com.salesmanager.shop.store.security.user.JWTUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) AuthenticationResponse(com.salesmanager.shop.store.security.AuthenticationResponse) 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) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

GenericRuntimeException (com.salesmanager.shop.store.api.exception.GenericRuntimeException)5 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)4 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)3 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)3 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 ServiceException (com.salesmanager.core.business.exception.ServiceException)2 Customer (com.salesmanager.core.model.customer.Customer)2 Order (com.salesmanager.core.model.order.Order)2 ReadableOrder (com.salesmanager.shop.model.order.v0.ReadableOrder)2 PersistableAnonymousOrder (com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder)2 PersistableOrder (com.salesmanager.shop.model.order.v1.PersistableOrder)2 Date (java.util.Date)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 OrderStatus (com.salesmanager.core.model.order.orderstatus.OrderStatus)1 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)1 User (com.salesmanager.core.model.user.User)1 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)1