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

130 lines
9.6 KiB
OpenEdge ABL

/**
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
* This Batch class is use for upsert Shopify's Smart collection into the Salesfroce
* Collection is group of products based on category
* Smart Collection is the collection of product which has set of rules/conditions
* Custom Collection is the collection of product which has not set of rules/conditions
* Collects all list of products which lies under the Collections
* 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 FetchSmartCollection_Batch implements Database.Batchable<Object>,Database.AllowsCallouts, Database.Stateful {
String linkForNextPage = ''; //pagination
Boolean isEnd = false;
String store = 'Default';
String updated_at_min = '';
String updated_at_max = '';
list<HIC_ShopifySync__HIC_Field_Mapping__c> collectionFieldsList;
public list<String> listOfSmartCollectionId = new list<String>();
public list<HIC_ShopifySync__HIC_Configuration__c > configurationList = new list<HIC_ShopifySync__HIC_Configuration__c >();
global FetchSmartCollection_Batch(String linkForNextPage, String store){
this.linkForNextPage = linkForNextPage;
this.store = store;
}
global FetchSmartCollection_Batch(String linkForNextPage, String store, String updated_at_min, String updated_at_max){
this.linkForNextPage = linkForNextPage;
this.store = store;
this.updated_at_min = updated_at_min;
this.updated_at_max = updated_at_max;
}
//Dpreceated
global FetchSmartCollection_Batch(String linkForNextPage){
this.linkForNextPage = linkForNextPage;
}
global Iterable<Object> start(Database.BatchableContext bc){
String smartResponse ='';
String JSONResponse = '';
String query = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__Chunk_Size__c,name, HIC_ShopifySync__Shopify_URL__c,HIC_ShopifySync__Default_Field_Mapping__c,HIC_ShopifySync__Specify_Close_Date__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__HIC_Configuration__c')+' from HIC_ShopifySync__HIC_Configuration__c where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Configuration__c') +' = \''+store+'\' limit 1';
configurationList = database.query(query);
collectionFieldsList = new List <HIC_ShopifySync__HIC_Field_Mapping__c>();
List<HIC_ShopifySync__HIC_Field_Mapping__c> fieldMapping = new List <HIC_ShopifySync__HIC_Field_Mapping__c>();
//System.debug('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__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__Collection__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999');
fieldMapping = database.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__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__Collection__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999');
if(!Test.isRunningTest()){
if(String.isNotBlank(linkForNextPage) && !isEnd){
JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('smart_collections.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+'&page_info='+linkForNextPage, configurationList);
}else{
JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('smart_collections.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+(updated_at_min!=''?'&updated_at_min='+updated_at_min:'')+(updated_at_max!=''?'&updated_at_max='+updated_at_max:''), configurationList);
}
if(String.isNotBlank(JSONResponse)){
Map<String,Object> main = (Map<String,Object>) JSON.deserializeUntyped(JSONResponse);
if(!main.isEmpty() && main.containskey('response') && main.containskey('link') ){
if( main.get('response') != null)
smartResponse = String.valueof(main.get('response'));
if(main.get('link') != null){
linkForNextPage = String.valueof(main.get('link'));
}else{
isEnd = true;
}
}
}
}else{
isEnd = true;
smartResponse = '{ "smart_collections": [{"id": 100793057344,"handle": "test-collection","title": "Test Collection","updated_at": "2019-01-14T03:59:48-12:00","body_html": "","published_at": "2019-01-09T22:51:33-12:00","sort_order": "best-selling","template_suffix": "","disjunctive": false,"rules": [{"column": "title","relation": "not_equals","condition": "null"}],"published_scope": "web","admin_graphql_api_id": "gid://shopify/Collection/100793057344"}]}';
}
collectionFieldsList = fieldMapping;
System.debug('smartResponse'+smartResponse);
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(smartResponse);
List<Object> objectList = (List<Object>) meta.get('smart_collections');
//Return list of Object or records from the JSON response
return objectList;
}
global void execute(Database.BatchableContext BC, List<Object> Scope){
Map<String,String> CollectionIdVsRulesResponse = new Map<String,String>();
for(object obj : Scope){
Map<String, Object> ObjectMap = (Map<String, Object>)obj;
ObjectMap.put('type',(Object)'Smart');
ObjectMap.put('rules',string.valueOf(JSON.serialize(ObjectMap.get('rules'))));
listOfSmartCollectionId.add(String.valueOf(ObjectMap.get('id')));
if(ObjectMap.containsKey('rules') && ObjectMap.get('rules') != null){
CollectionIdVsRulesResponse.put(String.valueOf(ObjectMap.get('id')),String.valueOf(ObjectMap.get('rules')));
}
}
//Process for mapping
if(!Test.isRunningTest()){
HIC_ShopifyUtility.processSobjectList(collectionFieldsList,Scope,'HIC_ShopifySync__Collection__c','HIC_ShopifySync__Shopify_Id__c',configurationList,false,store);
}
// Update Smart Collection rules into the Salesfroce
if(!CollectionIdVsRulesResponse.isEmpty() && CollectionIdVsRulesResponse != null){
HIC_ShopifyUtility.deleteSmartCollectionRules(CollectionIdVsRulesResponse.keySet());
HIC_ShopifyUtility.insertSmartCollectionRules(CollectionIdVsRulesResponse);
}
}
global void finish(Database.BatchableContext BC){
if(!isEnd){
//Call batch again
FetchSmartCollection_Batch callBatchAgain = new FetchSmartCollection_Batch(linkForNextPage,store,updated_at_min,updated_at_max);
Database.executeBatch(callBatchAgain,1);
}else{
//Call next batch for Custom Collectons
FetchCollections_Batch FetchCollections = new FetchCollections_Batch('',false,store,updated_at_min,updated_at_max);
if(!Test.isRunningTest()){
Database.executeBatch(FetchCollections);
}
//Call next batch for smart collects
if(!listOfSmartCollectionId.isEmpty() && listOfSmartCollectionId != null){
fetchCollectsForWebhooks callBatchAgain = new fetchCollectsForWebhooks(listOfSmartCollectionId,store);
if(!Test.isRunningTest())
Database.executeBatch(callBatchAgain,1);
}
}
}
}