Skip to content

Commit

Permalink
chore: add custom properties
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai committed Jun 10, 2024
1 parent 0a19f1d commit 1c73ed9
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable no-param-reassign */
const { removeUndefinedValues } = require('../../../../v0/util');

const locationAttributes = [
'address1',
'address2',
Expand All @@ -10,20 +13,39 @@ const locationAttributes = [
'ip',
];

function removeLocationAttributes(traits) {
const finalObject = {};
return Object.keys(traits).reduce((result, key) => {
if (!locationAttributes.includes(key)) {
finalObject[key] = traits[key];
}
return finalObject;
}, {});
const standardTraits = [
'email',
'phone_number',
'external_id',
'anonymous_id',
'_kx',
'first_name',
'last_name',
'organization',
'title',
'image',
];

function setStandardAndCustomTraits(traits) {
const standardAttributes = {};
const customAttributes = {};
return Object.keys(traits).reduce(
(result, key) => {
if (!locationAttributes.includes(key) && standardTraits.includes(key)) {
result.standardAttributes[key] = traits[key];
} else if (!locationAttributes.includes(key)) {
result.customAttributes[key] = traits[key];
}
return result;
},
{ standardAttributes, customAttributes },
);
}

function generateLocationObject({
traits: { address1, address2, city, country, latitude, longitude, region, zip, ip },
}) {
return {
const locationObject = {
address1,
address2,
city,
Expand All @@ -34,18 +56,21 @@ function generateLocationObject({
zip,
ip,
};

return removeUndefinedValues(locationObject);
}

function transformSingleMessage(data, metadata) {
const { context, traits } = data;
const location = generateLocationObject(data);
const { jobId } = metadata;
const traitsWithoutLocation = removeLocationAttributes(traits);
const { standardAttributes, customAttributes } = setStandardAndCustomTraits(traits);
const transformedSinglePayload = {
type: 'profile',
attributes: {
...traitsWithoutLocation,
...standardAttributes,
location,
properties: customAttributes,
anonymous_id: context.externalId[0].id,
jobIdentifier: `${context.externalId[0].id}:${jobId}`,
},
Expand All @@ -54,7 +79,13 @@ function transformSingleMessage(data, metadata) {
transformedSinglePayload.id = context.externalId[0].id || traits.id;
transformedSinglePayload.attributes.anonymous_id = context.externalId[0].id;

Check warning on line 80 in src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js#L80

Added line #L80 was not covered by tests
}
return transformedSinglePayload;
if (Object.keys(transformedSinglePayload.attributes.location).length === 0) {
delete transformedSinglePayload.attributes.location;

Check warning on line 83 in src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js

View check run for this annotation

Codecov / codecov/patch

src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js#L83

Added line #L83 was not covered by tests
}
if (Object.keys(transformedSinglePayload.attributes.properties).length === 0) {
delete transformedSinglePayload.attributes.properties;
}
return removeUndefinedValues(transformedSinglePayload);
}

function wrapCombinePayloads(transformedInputs, destinationObj) {
Expand Down
119 changes: 119 additions & 0 deletions test/integrations/destinations/klaviyo_bulk_upload/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,123 @@ export const data = [
},
},
},
{
name: 'klaviyo_bulk_upload',
description: 'Successful identify event with custom properties',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
message: {
channel: 'sources',
context: {
externalId: [
{
id: 'user1',
identifierType: 'userId',
type: 'KLAVIYO_BULK_UPLOAD-userProfiles',
},
],
mappedToDestination: 'true',
sources: {
job_id: '2gif2bMzsX1Nt0rbV1vcbAE3cxC',
job_run_id: 'cp5p5ilq47pqg38v2nfg',
task_run_id: 'cp5p5ilq47pqg38v2ng0',
version: '2051/merge',
},
},
traits: {
email: '[email protected]',
first_name: 'Testqwe0022',
last_name: 'user',
phone_number: '+919902330123',
ip: '213.5.6.41',
last_visit_date: '2020-10-01T00:00:00Z',
lastVisitService: ['Brazilian'],
},
type: 'identify',
userId: '1',
},
destination: {
DestinationDefinition: {
Config: {
cdkV2Enabled: true,
},
},
Config: {
privateApiKey: 'pk_dummy_123',
},
},
metadata: {
destinationId: 'destId',
workspaceId: 'wspId',
jobId: 1,
},
},
],
},
},
output: {
response: {
status: 200,
body: [
{
output: {
version: '1',
type: 'REST',
method: 'POST',
endpoint: '',
headers: {},
params: {},
body: {
JSON: {
data: {
type: 'profile-bulk-import-job',
attributes: {
profiles: {
data: [
{
type: 'profile',
attributes: {
email: '[email protected]',
first_name: 'Testqwe0022',
last_name: 'user',
phone_number: '+919902330123',
location: {
ip: '213.5.6.41',
},
anonymous_id: 'user1',
jobIdentifier: 'user1:1',
properties: {
lastVisitService: ['Brazilian'],
last_visit_date: '2020-10-01T00:00:00Z',
},
},
},
],
},
},
},
},
JSON_ARRAY: {},
XML: {},
FORM: {},
},
files: {},
userId: '',
},
statusCode: 200,
metadata: {
destinationId: 'destId',
workspaceId: 'wspId',
jobId: 1,
},
},
],
},
},
},
];

0 comments on commit 1c73ed9

Please sign in to comment.