From d7626f632fc4b3a5e7c9216cadabb55e81d220bb Mon Sep 17 00:00:00 2001 From: ItsSudip Date: Thu, 20 Jun 2024 10:26:32 +0530 Subject: [PATCH] chore: update drip handleHttpRequest call with metadata --- src/v0/destinations/drip/transform.js | 14 ++++++------ src/v0/destinations/drip/util.js | 33 +++++++++------------------ 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/v0/destinations/drip/transform.js b/src/v0/destinations/drip/transform.js index 4ccba076d0..1729ef3cdc 100644 --- a/src/v0/destinations/drip/transform.js +++ b/src/v0/destinations/drip/transform.js @@ -38,7 +38,7 @@ const { } = require('./util'); const { JSON_MIME_TYPE } = require('../../util/constant'); -const identifyResponseBuilder = async (message, { Config }) => { +const identifyResponseBuilder = async (message, { Config }, metadata) => { const id = getDestinationExternalID(message, 'dripId'); let email = getFieldValueFromMessage(message, 'email'); @@ -117,7 +117,7 @@ const identifyResponseBuilder = async (message, { Config }) => { response.method = defaultPostRequestConfig.requestMethod; const campaignId = getDestinationExternalID(message, 'dripCampaignId') || Config.campaignId; if (campaignId && email) { - const check = await createUpdateUser(finalpayload, Config, basicAuth); + const check = await createUpdateUser(finalpayload, Config, basicAuth, metadata); if (!check) { throw new NetworkInstrumentationError('Unable to create/update user.'); } @@ -139,7 +139,7 @@ const identifyResponseBuilder = async (message, { Config }) => { return response; }; -const trackResponseBuilder = async (message, { Config }) => { +const trackResponseBuilder = async (message, { Config }, metadata) => { const id = getDestinationExternalID(message, 'dripId'); let email = getValueFromMessage(message, [ @@ -162,7 +162,7 @@ const trackResponseBuilder = async (message, { Config }) => { event = event.trim().toLowerCase(); if (!Config.enableUserCreation && !id) { - const check = await userExists(Config, email); + const check = await userExists(Config, email, metadata); if (!check) { throw new NetworkInstrumentationError( 'User creation mode is disabled and user does not exist. Track call aborted.', @@ -239,7 +239,7 @@ const trackResponseBuilder = async (message, { Config }) => { }; const process = async (event) => { - const { message, destination } = event; + const { message, destination, metadata } = event; if (!message.type) { throw new InstrumentationError('Message Type is not present. Aborting message.'); } @@ -255,10 +255,10 @@ const process = async (event) => { 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); + response = await trackResponseBuilder(message, destination, metadata); break; default: throw new InstrumentationError(`Message type ${messageType} not supported`); diff --git a/src/v0/destinations/drip/util.js b/src/v0/destinations/drip/util.js index af5c8a485d..aaac468945 100644 --- a/src/v0/destinations/drip/util.js +++ b/src/v0/destinations/drip/util.js @@ -6,7 +6,7 @@ const { constructPayload, isDefinedAndNotNull } = require('../../util'); const { ENDPOINT, productMapping } = require('./config'); const tags = require('../../util/tags'); const { JSON_MIME_TYPE } = require('../../util/constant'); -const { httpGET, httpPOST } = require('../../../adapters/network'); +const { handleHttpRequest } = require('../../../adapters/network'); const isValidEmail = (email) => { const re = @@ -20,10 +20,11 @@ const isValidTimestamp = (timestamp) => { return re.test(String(timestamp)); }; -const userExists = async (Config, id) => { +const userExists = async (Config, id, metadata) => { const basicAuth = Buffer.from(Config.apiKey).toString('base64'); try { - const { response } = await httpGET( + const { httpResponse } = await handleHttpRequest( + 'get', `${ENDPOINT}/v2/${Config.accountId}/subscribers/${id}`, { headers: { @@ -32,6 +33,7 @@ const userExists = async (Config, id) => { }, }, { + metadata, destType: 'drip', feature: 'transformation', requestMethod: 'GET', @@ -39,6 +41,7 @@ const userExists = async (Config, id) => { module: 'router', }, ); + const { response } = httpResponse; if (response && response.status) { return response.status === 200; } @@ -66,9 +69,10 @@ const userExists = async (Config, id) => { } }; -const createUpdateUser = async (finalpayload, Config, basicAuth) => { +const createUpdateUser = async (finalpayload, Config, basicAuth, metadata) => { try { - const { response } = await httpPOST( + const { httpResponse } = await handleHttpRequest( + 'post', `${ENDPOINT}/v2/${Config.accountId}/subscribers`, finalpayload, { @@ -78,6 +82,7 @@ const createUpdateUser = async (finalpayload, Config, basicAuth) => { }, }, { + metadata, destType: 'drip', feature: 'transformation', requestMethod: 'POST', @@ -85,23 +90,7 @@ const createUpdateUser = async (finalpayload, Config, basicAuth) => { module: 'router', }, ); - // await myAxios.post( - // `${ENDPOINT}/v2/${Config.accountId}/subscribers`, - // finalpayload, - // { - // headers: { - // Authorization: `Basic ${basicAuth}`, - // 'Content-Type': JSON_MIME_TYPE, - // }, - // }, - // { - // destType: 'drip', - // feature: 'transformation', - // requestMethod: 'POST', - // endpointPath: '/subscribers', - // module: 'router', - // }, - // ); + const { response } = httpResponse; if (response) { return response.status === 200 || response.status === 201; }