diff --git a/src/v0/destinations/salesforce/transform.js b/src/v0/destinations/salesforce/transform.js index b8f032c5bf..9b7123c207 100644 --- a/src/v0/destinations/salesforce/transform.js +++ b/src/v0/destinations/salesforce/transform.js @@ -106,7 +106,7 @@ async function getSaleforceIdForRecord( objectType, identifierType, identifierValue, - destination, + { destination, metadata }, authorizationFlow, ) { const objSearchUrl = `${authorizationData.instanceUrl}/services/data/v${SF_API_VERSION}/parameterizedSearch/?q=${identifierValue}&sobject=${objectType}&in=${identifierType}&${objectType}.fields=id,${identifierType}`; @@ -117,6 +117,7 @@ async function getSaleforceIdForRecord( headers: getAuthHeader({ authorizationFlow, authorizationData }), }, { + metadata, destType: 'salesforce', feature: 'transformation', endpointPath: '/parameterizedSearch', @@ -156,9 +157,8 @@ async function getSaleforceIdForRecord( // // Default Object type will be "Lead" for backward compatibility async function getSalesforceIdFromPayload( - message, + { message, destination, metadata }, authorizationData, - destination, authorizationFlow, ) { // define default map @@ -201,7 +201,7 @@ async function getSalesforceIdFromPayload( objectType, identifierType, id, - destination, + { destination, metadata }, authorizationFlow, ); } @@ -233,6 +233,7 @@ async function getSalesforceIdFromPayload( headers: getAuthHeader({ authorizationFlow, authorizationData }), }, { + metadata, destType: 'salesforce', feature: 'transformation', endpointPath: '/parameterizedSearch', @@ -283,7 +284,11 @@ async function getSalesforceIdFromPayload( } // Function for handling identify events -async function processIdentify(message, authorizationData, destination, authorizationFlow) { +async function processIdentify( + { message, destination, metadata }, + authorizationData, + authorizationFlow, +) { const mapProperty = destination.Config.mapProperty === undefined ? true : destination.Config.mapProperty; // check the traits before hand @@ -305,9 +310,8 @@ async function processIdentify(message, authorizationData, destination, authoriz // get salesforce object map const salesforceMaps = await getSalesforceIdFromPayload( - message, + { message, destination, metadata }, authorizationData, - destination, authorizationFlow, ); @@ -331,10 +335,18 @@ async function processIdentify(message, authorizationData, destination, authoriz // Generic process function which invokes specific handler functions depending on message type // and event type where applicable -async function processSingleMessage(message, authorizationData, destination, authorizationFlow) { +async function processSingleMessage( + { message, destination, metadata }, + authorizationData, + authorizationFlow, +) { let response; if (message.type === EventType.IDENTIFY) { - response = await processIdentify(message, authorizationData, destination, authorizationFlow); + response = await processIdentify( + { message, destination, metadata }, + authorizationData, + authorizationFlow, + ); } else { throw new InstrumentationError(`message type ${message.type} is not supported`); } @@ -344,9 +356,8 @@ async function processSingleMessage(message, authorizationData, destination, aut async function process(event) { const authInfo = await collectAuthorizationInfo(event); const response = await processSingleMessage( - event.message, + event, authInfo.authorizationData, - event.destination, authInfo.authorizationFlow, ); return response; @@ -377,12 +388,7 @@ const processRouterDest = async (inputs, reqMetadata) => { // unprocessed payload return getSuccessRespEvents( - await processSingleMessage( - input.message, - authInfo.authorizationData, - input.destination, - authInfo.authorizationFlow, - ), + await processSingleMessage(input, authInfo.authorizationData, authInfo.authorizationFlow), [input.metadata], input.destination, ); diff --git a/src/v0/destinations/salesforce/utils.js b/src/v0/destinations/salesforce/utils.js index 85061ce2b2..161471c92e 100644 --- a/src/v0/destinations/salesforce/utils.js +++ b/src/v0/destinations/salesforce/utils.js @@ -100,7 +100,7 @@ const salesforceResponseHandler = (destResponse, sourceMessage, authKey, authori * Utility method to construct the header to be used for SFDC API calls * The "Authorization: Bearer " header element needs to be passed * for authentication for all SFDC REST API calls - * @param {*} destination + * @param {destination: Record, metadata: Record} * @returns */ const getAccessTokenOauth = (metadata) => ({ @@ -108,7 +108,7 @@ const getAccessTokenOauth = (metadata) => ({ instanceUrl: metadata.secret?.instance_url, }); -const getAccessToken = async (destination) => { +const getAccessToken = async ({ destination, metadata }) => { const accessTokenKey = destination.ID; return ACCESS_TOKEN_CACHE.get(accessTokenKey, async () => { @@ -136,6 +136,7 @@ const getAccessToken = async (destination) => { endpointPath: '/services/oauth2/token', requestMethod: 'POST', module: 'router', + metadata, }, ); // If the request fails, throwing error. @@ -172,7 +173,7 @@ const collectAuthorizationInfo = async (event) => { authorizationData = getAccessTokenOauth(event.metadata); } else { authorizationFlow = LEGACY; - authorizationData = await getAccessToken(event.destination); + authorizationData = await getAccessToken(event); } return { authorizationFlow, authorizationData }; }; diff --git a/src/v0/destinations/sfmc/transform.js b/src/v0/destinations/sfmc/transform.js index bf474ff3f0..a433179f9c 100644 --- a/src/v0/destinations/sfmc/transform.js +++ b/src/v0/destinations/sfmc/transform.js @@ -31,7 +31,7 @@ const CONTACT_KEY_KEY = 'Contact Key'; // DOC: https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/access-token-s2s.htm -const getToken = async (clientId, clientSecret, subdomain) => { +const getToken = async (clientId, clientSecret, subdomain, metadata) => { const { processedResponse: processedResponseSfmc } = await handleHttpRequest( 'post', `https://${subdomain}.${ENDPOINTS.GET_TOKEN}`, @@ -49,6 +49,7 @@ const getToken = async (clientId, clientSecret, subdomain) => { endpointPath: '/token', requestMethod: 'POST', module: 'router', + metadata, }, ); @@ -194,7 +195,7 @@ const responseBuilderForMessageEvent = (message, subDomain, authToken, hashMapEv return response; }; -const responseBuilderSimple = async (message, category, destination) => { +const responseBuilderSimple = async ({ message, destination, metadata }, category) => { const { clientId, clientSecret, @@ -213,7 +214,7 @@ const responseBuilderSimple = async (message, category, destination) => { // map from an event name to uuid as true or false to determine to send uuid as primary key or not. const hashMapUUID = getHashFromArray(eventToUUID, 'event', 'uuid'); // token needed for authorization for subsequent calls - const authToken = await getToken(clientId, clientSecret, subDomain); + const authToken = await getToken(clientId, clientSecret, subDomain, metadata); // map from an event name to an event definition key. const hashMapEventDefinition = getHashFromArray(eventToDefinitionMapping, 'from', 'to'); // if createOrUpdateContacts is true identify calls for create and update of contacts will not occur. @@ -270,7 +271,7 @@ const responseBuilderSimple = async (message, category, destination) => { throw new ConfigurationError(`Event type '${category.type}' not supported`); }; -const processEvent = async (message, destination) => { +const processEvent = async ({ message, destination, metadata }) => { if (!message.type) { throw new InstrumentationError('Event type is required'); } @@ -290,12 +291,12 @@ const processEvent = async (message, destination) => { } // build the response - const response = await responseBuilderSimple(message, category, destination); + const response = await responseBuilderSimple({ message, destination, metadata }, category); return response; }; const process = async (event) => { - const response = await processEvent(event.message, event.destination); + const response = await processEvent(event); return response; };