Skip to content

Commit

Permalink
fix: adding support for track call
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Apr 30, 2024
1 parent ed9e0cf commit f20b403
Show file tree
Hide file tree
Showing 3 changed files with 500 additions and 12 deletions.
29 changes: 28 additions & 1 deletion src/cdk/v2/destinations/emersys/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,46 @@ steps:
description: |
Builds identify payload.
ref:
condition: $.outputs.messageType === {{$.EventType.IDENTIFY}}
template: |
$.context.payload = $.buildIdentifyPayload(.message, .destination.Config,);
- name: preparePayloadForGroup
description: |
Builds group payload.
ref: https://dev.emarsys.com/docs/core-api-reference/1m0m70hy3tuov-add-contacts-to-a-contact-list
condition: $.outputs.messageType === {{$.EventType.GROUP}}
template: |
$.context.payload = $.buildGroupPayload(.message, .destination.Config,);
- name: preparePayloadForTrack
description: |
Builds track payload.
ref: https://dev.emarsys.com/docs/core-api-reference/fl0xx6rwfbwqb-trigger-an-external-event
condition: $.outputs.messageType === {{$.EventType.TRACK}}
template: |
const integrationObject = $.getIntegrationsObj(message, 'emersys');
const emersysIdentifierId = integrationObject?.customIdentifierId || ^.destination.Config.emersysCustomIdentifier || $.EMAIL_FIELD_ID;
const payload = {
key_id: emersysIdentifierId ,
external_id: $.deduceExternalIdValue(.message,emersysIdentifierId,.destination.Config.fieldMapping),
trigger_id: integrationObject.trigger_id,
data: properties.data,
attachment: Array.isArray(properties.attachment)
? properties.attachment
: [properties.attachment],
event_time: $.getFieldValueFromMessage(message, 'timestamp'),
};
$.context.payload = {
eventType: ^.message.type,
destinationPayload: {
payload: $.removeUndefinedAndNullValues(payload),
eventId: $.deduceEventId(.message,.destination.Config),
},
};
- name: buildResponse
template: |
const response = $.defaultRequestConfig();
response.body.JSON = $.context.payload;
response.endpoint = $.deduceEndPoint(.message,.destination.Config);
response.endpoint = $.deduceEndPoint($.context.payload,.destination.Config);
response.method = "POST";
response.headers = $.buildHeader(.destination.Config)
response
85 changes: 74 additions & 11 deletions src/cdk/v2/destinations/emersys/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ const crypto = require('crypto');

const {
InstrumentationError,
ConfigurationError,
isDefinedAndNotNullAndNotEmpty,
removeUndefinedAndNullAndEmptyValues,
removeUndefinedAndNullValues,
isDefinedAndNotNull,
getHashFromArray,
} = require('@rudderstack/integrations-lib');
const { getValueFromMessage } = require('rudder-transformer-cdk/build/utils');
const { getIntegrationsObj } = require('../../../../v0/util');
const {
getValueFromMessage,
getFieldValueFromMessage,
} = require('rudder-transformer-cdk/build/utils');
const { getIntegrationsObj, validateEventName } = require('../../../../v0/util');
const {
EMAIL_FIELD_ID,
MAX_BATCH_SIZE,
Expand Down Expand Up @@ -104,16 +109,21 @@ const findRudderPropertyByEmersysProperty = (emersysProperty, fieldMapping) => {
return item ? item.rudderProperty : null;
};

const buildGroupPayload = (message, destination) => {
const { emersysCustomIdentifier, defaultContactList, fieldMapping } = destination.Config;
const integrationObject = getIntegrationsObj(message, 'emersys');
const emersysIdentifier =
integrationObject?.customIdentifierId || emersysCustomIdentifier || EMAIL_FIELD_ID;
const deduceExternalIdValue = (message, emersysIdentifier, fieldMapping) => {
const configuredPayloadProperty = findRudderPropertyByEmersysProperty(
emersysIdentifier,
fieldMapping,
);
const externalIdValue = getValueFromMessage(message.context.traits, configuredPayloadProperty);
return externalIdValue;
};

const buildGroupPayload = (message, destination) => {
const { emersysCustomIdentifier, defaultContactList, fieldMapping } = destination.Config;
const integrationObject = getIntegrationsObj(message, 'emersys');
const emersysIdentifier =
integrationObject?.customIdentifierId || emersysCustomIdentifier || EMAIL_FIELD_ID;
const externalIdValue = deduceExternalIdValue(message, emersysIdentifier, fieldMapping);
if (!isDefinedAndNotNull(externalIdValue)) {
throw new InstrumentationError(
`No value found in payload for contact custom identifier of id ${emersysIdentifier}`,
Expand All @@ -132,18 +142,68 @@ const buildGroupPayload = (message, destination) => {
};
};

const deduceEndPoint = (message, destConfig, batchGroupId = undefined) => {
const deduceEventId = (message, destConfig) => {
let eventId;
const { eventsMapping } = destConfig;
const { event } = message;
validateEventName(event);
if (eventsMapping.length > 0) {
const keyMap = getHashFromArray(eventsMapping, 'from', 'to', false);
eventId = keyMap[event];
}
if (!eventId) {
throw new ConfigurationError(`${event} is not mapped to any Emersys external event. Aborting`);
}
};

const buildTrackPayload = (message, destination) => {
let eventId;
const { emersysCustomIdentifier, eventsMapping } = destination.Config;
const { event, properties } = message;

if (eventsMapping.length > 0) {
const keyMap = getHashFromArray(eventsMapping, 'from', 'to', false);
eventId = keyMap[event];
}
const integrationObject = getIntegrationsObj(message, 'emersys');
const emersysIdentifier =
integrationObject?.customIdentifierId || emersysCustomIdentifier || EMAIL_FIELD_ID;
const payload = {
key_id: emersysIdentifier,
external_id: '[email protected]',
trigger_id: integrationObject.trigger_id,
data: properties.data,
attachment: Array.isArray(properties.attatchment)
? properties.attatchment
: [properties.attatchment],
event_time: getFieldValueFromMessage(message, 'timestamp'),
};
return {
eventType: message.type,
destinationPayload: {
payload: removeUndefinedAndNullValues(payload),
event: eventId,
},
};
};

const deduceEndPoint = (finalPayload) => {
let endPoint;
let eventId;
let contactListId;
const { type, groupId } = message;
switch (type) {
const { eventType, destinationPayload } = finalPayload;
switch (eventType) {
case EVENT_TYPE.IDENTIFY:
endPoint = 'https://api.emarsys.net/api/v2/contact/?create_if_not_exists=1';
break;
case EVENT_TYPE.GROUP:
contactListId = batchGroupId || groupId || destConfig.defaultContactList;
contactListId = destinationPayload.contactListId;
endPoint = `https://api.emarsys.net/api/v2/contactlist/${contactListId}/add`;
break;
case EVENT_TYPE.TRACK:
eventId = destinationPayload.eventId;
endPoint = `https://api.emarsys.net/api/v2/event/${eventId}/trigger`;
break;
default:
break;
}
Expand Down Expand Up @@ -347,4 +407,7 @@ module.exports = {
createIdentifyBatches,
ensureSizeConstraints,
createGroupBatches,
buildTrackPayload,
deduceExternalIdValue,
deduceEventId,
};
Loading

0 comments on commit f20b403

Please sign in to comment.