From 1c73ed93dfff270b9477ad73d1ce09b006e97f7b Mon Sep 17 00:00:00 2001 From: Yashasvi Bajpai <33063622+yashasvibajpai@users.noreply.github.com> Date: Tue, 11 Jun 2024 04:14:31 +0530 Subject: [PATCH] chore: add custom properties --- .../destinations/klaviyo_bulk_upload/utils.js | 55 ++++++-- .../klaviyo_bulk_upload/processor/data.ts | 119 ++++++++++++++++++ 2 files changed, 162 insertions(+), 12 deletions(-) diff --git a/src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js b/src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js index 2ea6b8e54a..787176ffdd 100644 --- a/src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js +++ b/src/cdk/v2/destinations/klaviyo_bulk_upload/utils.js @@ -1,3 +1,6 @@ +/* eslint-disable no-param-reassign */ +const { removeUndefinedValues } = require('../../../../v0/util'); + const locationAttributes = [ 'address1', 'address2', @@ -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, @@ -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}`, }, @@ -54,7 +79,13 @@ function transformSingleMessage(data, metadata) { transformedSinglePayload.id = context.externalId[0].id || traits.id; transformedSinglePayload.attributes.anonymous_id = context.externalId[0].id; } - return transformedSinglePayload; + if (Object.keys(transformedSinglePayload.attributes.location).length === 0) { + delete transformedSinglePayload.attributes.location; + } + if (Object.keys(transformedSinglePayload.attributes.properties).length === 0) { + delete transformedSinglePayload.attributes.properties; + } + return removeUndefinedValues(transformedSinglePayload); } function wrapCombinePayloads(transformedInputs, destinationObj) { diff --git a/test/integrations/destinations/klaviyo_bulk_upload/processor/data.ts b/test/integrations/destinations/klaviyo_bulk_upload/processor/data.ts index eacb360c40..0a97f9035d 100644 --- a/test/integrations/destinations/klaviyo_bulk_upload/processor/data.ts +++ b/test/integrations/destinations/klaviyo_bulk_upload/processor/data.ts @@ -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: 'qwe22@mail.com', + 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: 'qwe22@mail.com', + 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, + }, + }, + ], + }, + }, + }, ];