Skip to content

Commit

Permalink
fix: review comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Nov 10, 2023
1 parent 9c257f0 commit 8c89d89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/v0/destinations/salesforce/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -37,4 +39,6 @@ module.exports = {
ignoredContactTraits: mappingConfig[ConfigCategory.IGNORE_CONTACT.name],
ACCESS_TOKEN_CACHE_TTL,
DESTINATION,
OAUTH,
LEGACY,
};
18 changes: 8 additions & 10 deletions src/v0/destinations/salesforce/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
identifyContactMappingJson,
ignoredLeadTraits,
ignoredContactTraits,
OAUTH,
} = require('./config');
const {
removeUndefinedValues,
Expand All @@ -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');

Expand Down Expand Up @@ -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;

Expand All @@ -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 };

Check warning on line 118 in src/v0/destinations/salesforce/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/salesforce/transform.js#L117-L118

Added lines #L117 - L118 were not covered by tests
const { processedResponse: processedsfSearchResponse } = await handleHttpRequest(
Expand Down Expand Up @@ -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}` }

Check warning on line 232 in src/v0/destinations/salesforce/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/salesforce/transform.js#L232

Added line #L232 was not covered by tests
: { Authorization: authorizationData.token };

Expand Down
22 changes: 16 additions & 6 deletions src/v0/destinations/salesforce/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Check warning on line 39 in src/v0/destinations/salesforce/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/salesforce/utils.js#L39

Added line #L39 was not covered by tests
Expand All @@ -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.
Expand Down Expand Up @@ -134,7 +136,7 @@ const getAccessToken = async (destination) => {
processedResponse,
`:- authentication failed during fetching access token.`,
accessTokenKey,
'legacy',
LEGACY,
);
}
const token = httpResponse.response.data;
Expand All @@ -144,7 +146,7 @@ const getAccessToken = async (destination) => {
processedResponse,
`:- authentication failed could not retrieve authorization token.`,
accessTokenKey,
'legacy',
LEGACY,
);
}
return {
Expand All @@ -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,
};

0 comments on commit 8c89d89

Please sign in to comment.