SyncMadeEasy/force-app/main/default/classes/FetchLocationWebhook.cls

90 lines
6.8 KiB
OpenEdge ABL

/**
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
* This Batch class is use for upsert Shopify's Location into the Salesfroce
* Shopify API Documentation: https://shopify.dev/docs/admin-api/rest/reference
* Create Shopify Store: https://www.shopify.com/signup?itcat=identity&itterm=create-new-store
* Shopify API Release 2020: https://www.shopify.in/partners/blog/shopify-api-release-january-2020
* What is Person Account in Salesforce ? https://help.salesforce.com/articleView?id=account_person.htm&type=5
* ──────────────────────────────────────────────────────────────────────────────────────────────────
* @author Sumit <sumit@hicShopify.com>
* ──────────────────────────────────────────────────────────────────────────────────────────────────
*/
global with sharing class FetchLocationWebhook implements Database.Batchable<String>, Database.allowscallouts, Database.Stateful{
String locationId;
String inventoryItemId;
String inventoryLevelResponse;
String store = 'Default';
List<HIC_ShopifySync__HIC_Configuration__c > configurationList;
public FetchLocationWebhook(String locationId, String inventoryItemId, String inventoryLevelResponse, string store){
this.locationId = locationId;
this.inventoryItemId = inventoryItemId;
this.inventoryLevelResponse = inventoryLevelResponse;
this.store = store;
}
global Iterable<String> start(Database.BatchableContext bc){
configurationList = new List<HIC_ShopifySync__HIC_Configuration__c >();
String queryConfiguration = 'Select '+CheckAccessibility.isAccessible('name,HIC_ShopifySync__Default_Field_Mapping__c,HIC_ShopifySync__Shopify_URL__c,HIC_ShopifySync__Specify_Close_Date__c, HIC_ShopifySync__API_Key__c,HIC_ShopifySync__Batch_Is_Processing__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__HIC_Configuration__c')+' from HIC_ShopifySync__HIC_Configuration__c where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HIC_ShopifySync__HIC_Configuration__c')+' = \''+store+'\' LIMIT 49999';
configurationList = database.query(queryConfiguration);
if(configurationList.isEmpty()){
return new List<String>(); // No configuration found, exit batch job
}
string locationResponse = '';
List <String> responseList = new List<String>();
if(!configurationList.isEmpty()){
if(!Test.IsRunningTest()){
locationResponse = HIC_ShopifyUtility.fetchAllDataUsingId('locations/'+locationId+'.json',configurationList);
System.debug('locationResponse --' +locationResponse);
}else{
locationResponse = '{ "location": { "id": 23173333056, "name": "Address: A-45, Udhyog Marg, A Block, Sector 2, Noida, Uttar Pradesh 201301", "address1": "Address: A-45, Udhyog Marg, A Block, Sector 2, Noida, Uttar Pradesh 201301", "address2": "56/37", "city": "Noida", "zip": "201301", "province": "Uttar Pradesh", "country": "IN", "phone": "8168642194", "created_at": "2019-01-07T20:19:16-12:00", "updated_at": "2019-01-07T20:19:16-12:00", "country_code": "IN", "country_name": "India", "province_code": "UP", "legacy": false, "active": true, "admin_graphql_api_id": "gid://shopify/Location/23173333056" } }';
}
}else{
return new List<String>();
}
responseList.add(locationResponse);
system.debug('responseList-->'+responseList);
return responseList;
}
global void execute(Database.BatchableContext bc , List<String> scope){
String companyTimeZone = [SELECT Id, TimeZoneSidKey FROM Organization Limit 1][0].TimeZoneSidKey;
Map<String, Object> main = (Map<String, Object>) JSON.deserializeUntyped(scope[0]);
Map<String, Object> objectMap = (Map<String, Object>)JSON.deserializeUntyped(jSon.serialize(main.get('location')));
list<HIC_ShopifySync__HIC_Field_Mapping__c> locationFieldsList = new list<HIC_ShopifySync__HIC_Field_Mapping__c>();
String query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__SF_Field__c, HIC_ShopifySync__Shopify_Fields__c, HIC_ShopifySync__Nested__c , HIC_ShopifySync__IsSFID__c, HIC_ShopifySync__Default_Value__c, HIC_ShopifySync__Is_Override__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' from HIC_ShopifySync__HIC_Field_Mapping__c Where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Object_Name__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \'HIC_ShopifySync__Location__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999';
locationFieldsList = database.query(query);
String listType = 'List<' + 'HIC_ShopifySync__Location__c' + '>';
list<sobject> LocationListToupsert = (List<SObject>)Type.forName(listType).newInstance();
LocationListToupsert.add(HIC_ShopifyUtility.processFieldTypeAndMapping(locationFieldsList,'HIC_ShopifySync__Location__c',objectMap,configurationList,companyTimeZone));
system.debug('**LocationListToupsert--'+LocationListToupsert);
try{
if(LocationListToupsert.Isempty()) return;
Schema.SObjectType objectType = Schema.getGlobalDescribe().get('HIC_ShopifySync__Location__c');
Map<String, SObjectField> fieldMap = objectType.getDescribe().fields.getMap();
Schema.SobjectField theField = fieldMap.get('HIC_ShopifySync__Shopify_Id__c');
EmailErrorCSV.makeCsvAndSendInEmail(JSON.serialize(scope),'HIC_ShopifySync__Location__c',LocationListToupsert,theField,true,store);
}catch(Exception e){
system.debug('**Error in upserting Location--'+e.getMessage());
}
}
global void finish(Database.BatchableContext bc){
FetchInventoryWebhook fetchInventory = new FetchInventoryWebhook(inventoryItemId, inventoryLevelResponse, configurationList,store);
Database.executeBatch(fetchInventory, 1);
}
}