diff --git a/src/v0/destinations/delighted/util.js b/src/v0/destinations/delighted/util.js index 2c92685fd7..489b2fc06c 100644 --- a/src/v0/destinations/delighted/util.js +++ b/src/v0/destinations/delighted/util.js @@ -3,7 +3,8 @@ const { InstrumentationError, NetworkError, } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); +const { handleHttpRequest } = require('../../../adapters/network'); +const { isHttpStatusSuccess } = require('../../util'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const { getValueFromMessage } = require('../../util'); const { ENDPOINT } = require('./config'); @@ -48,11 +49,9 @@ const userValidity = async (channel, Config, userId) => { } else if (channel === 'sms') { paramsdata.phone_number = userId; } - const basicAuth = Buffer.from(Config.apiKey).toString('base64'); - let response; - try { - response = await myAxios.get( + const { processedResponse: processedResponseDl } = await handleHttpRequest( + 'get', `${ENDPOINT}`, { headers: { @@ -63,34 +62,35 @@ const userValidity = async (channel, Config, userId) => { }, { destType: 'delighted', feature: 'transformation' }, ); - if (response && response.data && response.status === 200 && Array.isArray(response.data)) { - return response.data.length > 0; - } - throw new NetworkInstrumentationError('Invalid response'); - } catch (error) { - let errMsg = ''; - let errStatus = 400; - if (error.response && error.response.data) { - errMsg = JSON.stringify(error.response.data); - switch (error.response.status) { - case 422: - case 401: - case 406: - case 403: - errStatus = 400; - break; - case 500: - case 503: - errStatus = 500; - break; - default: - errStatus = 400; + + if (!isHttpStatusSuccess(processedResponseDl.status)) { + let errMsg = ''; + let errStatus = 400; + if (processedResponseDl.response) { + errMsg = JSON.stringify(processedResponseDl.response); + switch (processedResponseDl.status) { + case 422: + case 401: + case 406: + case 403: + errStatus = 400; + break; + case 500: + case 503: + errStatus = 500; + break; + default: + errStatus = 400; + } } + throw new NetworkError(`Error occurred while checking user : ${errMsg}`, errStatus, { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus), + }); } - throw new NetworkError(`Error occurred while checking user : ${errMsg}`, errStatus, { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus), - }); - } + if (processedResponseDl.status === 200 && Array.isArray(processedResponseDl.response)) { + return processedResponseDl.response.length > 0; + } + throw new NetworkInstrumentationError('Invalid response'); }; const eventValidity = (Config, message) => { const event = getValueFromMessage(message, 'event'); diff --git a/src/v0/destinations/drip/util.js b/src/v0/destinations/drip/util.js index a502cf0d20..3ca5fe4f4f 100644 --- a/src/v0/destinations/drip/util.js +++ b/src/v0/destinations/drip/util.js @@ -1,11 +1,11 @@ const { NetworkError, AbortedError } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const logger = require('../../../logger'); -const { constructPayload, isDefinedAndNotNull } = require('../../util'); +const { constructPayload, isDefinedAndNotNull, isHttpStatusSuccess } = require('../../util'); const { ENDPOINT, productMapping } = require('./config'); const tags = require('../../util/tags'); const { JSON_MIME_TYPE } = require('../../util/constant'); +const { handleHttpRequest } = require('../../../adapters/network'); const isValidEmail = (email) => { const re = @@ -21,9 +21,9 @@ const isValidTimestamp = (timestamp) => { const userExists = async (Config, id) => { const basicAuth = Buffer.from(Config.apiKey).toString('base64'); - let response; try { - response = await myAxios.get( + const { processedResponse: processedResponseDrip } = await handleHttpRequest( + 'get', `${ENDPOINT}/v2/${Config.accountId}/subscribers/${id}`, { headers: { @@ -33,17 +33,20 @@ const userExists = async (Config, id) => { }, { destType: 'drip', feature: 'transformation' }, ); - if (response && response.status) { - return response.status === 200; + + if (!isHttpStatusSuccess(processedResponseDrip.status)) { + throw new NetworkError( + 'Invalid response.', + processedResponseDrip.status, + { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseDrip.status), + }, + processedResponseDrip.response, + ); + } + if (processedResponseDrip?.status) { + return processedResponseDrip.status === 200; } - throw new NetworkError( - 'Invalid response.', - response?.status, - { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(response?.status), - }, - response, - ); } catch (error) { let errMsg = ''; let errStatus = 400; @@ -57,11 +60,12 @@ const userExists = async (Config, id) => { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus), }); } + return false; }; const createUpdateUser = async (finalpayload, Config, basicAuth) => { - try { - const response = await myAxios.post( + const { processedResponse: processedResponseDrip } = await handleHttpRequest( + 'post', `${ENDPOINT}/v2/${Config.accountId}/subscribers`, finalpayload, { @@ -72,17 +76,16 @@ const createUpdateUser = async (finalpayload, Config, basicAuth) => { }, { destType: 'drip', feature: 'transformation' }, ); - if (response) { - return response.status === 200 || response.status === 201; + + if (processedResponseDrip) { + return processedResponseDrip.status === 200 || processedResponseDrip.status === 201; } - throw new AbortedError('Invalid response.'); - } catch (error) { + let errMsg = ''; - if (error.response && error.response.data) { - errMsg = JSON.stringify(error.response.data); + if (processedResponseDrip.response) { + errMsg = JSON.stringify(processedResponseDrip.response); } throw new AbortedError(`Error occurred while creating or updating user : ${errMsg}`); - } }; const createList = (productList) => { diff --git a/src/v0/destinations/gainsight/transform.js b/src/v0/destinations/gainsight/transform.js index f47296f066..24431a3f22 100644 --- a/src/v0/destinations/gainsight/transform.js +++ b/src/v0/destinations/gainsight/transform.js @@ -103,7 +103,7 @@ const groupResponseBuilder = async (message, { Config }) => { payload = removeUndefinedAndNullValues(payload); let groupGsid; - if (resp.data.data.records.length === 0) { + if (resp?.data?.records?.length === 0) { groupGsid = await createGroup(payload, Config); } else { groupGsid = await updateGroup(payload, Config); diff --git a/src/v0/destinations/gainsight/util.js b/src/v0/destinations/gainsight/util.js index 39e666c1a5..40c5b3abba 100644 --- a/src/v0/destinations/gainsight/util.js +++ b/src/v0/destinations/gainsight/util.js @@ -3,17 +3,17 @@ const { RetryableError, NetworkError, } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const logger = require('../../../logger'); const { ENDPOINTS, getLookupPayload } = require('./config'); const tags = require('../../util/tags'); const { JSON_MIME_TYPE } = require('../../util/constant'); +const { handleHttpRequest } = require('../../../adapters/network'); +const { isHttpStatusSuccess } = require('../../util'); const searchGroup = async (groupName, Config) => { - let resp; - try { - resp = await myAxios.post( + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'post', `${ENDPOINTS.groupSearchEndpoint(Config.domain)}`, getLookupPayload(groupName), { @@ -24,28 +24,30 @@ const searchGroup = async (groupName, Config) => { }, { destType: 'gainsight', feature: 'transformation' }, ); - } catch (error) { - let errMessage = ''; - let errorStatus = 500; - if (error.response && error.response.data) { - errMessage = error.response.data.errorDesc; - errorStatus = error.response.status; + + if (!isHttpStatusSuccess(processedResponseGs.status) && processedResponseGs >= 400 && processedResponseGs < 500) { + let errMessage = ''; + let errorStatus = 500; + if (processedResponseGs.response) { + errMessage = processedResponseGs.response.errorDesc; + errorStatus = processedResponseGs.status; + } + throw new NetworkError(`failed to search group ${errMessage}`, errorStatus, { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus), + }, processedResponseGs.response); } - throw new NetworkError(`failed to search group ${errMessage}`, errorStatus, { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus), - }); - } - if (!resp || !resp.data || resp.status !== 200) { - throw new RetryableError('failed to search group'); + if (processedResponseGs.status !== 200) { + throw new RetryableError('failed to search group', processedResponseGs.status, { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status), + }, processedResponseGs.response); } - return resp; + return processedResponseGs.response; }; const createGroup = async (payload, Config) => { - let resp; - try { - resp = await myAxios.post( + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'post', `${ENDPOINTS.groupCreateEndpoint(Config.domain)}`, { records: [payload], @@ -58,28 +60,30 @@ const createGroup = async (payload, Config) => { }, { destType: 'gainsight', feature: 'transformation' }, ); - } catch (error) { + + if (!isHttpStatusSuccess(processedResponseGs.status) && processedResponseGs >= 400 && processedResponseGs < 500) { let errMessage = ''; let errorStatus = 500; - if (error.response && error.response.data) { - errMessage = error.response.data.errorDesc; - errorStatus = error.response.status; + if (processedResponseGs.response) { + errMessage = processedResponseGs.response.errorDesc; + errorStatus = processedResponseGs.status; } throw new NetworkError(`failed to create group ${errMessage}`, errorStatus, { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus), - }); + }, processedResponseGs.response); } - if (!resp || !resp.data || resp.status !== 200) { - throw new RetryableError('failed to create group'); + if (processedResponseGs.status !== 200) { + throw new RetryableError('failed to create group', processedResponseGs.status, { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status), + }, processedResponseGs.response); } - return resp.data.data.records[0].Gsid; + return processedResponseGs.response?.data?.records[0].Gsid; }; const updateGroup = async (payload, Config) => { - let resp; - try { - resp = await myAxios.put( + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'put', `${ENDPOINTS.groupUpdateEndpoint(Config.domain)}`, { records: [payload], @@ -95,22 +99,25 @@ const updateGroup = async (payload, Config) => { }, { destType: 'gainsight', feature: 'transformation' }, ); - } catch (error) { - let errMessage = ''; - let errorStatus = 500; - if (error.response && error.response.data) { - errMessage = error.response.data.errorDesc; - errorStatus = error.response.status; - } - throw new NetworkError(`failed to update group ${errMessage}`, errorStatus, { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus), - }); - } - if (!resp || !resp.data || resp.status !== 200) { - throw new RetryableError('failed to update group'); - } - return resp.data.data.records[0].Gsid; + if (!isHttpStatusSuccess(processedResponseGs.status) && processedResponseGs >= 400 && processedResponseGs < 500) { + let errMessage = ''; + let errorStatus = 500; + if (processedResponseGs.response) { + errMessage = processedResponseGs.response.errorDesc; + errorStatus = processedResponseGs.status; + } + throw new NetworkError(`failed to create group ${errMessage}`, errorStatus, { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus), + }, processedResponseGs.response); + } + + if (processedResponseGs.status !== 200) { + throw new RetryableError('failed to create group', processedResponseGs.status, { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status), + }, processedResponseGs.response); + } + return processedResponseGs.response?.data?.records[0].Gsid; }; /** diff --git a/src/v0/destinations/gainsight_px/util.js b/src/v0/destinations/gainsight_px/util.js index 5109286b3f..6b29b0e7a0 100644 --- a/src/v0/destinations/gainsight_px/util.js +++ b/src/v0/destinations/gainsight_px/util.js @@ -1,9 +1,9 @@ const { NetworkError } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); const { ENDPOINTS } = require('./config'); const tags = require('../../util/tags'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const { JSON_MIME_TYPE } = require('../../util/constant'); +const { handleHttpRequest } = require('../../../adapters/network'); const handleErrorResponse = (error, customErrMessage, expectedErrStatus, defaultStatus = 400) => { let errMessage = ''; @@ -46,9 +46,9 @@ const objectExists = async (id, Config, objectType) => { err = 'invalid response while searching account'; } - let response; try { - response = await myAxios.get( + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'get', url, { headers: { @@ -58,18 +58,18 @@ const objectExists = async (id, Config, objectType) => { }, { destType: 'gainsight_px', feature: 'transformation' }, ); - if (response && response.status === 200) { + if (processedResponseGs?.status === 200) { return { success: true, err: null }; } const defStatus = 400; - const status = response ? response.status || defStatus : defStatus; + const status = processedResponseGs.status || defStatus; throw new NetworkError( err, status, { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), }, - response, + processedResponseGs.response, ); } catch (error) { return handleErrorResponse(error, `error while fetching ${objectType}`, 404); @@ -77,9 +77,9 @@ const objectExists = async (id, Config, objectType) => { }; const createAccount = async (payload, Config) => { - let response; try { - response = await myAxios.post( + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'post', ENDPOINTS.ACCOUNTS_ENDPOINT, payload, { @@ -90,19 +90,17 @@ const createAccount = async (payload, Config) => { }, { destType: 'gainsight_px', feature: 'transformation' }, ); - if (response && response.status === 201) { + if (processedResponseGs?.status === 200) { return { success: true, err: null }; } - - const defStatus = 400; - const status = response ? response.status || defStatus : defStatus; + const status = processedResponseGs.status || 400 ; throw new NetworkError( 'invalid response while creating account', status, { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), }, - response, + processedResponseGs.response, ); } catch (error) { return handleErrorResponse(error, 'error while creating account', 400); @@ -110,9 +108,9 @@ const createAccount = async (payload, Config) => { }; const updateAccount = async (accountId, payload, Config) => { - let response; try { - response = await myAxios.put( + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'put', `${ENDPOINTS.ACCOUNTS_ENDPOINT}/${accountId}`, payload, { @@ -123,18 +121,22 @@ const updateAccount = async (accountId, payload, Config) => { }, { destType: 'gainsight_px', feature: 'transformation' }, ); - if (response && response.status === 204) { + if (processedResponseGs?.status === 200) { return { success: true, err: null }; } - const defStatus = 400; - const status = response ? response.status || defStatus : defStatus; + + if (processedResponseGs.status === 404 && processedResponseGs?.response?.externalapierror?.status === 'NOT_FOUND') { + return { success: false, err: null }; + } + + const status = processedResponseGs.status || 400; throw new NetworkError( 'invalid response while updating account', status, { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), }, - response, + processedResponseGs.response, ); } catch (error) { // it will only occur if the user does not exist diff --git a/src/v0/destinations/kustomer/util.js b/src/v0/destinations/kustomer/util.js index 571a03f139..9fdb2e5668 100644 --- a/src/v0/destinations/kustomer/util.js +++ b/src/v0/destinations/kustomer/util.js @@ -3,11 +3,11 @@ const lodash = require('lodash'); const set = require('set-value'); const get = require('get-value'); const { NetworkError, AbortedError } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); const { DEFAULT_BASE_ENDPOINT } = require('./config'); const { getType, isDefinedAndNotNull, isObject } = require('../../util'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const tags = require('../../util/tags'); +const { handleHttpRequest } = require('../../../adapters/network'); /** * RegExp to test a string for a ISO 8601 Date spec @@ -130,9 +130,10 @@ const handleResponse = (response) => { }; const fetchKustomer = async (url, destination) => { - let response; + let { processedResponse: processedResponseGs }; try { - response = await myAxios.get( + processedResponseGs = await handleHttpRequest( + 'get', url, { headers: { @@ -147,7 +148,7 @@ const fetchKustomer = async (url, destination) => { } throw new AbortedError(err.message); } - return handleResponse(response); + return handleResponse(processedResponseGs.response); }; module.exports = { diff --git a/src/v0/destinations/mailchimp/utils.js b/src/v0/destinations/mailchimp/utils.js index 94be0a24bd..688c1ead28 100644 --- a/src/v0/destinations/mailchimp/utils.js +++ b/src/v0/destinations/mailchimp/utils.js @@ -44,6 +44,8 @@ const MAILCHIMP_IDENTIFY_EXCLUSION = [ 'birthday', ]; + + /** * Returns common endpoint for mailchimp * @param {*} datacenterId <-- from webapp config diff --git a/src/v0/destinations/sfmc/transform.js b/src/v0/destinations/sfmc/transform.js index 879ca1989a..cc1bfc1bf3 100644 --- a/src/v0/destinations/sfmc/transform.js +++ b/src/v0/destinations/sfmc/transform.js @@ -4,7 +4,6 @@ const { ConfigurationError, InstrumentationError, } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); const { EventType } = require('../../../constants'); const { CONFIG_CATEGORIES, MAPPING_CONFIG, ENDPOINTS } = require('./config'); const { @@ -26,6 +25,7 @@ const { } = require('../../../adapters/utils/networkUtils'); const tags = require('../../util/tags'); const { JSON_MIME_TYPE } = require('../../util/constant'); +const { handleHttpRequest } = require('../../../adapters/network'); const CONTACT_KEY_KEY = 'Contact Key'; @@ -33,7 +33,8 @@ const CONTACT_KEY_KEY = 'Contact Key'; const getToken = async (clientId, clientSecret, subdomain) => { try { - const resp = await myAxios.post( + const { processedResponse: processedResponseSfmc } = await handleHttpRequest( + 'post', `https://${subdomain}.${ENDPOINTS.GET_TOKEN}`, { grant_type: 'client_credentials', @@ -45,17 +46,17 @@ const getToken = async (clientId, clientSecret, subdomain) => { }, { destType: 'sfmc', feature: 'transformation' }, ); - if (resp && resp.data) { - return resp.data.access_token; + if (processedResponseSfmc.response) { + return processedResponseSfmc.response.access_token; } - const status = resp.status || 400; + const status = processedResponseSfmc.status || 400; throw new NetworkError( 'Could not retrieve access token', status, { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), }, - resp, + processedResponseSfmc.response, ); } catch (error) { if (!isEmpty(error.response)) { diff --git a/src/v0/destinations/trengo/transform.js b/src/v0/destinations/trengo/transform.js index 06e5496a1e..2d776de268 100644 --- a/src/v0/destinations/trengo/transform.js +++ b/src/v0/destinations/trengo/transform.js @@ -9,7 +9,6 @@ const { InstrumentationError, NetworkInstrumentationError, } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); const { EventType } = require('../../../constants'); const { EndPoints, BASE_URL } = require('./config'); const { @@ -27,6 +26,7 @@ const { const tags = require('../../util/tags'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const { JSON_MIME_TYPE } = require('../../util/constant'); +const { handleHttpRequest } = require('../../../adapters/network'); /** * @@ -81,9 +81,10 @@ const validate = (email, phone, channelIdentifier) => { * In case no contact is founf for a particular identifer it returns -1 */ const lookupContact = async (term, destination) => { - let res; + let { processedResponse: processedResponseTrengo }; try { - res = await myAxios.get( + processedResponseTrengo = await handleHttpRequest( + 'get', `${BASE_URL}/contacts?page=1&term=${term}`, { headers: { @@ -98,15 +99,14 @@ const lookupContact = async (term, destination) => { throw new NetworkError( `Inside lookupContact, failed to make request: ${err.response?.statusText}`, status, - { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status), }, err.response, ); } - if (res && res.status === 200 && res.data && res.data.data && Array.isArray(res.data.data)) { - const { data } = res.data; + if (processedResponseTrengo.status === 200 && processedResponseTrengo.response?.data && Array.isArray(processedResponseTrengo.response.data)) { + const { data } = processedResponseTrengo.response; if (data.length > 1) { throw new NetworkInstrumentationError( `Inside lookupContact, duplicates present for identifier : ${term}`, diff --git a/src/v0/destinations/zendesk/transform.js b/src/v0/destinations/zendesk/transform.js index bf2bc01ed2..f8bf52722b 100644 --- a/src/v0/destinations/zendesk/transform.js +++ b/src/v0/destinations/zendesk/transform.js @@ -5,8 +5,6 @@ const { InstrumentationError, NetworkError, } = require('@rudderstack/integrations-lib'); -const myAxios = require('../../../util/myAxios'); - const { EventType } = require('../../../constants'); const { ConfigCategory, @@ -30,7 +28,7 @@ const { } = require('../../util'); const { getSourceName } = require('./util'); const logger = require('../../../logger'); -const { httpGET } = require('../../../adapters/network'); +const { httpGET, handleHttpRequest } = require('../../../adapters/network'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const tags = require('../../util/tags'); const { JSON_MIME_TYPE } = require('../../util/constant'); @@ -144,11 +142,11 @@ async function createUserFields(url, config, newFields, fieldJson) { }; try { - const response = await myAxios.post(url, fieldData, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('post', url, fieldData, config, { destType: 'zendesk', feature: 'transformation', }); - if (response.status !== 201) { + if (processedResponseZn.status !== 201) { logger.debug(`${NAME}:: Failed to create User Field : `, field); } } catch (error) { @@ -173,12 +171,12 @@ async function checkAndCreateUserFields( const config = { headers }; try { - const response = await myAxios.get(url, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('get', url, config, { destType: 'zendesk', feature: 'transformation', }); - const fields = get(response.data, fieldJson); - if (response.data && fields) { + const fields = get(processedResponseZn.response?.data, fieldJson); + if (processedResponseZn.response?.data && fields) { // get existing user_fields and concatenate them with default fields let existingKeys = fields.map((field) => field.key); existingKeys = existingKeys.concat(defaultFields[fieldJson]); @@ -280,16 +278,16 @@ async function getUserId(message, headers, baseEndpoint, type) { const config = { headers }; try { - const resp = await myAxios.get(url, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('get', url, config, { destType: 'zendesk', feature: 'transformation', }); - if (!resp || !resp.data || resp.data.count === 0) { + if (!processedResponseZn.response || processedResponseZn.response.count === 0) { logger.debug(`${NAME}:: User not found`); return undefined; } - const zendeskUserId = resp?.data?.users?.[0]?.id; + const zendeskUserId = processedResponseZn?.response?.users?.[0]?.id; return zendeskUserId; } catch (error) { // logger.debug( @@ -304,11 +302,11 @@ async function isUserAlreadyAssociated(userId, orgId, headers, baseEndpoint) { const url = `${baseEndpoint}/users/${userId}/organization_memberships.json`; const config = { headers }; try { - const response = await myAxios.get(url, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('get', url, config, { destType: 'zendesk', feature: 'transformation', }); - if (response?.data?.organization_memberships?.[0]?.organization_id === orgId) { + if (processedResponseZn.response?.organization_memberships?.[0]?.organization_id === orgId) { return true; } } catch (error) { @@ -336,18 +334,18 @@ async function createUser(message, headers, destinationConfig, baseEndpoint, typ const payload = { user: userObject }; try { - const resp = await myAxios.post(url, payload, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('post', url, payload, config, { destType: 'zendesk', feature: 'transformation', }); - if (!resp.data || !resp.data.user || !resp.data.user.id) { + if (!processedResponseZn.response || !processedResponseZn.response?.user || !processedResponseZn.response?.user?.id) { logger.debug(`${NAME}:: Couldn't create User: ${name}`); throw new NetworkInstrumentationError('user not found'); } - const userID = resp?.data?.user?.id; - const userEmail = resp?.data?.user.email; + const userID = processedResponseZn.response?.user?.id; + const userEmail = processedResponseZn.response?.user?.email; return { zendeskUserId: userID, email: userEmail }; } catch (error) { logger.debug(error); @@ -417,17 +415,17 @@ async function createOrganization(message, category, headers, destinationConfig, const config = { headers }; try { - const resp = await myAxios.post(url, payload, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('post', url, payload, config, { destType: 'zendesk', feature: 'transformation', }); - if (!resp.data || !resp.data.organization) { + if (!processedResponseZn.response || !processedResponseZn.response?.organization) { logger.debug(`${NAME}:: Couldn't create Organization: ${message.traits.name}`); return undefined; } - const orgId = resp?.data?.organization?.id; + const orgId = processedResponseZn.response?.organization?.id; return orgId; } catch (error) { logger.debug(`${NAME}:: Couldn't create Organization: ${message.traits.name}`); @@ -485,17 +483,17 @@ async function processIdentify(message, destinationConfig, headers, baseEndpoint const membershipUrl = `${baseEndpoint}users/${userId}/organization_memberships.json`; try { const config = { headers }; - const response = await myAxios.get(membershipUrl, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('get', membershipUrl, config, { destType: 'zendesk', feature: 'transformation', }); if ( - response.data && - response.data.organization_memberships && - response.data.organization_memberships.length > 0 && - orgId === response.data.organization_memberships[0].organization_id + processedResponseZn.response && + processedResponseZn.response.organization_memberships && + processedResponseZn.response.organization_memberships.length > 0 && + orgId === processedResponseZn.response.organization_memberships[0].organization_id ) { - const membershipId = response.data.organization_memberships[0]?.id; + const membershipId = processedResponseZn.response.organization_memberships[0]?.id; const deleteResponse = defaultRequestConfig(); deleteResponse.endpoint = `${baseEndpoint}users/${userId}/organization_memberships/${membershipId}.json`; @@ -532,11 +530,11 @@ async function processTrack(message, destinationConfig, headers, baseEndpoint) { const url = `${baseEndpoint}users/search.json?query=${userEmail}`; const config = { headers }; try { - const userResponse = await myAxios.get(url, config, { + const { processedResponse: processedResponseZn } = await handleHttpRequest('get', url, config, { destType: 'zendesk', feature: 'transformation', }); - if (!get(userResponse, 'data.users.0.id') || userResponse.data.count === 0) { + if (!get(processedResponseZn.response, 'data.users.0.id') || processedResponseZn.response.count === 0) { const { zendeskUserId, email } = await createUser( message, headers, @@ -552,7 +550,7 @@ async function processTrack(message, destinationConfig, headers, baseEndpoint) { zendeskUserID = zendeskUserId; userEmail = email; } - zendeskUserID = zendeskUserID || userResponse?.data?.users?.[0]?.id; + zendeskUserID = zendeskUserID || processedResponseZn.response?.users?.[0]?.id; } catch (error) { throw new NetworkError( `Failed to fetch user with email: ${userEmail} due to ${error.message}`,