From 9db15fa76b1f669af7eb9af9ed63bafdd124a792 Mon Sep 17 00:00:00 2001 From: Sahan He <45299562+sahanHe@users.noreply.github.com> Date: Wed, 10 Jan 2024 15:26:01 +0530 Subject: [PATCH] update shopify integration sample --- .../main.bal | 53 +++++++------------ 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/shopify-customer-to-salesforce-customer/main.bal b/shopify-customer-to-salesforce-customer/main.bal index df1243f..7282d9a 100644 --- a/shopify-customer-to-salesforce-customer/main.bal +++ b/shopify-customer-to-salesforce-customer/main.bal @@ -1,5 +1,4 @@ import ballerina/http; -import ballerina/log; import ballerina/regex; import ballerinax/salesforce as sf; @@ -8,39 +7,25 @@ sf:Client salesforce = check new (salesforceConfig); service /salesforce_bridge on new http:Listener(9090) { resource function post customers(@http:Payload ShopifyCustomer shopifyCustomer) returns error? { - SalesforceCustomer salesforceCustomer = transformCustomerData(shopifyCustomer); - error? e = updateSalesforce(salesforceCustomer); - if e is error { - log:printError("Error occurred while updating Salesforce for customer: " + salesforceCustomer.toJsonString(), e); - return e; + string firstName = shopifyCustomer.first_name ?: regex:split(shopifyCustomer.email, "@")[0]; + string lastName = shopifyCustomer.last_name ?: ""; + Address? shopifyAddress = shopifyCustomer.default_address; + string address = (shopifyAddress !is ()) ? string `${shopifyAddress.address1}, + ${shopifyAddress.address2}, ${shopifyAddress.city}, ${shopifyAddress.country}` : ""; + SalesforceCustomer salesforceCustomer = { + Name: string `${firstName} ${lastName}`, + Email__c: shopifyCustomer.email, + Address__c: address + }; + stream customerStream = check salesforce->query( + string `SELECT Id FROM HmartCustomer__c WHERE Email__c = '${salesforceCustomer.Email__c}'`); + record {|Id value;|}? existingCustomer = check customerStream.next(); + check customerStream.close(); + if existingCustomer is () { + _ = check salesforce->create("HmartCustomer__c", salesforceCustomer); + } else { + check salesforce->update("HmartCustomer__c", + existingCustomer.value.Id, salesforceCustomer); } } } - -function transformCustomerData(ShopifyCustomer shopifyCustomer) returns SalesforceCustomer { - string firstName = shopifyCustomer.first_name ?: regex:split(shopifyCustomer.email, "@")[0]; - string lastName = shopifyCustomer.last_name ?: ""; - string address = ""; - Address? shopifyAddress = shopifyCustomer.default_address; - if shopifyAddress !is () { - address = string `${shopifyAddress.address1}, ${shopifyAddress.address2}, ${shopifyAddress.city}, ${shopifyAddress.country}`; - } - SalesforceCustomer salesforceCustomer = { - Name: string `${firstName} ${lastName}`, - Email__c: shopifyCustomer.email, - Address__c: address - }; - return salesforceCustomer; -} - -function updateSalesforce(SalesforceCustomer sfCustomer) returns error? { - stream customerStream = check salesforce->query( - string `SELECT Id FROM HmartCustomer__c WHERE Email__c = '${sfCustomer.Email__c}'`); - record {|Id value;|}? existingCustomer = check customerStream.next(); - check customerStream.close(); - if existingCustomer is () { - _ = check salesforce->create("HmartCustomer__c", sfCustomer); - } else { - check salesforce->update("HmartCustomer__c", existingCustomer.value.Id, sfCustomer); - } -}