Skip to content

Commit

Permalink
chore: klaviyo multiple response action fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Sep 14, 2023
1 parent 90f9e5e commit 855fdce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 247 deletions.
24 changes: 11 additions & 13 deletions src/v0/destinations/klaviyo/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ const identifyRequestHandler = async (message, category, destination) => {
traitsInfo?.properties?.subscribe &&
(traitsInfo.properties?.listId || destination.Config?.listId)
) {
responseArray.push(subscribeUserToList(message, traitsInfo, destination));
return responseArray;
return [subscribeUserToList(message, traitsInfo, destination)];
}

return responseArray[0];
};

Expand Down Expand Up @@ -329,20 +329,18 @@ const processRouterDest = async (inputs, reqMetadata) => {
}
}),
);
let batchedSubscribeResponseList = [];
const batchedSubscribeResponseList = [];
if (subscribeRespList.length > 0) {
const { batchedResponseList, identifyResponseList } = batchSubscribeEvents(subscribeRespList);
const identifyEventsList = identifyResponseList.map((resp) => {
const response = getSuccessRespEvents(resp.message, [resp.metadata], resp.destination);
return {
...response,
action: 'suppress'
}
})
batchedSubscribeResponseList = [...batchedResponseList, ...identifyEventsList]
const batchedResponseList = batchSubscribeEvents(subscribeRespList);
batchedSubscribeResponseList.push(...batchedResponseList);
}
const nonSubscribeSuccessList = nonSubscribeRespList.map((resp) =>
getSuccessRespEvents(resp.message, [resp.metadata], resp.destination),
resp.message.body.JSON?.action
? {
...getSuccessRespEvents(resp.message, [resp.metadata], resp.destination),
action: resp.message.body.JSON.action,
}
: getSuccessRespEvents(resp.message, [resp.metadata], resp.destination),
);
batchResponseList = [...batchedSubscribeResponseList, ...nonSubscribeSuccessList];

Expand Down
21 changes: 6 additions & 15 deletions src/v0/destinations/klaviyo/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const profileUpdateResponseBuilder = (payload, profileId, category, privateApiKe
Accept: JSON_MIME_TYPE,
revision: '2023-02-22',
};
identifyResponse.body.JSON = removeUndefinedAndNullValues(payload);
identifyResponse.body.JSON = removeUndefinedAndNullValues({ ...payload, action: 'suppress' });
return identifyResponse;
};

Expand Down Expand Up @@ -236,7 +236,7 @@ const generateBatchedPaylaodForArray = (events) => {
* @param {*} subscribeResponseList
* @returns
*/
const groupSubsribeResponsesUsingListId = (subscribeResponseList) => {
const groupSubscribeResponsesUsingListId = (subscribeResponseList) => {
const subscribeEventGroups = _.groupBy(
subscribeResponseList,
(event) => event.message.body.JSON.data.attributes.list_id,
Expand Down Expand Up @@ -264,26 +264,17 @@ const getBatchedResponseList = (subscribeEventGroups) => {
};

const batchSubscribeEvents = (subscribeRespList) => {
const identifyResponseList = [];
subscribeRespList.forEach((event) => {
const processedEvent = event;
if (processedEvent.message.length === 2) {
// the array will contain one update profile reponse and one subscribe reponse
identifyResponseList.push({ message: event.message[0], metadata: event.metadata, destination: event.destination });
[processedEvent.message] = event.message.slice(1);
} else {
// for group events (it will contain only subscribe response)
[processedEvent.message] = event.message.slice(0);
}
// for group and identify events (it will contain only subscribe response)
[processedEvent.message] = event.message.slice(0);
});

const subscribeEventGroups = groupSubsribeResponsesUsingListId(subscribeRespList);
const subscribeEventGroups = groupSubscribeResponsesUsingListId(subscribeRespList);

const batchedResponseList = getBatchedResponseList(subscribeEventGroups);

return {
batchedResponseList, identifyResponseList
};
return batchedResponseList;
};

module.exports = {
Expand Down
136 changes: 1 addition & 135 deletions test/__tests__/data/klaviyo.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,50 +65,6 @@
}
},
"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": {
"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": {}
},
{
"version": "1",
"type": "REST",
Expand Down Expand Up @@ -222,53 +178,6 @@
}
},
"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": {
"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",
"friend.age": 25,
"friend.names.first": "Alice",
"friend.names.last": "Smith"
}
},
"id": "01GW3PHVY0MTCDGS0A1612HARX"
}
},
"JSON_ARRAY": {},
"XML": {},
"FORM": {}
},
"files": {}
},
{
"version": "1",
"type": "REST",
Expand Down Expand Up @@ -455,6 +364,7 @@
"params": {},
"body": {
"JSON": {
"action": "suppress",
"data": {
"type": "profile",
"attributes": {
Expand Down Expand Up @@ -553,50 +463,6 @@
}
},
"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": {
"data": {
"type": "profile",
"attributes": {
"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": "user@1"
}
},
"id": "01GW3PHVY0MTCDGS0A1612HARX"
}
},
"JSON_ARRAY": {},
"XML": {},
"FORM": {}
},
"files": {}
},
{
"version": "1",
"type": "REST",
Expand Down
2 changes: 0 additions & 2 deletions test/__tests__/data/klaviyo_router_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
"Flagged": false,
"Residence": "Shibuya",
"properties": {
"listId": "XUepkK",
"subscribe": true,
"consent": ["email", "sms"]
}
},
Expand Down
86 changes: 4 additions & 82 deletions test/__tests__/data/klaviyo_router_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,8 @@
"email": "[email protected]",
"phone_number": "+12 345 578 900",
"channels": {
"email": [
"MARKETING"
],
"sms": [
"MARKETING"
]
}
},
{
"email": "[email protected]",
"phone_number": "+12 345 578 900",
"channels": {
"email": [
"MARKETING"
],
"sms": [
"MARKETING"
]
"email": ["MARKETING"],
"sms": ["MARKETING"]
}
}
]
Expand All @@ -63,9 +47,6 @@
{
"jobId": 3
},
{
"jobId": 1
},
{
"jobId": 2
}
Expand Down Expand Up @@ -94,6 +75,7 @@
"params": {},
"body": {
"JSON": {
"action": "suppress",
"data": {
"type": "profile",
"attributes": {
Expand Down Expand Up @@ -139,66 +121,6 @@
}
}
},
{
"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": {
"data": {
"type": "profile",
"attributes": {
"email": "[email protected]",
"external_id": "test",
"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": {}
},
"metadata": [
{
"jobId": 2
}
],
"action": "suppress",
"batched": false,
"statusCode": 200,
"destination": {
"Config": {
"publicApiKey": "WJqij9",
"privateApiKey": "pk_b68c7b5163d98807fcb57e6f921216629d"
}
}
},
{
"error": "Event type random is not supported",
"metadata": [
Expand Down Expand Up @@ -239,4 +161,4 @@
}
}
}
]
]

0 comments on commit 855fdce

Please sign in to comment.