Skip to content

Commit

Permalink
chore: propagate metadata in following destinations
Browse files Browse the repository at this point in the history
- canny
- clickup
- custify
- freshmarketer
- freshsales
- wootric
  • Loading branch information
Sai Sankeerth committed Jun 19, 2024
1 parent 4ad49df commit e502b81
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 70 deletions.
16 changes: 8 additions & 8 deletions src/v0/destinations/canny/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const identifyResponseBuilder = (message, { Config }) => {
return responseBuilder(responseConfgs);
};

const getTrackResponse = async (apiKey, message, operationType) => {
const getTrackResponse = async (apiKey, message, operationType, metadata) => {
let endpoint;
let responseBody;
let contentType;
Expand All @@ -70,7 +70,7 @@ const getTrackResponse = async (apiKey, message, operationType) => {
}

payload.apiKey = apiKey;
const voterID = await retrieveUserId(apiKey, message);
const voterID = await retrieveUserId(apiKey, message, metadata);
payload.voterID = voterID;
endpoint = ConfigCategory.CREATE_VOTE.endpoint;
} else if (operationType === 'createPost') {
Expand All @@ -82,7 +82,7 @@ const getTrackResponse = async (apiKey, message, operationType) => {
validateCreatePostFields(payload);

payload.apiKey = apiKey;
payload.authorID = await retrieveUserId(apiKey, message);
payload.authorID = await retrieveUserId(apiKey, message, metadata);

endpoint = ConfigCategory.CREATE_POST.endpoint;
}
Expand All @@ -97,7 +97,7 @@ const getTrackResponse = async (apiKey, message, operationType) => {
return responseBuilder(responseConfgs);
};

const trackResponseBuilder = async (message, { Config }) => {
const trackResponseBuilder = async (message, { Config }, metadata) => {
const { apiKey, eventsToEvents } = Config;
const configuredEventsMap = getHashFromArrayWithDuplicate(eventsToEvents);

Expand All @@ -113,7 +113,7 @@ const trackResponseBuilder = async (message, { Config }) => {
// eslint-disable-next-line no-restricted-syntax
for (const destinationEvent of destinationEvents) {
// eslint-disable-next-line no-await-in-loop
const response = await getTrackResponse(apiKey, message, destinationEvent);
const response = await getTrackResponse(apiKey, message, destinationEvent, metadata);
responseArray.push(response);
}
}
Expand All @@ -122,7 +122,7 @@ const trackResponseBuilder = async (message, { Config }) => {
return responseArray;
};

const processEvent = (message, destination) => {
const processEvent = (message, destination, metadata) => {
if (!destination.Config.apiKey) {
throw new ConfigurationError('API Key is not present. Aborting message.');
}
Expand All @@ -137,15 +137,15 @@ const processEvent = (message, destination) => {
response = identifyResponseBuilder(message, destination);
break;
case EventType.TRACK:
response = trackResponseBuilder(message, destination);
response = trackResponseBuilder(message, destination, metadata);
break;
default:
throw new InstrumentationError('Message type not supported');
}
return response;
};

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

const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(inputs, process, reqMetadata);
Expand Down
3 changes: 2 additions & 1 deletion src/v0/destinations/canny/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { JSON_MIME_TYPE } = require('../../util/constant');
* @param message
* @returns canny userId
*/
const retrieveUserId = async (apiKey, message) => {
const retrieveUserId = async (apiKey, message, metadata) => {
const cannyId = getDestinationExternalID(message, 'cannyUserId');
if (cannyId) {
return cannyId;
Expand Down Expand Up @@ -43,6 +43,7 @@ const retrieveUserId = async (apiKey, message) => {
qs.stringify(requestBody),
{ headers },
{
metadata,
destType: 'canny',
feature: 'transformation',
endpointPath: `/v1/users/retrieve`,
Expand Down
9 changes: 5 additions & 4 deletions src/v0/destinations/clickup/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const responseBuilder = async (payload, listId, apiToken) => {
throw new TransformationError('Something went wrong while constructing the payload');
};

const trackResponseBuilder = async (message, destination) => {
const trackResponseBuilder = async (message, destination, metadata) => {
const { apiToken, keyToCustomFieldName } = destination.Config;
const { properties } = message;
const externalListId = getDestinationExternalID(message, 'clickUpListId');
Expand All @@ -45,6 +45,7 @@ const trackResponseBuilder = async (message, destination) => {
properties,
listId,
apiToken,
metadata,
);

let payload = constructPayload(message, MAPPING_CONFIG[CONFIG_CATEGORIES.TRACK.name]);
Expand All @@ -55,7 +56,7 @@ const trackResponseBuilder = async (message, destination) => {
return responseBuilder(payload, listId, apiToken);
};

const processEvent = async (message, destination) => {
const processEvent = async (message, destination, metadata) => {
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
Expand All @@ -64,13 +65,13 @@ const processEvent = async (message, destination) => {

const messageType = message.type.toLowerCase();
if (messageType === EventType.TRACK) {
return trackResponseBuilder(message, destination);
return trackResponseBuilder(message, destination, metadata);
}

throw new InstrumentationError(`Event type "${messageType}" is not supported`);
};

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

const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(inputs, process, reqMetadata);
Expand Down
13 changes: 10 additions & 3 deletions src/v0/destinations/clickup/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const getListOfAssignees = (message, type) => {
* @param {*} apiToken
* @returns
*/
const retrieveCustomFields = async (listId, apiToken) => {
const retrieveCustomFields = async (listId, apiToken, metadata) => {
const endpoint = getCustomFieldsEndPoint(listId);
const requestOptions = {
headers: {
Expand All @@ -220,6 +220,7 @@ const retrieveCustomFields = async (listId, apiToken) => {
endpointPath: '/list/listId/field',
requestMethod: 'GET',
module: 'router',
metadata,
});
const processedCustomFieldsResponse = processAxiosResponse(customFieldsResponse);

Expand Down Expand Up @@ -278,11 +279,17 @@ const extractUIMappedCustomFieldDetails = (
* @param {*} apiToken
* @returns [{"id":"b0f40a94-ea2a-4998-a514-8074d0eddcde","value":"https://www.rudderstack.com/"}]
*/
const customFieldsBuilder = async (keyToCustomFieldName, properties, listId, apiToken) => {
const customFieldsBuilder = async (
keyToCustomFieldName,
properties,
listId,
apiToken,
metadata,
) => {
const responseArray = [];
if (properties && keyToCustomFieldName) {
// retrieve available clickup custom field for the given list
const availableCustomFields = await retrieveCustomFields(listId, apiToken);
const availableCustomFields = await retrieveCustomFields(listId, apiToken, metadata);
// convert array to hashMap with key as field name and value as custom field object
const availableCustomFieldsMap = getHashFromArrayWithValueAsObject(
availableCustomFields,
Expand Down
10 changes: 5 additions & 5 deletions src/v0/destinations/custify/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { JSON_MIME_TYPE } = require('../../util/constant');
* @param {*} destination
* @returns
*/
const validateAndBuildResponse = async (message, destination) => {
const validateAndBuildResponse = async ({ message, destination, metadata }) => {
const messageType = message.type.toLowerCase();
const response = defaultRequestConfig();
let responseBody;
Expand All @@ -34,7 +34,7 @@ const validateAndBuildResponse = async (message, destination) => {
category = ConfigCategory.TRACK;
break;
case EventType.GROUP:
responseBody = await processGroup(message, destination);
responseBody = await processGroup(message, destination, metadata);
category = ConfigCategory.GROUP_USER;
break;
default:
Expand All @@ -57,14 +57,14 @@ const validateAndBuildResponse = async (message, destination) => {
return response;
};

const processSingleMessage = async (message, destination) => {
const processSingleMessage = async ({ message, destination, metadata }) => {
if (!message.type) {
throw new InstrumentationError('Message Type is not present. Ignoring message.');
}
return validateAndBuildResponse(message, destination);
return validateAndBuildResponse({ message, destination, metadata });
};

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

const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(inputs, process, reqMetadata);
Expand Down
7 changes: 4 additions & 3 deletions src/v0/destinations/custify/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const { JSON_MIME_TYPE } = require('../../util/constant');
* @param {*} Config
* @api https://docs.custify.com/#tag/Company/paths/~1company/post
*/
const createUpdateCompany = async (companyPayload, Config) => {
const createUpdateCompany = async (companyPayload, Config, metadata) => {
const companyResponse = await httpPOST(
ConfigCategory.GROUP_COMPANY.endpoint,
companyPayload,
Expand All @@ -43,6 +43,7 @@ const createUpdateCompany = async (companyPayload, Config) => {
endpointPath: `/company`,
requestMethod: 'POST',
module: 'router',
metadata,
},
);
const processedCompanyResponse = processAxiosResponse(companyResponse);
Expand Down Expand Up @@ -187,7 +188,7 @@ const processTrack = (message, { Config }) => {
* @api https://docs.custify.com/#tag/People/paths/~1people/post
* @api https://docs.custify.com/#tag/Company/paths/~1company/post
*/
const processGroup = async (message, { Config }) => {
const processGroup = async (message, { Config }, metadata) => {
let companyPayload = constructPayload(message, MappingConfig[ConfigCategory.GROUP_COMPANY.name]);
if (!companyPayload.company_id) {
throw new InstrumentationError('groupId Id is mandatory');
Expand All @@ -205,7 +206,7 @@ const processGroup = async (message, { Config }) => {
});
}
companyPayload = removeUndefinedAndNullValues(companyPayload);
await createUpdateCompany(companyPayload, Config);
await createUpdateCompany(companyPayload, Config, metadata);
const userPayload = constructPayload(message, MappingConfig[ConfigCategory.GROUP_USER.name]);
const { sendAnonymousId } = Config;
if (sendAnonymousId && !userPayload.user_id) {
Expand Down
26 changes: 15 additions & 11 deletions src/v0/destinations/freshmarketer/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const identifyResponseBuilder = (message, { Config }) => {
* @param {*} Config
* @returns
*/
const trackResponseBuilder = async (message, { Config }, event) => {
const trackResponseBuilder = async ({ message, destination: { Config }, metadata }, event) => {
if (!event) {
throw new InstrumentationError('Event name is required for track call.');
}
Expand All @@ -85,11 +85,12 @@ const trackResponseBuilder = async (message, { Config }, event) => {
payload,
message,
Config,
metadata,
);
break;
}
case 'lifecycle_stage': {
response.body.JSON = await UpdateContactWithLifeCycleStage(message, Config);
response.body.JSON = await UpdateContactWithLifeCycleStage(message, Config, metadata);
response.endpoint = `https://${Config.domain}${CONFIG_CATEGORIES.IDENTIFY.baseUrl}`;
break;
}
Expand All @@ -111,7 +112,7 @@ const trackResponseBuilder = async (message, { Config }, event) => {
* @param {*} Config
* @returns
*/
const groupResponseBuilder = async (message, { Config }) => {
const groupResponseBuilder = async ({ message, destination: { Config }, metadata }) => {
const groupType = get(message, 'traits.groupType');
if (!groupType) {
throw new InstrumentationError('groupType is required for Group call');
Expand All @@ -130,7 +131,7 @@ const groupResponseBuilder = async (message, { Config }) => {
response = updateAccountWOContact(payload, Config);
break;
}
const accountDetails = await getUserAccountDetails(payload, userEmail, Config);
const accountDetails = await getUserAccountDetails(payload, userEmail, Config, metadata);
response = identifyResponseConfig(Config);
response.body.JSON.contact = { sales_accounts: accountDetails };
response.body.JSON.unique_identifier = { emails: userEmail };
Expand All @@ -143,7 +144,7 @@ const groupResponseBuilder = async (message, { Config }) => {
'email is required for adding in the marketing lists. Aborting!',
);
}
const userDetails = await getContactsDetails(userEmail, Config);
const userDetails = await getContactsDetails(userEmail, Config, metadata);
const userId = userDetails.response?.contact?.id;
if (!userId) {
throw new NetworkInstrumentationError('Failed in fetching userId. Aborting!');
Expand All @@ -153,7 +154,7 @@ const groupResponseBuilder = async (message, { Config }) => {
if (listId) {
response = updateContactWithList(userId, listId, Config);
} else if (listName) {
listId = await createOrUpdateListDetails(listName, Config);
listId = await createOrUpdateListDetails(listName, Config, metadata);
if (!listId) {
throw new NetworkInstrumentationError('Failed in fetching listId. Aborting!');
}
Expand Down Expand Up @@ -198,7 +199,7 @@ function eventMappingHandler(message, destination) {
return [...mappedEvents];
}

const processEvent = async (message, destination) => {
const processEvent = async ({ message, destination, metadata }) => {
if (!message.type) {
throw new InstrumentationError('Message Type is not present. Aborting message.');
}
Expand All @@ -213,24 +214,27 @@ const processEvent = async (message, destination) => {
if (mappedEvents.length > 0) {
response = await Promise.all(
mappedEvents.map(async (mappedEvent) =>
trackResponseBuilder(message, destination, mappedEvent),
trackResponseBuilder({ message, destination, metadata }, mappedEvent),
),
);
} else {
response = await trackResponseBuilder(message, destination, get(message, 'event'));
response = await trackResponseBuilder(
{ message, destination, metadata },
get(message, 'event'),
);
}
break;
}
case EventType.GROUP:
response = await groupResponseBuilder(message, destination);
response = await groupResponseBuilder({ message, destination, metadata });
break;
default:
throw new InstrumentationError(`message type ${messageType} 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
Loading

0 comments on commit e502b81

Please sign in to comment.