Skip to content

Commit

Permalink
fix(delighted): replace myAxios utility with handleHttpRequest utility
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Apr 2, 2024
1 parent 042dd6d commit c5aaae3
Showing 1 changed file with 62 additions and 44 deletions.
106 changes: 62 additions & 44 deletions src/v0/destinations/delighted/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const {
InstrumentationError,
NetworkError,
} = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { getValueFromMessage } = require('../../util');
const { getValueFromMessage, isHttpStatusSuccess } = require('../../util');
const { ENDPOINT } = require('./config');
const tags = require('../../util/tags');
const { JSON_MIME_TYPE } = require('../../util/constant');
const { handleHttpRequest } = require('../../../adapters/network');

const isValidEmail = (email) => {
const re =
Expand Down Expand Up @@ -41,6 +41,30 @@ const isValidUserIdOrError = (channel, userId) => {
};
};

/**
* Returns final status
* @param {*} status
* @returns
*/
const getErrorStatus = (status) => {
let errStatus = status;

Check warning on line 50 in src/v0/destinations/delighted/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/delighted/util.js#L50

Added line #L50 was not covered by tests
switch (status) {
case 422:
case 401:
case 406:
case 403:
errStatus = 400;
break;
case 500:
case 503:
errStatus = 500;
break;
default:
errStatus = 400;

Check warning on line 63 in src/v0/destinations/delighted/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/delighted/util.js#L52-L63

Added lines #L52 - L63 were not covered by tests
}
return errStatus;

Check warning on line 65 in src/v0/destinations/delighted/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/delighted/util.js#L65

Added line #L65 was not covered by tests
};

const userValidity = async (channel, Config, userId) => {
const paramsdata = {};
if (channel === 'email') {
Expand All @@ -50,53 +74,47 @@ const userValidity = async (channel, Config, userId) => {
}

const basicAuth = Buffer.from(Config.apiKey).toString('base64');
let response;
try {
response = await myAxios.get(
`${ENDPOINT}`,
{
headers: {
Authorization: `Basic ${basicAuth}`,
'Content-Type': JSON_MIME_TYPE,
},
params: paramsdata,
const { processedResponse } = await handleHttpRequest(
'get',
`${ENDPOINT}`,
{
headers: {
Authorization: `Basic ${basicAuth}`,
'Content-Type': JSON_MIME_TYPE,
},
params: paramsdata,
},
{
destType: 'delighted',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/people.json',
module: 'router',
},
);

if (!isHttpStatusSuccess(processedResponse.status)) {
const errStatus = getErrorStatus(processedResponse.status);
throw new NetworkError(

Check warning on line 98 in src/v0/destinations/delighted/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/delighted/util.js#L97-L98

Added lines #L97 - L98 were not covered by tests
`Error occurred while checking user : ${JSON.stringify(processedResponse.response)}`,
errStatus,
{
destType: 'delighted',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/people.json',
module: 'router',
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus),
},
processedResponse,
);
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;
}
}
throw new NetworkError(`Error occurred while checking user : ${errMsg}`, errStatus, {
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errStatus),
});
}

if (
processedResponse &&
processedResponse.response &&
processedResponse.status === 200 &&
Array.isArray(processedResponse.response)
) {
return processedResponse.response.length > 0;
}

throw new NetworkInstrumentationError('Invalid response');

Check warning on line 117 in src/v0/destinations/delighted/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/delighted/util.js#L117

Added line #L117 was not covered by tests
};
const eventValidity = (Config, message) => {
const event = getValueFromMessage(message, 'event');
Expand Down

0 comments on commit c5aaae3

Please sign in to comment.