155 lines
12 KiB
OpenEdge ABL
155 lines
12 KiB
OpenEdge ABL
@RestResource(urlMapping='/UpdateCheckoutsFromShopify/*')
|
|
global with sharing class UpdateCheckoutsFromShopify {
|
|
@HttpPost
|
|
global static void UpdateCheckoutsFromShopify(){
|
|
RestRequest request = RestContext.request;
|
|
RestResponse response = RestContext.response;
|
|
String CheckoutResponse = request.requestBody.ToString().Trim();
|
|
System.debug('CheckoutResponse=== '+CheckoutResponse);
|
|
|
|
String store = 'Default';
|
|
Map <String, String> params = request.params;
|
|
System.debug('params=== '+params);
|
|
|
|
if(!params.isEmpty() && params.containsKey('store') && params.get('store') != null)
|
|
store = params.get('store');
|
|
|
|
InsertCheckoutsandCheckoutLineItemFromShopify(CheckoutResponse,store);
|
|
}
|
|
|
|
public static void InsertCheckoutsandCheckoutLineItemFromShopify(String CheckoutResponse, string store){
|
|
System.debug('Store == '+Store);
|
|
|
|
list<HIC_ShopifySync__HIC_Configuration__c > OrdOppChecklist = new list<HIC_ShopifySync__HIC_Configuration__c >();
|
|
string query;
|
|
if(!test.isRunningTest()){
|
|
query = 'Select '+CheckAccessibility.isAccessible(' name,HIC_ShopifySync__Default_Field_Mapping__c,HIC_ShopifySync__Batch_Is_Processing__c,HIC_ShopifySync__Specify_Close_Date__c, HIC_ShopifySync__Shopify_URL__c, HIC_ShopifySync__API_Key__c, HIC_ShopifySync__Private_Key_Password__c, HIC_ShopifySync__Opportunity__c, HIC_ShopifySync__Order__c, HIC_ShopifySync__Opportunity_Line_Item__c, HIC_ShopifySync__Order_Line_Item__c, HIC_ShopifySync__Order_Fulfillment__c, HIC_ShopifySync__Person_Account__c, HIC_ShopifySync__Account_Contact__c,HIC_ShopifySync__Default_Account__c ','HIC_ShopifySync__HIC_Configuration__c')+ ' from HIC_ShopifySync__HIC_Configuration__c where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HIC_ShopifySync__HIC_Configuration__c')+' = \''+store+'\' LIMIT 49999';
|
|
}else{
|
|
query = 'Select name, HIC_ShopifySync__Default_Field_Mapping__c,HIC_ShopifySync__Shopify_URL__c,HIC_ShopifySync__Specify_Close_Date__c,HIC_ShopifySync__Batch_Is_Processing__c,HIC_ShopifySync__API_Key__c, HIC_ShopifySync__Private_Key_Password__c, HIC_ShopifySync__Opportunity__c, HIC_ShopifySync__Order__c, HIC_ShopifySync__Opportunity_Line_Item__c, HIC_ShopifySync__Order_Line_Item__c, HIC_ShopifySync__Order_Fulfillment__c, HIC_ShopifySync__Person_Account__c, HIC_ShopifySync__Account_Contact__c,HIC_ShopifySync__Default_Account__c from HIC_ShopifySync__HIC_Configuration__c where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HIC_ShopifySync__HIC_Configuration__c')+' = \''+store+'\' LIMIT 49999';
|
|
}
|
|
OrdOppChecklist = database.query(query);
|
|
System.debug('OrdOppChecklist=== '+OrdOppChecklist);
|
|
|
|
String companyTimeZone = [SELECT Id, TimeZoneSidKey FROM Organization Limit 1][0].TimeZoneSidKey;
|
|
System.debug('companyTimeZone=== '+companyTimeZone);
|
|
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(CheckoutResponse);
|
|
System.debug('meta==== '+meta);
|
|
string CheckoutShopifyId = string.valueof(meta.get('id'));
|
|
System.debug('CheckoutShopifyId ==== '+CheckoutShopifyId);
|
|
List<HIC_ShopifySync__Checkout_LineItem__c> CheckoutsLineItemList = new List<HIC_ShopifySync__Checkout_LineItem__c>([Select HIC_ShopifySync__Shopify_Id__c from HIC_ShopifySync__Checkout_LineItem__c where HIC_ShopifySync__Checkout__r.HIC_ShopifySync__Shopify_Id__c =: CheckoutShopifyId ]);
|
|
System.debug('CheckoutsLineItemList==== '+CheckoutsLineItemList);
|
|
set<string> CheckoutLineShopifyIds = new set<string>();
|
|
System.debug('CheckoutLineShopifyIds ==== '+CheckoutLineShopifyIds);
|
|
if(!CheckoutsLineItemList.isEmpty()){
|
|
for(HIC_ShopifySync__Checkout_LineItem__c checkoutslineitems : CheckoutsLineItemList){
|
|
CheckoutLineShopifyIds.add(checkoutslineitems.HIC_ShopifySync__shopify_id__c);
|
|
}
|
|
}
|
|
|
|
Type typLstSObject = Type.ForName('List<HIC_ShopifySync__Checkout_LineItem__c>');
|
|
List<SObject> CheckoutandLineItemListToupsert = (List<SObject>)typLstSObject.NewInstance();
|
|
// map<string,string> productVsPricebookEntryMap = new map<string,string>();
|
|
// map<string,string> productVsUnitepriceMap = new map<string,string>();
|
|
list<Object> listofCheckoutandLineItem = new list<Object>();
|
|
if(meta != null && !meta.isEmpty() && meta.containskey('line_items'))
|
|
listofCheckoutandLineItem = (list<Object>)meta.get('line_items');
|
|
|
|
/*for(Object CheckoutsLineItemObj : listofCheckoutandLineItem ){
|
|
Map<String,Object> CheckoutsLineItemObjMap =(Map<String,Object>)CheckoutsLineItemObj;
|
|
//productVsPricebookEntryMap.put(String.valueof(CheckoutsLineItemObjMap.get('product_id')),null);
|
|
productVsUnitepriceMap.put(String.valueof(CheckoutsLineItemObjMap.get('product_id')),String.valueof(CheckoutsLineItemObjMap.get('price')));
|
|
}*/
|
|
Map<String,Object> accMap = (Map<String,Object>)meta.get('customer');
|
|
Map<String,Object> ShopifyIdvsAccountId = new Map<String,Object>();
|
|
if(Schema.sObjectType.Account.fields.getMap().containsKey( 'ispersonaccount' )){
|
|
if (OrdOppChecklist[0].HIC_ShopifySync__Person_Account__c ){
|
|
|
|
if(accMap != null && !accMap.isEmpty() && !(String.valueOf(accMap.get('id'))).contains('Person ')){
|
|
//objectMap.put('id','Person Account'+String.valueOf(objectMap.get('id')));
|
|
ShopifyIdvsAccountId.put('Person '+String.valueof(accMap.get('id')),meta.get('customer'));
|
|
}
|
|
}else if(!OrdOppChecklist[0].HIC_ShopifySync__Person_Account__c ){
|
|
|
|
if(!(String.valueOf(accMap.get('id'))).contains('Business '))
|
|
//objectMap.put('id','Business Account'+String.valueOf(objectMap.get('id')));
|
|
ShopifyIdvsAccountId.put('Business '+String.valueof(accMap.get('id')),meta.get('customer'));
|
|
}
|
|
}else{
|
|
system.debug('***HIC_ShopifySync__Person_Account__c*3*'+OrdOppChecklist[0].HIC_ShopifySync__Person_Account__c);
|
|
if(accMap != null && !accMap.isEmpty()&& meta != null && !meta.isEmpty() && !(String.valueOf(accMap.get('id'))).contains('Business ') && meta.containsKey('customer'))
|
|
//objectMap.put('id','Business Account'+String.valueOf(objectMap.get('id')));
|
|
ShopifyIdvsAccountId.put('Business '+String.valueof(accMap.get('id')),meta.get('customer'));
|
|
}
|
|
|
|
//ShopifyIdvsAccountId.put(String.valueof(accMap.get('id')),'false');
|
|
/*if(!OrdOppChecklist.isEmpty() && OrdOppChecklist[0].Person_Account__c== true){
|
|
HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, 'Account',true, OrdOppChecklist);
|
|
}else{
|
|
HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, 'Account',false,OrdOppChecklist );
|
|
}*/
|
|
if(accMap != null && !accMap.isEmpty()){
|
|
HIC_ShopifyUtility.InitializeAccount(ShopifyIdvsAccountId, OrdOppChecklist, store);
|
|
}
|
|
|
|
System.debug('ShopifyIdvsAccountId-->'+ShopifyIdvsAccountId);
|
|
|
|
list<HIC_ShopifySync__HIC_Field_Mapping__c> CheckoutandLineItemFieldList = new list<HIC_ShopifySync__HIC_Field_Mapping__c>([Select HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c, HIC_ShopifySync__Default_Value__c from HIC_ShopifySync__HIC_Field_Mapping__c Where HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Checkout_LineItem__c' AND HIC_ShopifySync__Store__c =: store]);
|
|
list<HIC_ShopifySync__HIC_Field_Mapping__c> CheckoutsFieldList = new list<HIC_ShopifySync__HIC_Field_Mapping__c>([Select HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c , HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c, HIC_ShopifySync__Default_Value__c from HIC_ShopifySync__HIC_Field_Mapping__c Where HIC_ShopifySync__Object_Name__c = 'HIC_ShopifySync__Checkout__c'AND HIC_ShopifySync__Store__c =: store ]);
|
|
|
|
String listType = 'List<' + 'HIC_ShopifySync__Checkout__c' + '>';
|
|
list<sobject> CheckoutListToupsert = (List<SObject>)Type.forName(listType).newInstance();
|
|
CheckoutListToupsert.add(HIC_ShopifyUtility.processFieldTypeAndMapping(CheckoutsFieldList,'HIC_ShopifySync__Checkout__c',meta,OrdOppChecklist,companyTimeZone));
|
|
List<Object> listofallCheckLines = new List<Object>();
|
|
if(meta != null && !meta.isEmpty() && meta.Containskey('line_items')){
|
|
listofCheckoutandLineItem = (list<Object>)meta.get('line_items');
|
|
Map<String, Object> chekLineObj = new Map<String, Object>();
|
|
for(Object checkLine : listofCheckoutandLineItem){
|
|
chekLineObj = (Map<String, Object>)checkLine;
|
|
chekLineObj.put('id', CheckoutShopifyId+'-'+chekLineObj.get('variant_id'));
|
|
chekLineObj.put('checkout_id', CheckoutShopifyId);
|
|
//checkLine = (Object)chekLineObj;
|
|
//listofallCheckLines.add(chekLineObj);
|
|
listofallCheckLines.add(chekLineObj);
|
|
}
|
|
CheckoutandLineItemListToupsert.addAll(HIC_ShopifyUtility.fillObjectValues(CheckoutandLineItemFieldList,'HIC_ShopifySync__Checkout_LineItem__c',listofallCheckLines,OrdOppChecklist));
|
|
}
|
|
|
|
if(!CheckoutListToupsert.Isempty()){ //return null;
|
|
Schema.SObjectType objectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Checkout__c');
|
|
Map<String, SObjectField> fieldMap = objectType.getDescribe().fields.getMap();
|
|
Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c');
|
|
|
|
try{
|
|
if(!Test.isRunningTest()){
|
|
EmailErrorCSV.makeCsvAndSendInEmail(CheckoutResponse,'HIC_ShopifySync__Checkout__c',CheckoutListToupsert,theField,true,Store);
|
|
}
|
|
}catch(Exception e){
|
|
System.debug('Exception '+ e.getMessage() + ' at' +e.getLineNumber());
|
|
}
|
|
// HIC_ShopifyUtility.InitializePricebookEntryid(productVsPricebookEntryMap,productVsUnitepriceMap,OrdOppChecklist,store);
|
|
}
|
|
|
|
if(!CheckoutandLineItemListToupsert.Isempty()){ //return null;
|
|
Schema.SObjectType objectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Checkout_LineItem__c');
|
|
Map<String, SObjectField> fieldMap = objectType.getDescribe().fields.getMap();
|
|
Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c');
|
|
|
|
try{
|
|
if(!Test.isRunningTest()){
|
|
EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(listofallCheckLines),'HIC_ShopifySync__Checkout_LineItem__c',CheckoutandLineItemListToupsert,theField,true,store);
|
|
}
|
|
}catch(Exception e){
|
|
System.debug('Exception '+ e.getMessage() + ' at' +e.getLineNumber());
|
|
}
|
|
|
|
if(!CheckoutLineShopifyIds.Isempty()){
|
|
for(SObject CheckoutLineObj : CheckoutandLineItemListToupsert){
|
|
CheckoutLineShopifyIds.remove(string.valueOf(CheckoutLineObj.get('HIC_ShopifySync__Shopify_Id__c')));
|
|
}
|
|
if(!CheckoutLineShopifyIds.Isempty()){
|
|
delete [Select Id from HIC_ShopifySync__Checkout_LineItem__c where HIC_ShopifySync__Shopify_Id__c IN: CheckoutLineShopifyIds];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |