return orderCustomer;
}
public Customer saveNewsletterSubscription(final RequestData requestData, final String email) throws Exception {
Customer customer = customerService.getCustomerByLoginOrEmail(email);
MarketArea marketArea = requestData.getMarketArea();
// SANITY CHECK : CHECK IF THE OPTIN ALREADY EXIST FOR THE MARKET AREA
if(customer != null){
CustomerMarketArea customerMarketArea = customer.getCurrentCustomerMarketArea(marketArea.getId());
if(customerMarketArea != null){
CustomerOptin customerOptin = customerMarketArea.getOptins(CustomerOptin.OPTIN_TYPE_WWW_NEWSLETTER);
if(customerOptin != null){
throw new UniqueNewsletterSubscriptionException();
}
}
}
CustomerOptin customerOptin = new CustomerOptin();
customerOptin.setType(CustomerOptin.OPTIN_TYPE_WWW_NEWSLETTER);
customerOptin.setOrigin("MCOMMERCE");
// HANDLE OPTIN
if(customer != null){
customer = checkCustomerMarketArea(requestData, customer);
CustomerMarketArea customerMarketArea = customer.getCurrentCustomerMarketArea(marketArea.getId());
if(customerMarketArea == null){
customerMarketArea = new CustomerMarketArea();
customerMarketArea.addOptins(customerOptin);
customer.getCustomerMarketAreas().add(customerMarketArea);
} else {
customerMarketArea.addOptins(customerOptin);
}
} else {
customer = new Customer();
customer.setEmail(email);
customer.setAnonymous(true);
customer = checkCustomerMarketArea(requestData, customer);
CustomerMarketArea customerMarketArea = new CustomerMarketArea();
customerMarketArea.addOptins(customerOptin);
customer.getCustomerMarketAreas().add(customerMarketArea);
}
customer = updateCurrentCustomer(requestData, customer);
return customer;
}