Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: onboard cordial source #3593

Merged
merged 9 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/v0/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/v0/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", "properties.email"]
}
]
51 changes: 51 additions & 0 deletions src/v0/sources/cordial/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const path = require('path');
const fs = require('fs');
const md5 = require('md5');
const Message = require('../message');
const { CommonUtils } = require('../../../util/common');
const { generateUUID, isDefinedAndNotNull } = require('../../util');
const { eventsMapping } = require('./config');

const mapping = JSON.parse(fs.readFileSync(path.resolve(__dirname, './mapping.json'), 'utf-8'));
koladilip marked this conversation as resolved.
Show resolved Hide resolved

const processEvent = (event) => {
const message = new Message(`Cordial`);
let eventName = event.event?.a || event.event?.action;
if (Object.keys(eventsMapping).includes(eventName)) {
koladilip marked this conversation as resolved.
Show resolved Hide resolved
eventName = eventsMapping[eventName];

Check warning on line 15 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L15

Added line #L15 was not covered by tests
}
message.setEventType('track');
message.setEventName(eventName);
message.setPropertiesV2(event, mapping);

const externalId = [];
// setting up cordial contact_id to externalId
if (event.cID) {
externalId.push({

Check warning on line 24 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L24

Added line #L24 was not covered by tests
type: 'cordialContactId',
id: event.cID,
});
}
message.context.externalId = externalId;

if (!message.userId && event.email) {
// Treating userId as unique identifier
// If userId is not present, then generating it from email using md5 hash function
message.userId = md5(event.email);

Check warning on line 34 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L34

Added line #L34 was not covered by tests
}
if (!isDefinedAndNotNull(message.userId)) {
message.anonymousId = generateUUID();

Check warning on line 37 in src/v0/sources/cordial/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/sources/cordial/transform.js#L37

Added line #L37 was not covered by tests
}
// 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, ...event.contact };
message.properties = { ...message.properties, ...event.event.properties, ...event.event };
delete message.properties.properties;
return message;
};

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

module.exports = { process };
169 changes: 169 additions & 0 deletions test/integrations/sources/cordial/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import utils from '../../../../src/v0/util';

const defaultMockFns = () => {
jest.spyOn(utils, 'generateUUID').mockReturnValue('97fcd7b2-cc24-47d7-b776-057b7b199513');
};

export const data = [
Gauravudia marked this conversation as resolved.
Show resolved Hide resolved
{
name: 'cordial',
description: 'Simple Single object Input event with normal channel and action',
module: 'source',
version: 'v0',
input: {
request: {
body: [
{
contact: {
_id: '6690fe3655e334xx028xxx',
channels: {
email: {
address: '[email protected]',
subscribeStatus: 'subscribed',
subscribedAt: '2024-07-12T09:58:14+0000',
},
},
createdAt: '2024-07-12T09:58:14+0000',
address: {
city: 'San Miego',
},
first_name: 'John',
last_name: 'Doe',
lastUpdateSource: 'api',
lastModified: '2024-07-12T13:00:49+0000',
cID: '6690fe3655e334xx028xxx',
},
event: {
_id: '669141857b8cxxx1ba0da2xx',
cID: '6690fe3655e334xx028xxx',
ts: '2024-07-12T14:45:25+00:00',
ats: '2024-07-12T14:45:25+0000',
d: {
type: 'computer',
device: false,
platform: false,
browser: false,
robot: true,
},
a: 'browse',
tzo: -7,
rl: 'a',
UID: '4934ee07118197xx3f74d5xxxx7b0076',
time: '2024-07-12T14:45:25+0000',
action: 'browse',
bmID: '',
first: 0,
properties: {
category: 'Shirts',
url: 'http://example.com/shirts',
description: 'A really cool khaki shirt.',
price: 9.99,
title: 'Khaki Shirt',
test_key: 'value',
},
},
},
],
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
},
pathSuffix: '',
},
output: {
response: {
status: 200,
body: [
{
output: {
batch: [
{
context: {
library: {
name: 'unknown',
version: 'unknown',
},
integration: {
name: 'Cordial',
},
traits: {
userId: '6690fe3655e334xx028xxx',
email: '[email protected]',
_id: '6690fe3655e334xx028xxx',
channels: {
email: {
address: '[email protected]',
subscribeStatus: 'subscribed',
subscribedAt: '2024-07-12T09:58:14+0000',
},
},
createdAt: '2024-07-12T09:58:14+0000',
address: {
city: 'San Miego',
},
first_name: 'John',
last_name: 'Doe',
lastUpdateSource: 'api',
lastModified: '2024-07-12T13:00:49+0000',
cID: '6690fe3655e334xx028xxx',
},
device: {
type: 'computer',
device: false,
platform: false,
browser: false,
robot: true,
},
externalId: [],
Gauravudia marked this conversation as resolved.
Show resolved Hide resolved
},
integrations: {
Cordial: false,
},
type: 'track',
event: 'browse',
originalTimestamp: '2024-07-12T14:45:25+00:00',
properties: {
event_id: '669141857b8cxxx1ba0da2xx',
email: '[email protected]',
category: 'Shirts',
url: 'http://example.com/shirts',
description: 'A really cool khaki shirt.',
price: 9.99,
title: 'Khaki Shirt',
test_key: 'value',
_id: '669141857b8cxxx1ba0da2xx',
Gauravudia marked this conversation as resolved.
Show resolved Hide resolved
cID: '6690fe3655e334xx028xxx',
ts: '2024-07-12T14:45:25+00:00',
ats: '2024-07-12T14:45:25+0000',
d: {
Gauravudia marked this conversation as resolved.
Show resolved Hide resolved
type: 'computer',
device: false,
platform: false,
browser: false,
robot: true,
},
a: 'browse',
tzo: -7,
rl: 'a',
UID: '4934ee07118197xx3f74d5xxxx7b0076',
time: '2024-07-12T14:45:25+0000',
action: 'browse',
bmID: '',
first: 0,
},
userId: '6690fe3655e334xx028xxx',
timestamp: '2024-07-12T14:45:25+00:00',
sentAt: '2024-07-12T14:45:25+00:00',
},
],
},
},
],
},
},
mockFns: () => {
defaultMockFns();
},
},
];
Loading