Skip to content

Commit

Permalink
chore: introduce new statuscode for filter and suppress events
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Sep 25, 2023
1 parent cec8b8b commit 14aa4e0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 131 deletions.
2 changes: 2 additions & 0 deletions src/util/errorNotifier/bugsnag.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
UnhandledStatusCodeError,
UnauthorizedError,
NetworkInstrumentationError,
FilteredEventsError,
} = require('../../v0/util/errorTypes');

const {
Expand All @@ -48,6 +49,7 @@ const errorTypesDenyList = [
NetworkInstrumentationError,
CDKCustomError,
DataValidationError,
FilteredEventsError,
];

const pathsDenyList = [
Expand Down
9 changes: 6 additions & 3 deletions src/v0/destinations/braze/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ const {
simpleProcessRouterDestSync,
simpleProcessRouterDest,
} = require('../../util');
const { InstrumentationError, NetworkError, FilteredEventsError } = require('../../util/errorTypes');
const {
InstrumentationError,
NetworkError,
FilteredEventsError,
} = require('../../util/errorTypes');
const {
ConfigCategory,
mappingConfig,
Expand Down Expand Up @@ -80,7 +84,7 @@ function getIdentifyPayload(message) {
let payload = {};
payload = setAliasObjectWithAnonId(payload, message);
payload = setExternalId(payload, message);
return { aliases_to_identify: [payload], merge_behavior: "merge" };
return { aliases_to_identify: [payload], merge_behavior: 'merge' };
}

function populateCustomAttributesWithOperation(
Expand Down Expand Up @@ -239,7 +243,6 @@ function processTrackWithUserAttributes(message, destination, mappingJson, proce
} else {
throw new FilteredEventsError(
'[Braze Deduplication]: Duplicate user detected, the user is dropped',
'filtered'
);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/v0/destinations/braze/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,14 @@ const processBatch = (transformedEvents) => {
const purchaseArray = [];
const successMetadata = [];
const failureResponses = [];
const filteredResponses = [];
const subscriptionsArray = [];
const mergeUsersArray = [];
for (const transformedEvent of transformedEvents) {
if (!isHttpStatusSuccess(transformedEvent?.statusCode)) {
failureResponses.push(transformedEvent);
} else if (transformedEvent?.statusCode === 298) {
filteredResponses.push(transformedEvent);
} else if (transformedEvent?.batchedRequest?.body?.JSON) {
const { attributes, events, purchases, subscription_groups, merge_updates } =
transformedEvent.batchedRequest.body.JSON;
Expand Down Expand Up @@ -446,6 +449,10 @@ const processBatch = (transformedEvents) => {
finalResponse.push(...failureResponses);
}

if (filteredResponses.length > 0) {
finalResponse.push(...filteredResponses);
}

return finalResponse;
};

Expand Down
11 changes: 5 additions & 6 deletions src/v0/destinations/klaviyo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const {
populateCustomFieldsFromTraits,
batchSubscribeEvents,
getIdFromNewOrExistingProfile,
profileUpdateResponseBuilder,
} = require('./util');
const {
defaultRequestConfig,
Expand Down Expand Up @@ -106,10 +105,10 @@ const identifyRequestHandler = async (message, category, destination) => {
},
};

const profileId = await getIdFromNewOrExistingProfile(endpoint, payload, requestOptions);
const response = await getIdFromNewOrExistingProfile(endpoint, payload, requestOptions);

// Update Profile
const responseArray = [profileUpdateResponseBuilder(payload, profileId, category, privateApiKey)];
const responseArray = [{ error: response }];

// check if user wants to subscribe profile or not and listId is present or not
if (
Expand Down Expand Up @@ -336,10 +335,10 @@ const processRouterDest = async (inputs, reqMetadata) => {
batchedSubscribeResponseList.push(...batchedResponseList);
}
const nonSubscribeSuccessList = nonSubscribeRespList.map((resp) =>
resp.message.body.JSON?.action
resp.message?.error
? {
...getSuppressRespEvents(resp.message, [resp.metadata], resp.destination),
action: resp.message.body.JSON.action,
...getSuppressRespEvents({}, [resp.metadata], resp.destination),
error: resp.message.error,
}
: getSuccessRespEvents(resp.message, [resp.metadata], resp.destination),
);
Expand Down
30 changes: 7 additions & 23 deletions src/v0/destinations/klaviyo/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
removeUndefinedAndNullValues,
defaultBatchRequestConfig,
getSuccessRespEvents,
defaultPatchRequestConfig,
} = require('../../util');

const { BASE_ENDPOINT, MAPPING_CONFIG, CONFIG_CATEGORIES, MAX_BATCH_SIZE } = require('./config');
Expand All @@ -33,7 +32,7 @@ const { client: errNotificationClient } = require('../../../util/errorNotifier')
* @returns
*/
const getIdFromNewOrExistingProfile = async (endpoint, payload, requestOptions) => {
let profileId;
let response;
const endpointPath = '/api/profiles';
const { processedResponse: resp } = await handleHttpRequest(
'post',
Expand All @@ -46,15 +45,17 @@ const getIdFromNewOrExistingProfile = async (endpoint, payload, requestOptions)
endpointPath,
},
);

if (resp.status === 201) {
profileId = resp.response?.data?.id;
const { data } = resp.response;
response = { id: data.id, attributes: data.attributes };
} else if (resp.status === 409) {
const { errors } = resp.response;
profileId = errors?.[0]?.meta?.duplicate_profile_id;
response = errors;
}

if (profileId) {
return profileId;
if (response) {
return response;
}

let statusCode = resp.status;
Expand All @@ -78,22 +79,6 @@ const getIdFromNewOrExistingProfile = async (endpoint, payload, requestOptions)
);
};

const profileUpdateResponseBuilder = (payload, profileId, category, privateApiKey) => {
const updatedPayload = payload;
const identifyResponse = defaultRequestConfig();
updatedPayload.data.id = profileId;
identifyResponse.endpoint = `${BASE_ENDPOINT}${category.apiUrl}/${profileId}`;
identifyResponse.method = defaultPatchRequestConfig.requestMethod;
identifyResponse.headers = {
Authorization: `Klaviyo-API-Key ${privateApiKey}`,
'Content-Type': JSON_MIME_TYPE,
Accept: JSON_MIME_TYPE,
revision: '2023-02-22',
};
identifyResponse.body.JSON = removeUndefinedAndNullValues({ ...payload, action: 'suppress' });
return identifyResponse;
};

/**
* This function is used for creating response for subscribing users to a particular list.
* DOCS: https://developers.klaviyo.com/en/v2023-02-22/reference/subscribe_profiles
Expand Down Expand Up @@ -284,5 +269,4 @@ module.exports = {
generateBatchedPaylaodForArray,
batchSubscribeEvents,
getIdFromNewOrExistingProfile,
profileUpdateResponseBuilder,
};
17 changes: 8 additions & 9 deletions src/v0/util/errorTypes/filteredEventsError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ const tags = require('../tags');
const { BaseError } = require('./base');

class FilteredEventsError extends BaseError {
constructor(message, action = null, statusCode=298) {
const finalStatTags = {
[tags.TAG_NAMES.ERROR_CATEGORY]: tags.ERROR_CATEGORIES.TRANSFORMATION,
[tags.TAG_NAMES.ERROR_TYPE]: tags.ERROR_TYPES.FILTERED,
};
super(message, statusCode, finalStatTags);
this.action = action
}
constructor(message, statusCode = 298) {
const finalStatTags = {
[tags.TAG_NAMES.ERROR_CATEGORY]: tags.ERROR_CATEGORIES.TRANSFORMATION,
[tags.TAG_NAMES.ERROR_TYPE]: tags.ERROR_TYPES.FILTERED,
};
super(message, statusCode, finalStatTags);
}
}

module.exports = FilteredEventsError;
module.exports = FilteredEventsError;
1 change: 1 addition & 0 deletions test/__mocks__/klaviyo.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const klaviyoPostRequestHandler = (url, payload) => {
data: {
data: {
id: '01GW3PHVY0MTCDGS0A1612HARX',
attributes: {}
},
}
};
Expand Down
47 changes: 4 additions & 43 deletions test/__tests__/data/klaviyo.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,49 +351,10 @@
}
},
"output": {
"version": "1",
"type": "REST",
"method": "PATCH",
"endpoint": "https://a.klaviyo.com/api/profiles/01GW3PHVY0MTCDGS0A1612HARX",
"headers": {
"Accept": "application/json",
"Authorization": "Klaviyo-API-Key pk_b68c7b5163d98807fcb57e6f921216629d",
"Content-Type": "application/json",
"revision": "2023-02-22"
},
"params": {},
"body": {
"JSON": {
"action": "suppress",
"data": {
"type": "profile",
"attributes": {
"external_id": "user@1",
"email": "[email protected]",
"first_name": "Test",
"last_name": "Rudderlabs",
"phone_number": "+12 345 578 900",
"title": "Developer",
"organization": "Rudder",
"location": {
"city": "Tokyo",
"region": "Kanto",
"country": "JP",
"zip": "100-0001"
},
"properties": {
"Flagged": false,
"Residence": "Shibuya"
}
},
"id": "01GW3PHVY0MTCDGS0A1612HARX"
}
},
"JSON_ARRAY": {},
"XML": {},
"FORM": {}
},
"files": {}
"error": {
"attributes": {},
"id": "01GW3PHVY0MTCDGS0A1612HARX"
}
}
},
{
Expand Down
53 changes: 6 additions & 47 deletions test/__tests__/data/klaviyo_router_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,59 +61,18 @@
}
},
{
"batchedRequest": {
"version": "1",
"type": "REST",
"method": "PATCH",
"endpoint": "https://a.klaviyo.com/api/profiles/01GW3PHVY0MTCDGS0A1612HARX",
"headers": {
"Accept": "application/json",
"Authorization": "Klaviyo-API-Key pk_b68c7b5163d98807fcb57e6f921216629d",
"Content-Type": "application/json",
"revision": "2023-02-22"
},
"params": {},
"body": {
"JSON": {
"action": "suppress",
"data": {
"type": "profile",
"attributes": {
"external_id": "test",
"email": "[email protected]",
"first_name": "Test",
"last_name": "Rudderlabs",
"phone_number": "+12 345 578 900",
"title": "Developer",
"organization": "Rudder",
"location": {
"city": "Tokyo",
"region": "Kanto",
"country": "JP",
"zip": "100-0001"
},
"properties": {
"Flagged": false,
"Residence": "Shibuya"
}
},
"id": "01GW3PHVY0MTCDGS0A1612HARX"
}
},
"JSON_ARRAY": {},
"XML": {},
"FORM": {}
},
"files": {}
},
"batchedRequest": {},
"metadata": [
{
"jobId": 1
}
],
"batched": false,
"action": "suppress",
"statusCode": 200,
"error": {
"attributes": {},
"id": "01GW3PHVY0MTCDGS0A1612HARX"
},
"statusCode": 299,
"destination": {
"Config": {
"publicApiKey": "WJqij9",
Expand Down

0 comments on commit 14aa4e0

Please sign in to comment.