From e502b8108e06597e182008cc26b47049ef76f880 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Wed, 19 Jun 2024 15:23:18 +0530 Subject: [PATCH] chore: propagate metadata in following destinations - canny - clickup - custify - freshmarketer - freshsales - wootric --- src/v0/destinations/canny/transform.js | 16 +++++----- src/v0/destinations/canny/util.js | 3 +- src/v0/destinations/clickup/transform.js | 9 +++--- src/v0/destinations/clickup/util.js | 13 ++++++-- src/v0/destinations/custify/transform.js | 10 +++--- src/v0/destinations/custify/util.js | 7 ++-- .../destinations/freshmarketer/transform.js | 26 ++++++++------- src/v0/destinations/freshmarketer/utils.js | 32 +++++++++++++------ src/v0/destinations/freshsales/transform.js | 23 +++++++------ src/v0/destinations/freshsales/utils.js | 31 ++++++++++++------ src/v0/destinations/wootric/transform.js | 12 +++---- src/v0/destinations/wootric/util.js | 6 ++-- 12 files changed, 118 insertions(+), 70 deletions(-) diff --git a/src/v0/destinations/canny/transform.js b/src/v0/destinations/canny/transform.js index f4364e1fb7..69c9a5b717 100644 --- a/src/v0/destinations/canny/transform.js +++ b/src/v0/destinations/canny/transform.js @@ -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; @@ -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') { @@ -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; } @@ -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); @@ -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); } } @@ -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.'); } @@ -137,7 +137,7 @@ 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'); @@ -145,7 +145,7 @@ const processEvent = (message, destination) => { 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); diff --git a/src/v0/destinations/canny/util.js b/src/v0/destinations/canny/util.js index 1d03eed4b9..eeefc141db 100644 --- a/src/v0/destinations/canny/util.js +++ b/src/v0/destinations/canny/util.js @@ -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; @@ -43,6 +43,7 @@ const retrieveUserId = async (apiKey, message) => { qs.stringify(requestBody), { headers }, { + metadata, destType: 'canny', feature: 'transformation', endpointPath: `/v1/users/retrieve`, diff --git a/src/v0/destinations/clickup/transform.js b/src/v0/destinations/clickup/transform.js index 0637d65bd4..799fdf7e7b 100644 --- a/src/v0/destinations/clickup/transform.js +++ b/src/v0/destinations/clickup/transform.js @@ -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'); @@ -45,6 +45,7 @@ const trackResponseBuilder = async (message, destination) => { properties, listId, apiToken, + metadata, ); let payload = constructPayload(message, MAPPING_CONFIG[CONFIG_CATEGORIES.TRACK.name]); @@ -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'); } @@ -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); diff --git a/src/v0/destinations/clickup/util.js b/src/v0/destinations/clickup/util.js index 74e961906c..5da4192b5b 100644 --- a/src/v0/destinations/clickup/util.js +++ b/src/v0/destinations/clickup/util.js @@ -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: { @@ -220,6 +220,7 @@ const retrieveCustomFields = async (listId, apiToken) => { endpointPath: '/list/listId/field', requestMethod: 'GET', module: 'router', + metadata, }); const processedCustomFieldsResponse = processAxiosResponse(customFieldsResponse); @@ -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, diff --git a/src/v0/destinations/custify/transform.js b/src/v0/destinations/custify/transform.js index 6b08be1c56..d13a476a68 100644 --- a/src/v0/destinations/custify/transform.js +++ b/src/v0/destinations/custify/transform.js @@ -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; @@ -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: @@ -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); diff --git a/src/v0/destinations/custify/util.js b/src/v0/destinations/custify/util.js index b6f3446503..f35dd4dd23 100644 --- a/src/v0/destinations/custify/util.js +++ b/src/v0/destinations/custify/util.js @@ -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, @@ -43,6 +43,7 @@ const createUpdateCompany = async (companyPayload, Config) => { endpointPath: `/company`, requestMethod: 'POST', module: 'router', + metadata, }, ); const processedCompanyResponse = processAxiosResponse(companyResponse); @@ -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'); @@ -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) { diff --git a/src/v0/destinations/freshmarketer/transform.js b/src/v0/destinations/freshmarketer/transform.js index aa0e03811d..9cf9757441 100644 --- a/src/v0/destinations/freshmarketer/transform.js +++ b/src/v0/destinations/freshmarketer/transform.js @@ -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.'); } @@ -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; } @@ -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'); @@ -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 }; @@ -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!'); @@ -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!'); } @@ -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.'); } @@ -213,16 +214,19 @@ 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`); @@ -230,7 +234,7 @@ const processEvent = async (message, destination) => { 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); diff --git a/src/v0/destinations/freshmarketer/utils.js b/src/v0/destinations/freshmarketer/utils.js index c80711ff8d..879cd6eace 100644 --- a/src/v0/destinations/freshmarketer/utils.js +++ b/src/v0/destinations/freshmarketer/utils.js @@ -37,7 +37,7 @@ const getHeaders = (apiKey) => { * @returns * ref: https://developers.freshworks.com/crm/api/#upsert_an_account */ -const createUpdateAccount = async (payload, Config) => { +const createUpdateAccount = async (payload, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -52,6 +52,7 @@ const createUpdateAccount = async (payload, Config) => { endpointPath: `/crm/sales/api/sales_accounts/upsert`, requestMethod: 'POST', module: 'router', + metadata, }); accountResponse = processAxiosResponse(accountResponse); if (accountResponse.status !== 200 && accountResponse.status !== 201) { @@ -80,7 +81,7 @@ const createUpdateAccount = async (payload, Config) => { * @returns * ref: https://developers.freshworks.com/crm/api/#upsert_a_contact */ -const getUserAccountDetails = async (payload, userEmail, Config) => { +const getUserAccountDetails = async (payload, userEmail, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -99,6 +100,7 @@ const getUserAccountDetails = async (payload, userEmail, Config) => { endpointPath: `crm/sales/api/contacts/upsert?include=sales_accounts`, requestMethod: 'POST', module: 'router', + metadata, }); userSalesAccountResponse = processAxiosResponse(userSalesAccountResponse); if (userSalesAccountResponse.status !== 200 && userSalesAccountResponse.status !== 201) { @@ -117,7 +119,7 @@ const getUserAccountDetails = async (payload, userEmail, Config) => { if (!accountDetails) { throw new NetworkInstrumentationError('Fails in fetching user accountDetails'); } - const accountId = await createUpdateAccount(payload, Config); + const accountId = await createUpdateAccount(payload, Config, metadata); const accountDetail = { id: accountId, is_primary: false, @@ -139,7 +141,7 @@ const getUserAccountDetails = async (payload, userEmail, Config) => { * @returns * ref: https://developers.freshworks.com/crm/api/#upsert_an_account */ -const createOrUpdateListDetails = async (listName, Config) => { +const createOrUpdateListDetails = async (listName, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -151,6 +153,7 @@ const createOrUpdateListDetails = async (listName, Config) => { endpointPath: `/crm/sales/api/lists`, requestMethod: 'GET', module: 'router', + metadata, }); listResponse = processAxiosResponse(listResponse); if (listResponse.status !== 200) { @@ -173,6 +176,7 @@ const createOrUpdateListDetails = async (listName, Config) => { endpointPath: `/crm/sales/api/lists`, requestMethod: 'POST', module: 'router', + metadata, }); listResponse = processAxiosResponse(listResponse); if (listResponse.status !== 200) { @@ -231,7 +235,7 @@ const updateContactWithList = (userId, listId, Config) => { * @returns * ref: https://developers.freshworks.com/crm/api/#upsert_a_contact */ -const getContactsDetails = async (userEmail, Config) => { +const getContactsDetails = async (userEmail, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -250,6 +254,7 @@ const getContactsDetails = async (userEmail, Config) => { endpointPath: `/crm/sales/api/contacts/upsert`, requestMethod: 'POST', module: 'router', + metadata, }); userResponse = processAxiosResponse(userResponse); if (userResponse.status !== 200 && userResponse.status !== 201) { @@ -276,8 +281,14 @@ const getContactsDetails = async (userEmail, Config) => { * returns */ -const responseBuilderWithContactDetails = async (email, Config, payload, salesActivityTypeId) => { - const userDetails = await getContactsDetails(email, Config); +const responseBuilderWithContactDetails = async ( + email, + Config, + payload, + salesActivityTypeId, + metadata, +) => { + const userDetails = await getContactsDetails(email, Config, metadata); const userId = userDetails.response?.contact?.id; if (!userId) { throw new NetworkInstrumentationError('Failed in fetching userId. Aborting!'); @@ -295,7 +306,7 @@ const responseBuilderWithContactDetails = async (email, Config, payload, salesAc * @param {*} Config - headers, apiKey... * ref: https://developers.freshworks.com/crm/api/#admin_configuration */ -const UpdateContactWithLifeCycleStage = async (message, Config) => { +const UpdateContactWithLifeCycleStage = async (message, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -326,6 +337,7 @@ const UpdateContactWithLifeCycleStage = async (message, Config) => { endpointPath: `/crm/sales/api/selector/lifecycle_stages`, requestMethod: 'GET', module: 'router', + metadata, }); lifeCycleStagesResponse = processAxiosResponse(lifeCycleStagesResponse); if (lifeCycleStagesResponse.status !== 200) { @@ -368,7 +380,7 @@ const UpdateContactWithLifeCycleStage = async (message, Config) => { * @param {*} Config - headers, apiKey... * ref: https://developers.freshworks.com/crm/api/#list_all_sales_activities */ -const UpdateContactWithSalesActivity = async (payload, message, Config) => { +const UpdateContactWithSalesActivity = async (payload, message, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -414,6 +426,7 @@ const UpdateContactWithSalesActivity = async (payload, message, Config) => { endpointPath: `/crm/sales/api/selector/sales_activity_types`, requestMethod: 'GET', module: 'router', + metadata, }); salesActivityResponse = processAxiosResponse(salesActivityResponse); if (salesActivityResponse.status !== 200) { @@ -452,6 +465,7 @@ const UpdateContactWithSalesActivity = async (payload, message, Config) => { Config, payload, salesActivityDetails.id, + metadata, ); } diff --git a/src/v0/destinations/freshsales/transform.js b/src/v0/destinations/freshsales/transform.js index 096a2d749c..37c081be49 100644 --- a/src/v0/destinations/freshsales/transform.js +++ b/src/v0/destinations/freshsales/transform.js @@ -66,7 +66,7 @@ const identifyResponseBuilder = (message, { Config }) => { * @param {*} Config * @returns */ -const trackResponseBuilder = async (message, { Config }, event) => { +const trackResponseBuilder = async ({ message, destination: { Config }, metadata }, event) => { let payload; const response = defaultRequestConfig(); @@ -78,11 +78,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; } @@ -100,7 +101,7 @@ const trackResponseBuilder = async (message, { Config }, event) => { * @param {*} Config * @returns */ -const groupResponseBuilder = async (message, { Config }) => { +const groupResponseBuilder = async ({ message, destination: { Config }, metadata }) => { const payload = constructPayload(message, MAPPING_CONFIG[CONFIG_CATEGORIES.GROUP.name]); if (!payload) { // fail-safety for developer error @@ -114,7 +115,7 @@ const groupResponseBuilder = async (message, { Config }) => { return updateAccountWOContact(payload, Config); } - const accountDetails = await getUserAccountDetails(payload, userEmail, Config); + const accountDetails = await getUserAccountDetails(payload, userEmail, Config, metadata); const responseIdentify = identifyResponseConfig(Config); responseIdentify.body.JSON.contact = { sales_accounts: accountDetails }; responseIdentify.body.JSON.unique_identifier = { emails: userEmail }; @@ -146,7 +147,8 @@ function eventMappingHandler(message, destination) { return [...mappedEvents]; } -const processEvent = async (message, destination) => { +const processEvent = async (event) => { + const { message, destination, metadata } = event; if (!message.type) { throw new InstrumentationError('Message Type is not present. Aborting message.'); } @@ -162,25 +164,28 @@ const processEvent = async (message, destination) => { if (mappedEvents.length > 0) { const respList = await Promise.all( mappedEvents.map(async (mappedEvent) => - trackResponseBuilder(message, destination, mappedEvent), + trackResponseBuilder({ message, destination, metadata }, mappedEvent), ), ); response = respList; } 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); diff --git a/src/v0/destinations/freshsales/utils.js b/src/v0/destinations/freshsales/utils.js index 977bde0abb..14cca0a3d6 100644 --- a/src/v0/destinations/freshsales/utils.js +++ b/src/v0/destinations/freshsales/utils.js @@ -35,7 +35,7 @@ const getHeaders = (apiKey) => { * @returns * ref: https://developers.freshworks.com/crm/api/#upsert_an_account */ -const createUpdateAccount = async (payload, Config) => { +const createUpdateAccount = async (payload, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -50,6 +50,7 @@ const createUpdateAccount = async (payload, Config) => { endpointPath: `/crm/sales/api/sales_accounts/upsert`, requestMethod: 'POST', module: 'router', + metadata, }); accountResponse = processAxiosResponse(accountResponse); if (accountResponse.status !== 200 && accountResponse.status !== 201) { @@ -77,7 +78,7 @@ const createUpdateAccount = async (payload, Config) => { * @returns * ref: https://developers.freshworks.com/crm/api/#upsert_a_contact */ -const getUserAccountDetails = async (payload, userEmail, Config) => { +const getUserAccountDetails = async (payload, userEmail, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -96,6 +97,7 @@ const getUserAccountDetails = async (payload, userEmail, Config) => { endpointPath: `/crm/sales/api/contacts/upsert?include=sales_accounts`, requestMethod: 'POST', module: 'router', + metadata, }); userSalesAccountResponse = processAxiosResponse(userSalesAccountResponse); if (userSalesAccountResponse.status !== 200 && userSalesAccountResponse.status !== 201) { @@ -114,7 +116,7 @@ const getUserAccountDetails = async (payload, userEmail, Config) => { if (!accountDetails) { throw new NetworkInstrumentationError('Fails in fetching user accountDetails'); } - const accountId = await createUpdateAccount(payload, Config); + const accountId = await createUpdateAccount(payload, Config, metadata); const accountDetail = { id: accountId, is_primary: false, @@ -135,7 +137,7 @@ const getUserAccountDetails = async (payload, userEmail, Config) => { * @returns * ref: https://developers.freshworks.com/crm/api/#upsert_a_contact */ -const getContactsDetails = async (userEmail, Config) => { +const getContactsDetails = async (userEmail, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -154,6 +156,7 @@ const getContactsDetails = async (userEmail, Config) => { endpointPath: `/crm/sales/api/contacts/upsert`, requestMethod: 'POST', module: 'router', + metadata, }); userResponse = processAxiosResponse(userResponse); if (userResponse.status !== 200 && userResponse.status !== 201) { @@ -180,8 +183,14 @@ const getContactsDetails = async (userEmail, Config) => { * returns */ -const responseBuilderWithContactDetails = async (email, Config, payload, salesActivityTypeId) => { - const userDetails = await getContactsDetails(email, Config); +const responseBuilderWithContactDetails = async ( + email, + Config, + payload, + salesActivityTypeId, + metadata, +) => { + const userDetails = await getContactsDetails(email, Config, metadata); const userId = userDetails.response?.contact?.id; if (!userId) { throw new NetworkInstrumentationError('Failed in fetching userId. Aborting!', userDetails); @@ -201,7 +210,7 @@ const responseBuilderWithContactDetails = async (email, Config, payload, salesAc * @param {*} Config - headers, apiKey... * ref: https://developers.freshworks.com/crm/api/#list_all_sales_activities */ -const UpdateContactWithSalesActivity = async (payload, message, Config) => { +const UpdateContactWithSalesActivity = async (payload, message, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -230,11 +239,12 @@ const UpdateContactWithSalesActivity = async (payload, message, Config) => { sales_activity_type_id: payload.sales_activity_type_id, }; } else { - responseBody = responseBuilderWithContactDetails( + responseBody = await responseBuilderWithContactDetails( email, Config, payload, payload.sales_activity_type_id, + metadata, ); } return responseBody; @@ -247,6 +257,7 @@ const UpdateContactWithSalesActivity = async (payload, message, Config) => { endpointPath: `/crm/sales/api/sales_activity_types`, requestMethod: 'GET', module: 'router', + metadata, }); salesActivityResponse = processAxiosResponse(salesActivityResponse); if (salesActivityResponse.status !== 200) { @@ -285,6 +296,7 @@ const UpdateContactWithSalesActivity = async (payload, message, Config) => { Config, payload, salesActivityDetails.id, + metadata, ); } @@ -298,7 +310,7 @@ const UpdateContactWithSalesActivity = async (payload, message, Config) => { * @param {*} Config - headers, apiKey... * ref: https://developers.freshworks.com/crm/api/#admin_configuration */ -const UpdateContactWithLifeCycleStage = async (message, Config) => { +const UpdateContactWithLifeCycleStage = async (message, Config, metadata) => { const requestOptions = { headers: getHeaders(Config.apiKey), }; @@ -329,6 +341,7 @@ const UpdateContactWithLifeCycleStage = async (message, Config) => { endpointPath: `/crm/sales/api/lifecycle_stages`, requestMethod: 'GET', module: 'router', + metadata, }); lifeCycleStagesResponse = processAxiosResponse(lifeCycleStagesResponse); if (lifeCycleStagesResponse.status !== 200) { diff --git a/src/v0/destinations/wootric/transform.js b/src/v0/destinations/wootric/transform.js index f8b4274af7..940d6e9e5d 100644 --- a/src/v0/destinations/wootric/transform.js +++ b/src/v0/destinations/wootric/transform.js @@ -37,17 +37,17 @@ const responseBuilder = async (payload, endpoint, method, accessToken) => { throw new TransformationError('Something went wrong while constructing the payload'); }; -const identifyResponseBuilder = async (message, destination) => { +const identifyResponseBuilder = async ({ message, destination, metadata }) => { let payload; let endpoint; let method; let builder; - const accessToken = await getAccessToken(destination); + const accessToken = await getAccessToken(destination, metadata); const rawEndUserId = getDestinationExternalID(message, 'wootricEndUserId'); const userId = getFieldValueFromMessage(message, 'userIdOnly'); - const userDetails = await retrieveUserDetails(rawEndUserId, userId, accessToken); + const userDetails = await retrieveUserDetails(rawEndUserId, userId, accessToken, metadata); const wootricEndUserId = userDetails?.id; // If user already exist we will update it else creates a new user @@ -132,7 +132,7 @@ const trackResponseBuilder = async (message, destination) => { return responseBuilder(payload, endpoint, method, accessToken); }; -const processEvent = async (message, destination) => { +const processEvent = async ({ message, destination, metadata }) => { if (!message.type) { throw new InstrumentationError('Event type is required'); } @@ -140,7 +140,7 @@ const processEvent = async (message, destination) => { let response; switch (messageType) { case EventType.IDENTIFY: - response = await identifyResponseBuilder(message, destination); + response = await identifyResponseBuilder({ message, destination, metadata }); break; case EventType.TRACK: response = await trackResponseBuilder(message, destination); @@ -151,7 +151,7 @@ const processEvent = async (message, destination) => { 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); diff --git a/src/v0/destinations/wootric/util.js b/src/v0/destinations/wootric/util.js index c2505c635b..398fe94c7d 100644 --- a/src/v0/destinations/wootric/util.js +++ b/src/v0/destinations/wootric/util.js @@ -20,7 +20,7 @@ const ACCESS_TOKEN_CACHE = new Cache(ACCESS_TOKEN_CACHE_TTL_SECONDS); * @param {*} destination * @returns */ -const getAccessToken = async (destination) => { +const getAccessToken = async (destination, metadata) => { const { username, password, accountToken } = destination.Config; const accessTokenKey = destination.ID; @@ -49,6 +49,7 @@ const getAccessToken = async (destination) => { endpointPath: `/oauth/token`, requestMethod: 'POST', module: 'router', + metadata, }); const processedAuthResponse = processAxiosResponse(wootricAuthResponse); // If the request fails, throwing error. @@ -79,7 +80,7 @@ const getAccessToken = async (destination) => { * @returns */ -const retrieveUserDetails = async (endUserId, externalId, accessToken) => { +const retrieveUserDetails = async (endUserId, externalId, accessToken, metadata) => { let endpoint; if (isDefinedAndNotNullAndNotEmpty(endUserId)) { endpoint = `${BASE_ENDPOINT}/${VERSION}/end_users/${endUserId}`; @@ -104,6 +105,7 @@ const retrieveUserDetails = async (endUserId, externalId, accessToken) => { endpointPath: `/v1/end_users/`, requestMethod: 'GET', module: 'router', + metadata, }); const processedUserResponse = processAxiosResponse(userResponse);