Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fix sonar issues in various components #4017

Merged
merged 9 commits into from
Feb 5, 2025
2 changes: 1 addition & 1 deletion src/v0/destinations/braze/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ const collectStatsForAliasFailure = (brazeResponse, destinationId) => {
if (!isDefinedAndNotNull(brazeResponse)) {
return;
}
const { aliases_processed: aliasesProcessed, errors } = brazeResponse;
const { aliases_processed: aliasesProcessed } = brazeResponse;
if (aliasesProcessed === 0) {
stats.increment('braze_alias_failure_count', { destination_id: destinationId });
}
Expand Down
1 change: 0 additions & 1 deletion src/v0/destinations/clevertap/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ const process = (event) => processEvent(event.message, event.destination);
const processRouterDest = (inputs, reqMetadata) => {
const eventsChunk = [];
const errorRespList = [];
// const { destination } = inputs[0];

inputs.forEach((event) => {
try {
Expand Down
214 changes: 106 additions & 108 deletions src/v0/destinations/fb_custom_audience/recordTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,68 @@
generateAppSecretProof,
} = require('./util');

const processRecordEventArray = (
recordChunksArray,
userSchema,
isHashRequired,
disableFormat,
paramsPayload,
prepareParams,
destination,
operation,
audienceId,
) => {
/**
* Processes a single record and updates the data element.
* @param {Object} record - The record to process.
* @param {Array} userSchema - The schema defining user properties.
* @param {boolean} isHashRequired - Whether hashing is required.
* @param {boolean} disableFormat - Whether formatting is disabled.
* @returns {Object} - The processed data element and metadata.
*/
const processRecord = (record, userSchema, isHashRequired, disableFormat) => {
const { fields } = record.message;
let dataElement = [];
let nullUserData = true;

userSchema.forEach((eachProperty) => {
const userProperty = fields[eachProperty];
let updatedProperty = userProperty;

if (isHashRequired && !disableFormat) {
updatedProperty = ensureApplicableFormat(eachProperty, userProperty);
}

dataElement = getUpdatedDataElement(dataElement, isHashRequired, eachProperty, updatedProperty);

if (dataElement[dataElement.length - 1]) {
nullUserData = false;
}
});

if (nullUserData) {
koladilip marked this conversation as resolved.
Show resolved Hide resolved
stats.increment('fb_custom_audience_event_having_all_null_field_values_for_a_user', {

Check warning on line 55 in src/v0/destinations/fb_custom_audience/recordTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/fb_custom_audience/recordTransform.js#L55

Added line #L55 was not covered by tests
destinationId: record.destination.ID,
nullFields: userSchema,
});
}

return { dataElement, metadata: record.metadata };
};

/**
* Processes an array of record chunks and prepares the payload for sending.
* @param {Array} recordChunksArray - The array of record chunks.
* @param {Object} config - Configuration object containing userSchema, isHashRequired, disableFormat, etc.
* @param {Object} destination - The destination configuration.
* @param {string} operation - The operation to perform (e.g., 'add', 'remove').
* @param {string} audienceId - The audience ID.
* @returns {Array} - The response events to send.
*/
const processRecordEventArray = (recordChunksArray, config, destination, operation, audienceId) => {
const { userSchema, isHashRequired, disableFormat, paramsPayload, prepareParams } = config;
const toSendEvents = [];
const metadata = [];

recordChunksArray.forEach((recordArray) => {
const data = [];
recordArray.forEach((input) => {
const { fields } = input.message;
let dataElement = [];
let nullUserData = true;

userSchema.forEach((eachProperty) => {
const userProperty = fields[eachProperty];
let updatedProperty = userProperty;

if (isHashRequired && !disableFormat) {
updatedProperty = ensureApplicableFormat(eachProperty, userProperty);
}

dataElement = getUpdatedDataElement(
dataElement,
isHashRequired,
eachProperty,
updatedProperty,
);

if (dataElement[dataElement.length - 1]) {
nullUserData = false;
}
});

if (nullUserData) {
stats.increment('fb_custom_audience_event_having_all_null_field_values_for_a_user', {
destinationId: destination.ID,
nullFields: userSchema,
});
}
data.push(dataElement);
metadata.push(input.metadata);
const data = recordArray.map((input) => {
const { dataElement, metadata: recordMetadata } = processRecord(
input,
userSchema,
isHashRequired,
disableFormat,
);
metadata.push(recordMetadata);
return dataElement;
});

const prepareFinalPayload = lodash.cloneDeep(paramsPayload);
Expand All @@ -90,16 +104,19 @@
};

const builtResponse = responseBuilderSimple(wrappedResponse, audienceId);

toSendEvents.push(builtResponse);
});
});

const response = getSuccessRespEvents(toSendEvents, metadata, destination, true);

return response;
return getSuccessRespEvents(toSendEvents, metadata, destination, true);
};

/**
* Prepares the payload for the given events and configuration.
* @param {Array} events - The events to process.
* @param {Object} config - The configuration object.
* @returns {Array} - The final response payload.
*/
function preparePayload(events, config) {
const { audienceId, userSchema, isRaw, type, subType, isHashRequired, disableFormat } = config;
const { destination } = events[0];
Expand Down Expand Up @@ -138,72 +155,39 @@
record.message.action?.toLowerCase(),
);

let insertResponse;
let deleteResponse;
let updateResponse;

if (groupedRecordsByAction.delete) {
const deleteRecordChunksArray = returnArrayOfSubarrays(
groupedRecordsByAction.delete,
MAX_USER_COUNT,
);
deleteResponse = processRecordEventArray(
deleteRecordChunksArray,
cleanUserSchema,
isHashRequired,
disableFormat,
paramsPayload,
prepareParams,
destination,
'remove',
audienceId,
);
}

if (groupedRecordsByAction.insert) {
const insertRecordChunksArray = returnArrayOfSubarrays(
groupedRecordsByAction.insert,
MAX_USER_COUNT,
);

insertResponse = processRecordEventArray(
insertRecordChunksArray,
cleanUserSchema,
isHashRequired,
disableFormat,
paramsPayload,
prepareParams,
destination,
'add',
audienceId,
);
}
const processAction = (action, operation) => {
if (groupedRecordsByAction[action]) {
const recordChunksArray = returnArrayOfSubarrays(
groupedRecordsByAction[action],
MAX_USER_COUNT,
);
return processRecordEventArray(
recordChunksArray,
{
userSchema: cleanUserSchema,
isHashRequired,
disableFormat,
paramsPayload,
prepareParams,
},
destination,
operation,
audienceId,
);
}
return null;
};

if (groupedRecordsByAction.update) {
const updateRecordChunksArray = returnArrayOfSubarrays(
groupedRecordsByAction.update,
MAX_USER_COUNT,
);
updateResponse = processRecordEventArray(
updateRecordChunksArray,
cleanUserSchema,
isHashRequired,
disableFormat,
paramsPayload,
prepareParams,
destination,
'add',
audienceId,
);
}
const deleteResponse = processAction('delete', 'remove');
const insertResponse = processAction('insert', 'add');
const updateResponse = processAction('update', 'add');

const errorResponse = getErrorResponse(groupedRecordsByAction);

const finalResponse = createFinalResponse(
deleteResponse,
insertResponse,
updateResponse,

errorResponse,
);
if (finalResponse.length === 0) {
Expand All @@ -214,6 +198,11 @@
return finalResponse;
}

/**
* Processes record inputs for V1 flow.
* @param {Array} groupedRecordInputs - The grouped record inputs.
* @returns {Array} - The processed payload.
*/
function processRecordInputsV1(groupedRecordInputs) {
const { destination } = groupedRecordInputs[0];
const { message } = groupedRecordInputs[0];
Expand All @@ -239,11 +228,15 @@
});
}

/**
* Processes record inputs for V2 flow.
* @param {Array} groupedRecordInputs - The grouped record inputs.
* @returns {Array} - The processed payload.
*/
const processRecordInputsV2 = (groupedRecordInputs) => {
const { connection, message } = groupedRecordInputs[0];
const { isHashRequired, disableFormat, type, subType, isRaw, audienceId } =
connection.config.destination;
// Ref: https://www.notion.so/rudderstacks/VDM-V2-Final-Config-and-Record-EventPayload-8cc80f3d88ad46c7bc43df4b87a0bbff
const identifiers = message?.identifiers;
let userSchema;
if (identifiers) {
Expand All @@ -267,6 +260,11 @@
});
};

/**
* Processes record inputs based on the flow type.
* @param {Array} groupedRecordInputs - The grouped record inputs.
* @returns {Array} - The processed payload.
*/
function processRecordInputs(groupedRecordInputs) {
const event = groupedRecordInputs[0];
// First check for rETL flow and second check for ES flow
Expand Down
1 change: 0 additions & 1 deletion src/v0/destinations/ga4_v2/customMappingsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ const handleCustomMappings = (message, Config) => {
const boilerplateOperations = (ga4Payload, message, Config, eventName) => {
removeReservedParameterPrefixNames(ga4Payload.events[0].params);
ga4Payload.events[0].name = eventName;
const integrationsObj = getIntegrationsObj(message, 'ga4_v2');
koladilip marked this conversation as resolved.
Show resolved Hide resolved

if (ga4Payload.events[0].params) {
ga4Payload.events[0].params = removeInvalidParams(
Expand Down
1 change: 0 additions & 1 deletion src/v0/destinations/google_cloud_function/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function generateBatchedPayload(events) {
let batchEventResponse = events.map((event) => event.message);
// Batch event into dest batch structure
events.forEach((ev) => {
// batchResponseList.push(ev.message.body.JSON);
metadata.push(ev.metadata);
});
batchEventResponse = {
Expand Down
4 changes: 2 additions & 2 deletions src/v0/destinations/intercom/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ function validateAndBuildResponse(message, payload, category, destination) {
};
response.userId = message.anonymousId;
const messageType = message.type.toLowerCase();
respList.push(response);
koladilip marked this conversation as resolved.
Show resolved Hide resolved
switch (messageType) {
case EventType.IDENTIFY:
response.body.JSON = removeUndefinedAndNullValues(
Expand All @@ -188,7 +189,6 @@ function validateAndBuildResponse(message, payload, category, destination) {
break;
case EventType.GROUP: {
response.body.JSON = removeUndefinedAndNullValues(buildCustomAttributes(message, payload));
respList.push(response);
if (checkIfEmailOrUserIdPresent(message, destination.Config)) {
const attachUserAndCompanyResponse = attachUserAndCompany(message, destination.Config);
attachUserAndCompanyResponse.userId = message.anonymousId;
Expand All @@ -200,7 +200,7 @@ function validateAndBuildResponse(message, payload, category, destination) {
throw new InstrumentationError(`Message type ${messageType} not supported`);
}

return messageType === EventType.GROUP ? respList : response;
return respList;
}

function processSingleMessage(message, destination) {
Expand Down
2 changes: 1 addition & 1 deletion test/integrations/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ beforeAll(async () => {
});

afterAll(async () => {
await createHttpTerminator({ server }).terminate();
if (opts.generate === 'true') {
const callsDataStr = responses.join('\n');
const calls = `
Expand All @@ -89,7 +90,6 @@ afterAll(async () => {
`;
appendFileSync(join(__dirname, 'destinations', opts.destination, 'network.ts'), calls);
}
await createHttpTerminator({ server }).terminate();
});
let mockAdapter;
if (!opts.generate || opts.generate === 'false') {
Expand Down
Loading