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

138 lines
8.9 KiB
OpenEdge ABL

/**
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
* This web service class is use for handle Shopify's Customer webhook response.
* Trigger on customer delete.
* 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>
* ──────────────────────────────────────────────────────────────────────────────────────────────────
*/
@RestResource(UrlMapping='/DeleteCustomerFromShopify/*')
global with sharing class DeleteCustomerFromShopify {
@HttpPost
global static void DeleteCustomer() {
RestRequest request = RestContext.request;
RestResponse response = RestContext.response;
String store = 'Default';
Map<String, String> params = request.params;
if(!params.isEmpty() && params.containsKey('store') && params.get('store') != null) {
store = params.get('store');
}
String JSONResponseBody = request.requestBody.toString().trim();
System.debug('JSONResponseBodyCustomerDelete--->'+JSONResponseBody);
Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(JSONResponseBody);
List<HIC_ShopifySync__HIC_Configuration__c> listofConfiguration = new List<HIC_ShopifySync__HIC_Configuration__c>();
String query = 'SELECT '+CheckAccessibility.isAccessible('id, name, HIC_ShopifySync__Enable_Lead__c, HIC_ShopifySync__Update_Identified_Lead_Records__c, HIC_ShopifySync__Upsert_Lead_basis_on_Email__c, HIC_ShopifySync__Turn_on_DND_synced_Customers__c, HIC_ShopifySync__Upsert_Customer_basis_on_Email__c, HIC_ShopifySync__Upsert_Account_basis_on_Customer_Company__c, HIC_ShopifySync__Account_Contact__c, HIC_ShopifySync__Person_Account__c, HIC_ShopifySync__Default_Account__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__HIC_Configuration__c') +
' FROM HIC_ShopifySync__HIC_Configuration__c WHERE ' + CheckAccessibility.isAccessible('HIC_ShopifySync__Store__c', 'HIC_ShopifySync__HIC_Configuration__c') + ' = \'' + store + '\' LIMIT 1';
listofConfiguration = Database.query(query);
if(!listofConfiguration.isEmpty()) {
if(listofConfiguration[0].HIC_ShopifySync__Enable_Lead__c || listofConfiguration[0].HIC_ShopifySync__Update_Identified_Lead_Records__c || listofConfiguration[0].HIC_ShopifySync__Upsert_Lead_basis_on_Email__c) {
DeleteLead(meta, listofConfiguration[0], JSONResponseBody, store);
} else {
DeleteAccountContact(meta, listofConfiguration[0], JSONResponseBody, store);
}
}
}
public static void DeleteLead(Map<String, Object> meta, HIC_ShopifySync__HIC_Configuration__c config, String JSONResponseBody, String store) {
String leadDeleteId = String.valueOf(meta.get('id'));
List<Lead> listOfLeadForDelete = new List<Lead>();
// Check if config is not null and required field is not null
if (Schema.sObjectType.Lead.isAccessible() &&
Schema.sObjectType.Lead.fields.HIC_ShopifySync__Shopify_id__c.isAccessible() &&
config != null && (config.HIC_ShopifySync__Enable_Lead__c || config.HIC_ShopifySync__Update_Identified_Lead_Records__c || config.HIC_ShopifySync__Upsert_Lead_basis_on_Email__c) ){
listOfLeadForDelete = [SELECT HIC_ShopifySync__Shopify_id__c FROM Lead WHERE HIC_ShopifySync__Shopify_id__c = :leadDeleteId LIMIT 1];
}
List<Database.DeleteResult> deleteRecordsList = new List<Database.DeleteResult>();
try {
Boolean error = false;
if (!listOfLeadForDelete.isEmpty() && CheckAccessibility.isDeletable('Lead')) {
deleteRecordsList = Database.delete(listOfLeadForDelete, false);
if (!deleteRecordsList.isEmpty()) {
EmailerrorCSV.DeleteRecords('Lead', JSONResponseBody, deleteRecordsList, listOfLeadForDelete, store);
}
for (Database.DeleteResult result : deleteRecordsList) {
if (!result.isSuccess()) {
error = true;
}
}
}
} catch (Exception e) {
System.debug('DML Error: ' + e.getMessage());
}
}
public static void DeleteAccountContact(Map<String, Object> meta, HIC_ShopifySync__HIC_Configuration__c config, String JSONResponseBody, String store) {
String customerShopifyIdForDelete;
if( config != null&& !config.HIC_ShopifySync__Turn_on_DND_synced_Customers__c){
if(config.HIC_ShopifySync__Person_Account__c) {
customerShopifyIdForDelete = 'Person ' + String.valueOf(meta.get('id'));
} else {
customerShopifyIdForDelete = 'Business ' + String.valueOf(meta.get('id'));
}
String contactDeleteId = String.valueOf(meta.get('id'));
List<Account> listOfAccountForDelete = new List<Account>();
List<Contact> listOfContactForDelete = new List<Contact>();
if(Schema.sObjectType.Account.isAccessible() && Schema.sObjectType.Account.fields.HIC_ShopifySync__Shopify_id__c.isAccessible() && (config.HIC_ShopifySync__Person_Account__c || !config.HIC_ShopifySync__Upsert_Account_basis_on_Customer_Company__c)) {
listOfAccountForDelete = [SELECT HIC_ShopifySync__Shopify_id__c FROM Account WHERE HIC_ShopifySync__Shopify_id__c = :customerShopifyIdForDelete];
}
if(!config.HIC_ShopifySync__Person_Account__c && Schema.sObjectType.Contact.isAccessible() && Schema.sObjectType.Contact.fields.Shopify_id__c.isAccessible()) {
listOfContactForDelete = [SELECT HIC_ShopifySync__Shopify_id__c FROM Contact WHERE HIC_ShopifySync__Shopify_id__c = :customerShopifyIdForDelete];
}
List<Database.DeleteResult> deleteRecordsList = new List<Database.DeleteResult>();
try {
Boolean error = false;
if(!listOfAccountForDelete.isEmpty()) {
if(CheckAccessibility.isDeletable('Account')) {
deleteRecordsList = Database.delete(listOfAccountForDelete, false);
if(!deleteRecordsList.isEmpty()) {
EmailerrorCSV.DeleteRecords('Account', JSONResponseBody, deleteRecordsList, listOfAccountForDelete, store);
}
}
for(Database.DeleteResult result : deleteRecordsList) {
if(!result.isSuccess()) {
error = true;
}
}
deleteRecordsList.clear();
}
if(!listOfContactForDelete.isEmpty() && !error) {
if(CheckAccessibility.isDeletable('Contact')) {
deleteRecordsList = Database.delete(listOfContactForDelete, false);
if(!deleteRecordsList.isEmpty()) {
EmailerrorCSV.DeleteRecords('Contact', JSONResponseBody, deleteRecordsList, listOfContactForDelete, store);
}
}
}
} catch(Exception e) {
System.debug('DML Error'+e.getMessage());
}
}
}
}