167 lines
11 KiB
OpenEdge ABL
167 lines
11 KiB
OpenEdge ABL
/**
|
|
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
|
|
* This Batch class is use for handle Shopify's Custom / Smart Collection and Collects.
|
|
* Trigger on Smart Collection's collects create or update.
|
|
* 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 fetchCollectsForWebhooks implements Database.Batchable<string>,Database.AllowsCallouts,database.stateful {
|
|
|
|
global string collectionId{get;set;}
|
|
global list<string> listOfCollectionId;
|
|
String linkForNextPage = '';
|
|
Boolean isEnd = false;
|
|
String JSONResponse = '';
|
|
String store = 'Default';
|
|
|
|
|
|
List<HIC_ShopifySync__HIC_Field_Mapping__c> collectionFieldsList;
|
|
public List<HIC_ShopifySync__HIC_Configuration__c > configurationList = new List<HIC_ShopifySync__HIC_Configuration__c >();
|
|
Boolean dontRun = false;
|
|
|
|
global fetchCollectsForWebhooks(list<string> listOfCollectionId,String store){
|
|
this.listOfCollectionId = listOfCollectionId;
|
|
//dontRun = true;
|
|
this.store = store;
|
|
}
|
|
global fetchCollectsForWebhooks(list<string> listOfCollectionId,String store, Boolean isWebhook){
|
|
this.listOfCollectionId = listOfCollectionId;
|
|
dontRun = true;
|
|
this.store = store;
|
|
}
|
|
global fetchCollectsForWebhooks(String linkForNextPage , Boolean isEnd, String store){
|
|
this.linkForNextPage = linkForNextPage;
|
|
this.isEnd = isEnd;
|
|
this.store = store;
|
|
}
|
|
|
|
//Deprecated
|
|
global fetchCollectsForWebhooks(String linkForNextPage , Boolean isEnd){
|
|
this.linkForNextPage = linkForNextPage;
|
|
this.isEnd = isEnd;
|
|
}
|
|
//Deprecated
|
|
global fetchCollectsForWebhooks(list<string> listOfCollectionId){
|
|
this.listOfCollectionId = listOfCollectionId;
|
|
dontRun = true;
|
|
}
|
|
|
|
global Iterable<string> start(Database.BatchableContext bc){
|
|
if(listOfCollectionId != null && !listOfCollectionId.isEmpty()) {
|
|
list<HIC_ShopifySync__Collect__c> listtodeletecollects = new list<HIC_ShopifySync__Collect__c>();
|
|
|
|
|
|
listtodeletecollects = [Select id from HIC_ShopifySync__Collect__c where HIC_ShopifySync__Collection__r.HIC_ShopifySync__Shopify_Id__c IN : listOfCollectionId WITH SECURITY_ENFORCED Limit 49999 ];
|
|
|
|
|
|
try{
|
|
if(!listtodeletecollects.isEmpty()){
|
|
if(CheckAccessibility.isDeletable('HIC_ShopifySync__Collect__c'))
|
|
database.delete (listtodeletecollects,false);
|
|
}
|
|
}catch(Exception e){
|
|
system.debug('Error: '+e.getMessage());
|
|
}
|
|
return listOfCollectionId;
|
|
}else if(linkForNextPage != ''){
|
|
|
|
return new list<string> {'linkForNextPage'};
|
|
}else{
|
|
isEnd = true;
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
global void execute(Database.BatchableContext BC, List<string> collectionList){
|
|
if(collectionList.isEmpty() || collectionList == null ){
|
|
isEnd = true;
|
|
return;
|
|
}
|
|
|
|
if(!collectionList.isEmpty() && collectionList[0] != 'linkForNextPage') collectionId = collectionList[0];
|
|
|
|
String collectsResponse = '';
|
|
String queryConfiguration = 'Select '+CheckAccessibility.isAccessible('HIC_ShopifySync__Chunk_Size__c,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__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('name','HIC_ShopifySync__HIC_Configuration__c') +' '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HHIC_ShopifySync__HIC_Configuration__c')+' = \''+store+'\' LIMIT 49999';
|
|
configurationList = database.query(queryConfiguration);
|
|
|
|
if(!Test.isRunningTest()){
|
|
if(String.isNotBlank(linkForNextPage) && !isEnd ){
|
|
JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('collects.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c+'&page_info='+linkForNextPage, configurationList);
|
|
linkForNextPage = '';
|
|
}else{
|
|
if(String.isNotBlank(collectionId)){
|
|
JSONResponse = HIC_ShopifyUtility.fetchAllDataWithLinkForNextPage('/collections/'+collectionId+'/products.json?limit='+configurationList[0].HIC_ShopifySync__Chunk_Size__c,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)
|
|
collectsResponse = String.valueof(main.get('response'));
|
|
|
|
if(main.get('link') != null){
|
|
linkForNextPage = String.valueof(main.get('link'));
|
|
}else{
|
|
isEnd = true;
|
|
}
|
|
}else{
|
|
isEnd = true;
|
|
}
|
|
}else{
|
|
isEnd = true;
|
|
}
|
|
}else{
|
|
collectsResponse = '{ "products": [ { "id": 4772723130544, "title": "Product 1", "body_html": "", "vendor": "madeeasy8", "product_type": "", "created_at": "2020-10-08T17:05:23+05:30", "handle": "product-1", "updated_at": "2020-10-08T17:05:25+05:30", "published_at": "2020-10-08T17:05:24+05:30", "template_suffix": "", "published_scope": "web", "tags": "", "admin_graphql_api_id": "gid://shopify/Product/4772723130544", "options": [ { "id": 6135302783152, "product_id": 4772723130544, "name": "Title", "position": 1 } ], "images": [], "image": null } ] }' ;
|
|
}
|
|
|
|
System.debug('collectsResponse--> '+collectsResponse);
|
|
|
|
if(!String.isNotBlank(collectsResponse)) return ;
|
|
|
|
|
|
collectionFieldsList = 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__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__Collect__c\' AND '+CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c','HIC_ShopifySync__HIC_Field_Mapping__c')+' = \''+store+'\' LIMIT 49999';
|
|
collectionFieldsList = database.query(query);
|
|
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(collectsResponse);
|
|
if(!meta.isEmpty() && meta.containskey('products') && meta.get('products') != null) {
|
|
List<Object> objectList = (List<Object>) meta.get('products');
|
|
if(!objectList.isEmpty() && objectList != null){
|
|
for(Object pr : objectList){
|
|
Map<String,Object> prdmap = (Map<String,Object>) pr;
|
|
if(!prdmap.isEmpty() && prdmap != null){
|
|
prdmap.put('collection_id',collectionId);
|
|
prdmap.put('product_id',String.valueOf(prdmap.get('id')));
|
|
prdmap.put('id',String.valueOf(prdmap.get('id')+collectionId));
|
|
}
|
|
}
|
|
if(!Test.isRunningTest()){
|
|
HIC_ShopifyUtility.processSobjectList(collectionFieldsList,objectList,'HIC_ShopifySync__Collect__c','HIC_ShopifySync__Shopify_Id__c',configurationList,dontRun,store);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
global void finish(Database.BatchableContext BC){
|
|
List <HIC_ShopifySync__HIC_Configuration__c> configListToCheckObjects = new List <HIC_ShopifySync__HIC_Configuration__c>();
|
|
String query = 'Select '+CheckAccessibility.isAccessible('id, HIC_ShopifySync__Draft_Order_Sync__c, HIC_ShopifySync__Default_Field_Mapping__c, HIC_ShopifySync__Collection_Sync__c,HIC_ShopifySync__IsCurrencyChanged__c,HIC_ShopifySync__Batch_Is_Processing__c,HIC_ShopifySync__Shopify_URL__c, HIC_ShopifySync__Specify_Close_Date__c,HIC_ShopifySync__API_Key__c, HIC_ShopifySync__Private_Key_Password__c, HIC_ShopifySync__Inventory_Location__c, HIC_ShopifySync__Enable_Draft_Order__c, name, 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';
|
|
configListToCheckObjects = database.query(query) ;
|
|
if(!isEnd){
|
|
if(!Test.isRunningTest()){
|
|
if(String.isNotBlank(linkForNextPage)){
|
|
fetchCollectsForWebhooks callBatchAgain = new fetchCollectsForWebhooks(linkForNextPage,false,store);
|
|
Database.executeBatch(callBatchAgain,1);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
} |