Skip to content

Commit

Permalink
fix: revert "remove ztringification for nested objects and arrays in …
Browse files Browse the repository at this point in the history
…event data for custom events" commit

This reverts commit 9157b28.
  • Loading branch information
yashasvibajpai committed Feb 5, 2024
1 parent f86aa60 commit ff39dac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
60 changes: 52 additions & 8 deletions src/v0/destinations/clevertap/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,51 @@ const responseWrapper = (payload, destination) => {
return response;
};

/**
* Expected behaviors:
payload = { "finalPayload": {
"device": { "device": "{\"browser\":{\"name\":\"Chrome121\",\"version\":\"106.0.0.0\"},\"os\":{\"version\":\"10.15.7\"}}",
"browser": { "name": "macOS",
"name": "Chrome121", "platform": "web"
"version": "106.0.0.0" }
},
"os": {
"version": "10.15.7"
}
},
"name": "macOS",
"platform": "web"
}
*
}
* This function stringify the payload attributes if it's an array or objects.
* @param {*} payload
* @returns
* return the final payload after converting to the relevant data-types.
*/
const convertObjectAndArrayToString = (payload, event) => {
const finalPayload = {};
if (payload) {
Object.keys(payload).forEach((key) => {
if (payload[key] && (Array.isArray(payload[key]) || typeof payload[key] === 'object')) {
finalPayload[key] = JSON.stringify(payload[key]);
} else {
finalPayload[key] = payload[key];
}
});
if (event === 'Charged' && finalPayload.Items) {
finalPayload.Items = JSON.parse(finalPayload.Items);
if (
!Array.isArray(finalPayload.Items) ||
(Array.isArray(finalPayload.Items) && typeof finalPayload.Items[0] !== 'object')
) {
throw new InstrumentationError('Products property value must be an array of objects');
}
}
}
return finalPayload;
};

// generates clevertap identify payload with both objectId and identity
const mapIdentifyPayloadWithObjectId = (message, profile) => {
const userId = getFieldValueFromMessage(message, 'userIdOnly');
Expand Down Expand Up @@ -265,14 +310,6 @@ const responseBuilderSimple = (message, category, destination) => {
['properties'],
['checkout_id', 'revenue', 'products', 'ts'],
);

if (
!Array.isArray(eventPayload.evtData.Items) ||
(Array.isArray(eventPayload.evtData.Items) &&
typeof eventPayload.evtData.Items[0] !== 'object')
) {
throw new InstrumentationError('Products property value must be an array of objects');
}
}
// For other type of events we need to follow payload for sending events
// Source: https://developer.clevertap.com/docs/upload-events-api
Expand All @@ -281,6 +318,13 @@ const responseBuilderSimple = (message, category, destination) => {
eventPayload = constructPayload(message, MAPPING_CONFIG[category.name]);
}
eventPayload.type = 'event';
// stringify the evtData if it's an Object or array.
if (eventPayload.evtData) {
eventPayload.evtData = convertObjectAndArrayToString(
eventPayload.evtData,
eventPayload.evtName,
);
}

// setting identification for tracking payload here based on destination config
if (destination.Config.enableObjectIdMapping) {
Expand Down
10 changes: 0 additions & 10 deletions test/integrations/destinations/clevertap/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,11 +1462,6 @@ export const data = [
properties: {
country_region: 'India',
test: 'abc',
listCategories: ['list1', 'list2'],
objectCategories: {
category1: 'value1',
category2: 'value2',
},
},
receivedAt: '2021-08-20T12:49:07.691Z',
rudderId: '138c4214-b537-4f77-9dea-9abde70b5147',
Expand Down Expand Up @@ -1501,11 +1496,6 @@ export const data = [
evtData: {
country_region: 'India',
test: 'abc',
listCategories: ['list1', 'list2'],
objectCategories: {
category1: 'value1',
category2: 'value2',
},
},
type: 'event',
objectId: 'cd3a4439-7df0-4475-acb9-6659c7c4dfe3',
Expand Down

0 comments on commit ff39dac

Please sign in to comment.