Skip to content

Commit

Permalink
feat: onboard cordial source (#3593)
Browse files Browse the repository at this point in the history
* feat: onboard cordial source

* chore: test cases added

* chore: minor fix

* chore: changed v0 to v1

* chore: updated test case with batched payload

* chore: updated test case

* chore: removed cart object

* chore: test cases updated and few minor fixes

* chore: test cases updated for d and _id
  • Loading branch information
aanshi07 authored Jul 26, 2024
1 parent 99123f1 commit cdea69c
Show file tree
Hide file tree
Showing 4 changed files with 707 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/v1/sources/cordial/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const eventsMapping = {
crdl_app_install: 'Application Installed',
crdl_app_open: 'Application Opened',
};

module.exports = { eventsMapping };
22 changes: 22 additions & 0 deletions src/v1/sources/cordial/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"sourceKeys": "event._id",
"destKeys": "properties.event_id"
},
{
"sourceKeys": "contact._id",
"destKeys": ["context.traits.userId", "userId"]
},
{
"sourceKeys": ["event.ts", "event.ats"],
"destKeys": ["timestamp", "sentAt", "originalTimestamp"]
},
{
"sourceKeys": "event.d",
"destKeys": "context.device"
},
{
"sourceKeys": "contact.channels.email.address",
"destKeys": ["context.traits.email"]
}
]
51 changes: 51 additions & 0 deletions src/v1/sources/cordial/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Message = require('../../../v0/sources/message');
const { CommonUtils } = require('../../../util/common');
const { generateUUID, isDefinedAndNotNull } = require('../../../v0/util');
const { eventsMapping } = require('./config');

const mapping = require('./mapping.json');

const processEvent = (inputPaylaod) => {
const message = new Message(`Cordial`);
let eventName = inputPaylaod.event?.a || inputPaylaod.event?.action;
if (eventName in eventsMapping) {
eventName = eventsMapping[eventName];
}
message.setEventType('track');
message.setEventName(eventName);
message.setPropertiesV2(inputPaylaod, mapping);

const externalId = [];
// setting up cordial contact_id to externalId
if (inputPaylaod.contact.cID) {
externalId.push({
type: 'cordialContactId',
id: inputPaylaod.contact.cID,
});
}
message.context.externalId = externalId;

if (!isDefinedAndNotNull(message.userId)) {
message.anonymousId = generateUUID();
}
// Due to multiple mappings to the same destination path object some are not showing up due to which we are doing the following
message.context.traits = { ...message.context.traits, ...inputPaylaod.contact };
message.properties = {
...message.properties,
...inputPaylaod.event.properties,
...inputPaylaod.event,
};
delete message.properties.properties;
delete message.properties.d;
// eslint-disable-next-line no-underscore-dangle
delete message.properties._id;
return message;
};

const process = (inputEvent) => {
const { event: events } = inputEvent;
const eventsArray = CommonUtils.toArray(events);
return eventsArray.map(processEvent);
};

exports.process = process;
Loading

0 comments on commit cdea69c

Please sign in to comment.