From 9ee284721d10d05db82bfcc6b431ea24940025fe Mon Sep 17 00:00:00 2001 From: shrouti1507 Date: Thu, 12 Dec 2024 18:14:56 +0530 Subject: [PATCH] fix: review comments addressed --- src/v0/destinations/iterable/config.js | 19 ++++---- src/v0/destinations/iterable/util.js | 56 +++++++++++++---------- src/v0/destinations/iterable/util.test.js | 30 +++++++++--- 3 files changed, 66 insertions(+), 39 deletions(-) diff --git a/src/v0/destinations/iterable/config.js b/src/v0/destinations/iterable/config.js index 576717504e..a0be8544aa 100644 --- a/src/v0/destinations/iterable/config.js +++ b/src/v0/destinations/iterable/config.js @@ -87,19 +87,21 @@ const IDENTIFY_MAX_BODY_SIZE_IN_BYTES = 4000000; const TRACK_MAX_BATCH_SIZE = 8000; -const API_RESPONSE_PATHS = [ - 'invalidEmails', +const ITERABLE_RESPONSE_USER_ID_PATHS = [ 'invalidUserIds', - 'disallowedEventNames', - 'failedUpdates.invalidEmails', 'failedUpdates.invalidUserIds', - 'failedUpdates.notFoundEmails', 'failedUpdates.notFoundUserIds', - 'failedUpdates.forgottenEmails', 'failedUpdates.forgottenUserIds', 'failedUpdates.conflictUserIds', - 'failedUpdates.conflictEmails', 'failedUpdates.invalidDataUserIds', +]; + +const ITERABLE_RESPONSE_EMAIL_PATHS = [ + 'invalidEmails', + 'failedUpdates.invalidEmails', + 'failedUpdates.notFoundEmails', + 'failedUpdates.forgottenEmails', + 'failedUpdates.conflictEmails', 'failedUpdates.invalidDataEmails', ]; @@ -110,6 +112,7 @@ module.exports = { TRACK_MAX_BATCH_SIZE, IDENTIFY_MAX_BATCH_SIZE, IDENTIFY_MAX_BODY_SIZE_IN_BYTES, - API_RESPONSE_PATHS, + ITERABLE_RESPONSE_USER_ID_PATHS, + ITERABLE_RESPONSE_EMAIL_PATHS, BULK_ENDPOINTS, }; diff --git a/src/v0/destinations/iterable/util.js b/src/v0/destinations/iterable/util.js index 2a604f9bea..05f6075096 100644 --- a/src/v0/destinations/iterable/util.js +++ b/src/v0/destinations/iterable/util.js @@ -16,8 +16,10 @@ const { TRACK_MAX_BATCH_SIZE, IDENTIFY_MAX_BATCH_SIZE, IDENTIFY_MAX_BODY_SIZE_IN_BYTES, - API_RESPONSE_PATHS, + // API_RESPONSE_PATHS, constructEndpoint, + ITERABLE_RESPONSE_USER_ID_PATHS, + ITERABLE_RESPONSE_EMAIL_PATHS, } = require('./config'); const { JSON_MIME_TYPE } = require('../../util/constant'); const { EventType, MappedToDestinationKey } = require('../../../constants'); @@ -772,39 +774,44 @@ function checkIfEventIsAbortableAndExtractErrorMessage(event, destinationRespons return { isAbortable: false, errorMsg: '' }; } - // Flatten dataFields values into a single array - const dataFieldsValues = event.dataFields ? Object.values(event.dataFields).flat() : []; - - const eventValues = new Set( - [ - event.email, - event.preferUserId, - event.mergeNestedObjects, - event.userId, - event.eventName, - event.id, - event.createdAt, - event.campaignId, - event.templateId, - event.createNewFields, - ...dataFieldsValues, // Spread the flattened dataFields values - ].filter((value) => value !== undefined), - ); + const eventValues = { + email: event.email, + userId: event.userId, + eventName: event.eventName, + }; - const matchingPath = API_RESPONSE_PATHS.find((path) => { + const isValueInResponseArray = (path, value) => { const responseArray = path .split('.') .reduce((obj, key) => obj?.[key], destinationResponse.response); + return Array.isArray(responseArray) && responseArray.includes(value); + }; - return Array.isArray(responseArray) && responseArray.some((value) => eventValues.has(value)); - }); + const matchingPath = + ITERABLE_RESPONSE_USER_ID_PATHS.find((userIdPath) => + isValueInResponseArray(userIdPath, eventValues.userId), + ) || + ITERABLE_RESPONSE_EMAIL_PATHS.find((emailPath) => + isValueInResponseArray(emailPath, eventValues.email), + ) || + isValueInResponseArray('disallowedEventNames', eventValues.eventName); if (matchingPath) { const responseArray = matchingPath .split('.') .reduce((obj, key) => obj?.[key], destinationResponse.response); - - const matchingValue = responseArray.find((value) => eventValues.has(value)); + const matchingValue = responseArray.find((value) => { + if (ITERABLE_RESPONSE_EMAIL_PATHS.some((emailPath) => matchingPath.includes(emailPath))) { + return value === eventValues.email; + } + if (ITERABLE_RESPONSE_USER_ID_PATHS.some((userIdPath) => matchingPath.includes(userIdPath))) { + return value === eventValues.userId; + } + if (matchingPath.includes('disallowedEventNames')) { + return value === eventValues.eventName; + } + return false; + }); return { isAbortable: true, @@ -812,7 +819,6 @@ function checkIfEventIsAbortableAndExtractErrorMessage(event, destinationRespons }; } - // Return false and an empty error message if no error is found return { isAbortable: false, errorMsg: '' }; } diff --git a/src/v0/destinations/iterable/util.test.js b/src/v0/destinations/iterable/util.test.js index 52032a38dc..ea3d7ef617 100644 --- a/src/v0/destinations/iterable/util.test.js +++ b/src/v0/destinations/iterable/util.test.js @@ -864,10 +864,9 @@ describe('iterable utils test', () => { // Returns appropriate error message for abortable event - // Processes events with additional dataFields correctly - it('should process events with additional dataFields correctly', () => { + it('should find the right value for which it should fail and passes otherwise', () => { const event = { - email: 'test@example.com', + email: 'test', userId: 'user123', eventName: 'purchase', dataFields: { customField1: 'value1', customField2: 'value2' }, @@ -876,16 +875,35 @@ describe('iterable utils test', () => { response: { failCount: 1, failedUpdates: { - invalidDataEmails: ['value1'], + invalidEmails: ['test'], }, }, }; const result = checkIfEventIsAbortableAndExtractErrorMessage(event, destinationResponse); expect(result).toEqual({ isAbortable: true, - errorMsg: - 'Request failed for value "value1" because it is "failedUpdates.invalidDataEmails".', + errorMsg: 'Request failed for value "test" because it is "failedUpdates.invalidEmails".', }); }); + + it('should find the right value for which it should fail', () => { + const event = { + email: 'test@gmail.com', + userId: 'user123', + eventName: 'purchase', + dataFields: { customField1: 'test', customField2: 'value2' }, + }; + const destinationResponse = { + response: { + failCount: 1, + failedUpdates: { + invalidEmails: ['test'], + }, + }, + }; + const result = checkIfEventIsAbortableAndExtractErrorMessage(event, destinationResponse); + expect(result.isAbortable).toBe(false); + expect(result.errorMsg).toBe(''); + }); }); });