Skip to content

Commit

Permalink
refactor: code to address sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Dec 9, 2024
1 parent 12621c8 commit 3cddf41
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 181 deletions.
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 26 additions & 36 deletions src/cdk/v2/destinations/pinterest_tag/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-param-reassign */
const sha256 = require('sha256');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');

const { API_VERSION } = require('./config');
const { CommonUtils } = require('../../../../util/common');

const VALID_ACTION_SOURCES = ['app_android', 'app_ios', 'web', 'offline'];

Expand All @@ -21,9 +21,15 @@ const ecomEventMaps = [
},
];

const USER_NON_ARRAY_PROPERTIES = ['client_user_agent', 'client_ip_address'];
const USER_NON_ARRAY_PROPERTIES = [
'client_user_agent',
'client_ip_address',
'click_id',
'partner_id',
];

const getHashedValue = (key, value) => {
const transformValue = (key, value) => {
const arrayValue = CommonUtils.toArray(value);
switch (key) {
case 'em':
case 'ct':
Expand All @@ -32,33 +38,18 @@ const getHashedValue = (key, value) => {
case 'ln':
case 'fn':
case 'ge':
value = Array.isArray(value)
? value.map((val) => val.toString().trim().toLowerCase())
: value.toString().trim().toLowerCase();
break;
return arrayValue.map((val) => val.toString().trim().toLowerCase());
case 'ph':
// phone numbers should only contain digits & should not contain leading zeros
value = Array.isArray(value)
? value.map((val) => val.toString().replace(/\D/g, '').replace(/^0+/, ''))
: value.toString().replace(/\D/g, '').replace(/^0+/, '');
break;
return arrayValue.map((val) => val.toString().replace(/\D/g, '').replace(/^0+/, ''));
case 'zp':
// zip fields should only contain digits
value = Array.isArray(value)
? value.map((val) => val.toString().trim().replace(/\D/g, ''))
: value.toString().replace(/\D/g, '');
break;
case 'hashed_maids':
case 'external_id':
case 'db':
// no action needed on value
break;
return arrayValue.map((val) => val.toString().trim().replace(/\D/g, ''));
default:
return String(value);
return arrayValue;
}
return Array.isArray(value) ? value.map((val) => sha256(val)) : [sha256(value)];
};

const getHashedValue = (key, value) => transformValue(key, value).map((val) => sha256(val));

/**
*
* @param {*} userPayload Payload mapped from user fields
Expand All @@ -67,10 +58,15 @@ const getHashedValue = (key, value) => {
* Ref: https://s.pinimg.com/ct/docs/conversions_api/dist/v3.html
*/
const processUserPayload = (userPayload) => {
Object.keys(userPayload).forEach((key) => {
userPayload[key] = getHashedValue(key, userPayload[key]);
const newPayload = { ...userPayload };
Object.keys(newPayload).forEach((key) => {
if (USER_NON_ARRAY_PROPERTIES.includes(key)) {
newPayload[key] = String(newPayload[key]);
} else {
newPayload[key] = getHashedValue(key, newPayload[key]);
}
});
return userPayload;
return newPayload;
};

/**
Expand Down Expand Up @@ -99,22 +95,16 @@ const processHashedUserPayload = (userPayload, message) => {
const processedHashedUserPayload = {};
Object.keys(userPayload).forEach((key) => {
if (!USER_NON_ARRAY_PROPERTIES.includes(key)) {
if (Array.isArray(userPayload[key])) {
processedHashedUserPayload[key] = [...userPayload[key]];
} else {
processedHashedUserPayload[key] = [userPayload[key]];
}
processedHashedUserPayload[key] = CommonUtils.toArray(userPayload[key]);
} else {
processedHashedUserPayload[key] = userPayload[key];
}
});
// multiKeyMap will works on only specific values like m, male, MALE, f, F, Female
// if hashed data is sent from the user, it is directly set over here
const gender = message.traits?.gender || message.context?.traits?.gender;
if (gender && Array.isArray(gender)) {
processedHashedUserPayload.ge = [...gender];
} else if (gender) {
processedHashedUserPayload.ge = [gender];
if (gender) {
processedHashedUserPayload.ge = CommonUtils.toArray(gender);
}
return processedHashedUserPayload;
};
Expand Down
3 changes: 2 additions & 1 deletion src/v0/destinations/amazon_audience/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const buildResponseWithUsers = (users, action, config, jobIdList, secret) => {
if (!secret?.clientId) {
throw new OAuthSecretError('OAuth - Client Id not found');
}
const externalId = `Rudderstack_${sha256(`${jobIdList}`)}`;
const jobIdHash = sha256(String(jobIdList));
const externalId = `Rudderstack_${jobIdHash}`;
const response = defaultRequestConfig();
response.endpoint = '';
response.method = defaultPostRequestConfig.requestMethod;
Expand Down
Loading

0 comments on commit 3cddf41

Please sign in to comment.