Skip to content

Commit

Permalink
feat: onboard msl changes for new record event
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai committed Sep 22, 2023
1 parent 36c7f06 commit c50310d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
53 changes: 47 additions & 6 deletions src/v0/destinations/marketo_static_list/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const batchResponseBuilder = (message, Config, token, leadIds, operation) => {
const processEvent = (input) => {
const { token, message, destination } = input;
const { Config } = destination;
validateMessageType(message, ['audiencelist']);
validateMessageType(message, ['audiencelist', 'record']);
const response = [];
let toAdd;
let toRemove;
Expand All @@ -74,14 +74,14 @@ const processEvent = (input) => {
(Array.isArray(toAdd) && toAdd.length > 0) ||
(Array.isArray(toRemove) && toRemove.length > 0)
) {
if (Array.isArray(toAdd) && toAdd.length > 0) {
const payload = batchResponseBuilder(message, Config, token, toAdd, 'add');
if (Array.isArray(toRemove) && toRemove.length > 0) {
const payload = batchResponseBuilder(message, Config, token, toRemove, 'remove');
if (payload) {
response.push(...payload);
}
}
if (Array.isArray(toRemove) && toRemove.length > 0) {
const payload = batchResponseBuilder(message, Config, token, toRemove, 'remove');
if (Array.isArray(toAdd) && toAdd.length > 0) {
const payload = batchResponseBuilder(message, Config, token, toAdd, 'add');
if (payload) {
response.push(...payload);
}
Expand All @@ -93,6 +93,36 @@ const processEvent = (input) => {
}
return response;
};

function transformForRecordEvent(inputs, leadIdObj) {
const tokenisedInputs = inputs.map((input) => {
const { message } = input;
const { fields, action } = message;
if (!message.properties) {
message.properties = {};
}
if (!message.properties.listData) {
message.properties.listData = { add: [], remove: [] };
}
// message.properties.listData = message.properties.listData || { add: [], remove: [] };
if (action === 'insert') {
leadIdObj.insert.push({ id: fields.id });
message.properties.listData.add.push({ id: fields.id });
} else if (action === 'delete') {
leadIdObj.delete.push({ id: fields.id });
message.properties.listData.remove.push({ id: fields.id });
} else {
throw new InstrumentationError('Invalid action type');
}
return input;
});
const finalInput = [tokenisedInputs[0]];
finalInput[0].message.properties.listData.add = leadIdObj.insert;
finalInput[0].message.properties.listData.remove = leadIdObj.delete;

return finalInput;
}

const process = async (event) => {
const token = await getAuthToken(formatConfig(event.destination));

Expand All @@ -107,6 +137,7 @@ const processRouterDest = async (inputs, reqMetadata) => {
// Token needs to be generated for marketo which will be done on input level.
// If destination information is not present Error should be thrown
let token;
let respList;
try {
token = await getAuthToken(formatConfig(inputs[0].destination));
if (!token) {
Expand Down Expand Up @@ -140,7 +171,17 @@ const processRouterDest = async (inputs, reqMetadata) => {
// If true then previous status is 500 and every subsequent event output should be
// sent with status code 500 to the router to be retried.
const tokenisedInputs = inputs.map((input) => ({ ...input, token }));
const respList = await simpleProcessRouterDest(tokenisedInputs, processEvent, reqMetadata);
const leadIdObj = {
insert: [],
delete: [],
};
let finalInputForRecordEvent;
if (inputs[0].message.channel === 'sources') {
finalInputForRecordEvent = transformForRecordEvent(tokenisedInputs, leadIdObj);
respList = await simpleProcessRouterDest(finalInputForRecordEvent, processEvent, reqMetadata);
} else {
respList = await simpleProcessRouterDest(tokenisedInputs, processEvent, reqMetadata);
}
return respList;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export const data = [
output: {
version: '1',
type: 'REST',
method: 'POST',
method: 'DELETE',
endpoint:
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=1&id=2&id=3',
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=4&id=5&id=6',
headers: {
Authorization: 'Bearer access_token_success',
'Content-Type': 'application/json',
Expand All @@ -80,9 +80,9 @@ export const data = [
output: {
version: '1',
type: 'REST',
method: 'DELETE',
method: 'POST',
endpoint:
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=4&id=5&id=6',
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=1&id=2&id=3',
headers: {
Authorization: 'Bearer access_token_success',
'Content-Type': 'application/json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1232,9 +1232,9 @@ export const data = [
{
version: '1',
type: 'REST',
method: 'POST',
method: 'DELETE',
endpoint:
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=1&id=2&id=3',
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=4&id=5&id=6',
headers: {
Authorization: 'Bearer access_token_success',
'Content-Type': 'application/json',
Expand All @@ -1251,9 +1251,9 @@ export const data = [
{
version: '1',
type: 'REST',
method: 'DELETE',
method: 'POST',
endpoint:
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=4&id=5&id=6',
'https://marketo_acct_id_success.mktorest.com/rest/v1/lists/1234/leads.json?id=1&id=2&id=3',
headers: {
Authorization: 'Bearer access_token_success',
'Content-Type': 'application/json',
Expand Down

0 comments on commit c50310d

Please sign in to comment.