From 9f6ae10048ee93d4c969131c8a3fa9d5b9cc242c Mon Sep 17 00:00:00 2001 From: AASHISH MALIK Date: Wed, 6 Dec 2023 12:20:34 +0530 Subject: [PATCH] fix: addressed comments --- src/v0/destinations/drip/util.js | 38 ++--- src/v0/destinations/gainsight/util.js | 190 ++++++++++++++--------- src/v0/destinations/gainsight_px/util.js | 2 +- src/v0/destinations/kustomer/util.js | 15 +- src/v0/destinations/mailchimp/utils.js | 33 ++-- 5 files changed, 157 insertions(+), 121 deletions(-) diff --git a/src/v0/destinations/drip/util.js b/src/v0/destinations/drip/util.js index 3ca5fe4f4f..fce57bafae 100644 --- a/src/v0/destinations/drip/util.js +++ b/src/v0/destinations/drip/util.js @@ -64,28 +64,28 @@ const userExists = async (Config, id) => { }; const createUpdateUser = async (finalpayload, Config, basicAuth) => { - const { processedResponse: processedResponseDrip } = await handleHttpRequest( - 'post', - `${ENDPOINT}/v2/${Config.accountId}/subscribers`, - finalpayload, - { - headers: { - Authorization: `Basic ${basicAuth}`, - 'Content-Type': JSON_MIME_TYPE, - }, + const { processedResponse: processedResponseDrip } = await handleHttpRequest( + 'post', + `${ENDPOINT}/v2/${Config.accountId}/subscribers`, + finalpayload, + { + headers: { + Authorization: `Basic ${basicAuth}`, + 'Content-Type': JSON_MIME_TYPE, }, - { destType: 'drip', feature: 'transformation' }, - ); + }, + { destType: 'drip', feature: 'transformation' }, + ); - if (processedResponseDrip) { - return processedResponseDrip.status === 200 || processedResponseDrip.status === 201; - } + if (processedResponseDrip) { + return processedResponseDrip.status === 200 || processedResponseDrip.status === 201; + } - let errMsg = ''; - if (processedResponseDrip.response) { - errMsg = JSON.stringify(processedResponseDrip.response); - } - throw new AbortedError(`Error occurred while creating or updating user : ${errMsg}`); + let errMsg = ''; + 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/util.js b/src/v0/destinations/gainsight/util.js index 40c5b3abba..735fb4c035 100644 --- a/src/v0/destinations/gainsight/util.js +++ b/src/v0/destinations/gainsight/util.js @@ -12,112 +12,154 @@ const { handleHttpRequest } = require('../../../adapters/network'); const { isHttpStatusSuccess } = require('../../util'); const searchGroup = async (groupName, Config) => { - const { processedResponse: processedResponseGs } = await handleHttpRequest( - 'post', - `${ENDPOINTS.groupSearchEndpoint(Config.domain)}`, - getLookupPayload(groupName), - { - headers: { - Accesskey: Config.accessKey, - 'Content-Type': JSON_MIME_TYPE, - }, + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'post', + `${ENDPOINTS.groupSearchEndpoint(Config.domain)}`, + getLookupPayload(groupName), + { + headers: { + Accesskey: Config.accessKey, + 'Content-Type': JSON_MIME_TYPE, }, - { destType: 'gainsight', feature: 'transformation' }, - ); + }, + { destType: 'gainsight', feature: 'transformation' }, + ); - 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); + if ( + !isHttpStatusSuccess(processedResponseGs.status) && + processedResponseGs.status >= 400 && + processedResponseGs.status < 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, + ); + } if (processedResponseGs.status !== 200) { - throw new RetryableError('failed to search group', processedResponseGs.status, { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status), - }, processedResponseGs.response); + throw new RetryableError( + 'failed to search group', + processedResponseGs.status, + { + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status), + }, + processedResponseGs.response, + ); } return processedResponseGs.response; }; const createGroup = async (payload, Config) => { - const { processedResponse: processedResponseGs } = await handleHttpRequest( - 'post', - `${ENDPOINTS.groupCreateEndpoint(Config.domain)}`, - { - records: [payload], + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'post', + `${ENDPOINTS.groupCreateEndpoint(Config.domain)}`, + { + records: [payload], + }, + { + headers: { + Accesskey: Config.accessKey, + 'Content-Type': JSON_MIME_TYPE, }, - { - headers: { - Accesskey: Config.accessKey, - 'Content-Type': JSON_MIME_TYPE, - }, - }, - { destType: 'gainsight', feature: 'transformation' }, - ); + }, + { destType: 'gainsight', feature: 'transformation' }, + ); - if (!isHttpStatusSuccess(processedResponseGs.status) && processedResponseGs >= 400 && processedResponseGs < 500) { + if ( + !isHttpStatusSuccess(processedResponseGs.status) && + processedResponseGs.status >= 400 && + processedResponseGs.status < 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); + 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); + 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; }; const updateGroup = async (payload, Config) => { - const { processedResponse: processedResponseGs } = await handleHttpRequest( - 'put', - `${ENDPOINTS.groupUpdateEndpoint(Config.domain)}`, - { - records: [payload], + const { processedResponse: processedResponseGs } = await handleHttpRequest( + 'put', + `${ENDPOINTS.groupUpdateEndpoint(Config.domain)}`, + { + records: [payload], + }, + { + headers: { + Accesskey: Config.accessKey, + 'Content-Type': JSON_MIME_TYPE, + }, + params: { + keys: 'Name', }, + }, + { destType: 'gainsight', feature: 'transformation' }, + ); + + if ( + !isHttpStatusSuccess(processedResponseGs.status) && + processedResponseGs.status >= 400 && + processedResponseGs.status < 500 + ) { + let errMessage = ''; + let errorStatus = 500; + if (processedResponseGs.response) { + errMessage = processedResponseGs.response.errorDesc; + errorStatus = processedResponseGs.status; + } + throw new NetworkError( + `failed to update group ${errMessage}`, + errorStatus, { - headers: { - Accesskey: Config.accessKey, - 'Content-Type': JSON_MIME_TYPE, - }, - params: { - keys: 'Name', - }, + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus), }, - { destType: 'gainsight', feature: 'transformation' }, + processedResponseGs.response, ); + } - 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, { + if (processedResponseGs.status !== 200) { + throw new RetryableError( + 'failed to update group', + processedResponseGs.status, + { [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseGs.status), - }, processedResponseGs.response); - } - return processedResponseGs.response?.data?.records[0].Gsid; + }, + 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 f4f6779d64..91a9b024c8 100644 --- a/src/v0/destinations/gainsight_px/util.js +++ b/src/v0/destinations/gainsight_px/util.js @@ -105,7 +105,7 @@ const updateAccount = async (accountId, payload, Config) => { return { success: false, err: null }; } - return handleErrorResponse(processedResponseGs.response, `error while updating account`, 400); + return handleErrorResponse(processedResponseGs, `error while updating account`, 400); }; /** diff --git a/src/v0/destinations/kustomer/util.js b/src/v0/destinations/kustomer/util.js index c7daffeddf..c84f35a5a3 100644 --- a/src/v0/destinations/kustomer/util.js +++ b/src/v0/destinations/kustomer/util.js @@ -2,9 +2,9 @@ const lodash = require('lodash'); const set = require('set-value'); const get = require('get-value'); -const { NetworkError, AbortedError } = require('@rudderstack/integrations-lib'); +const { NetworkError } = require('@rudderstack/integrations-lib'); const { DEFAULT_BASE_ENDPOINT } = require('./config'); -const { getType, isDefinedAndNotNull, isObject, isHttpStatusSuccess } = require('../../util'); +const { getType, isDefinedAndNotNull, isObject } = require('../../util'); const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils'); const tags = require('../../util/tags'); const { handleHttpRequest } = require('../../../adapters/network'); @@ -101,7 +101,7 @@ const handleResponse = (response, status) => { const { data } = response; switch (status) { case 200: - if (data.id) { + if (data?.id) { return { userExists: true, targetUrl: `${DEFAULT_BASE_ENDPOINT}/v1/customers/${data.id}?replace=false`, @@ -141,14 +141,7 @@ const fetchKustomer = async (url, destination) => { { destType: 'kustomer', feature: 'transformation' }, ); - if (isHttpStatusSuccess(processedResponseGs.status)) { - return handleResponse(processedResponseGs.response, processedResponseGs.status); - } - - if (!isHttpStatusSuccess(processedResponseGs.status)) { - return handleResponse(processedResponseGs.response, processedResponseGs.status); - } - throw new AbortedError(processedResponseGs); + return handleResponse(processedResponseGs.response, processedResponseGs.status); }; module.exports = { diff --git a/src/v0/destinations/mailchimp/utils.js b/src/v0/destinations/mailchimp/utils.js index 688c1ead28..212ffef468 100644 --- a/src/v0/destinations/mailchimp/utils.js +++ b/src/v0/destinations/mailchimp/utils.js @@ -22,7 +22,6 @@ const tags = require('../../util/tags'); const { JSON_MIME_TYPE } = require('../../util/constant'); const { handleHttpRequest } = require('../../../adapters/network'); - const ADDRESS_MANDATORY_FIELDS = ['addr1', 'city', 'state', 'zip']; const MAILCHIMP_IDENTIFY_EXCLUSION = [ @@ -44,8 +43,6 @@ const MAILCHIMP_IDENTIFY_EXCLUSION = [ 'birthday', ]; - - /** * Returns common endpoint for mailchimp * @param {*} datacenterId <-- from webapp config @@ -190,23 +187,27 @@ const checkIfMailExists = async (apiKey, datacenterId, audienceId, email) => { const checkIfDoubleOptIn = async (apiKey, datacenterId, audienceId) => { const url = `${getMailChimpBaseEndpoint(datacenterId, audienceId)}`; const basicAuth = Buffer.from(`apiKey:${apiKey}`).toString('base64'); - const { processedResponse: processedResponseMailChip } = await handleHttpRequest( - 'get', - url, + const { processedResponse: processedResponseMailChip } = await handleHttpRequest( + 'get', + url, + { + headers: { + Authorization: `Basic ${basicAuth}`, + }, + }, + { destType: 'mailchimp', feature: 'transformation' }, + ); + + if (!isHttpStatusSuccess(processedResponseMailChip.status)) { + throw new NetworkError( + 'User does not have access to the requested operation', + processedResponseMailChip.status, { - headers: { - Authorization: `Basic ${basicAuth}`, - }, + [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseMailChip.status), }, - { destType: 'mailchimp', feature: 'transformation' }, ); + } - if (!isHttpStatusSuccess(processedResponseMailChip.status)) { - throw new NetworkError('User does not have access to the requested operation', processedResponseMailChip.status, { - [tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponseMailChip.status), - }); - } - return !!processedResponseMailChip.response.double_optin; };