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

fix: mailjet source operating on array instead object #2999

Merged
merged 5 commits into from
Jan 19, 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
84 changes: 44 additions & 40 deletions src/v0/sources/mailjet/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,49 @@
// import mapping json using JSON.parse to preserve object key order
const mapping = JSON.parse(fs.readFileSync(path.resolve(__dirname, './mapping.json'), 'utf-8'));

function process(event) {
const message = new Message(`MailJet`);

// event type is always track
const eventType = 'track';

message.setEventType(eventType);

message.setEventName(event.event);

message.setPropertiesV2(event, mapping);

if (event.time) {
const ts = new Date(event.time * 1000).toISOString();
message.setProperty('originalTimestamp', ts);
}

const externalId = [];
// setting up mailjet contact_id and list_id to externalId
if (event.mj_contact_id) {
externalId.push({
type: 'mailjetContactId',
id: event.mj_contact_id,
});
}
if (event.mj_list_id) {
externalId.push({
type: 'mailjetListId',
id: event.mj_list_id,
});
}
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);
}
return message;
}
const processEvent = (eventList) => {
const messages = [];
eventList.forEach((event) => {
const message = new Message(`MailJet`);
// event type is always track
const eventType = 'track';
message.setEventType(eventType);
message.setEventName(event.event);
message.setPropertiesV2(event, mapping);

if (event.time) {
const ts = new Date(event.time * 1000).toISOString();
message.setProperty('originalTimestamp', ts);
}

const externalId = [];
// setting up mailjet contact_id and list_id to externalId
if (event.mj_contact_id) {
externalId.push({
type: 'mailjetContactId',
id: event.mj_contact_id,
});
}
if (event.mj_list_id) {
externalId.push({

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L34 was not covered by tests
type: 'mailjetListId',
id: event.mj_list_id,
});
}
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);
}
messages.push(message);
});

return messages;
};

// This fucntion just converts the incoming payload to array of already not and sends it to processEvent
const process = (event) => (Array.isArray(event) ? processEvent(event) : processEvent([event]));
koladilip marked this conversation as resolved.
Show resolved Hide resolved

module.exports = { process };
Loading
Loading