From 8c89d8953e386cb64bd61169927d16ba2b49c2a5 Mon Sep 17 00:00:00 2001 From: shrouti1507 Date: Fri, 10 Nov 2023 18:47:59 +0530 Subject: [PATCH] fix: review comments addressed --- src/v0/destinations/salesforce/config.js | 4 ++++ src/v0/destinations/salesforce/transform.js | 18 ++++++++--------- src/v0/destinations/salesforce/utils.js | 22 +++++++++++++++------ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/v0/destinations/salesforce/config.js b/src/v0/destinations/salesforce/config.js index 6c0ccde80f..1425bad51b 100644 --- a/src/v0/destinations/salesforce/config.js +++ b/src/v0/destinations/salesforce/config.js @@ -24,6 +24,8 @@ const SF_TOKEN_REQUEST_URL = 'https://login.salesforce.com/services/oauth2/token const SF_TOKEN_REQUEST_URL_SANDBOX = 'https://test.salesforce.com/services/oauth2/token'; const DESTINATION = 'Salesforce'; +const OAUTH = 'oauth'; +const LEGACY = 'legacy'; const mappingConfig = getMappingConfig(ConfigCategory, __dirname); @@ -37,4 +39,6 @@ module.exports = { ignoredContactTraits: mappingConfig[ConfigCategory.IGNORE_CONTACT.name], ACCESS_TOKEN_CACHE_TTL, DESTINATION, + OAUTH, + LEGACY, }; diff --git a/src/v0/destinations/salesforce/transform.js b/src/v0/destinations/salesforce/transform.js index 4038c59ef7..123798cb2c 100644 --- a/src/v0/destinations/salesforce/transform.js +++ b/src/v0/destinations/salesforce/transform.js @@ -11,6 +11,7 @@ const { identifyContactMappingJson, ignoredLeadTraits, ignoredContactTraits, + OAUTH, } = require('./config'); const { removeUndefinedValues, @@ -28,7 +29,7 @@ const { generateErrorObject, isHttpStatusSuccess, } = require('../../util'); -const { salesforceResponseHandler, collectAuthorizationInfo } = require('./utils'); +const { salesforceResponseHandler, collectAuthorizationInfo, getAuthHeader } = require('./utils'); const { handleHttpRequest } = require('../../../adapters/network'); const { JSON_MIME_TYPE } = require('../../util/constant'); @@ -89,15 +90,12 @@ function responseBuilderSimple( } const response = defaultRequestConfig(); - const getAuthHeader = (authInfo) => { - const { authorizationFlow, authorizationData } = authInfo; - return authorizationFlow === 'oauth' - ? { Authorization: `Bearer ${authorizationData.token}` } - : { Authorization: authorizationData.token }; - } response.method = defaultPostRequestConfig.requestMethod; - response.headers = finalHeader; + response.headers = { + 'Content-Type': JSON_MIME_TYPE, + ...getAuthHeader({ authorizationFlow, authorizationData }), + }; response.body.JSON = removeUndefinedValues(rawPayload); response.endpoint = targetEndpoint; @@ -115,7 +113,7 @@ async function getSaleforceIdForRecord( ) { const objSearchUrl = `${authorizationData.instanceUrl}/services/data/v${SF_API_VERSION}/parameterizedSearch/?q=${identifierValue}&sobject=${objectType}&in=${identifierType}&${objectType}.fields=id,${identifierType}`; const finalHeader = - authorizationFlow === 'oauth' + authorizationFlow === OAUTH ? { Authorization: `Bearer ${authorizationData.token}` } : { Authorization: authorizationData.token }; const { processedResponse: processedsfSearchResponse } = await handleHttpRequest( @@ -230,7 +228,7 @@ async function getSalesforceIdFromPayload( } const leadQueryUrl = `${authorizationData.instanceUrl}/services/data/v${SF_API_VERSION}/parameterizedSearch/?q=${email}&sobject=Lead&Lead.fields=id,IsConverted,ConvertedContactId,IsDeleted`; const finalHeader = - authorizationFlow === 'oauth' + authorizationFlow === OAUTH ? { Authorization: `Bearer ${authorizationData.token}` } : { Authorization: authorizationData.token }; diff --git a/src/v0/destinations/salesforce/utils.js b/src/v0/destinations/salesforce/utils.js index 3ff340b100..aacc737842 100644 --- a/src/v0/destinations/salesforce/utils.js +++ b/src/v0/destinations/salesforce/utils.js @@ -11,6 +11,8 @@ const { SF_TOKEN_REQUEST_URL_SANDBOX, SF_TOKEN_REQUEST_URL, DESTINATION, + LEGACY, + OAUTH, } = require('./config'); const ACCESS_TOKEN_CACHE = new Cache(ACCESS_TOKEN_CACHE_TTL); @@ -31,7 +33,7 @@ const salesforceResponseHandler = (destResponse, sourceMessage, authKey, authori const matchErrorCode = (errorCode) => response && Array.isArray(response) && response.some((resp) => resp?.errorCode === errorCode); if (status === 401 && authKey && matchErrorCode('INVALID_SESSION_ID')) { - if (authorizationFlow === 'legacy') { + if (authorizationFlow === LEGACY) { // checking for invalid/expired token errors and evicting cache in that case // rudderJobMetadata contains some destination info which is being used to evict the cache ACCESS_TOKEN_CACHE.del(authKey); @@ -40,7 +42,7 @@ const salesforceResponseHandler = (destResponse, sourceMessage, authKey, authori `${DESTINATION} Request Failed - due to "INVALID_SESSION_ID", (Retryable) ${sourceMessage}`, 500, response, - authorizationFlow === 'legacy' ? '' : getAuthErrCategoryFromStCode(status), + authorizationFlow === LEGACY ? '' : getAuthErrCategoryFromStCode(status), ); } else if (status === 403 && matchErrorCode('REQUEST_LIMIT_EXCEEDED')) { // If the error code is REQUEST_LIMIT_EXCEEDED, you’ve exceeded API request limits in your org. @@ -134,7 +136,7 @@ const getAccessToken = async (destination) => { processedResponse, `:- authentication failed during fetching access token.`, accessTokenKey, - 'legacy', + LEGACY, ); } const token = httpResponse.response.data; @@ -144,7 +146,7 @@ const getAccessToken = async (destination) => { processedResponse, `:- authentication failed could not retrieve authorization token.`, accessTokenKey, - 'legacy', + LEGACY, ); } return { @@ -158,18 +160,26 @@ const collectAuthorizationInfo = async (event) => { let authorizationFlow; let authorizationData; if (isDefinedAndNotNull(event.metadata?.secret)) { - authorizationFlow = 'oauth'; + authorizationFlow = OAUTH; authorizationData = getAccessTokenOauth(event.metadata); } else { - authorizationFlow = 'legacy'; + authorizationFlow = LEGACY; authorizationData = await getAccessToken(event.destination); } return { authorizationFlow, authorizationData }; }; +const getAuthHeader = (authInfo) => { + const { authorizationFlow, authorizationData } = authInfo; + return authorizationFlow === OAUTH + ? { Authorization: `Bearer ${authorizationData.token}` } + : { Authorization: authorizationData.token }; +}; + module.exports = { getAccessTokenOauth, salesforceResponseHandler, getAccessToken, collectAuthorizationInfo, + getAuthHeader, };