Skip to content

Commit

Permalink
chore: propagate metadata for below destinations
Browse files Browse the repository at this point in the history
- monday
- profitwell
- sendgrid
- sendinblue
  • Loading branch information
Sai Sankeerth committed Jun 19, 2024
1 parent e502b81 commit a1f4f0f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
10 changes: 5 additions & 5 deletions src/v0/destinations/monday/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const responseBuilder = (payload, endpoint, apiToken) => {
* @param {*} param1
* @returns
*/
const trackResponseBuilder = async (message, { Config }) => {
const trackResponseBuilder = async ({ message, destination: { Config }, metadata }) => {
const { apiToken } = Config;
let boardId = getDestinationExternalID(message, 'boardId');
const event = get(message, 'event');
Expand All @@ -54,14 +54,14 @@ const trackResponseBuilder = async (message, { Config }) => {
}
const endpoint = ENDPOINT;

const processedResponse = await getBoardDetails(endpoint, boardId, apiToken);
const processedResponse = await getBoardDetails(endpoint, boardId, apiToken, metadata);

const payload = populatePayload(message, Config, processedResponse);

return responseBuilder(payload, endpoint, apiToken);
};

const processEvent = async (message, destination) => {
const processEvent = async ({ message, destination, metadata }) => {
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
Expand All @@ -71,14 +71,14 @@ const processEvent = async (message, destination) => {
const messageType = message.type.toLowerCase();
let response;
if (messageType === EventType.TRACK) {
response = await trackResponseBuilder(message, destination);
response = await trackResponseBuilder({ message, destination, metadata });
} else {
throw new InstrumentationError(`Event type ${messageType} is not supported`);
}
return response;
};

const process = async (event) => processEvent(event.message, event.destination);
const process = async (event) => processEvent(event);

const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(inputs, process, reqMetadata);
Expand Down
3 changes: 2 additions & 1 deletion src/v0/destinations/monday/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const mapColumnValues = (properties, columnToPropertyMapping, board) => {
* @param {*} apiToken
* @returns
*/
const getBoardDetails = async (url, boardID, apiToken) => {
const getBoardDetails = async (url, boardID, apiToken, metadata) => {
const clientResponse = await httpPOST(
url,
{
Expand All @@ -197,6 +197,7 @@ const getBoardDetails = async (url, boardID, apiToken) => {
endpointPath: '/v2',
requestMethod: 'POST',
module: 'router',
metadata,
},
);
const boardDetailsResponse = processAxiosResponse(clientResponse);
Expand Down
13 changes: 7 additions & 6 deletions src/v0/destinations/profitwell/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const tags = require('../../util/tags');
const { JSON_MIME_TYPE } = require('../../util/constant');

const identifyResponseBuilder = async (message, { Config }) => {
const identifyResponseBuilder = async ({ message, destination: { Config }, metadata }) => {
const { userId, userAlias, subscriptionId, subscriptionAlias } =
validatePayloadAndRetunImpIds(message);
let finalSubscriptionId = subscriptionId;
let finalSubscriptionAlias = subscriptionAlias;

const targetUrl = `${BASE_ENDPOINT}/v2/users/${userId || userAlias}/`;
const res = await getSubscriptionHistory(targetUrl, {
const options = {
headers: {
Authorization: Config.privateApiKey,
},
});
};

const targetUrl = `${BASE_ENDPOINT}/v2/users/${userId || userAlias}/`;
const res = await getSubscriptionHistory(targetUrl, options, metadata);

let payload;
const response = defaultRequestConfig();
Expand Down Expand Up @@ -159,7 +160,7 @@ const process = async (event) => {

let response;
if (messageType === EventType.IDENTIFY) {
response = await identifyResponseBuilder(message, destination);
response = await identifyResponseBuilder(event);
} else {
throw new InstrumentationError(`Event type ${messageType} is not supported`);
}
Expand Down
3 changes: 2 additions & 1 deletion src/v0/destinations/profitwell/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const CURRENCY_CODES = [
'zwl',
];

const getSubscriptionHistory = async (endpoint, options) => {
const getSubscriptionHistory = async (endpoint, options, metadata) => {
const requestOptions = {
method: 'get',
...options,
Expand All @@ -191,6 +191,7 @@ const getSubscriptionHistory = async (endpoint, options) => {
endpointPath: '/users/userId',
requestMethod: 'GET',
module: 'router',
metadata,
});
return res;
};
Expand Down
15 changes: 8 additions & 7 deletions src/v0/destinations/sendgrid/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ const responseBuilder = (payload, method, endpoint, apiKey) => {
throw new TransformationError(ErrorMessage.FailedToConstructPayload);
};

const identifyResponseBuilder = async (message, destination) => {
const identifyResponseBuilder = async ({ message, destination, metadata }) => {
validateIdentifyPayload(message);
const builder = await createOrUpdateContactPayloadBuilder(message, destination);
const builder = await createOrUpdateContactPayloadBuilder({ message, destination, metadata });
const { payload, method, endpoint } = builder;
const { apiKey } = destination.Config;
return responseBuilder(payload, method, endpoint, apiKey);
};

const trackResponseBuilder = async (message, { Config }) => {
const trackResponseBuilder = async ({ message, destination: { Config } }) => {
validateTrackPayload(message, Config);
let payload = {};
payload = constructPayload(message, MAPPING_CONFIG[CONFIG_CATEGORIES.TRACK.name]);
Expand Down Expand Up @@ -123,7 +123,8 @@ const trackResponseBuilder = async (message, { Config }) => {
return responseBuilder(payload, method, endpoint, apiKey);
};

const processEvent = async (message, destination) => {
const processEvent = async (event) => {
const { message, destination } = event;
// Validating if message type is even given or not
if (!message.type) {
throw new InstrumentationError('Event type is required');
Expand All @@ -137,18 +138,18 @@ const processEvent = async (message, destination) => {
let response;
switch (messageType) {
case EventType.IDENTIFY:
response = await identifyResponseBuilder(message, destination);
response = await identifyResponseBuilder(event);
break;
case EventType.TRACK:
response = await trackResponseBuilder(message, destination);
response = await trackResponseBuilder(event);
break;
default:
throw new InstrumentationError(`Event type ${messageType} is not supported`);
}
return response;
};

const process = (event) => processEvent(event.message, event.destination);
const process = (event) => processEvent(event);

const generateBatchedPaylaodForArray = (events, combination) => {
let batchEventResponse = defaultBatchRequestConfig();
Expand Down
11 changes: 6 additions & 5 deletions src/v0/destinations/sendgrid/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ const getContactListIds = (message, destination) => {
* @param {*} destination
* @returns
*/
const fetchCustomFields = async (destination) => {
const fetchCustomFields = async ({ destination, metadata }) => {
const { apiKey } = destination.Config;
return customFieldsCache.get(destination.ID, async () => {
const requestOptions = {
Expand All @@ -448,6 +448,7 @@ const fetchCustomFields = async (destination) => {
endpointPath: '/marketing/field_definitions',
requestMethod: 'GET',
module: 'router',
metadata,
});
const processedResponse = processAxiosResponse(resonse);
if (isHttpStatusSuccess(processedResponse.status)) {
Expand Down Expand Up @@ -475,14 +476,14 @@ const fetchCustomFields = async (destination) => {
* @param {*} contactDetails
* @returns
*/
const getCustomFields = async (message, destination) => {
const getCustomFields = async ({ message, destination, metadata }) => {
const customFields = {};
const payload = get(message, 'context.traits');
const { customFieldsMapping } = destination.Config;
const fieldsMapping = getHashFromArray(customFieldsMapping, 'from', 'to', false);
const fields = Object.keys(fieldsMapping);
if (fields.length > 0) {
const destinationCustomFields = await fetchCustomFields(destination);
const destinationCustomFields = await fetchCustomFields({ destination, metadata });
const customFieldNameToIdMapping = {};
const customFieldNamesArray = destinationCustomFields.map((destinationCustomField) => {
const { id, name } = destinationCustomField;
Expand Down Expand Up @@ -511,13 +512,13 @@ const getCustomFields = async (message, destination) => {
* @param {*} destination
* @returns
*/
const createOrUpdateContactPayloadBuilder = async (message, destination) => {
const createOrUpdateContactPayloadBuilder = async ({ message, destination, metadata }) => {
const contactDetails = constructPayload(message, MAPPING_CONFIG[CONFIG_CATEGORIES.IDENTIFY.name]);
if (contactDetails.address_line_1) {
contactDetails.address_line_1 = flattenAddress(contactDetails.address_line_1);
}
const contactListIds = getContactListIds(message, destination);
contactDetails.custom_fields = await getCustomFields(message, destination);
contactDetails.custom_fields = await getCustomFields({ message, destination, metadata });
const payload = { contactDetails, contactListIds };
const { endpoint } = CONFIG_CATEGORIES.IDENTIFY;
const method = defaultPutRequestConfig.requestMethod;
Expand Down
15 changes: 8 additions & 7 deletions src/v0/destinations/sendinblue/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const updateDOIContactResponseBuilder = (message, destination, identifier) => {
);
};

const createOrUpdateDOIContactResponseBuilder = async (message, destination) => {
const createOrUpdateDOIContactResponseBuilder = async ({ message, destination, metadata }) => {
let email = getFieldValueFromMessage(message, 'emailOnly');
const phone = getFieldValueFromMessage(message, 'phone');

Expand All @@ -196,7 +196,7 @@ const createOrUpdateDOIContactResponseBuilder = async (message, destination) =>
}

const { apiKey } = destination.Config;
const contactExists = await checkIfContactExists(identifier, apiKey);
const contactExists = await checkIfContactExists(identifier, apiKey, metadata);

if (contactExists) {
return updateDOIContactResponseBuilder(message, destination, identifier);
Expand All @@ -205,7 +205,7 @@ const createOrUpdateDOIContactResponseBuilder = async (message, destination) =>
return createDOIContactResponseBuilder(message, destination);
};

const identifyResponseBuilder = async (message, destination) => {
const identifyResponseBuilder = async ({ message, destination, metadata }) => {
const { doi } = destination.Config;
if (!doi) {
const unlinkListIds = getListIds(message, 'sendinblueUnlinkListIds');
Expand All @@ -215,7 +215,7 @@ const identifyResponseBuilder = async (message, destination) => {
return createOrUpdateContactResponseBuilder(message, destination);
}

return createOrUpdateDOIContactResponseBuilder(message, destination);
return createOrUpdateDOIContactResponseBuilder({ message, destination, metadata });
};

// ref:- https://tracker-doc.sendinblue.com/reference/trackevent-3
Expand Down Expand Up @@ -305,7 +305,8 @@ const pageResponseBuilder = (message, destination) => {
return responseBuilder(payload, endpoint, destination, true);
};

const processEvent = async (message, destination) => {
const processEvent = async (event) => {
const { message, destination } = event;
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
Expand All @@ -314,7 +315,7 @@ const processEvent = async (message, destination) => {
let response;
switch (messageType) {
case EventType.IDENTIFY:
response = await identifyResponseBuilder(message, destination);
response = await identifyResponseBuilder(event);
break;
case EventType.TRACK:
response = trackResponseBuilder(message, destination);
Expand All @@ -328,7 +329,7 @@ const processEvent = async (message, destination) => {
return response;
};

const process = (event) => processEvent(event.message, event.destination);
const process = (event) => processEvent(event);

const processRouterDest = async (inputs) => {
const respList = await simpleProcessRouterDest(inputs, process, process);
Expand Down
3 changes: 2 additions & 1 deletion src/v0/destinations/sendinblue/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const validateEmailAndPhone = (email, phone = null) => {
*/
const prepareEmailFromPhone = (phone) => `${phone.replace('+', '')}${EMAIL_SUFFIX}`;

const checkIfContactExists = async (identifier, apiKey) => {
const checkIfContactExists = async (identifier, apiKey, metadata) => {
const endpoint = getContactDetailsEndpoint(identifier);
const requestOptions = {
headers: prepareHeader(apiKey),
Expand All @@ -63,6 +63,7 @@ const checkIfContactExists = async (identifier, apiKey) => {
endpointPath: '/contacts',
requestMethod: 'GET',
module: 'router',
metadata,
});

const processedContactDetailsResponse = processAxiosResponse(contactDetailsResponse);
Expand Down

0 comments on commit a1f4f0f

Please sign in to comment.