Service
Service
WSMagentoCartAPIServiceClassHandler(String jsonReqBody){
Type resultType =
Type.forName('List<WSMagentoCartAPIRequestWrapper>.class');
List<WSMagentoCartAPIRequestWrapper> listOfCartAPIreqWrapWrapper
=(List<WSMagentoCartAPIRequestWrapper>)JSON.deserialize(jsonReqBody, resultType);
System.debug('listOfCartAPIreqWrapWrapper'+listOfCartAPIreqWrapWrapper);
// insert Account, order and orderitem
List<Account> accounts = new List<Account>();
System.debug('cart**'+cart);
System.debug('cart.customer**'+cart.customer);
if(!String.isEmpty(cart.customer.email)){
emailSet.add(cart.customer.email);
}
if(!String.isEmpty(cart.billing_address.email)){
emailSet.add(cart.billing_address.email);
}
if(!String.isEmpty(cart.customer.phone)){
phoneSet.add(cart.customer.phone);
}
if(!String.isEmpty(cart.billing_address.telephone)){
phoneSet.add(cart.billing_address.telephone);
}
skuIds.add(item.sku);
}
}
}
List<Account> listOfExistingAccounts;
try{
listOfExistingAccounts =
AccountSelector.getAccountByPhoneEmailAndAlternatePhone(phoneSet,emailSet,phoneSet)
;
}catch(Exception e){
LogHandler.logInformation('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage());
LogHandler.logException('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage(), e.getLineNumber());
System.debug('Error Occured'+e.getMessage());
System.debug('Error Occured'+e.getLineNumber());
}
mapOfPhoneAndEmailWithAccount.put(exAcc.Alternative_Phone__c,exAcc);
}
}
// Pending :- search the lead also and apply same logic if existing
then create a activity of type website cart.
// or else create a new lead with V4 and source as website cart.
List<Lead> listOfExistingLeads;
try{
listOfExistingLeads =
LeadSelector.getLeadByPhoneEmailAndAlternatePhone(phoneSet,emailSet,phoneSet);
}catch(Exception e){
LogHandler.logInformation('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage());
LogHandler.logException('WSMagentoCartAPIServiceClass',
'WSMagentoCartAPIServiceClassHandler', e.getMessage(), e.getLineNumber());
System.debug('Error Occured'+e.getMessage());
System.debug('Error Occured'+e.getLineNumber());
}
for(WSMagentoCartAPIRequestWrapper reqWrap :
listOfCartAPIreqWrapWrapper){
Account acc;
if(!String.isEmpty(custPhoneKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(custPhoneKey)){
acc = mapOfPhoneAndEmailWithAccount.get(custPhoneKey);
}
else if(!String.isEmpty(custEmailKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(custEmailKey)){
acc = mapOfPhoneAndEmailWithAccount.get(custEmailKey);
}
else if(!String.isEmpty(billPhoneKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(billPhoneKey)){
acc = mapOfPhoneAndEmailWithAccount.get(billPhoneKey);
}
else if(!String.isEmpty(billEmailKey) &&
mapOfPhoneAndEmailWithAccount.containsKey(billEmailKey)){
acc = mapOfPhoneAndEmailWithAccount.get(billEmailKey);
}
else{
}
if(acc==null){
acc= New Account();
acc = CartAPIHelper.createAccount(reqWrap);
newAccLst.add(acc);
}// end of acc
Lead ld;
Task task;
if(!String.isEmpty(custPhoneKey) &&
mapOfPhoneAndEmailWithLead.containsKey(custPhoneKey)){
ld = mapOfPhoneAndEmailWithLead.get(custPhoneKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);
}
else if(!String.isEmpty(custEmailKey) &&
mapOfPhoneAndEmailWithLead.containsKey(custEmailKey)){
ld = mapOfPhoneAndEmailWithLead.get(custEmailKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);
}
else if(!String.isEmpty(billPhoneKey) &&
mapOfPhoneAndEmailWithLead.containsKey(billPhoneKey)){
ld = mapOfPhoneAndEmailWithLead.get(billPhoneKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);
}
else if(!String.isEmpty(billEmailKey) &&
mapOfPhoneAndEmailWithLead.containsKey(billEmailKey)){
ld = mapOfPhoneAndEmailWithLead.get(billEmailKey);
task = CartAPIHelper.createTask(ld,
LeadConstants.TASK_LEAD_SUBJECT_CART_ENQUIRY);
listOfTask.add(task);
}
else{
}
if(ld==null){
newLeadLst.add(ld);
}
Cart__c cartOrder = CartAPIHelper.createOrder(reqWrap,acc); //
pending :- change this to Cart and lead logic.
if(acc.id==null){
relUtil.registerRelationship(cartOrder,acc,Cart__c.Account__c);
}
newOrderLst.add(cartOrder);
Cart_Item__c cartItem =
CartAPIHelper.createOrderItem(item,reqWrap.cart_id); // pending :-change this to
Cart Item.
if(skuToProductId.containskey(item.sku.toUppercase())){
cartItem.Product__c=skuToProductId.get(item.sku.toUppercase()).Id; // pending :-
once change to Cart item remove commment.
}
else{
// Need to handle this exception or insert product.
relUtil.registerRelationship(cartItem,cartOrder,Cart_Item__c.Cart__c);
newOrderItemsLst.add(cartItem);
}
try{
if(newAccLst.size()>0){
insert newAccLst;
if(relUtil.relationshipMap.containskey('Lead')){
relUtil.resolve('Lead');
}
if(newLeadLst.size()>0){
upsert newLeadLst Id;
}
if(relUtil.relationshipMap.containskey('Cart__c')){
relUtil.resolve('Cart__c');
}
if(newOrderLst.size()>0){
relUtil.resolve('Cart_Item__c');
}catch(Exception e){
return listOfCartAPIResponseWrapper;