Skip to content

Commit

Permalink
fix: merge same emails, phone
Browse files Browse the repository at this point in the history
  • Loading branch information
AASHISH MALIK committed Dec 6, 2024
1 parent 36a2ca3 commit 35db010
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 7 deletions.
122 changes: 122 additions & 0 deletions src/v0/destinations/braze/braze.util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
getPurchaseObjs,
setAliasObject,
handleReservedProperties,
combineSubscriptionGroups,
} = require('./util');
const { processBatch } = require('./util');
const {
Expand Down Expand Up @@ -1758,3 +1759,124 @@ describe('handleReservedProperties', () => {
});
});
});

describe('combineSubscriptionGroups', () => {
it('should merge external_ids, emails, and phones for the same subscription_group_id and subscription_state', () => {
const input = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1', 'id2'],
emails: ['email1@example.com', 'email2@example.com'],
phones: ['+1234567890', '+0987654321'],
},
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id2', 'id3'],
emails: ['email2@example.com', 'email3@example.com'],
phones: ['+1234567890', '+1122334455'],
},
];

const expectedOutput = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1', 'id2', 'id3'],
emails: ['email1@example.com', 'email2@example.com', 'email3@example.com'],
phones: ['+1234567890', '+0987654321', '+1122334455'],
},
];

const result = combineSubscriptionGroups(input);
expect(result).toEqual(expectedOutput);
});

it('should handle groups with missing external_ids, emails, or phones', () => {
const input = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1'],
},
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
emails: ['email1@example.com'],
},
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
phones: ['+1234567890'],
},
];

const expectedOutput = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1'],
emails: ['email1@example.com'],
phones: ['+1234567890'],
},
];

const result = combineSubscriptionGroups(input);
expect(result).toEqual(expectedOutput);
});

it('should handle multiple unique subscription groups', () => {
const input = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1'],
},
{
subscription_group_id: 'group2',
subscription_state: 'Unsubscribed',
external_ids: ['id2'],
emails: ['email2@example.com'],
},
];

const expectedOutput = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1'],
},
{
subscription_group_id: 'group2',
subscription_state: 'Unsubscribed',
external_ids: ['id2'],
emails: ['email2@example.com'],
},
];

const result = combineSubscriptionGroups(input);
expect(result).toEqual(expectedOutput);
});

it('should not include undefined fields in the output', () => {
const input = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1'],
},
];

const expectedOutput = [
{
subscription_group_id: 'group1',
subscription_state: 'Subscribed',
external_ids: ['id1'],
},
];

const result = combineSubscriptionGroups(input);
expect(result).toEqual(expectedOutput);
});
});
31 changes: 24 additions & 7 deletions src/v0/destinations/braze/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,40 @@ const getEndpointFromConfig = (destination) => {
return endpoint;
};

// Merges the external_ids with the same group id and state
const deduplicateSubscriptionGroups = (subscriptionGroups) => {
// Merges external_ids, emails, and phones for entries with the same subscription_group_id and subscription_state
const combineSubscriptionGroups = (subscriptionGroups) => {
const uniqueGroups = {};

subscriptionGroups.forEach((group) => {
const key = `${group.subscription_group_id}-${group.subscription_state}`;
if (!uniqueGroups[key]) {
uniqueGroups[key] = {
...group,
external_ids: [...(group.external_ids || [])],
emails: [...(group.emails || [])],
phones: [...(group.phones || [])],
};
} else {
uniqueGroups[key].external_ids.push(...(group.external_ids || []));
uniqueGroups[key].emails.push(...(group.emails || []));
uniqueGroups[key].phones.push(...(group.phones || []));
}
});

return Object.values(uniqueGroups).map((group) => {
const result = {
subscription_group_id: group.subscription_group_id,
subscription_state: group.subscription_state,
external_ids: [...new Set(group.external_ids)],
};
if (group.emails?.length) {
result.emails = [...new Set(group.emails)];
}
if (group.phones?.length) {
result.phones = [...new Set(group.phones)];
}
return result;
});
return Object.values(uniqueGroups).map((group) => ({
...group,
external_ids: [...new Set(group.external_ids)],
}));
};

const CustomAttributeOperationUtil = {
Expand Down Expand Up @@ -407,7 +423,7 @@ function prepareGroupAndAliasBatch(arrayChunks, responseArray, destination, type
}

// Deduplicate the subscription groups before constructing the response body
const deduplicatedSubscriptionGroups = deduplicateSubscriptionGroups(subscription_groups);
const deduplicatedSubscriptionGroups = combineSubscriptionGroups(subscription_groups);

response.body.JSON = removeUndefinedAndNullValues({
subscription_groups: deduplicatedSubscriptionGroups,
Expand Down Expand Up @@ -793,4 +809,5 @@ module.exports = {
collectStatsForAliasFailure,
collectStatsForAliasMissConfigurations,
handleReservedProperties,
combineSubscriptionGroups,
};

0 comments on commit 35db010

Please sign in to comment.