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 Dec 12, 2024
1 parent aae7b3d commit 9ee2847
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 39 deletions.
19 changes: 11 additions & 8 deletions src/v0/destinations/iterable/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand All @@ -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,
};
56 changes: 31 additions & 25 deletions src/v0/destinations/iterable/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -772,47 +774,51 @@ 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;

Check warning on line 808 in src/v0/destinations/iterable/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/iterable/util.js#L808

Added line #L808 was not covered by tests
}
if (matchingPath.includes('disallowedEventNames')) {
return value === eventValues.eventName;

Check warning on line 811 in src/v0/destinations/iterable/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/iterable/util.js#L811

Added line #L811 was not covered by tests
}
return false;

Check warning on line 813 in src/v0/destinations/iterable/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/iterable/util.js#L813

Added line #L813 was not covered by tests
});

return {
isAbortable: true,
errorMsg: `Request failed for value "${matchingValue}" because it is "${matchingPath}".`,
};
}

// Return false and an empty error message if no error is found
return { isAbortable: false, errorMsg: '' };
}

Expand Down
30 changes: 24 additions & 6 deletions src/v0/destinations/iterable/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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: '[email protected]',
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('');
});
});
});

0 comments on commit 9ee2847

Please sign in to comment.