Skip to content

Commit

Permalink
chore: move gainsight_px from myaxios to httpget,put and post (#3464)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSudip authored Aug 5, 2024
1 parent b6ab2ba commit 98e9b00
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 127 deletions.
28 changes: 14 additions & 14 deletions src/v0/destinations/gainsight_px/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const { JSON_MIME_TYPE } = require('../../util/constant');
/**
* Create/Update a User with user attributes
*/
const identifyResponseBuilder = async (message, { Config }) => {
const identifyResponseBuilder = async (message, { Config }, metadata) => {
const userId = getFieldValueFromMessage(message, 'userId');
if (!userId) {
throw new InstrumentationError('userId or anonymousId is required for identify');
Expand All @@ -51,7 +51,7 @@ const identifyResponseBuilder = async (message, { Config }) => {
'Content-Type': JSON_MIME_TYPE,
};

const { success: isUserPresent } = await objectExists(userId, Config, 'user');
const { success: isUserPresent } = await objectExists(userId, Config, 'user', metadata);

let payload = constructPayload(message, identifyMapping);
const name = getValueFromMessage(message, ['traits.name', 'context.traits.name']);
Expand Down Expand Up @@ -110,7 +110,7 @@ const identifyResponseBuilder = async (message, { Config }) => {
* Pros: Will make atleast 2 API call and at most 3 API calls
* Cons: There might be some unwanted accounts
*/
const newGroupResponseBuilder = async (message, { Config }) => {
const newGroupResponseBuilder = async (message, { Config }, metadata) => {
const userId = getFieldValueFromMessage(message, 'userId');
if (!userId) {
throw new InstrumentationError('userId or anonymousId is required for group');
Expand Down Expand Up @@ -140,12 +140,12 @@ const newGroupResponseBuilder = async (message, { Config }) => {
payload = removeUndefinedAndNullValues(payload);

// update account
const { success: updateSuccess, err } = await updateAccount(groupId, payload, Config);
const { success: updateSuccess, err } = await updateAccount(groupId, payload, Config, metadata);
// will not throw error if it is due to unavailable accounts
if (!updateSuccess && err === null) {
// create account
payload.id = groupId;
const { success: createSuccess, error } = await createAccount(payload, Config);
const { success: createSuccess, error } = await createAccount(payload, Config, metadata);
if (!createSuccess) {
throw new ConfigurationError(`failed to create account for group: ${error}`);
}
Expand All @@ -172,13 +172,13 @@ const newGroupResponseBuilder = async (message, { Config }) => {
/**
* Associates a User with an Account.
*/
const groupResponseBuilder = async (message, { Config }) => {
const groupResponseBuilder = async (message, { Config }, metadata) => {
const userId = getFieldValueFromMessage(message, 'userId');
if (!userId) {
throw new InstrumentationError('userId or anonymousId is required for group');
}

const { success: isPresent, err: e } = await objectExists(userId, Config, 'user');
const { success: isPresent, err: e } = await objectExists(userId, Config, 'user', metadata);
if (!isPresent) {
throw new InstrumentationError(`aborting group call: ${e}`);
}
Expand All @@ -188,7 +188,7 @@ const groupResponseBuilder = async (message, { Config }) => {
throw new InstrumentationError('groupId is required for group');
}

const { success: accountIsPresent } = await objectExists(groupId, Config, 'account');
const { success: accountIsPresent } = await objectExists(groupId, Config, 'account', metadata);

let payload = constructPayload(message, groupMapping);
let customAttributes = {};
Expand All @@ -210,14 +210,14 @@ const groupResponseBuilder = async (message, { Config }) => {

if (accountIsPresent) {
// update account
const { success: updateSuccess, err } = await updateAccount(groupId, payload, Config);
const { success: updateSuccess, err } = await updateAccount(groupId, payload, Config, metadata);
if (!updateSuccess) {
throw new ConfigurationError(`failed to update account for group: ${err}`);
}
} else {
// create account
payload.id = groupId;
const { success: createSuccess, err } = await createAccount(payload, Config);
const { success: createSuccess, err } = await createAccount(payload, Config, metadata);
if (!createSuccess) {
throw new ConfigurationError(`failed to create account for group: ${err}`);
}
Expand Down Expand Up @@ -279,7 +279,7 @@ const trackResponseBuilder = (message, { Config }) => {
* Processing Single event
*/
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.');
}
Expand All @@ -301,16 +301,16 @@ 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 = trackResponseBuilder(message, destination);
break;
case EventType.GROUP:
if (limitAPIForGroup) {
response = await newGroupResponseBuilder(message, destination);
response = await newGroupResponseBuilder(message, destination, metadata);
} else {
response = await groupResponseBuilder(message, destination);
response = await groupResponseBuilder(message, destination, metadata);
}
break;
default:
Expand Down
179 changes: 66 additions & 113 deletions src/v0/destinations/gainsight_px/util.js
Original file line number Diff line number Diff line change
@@ -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 destResp;
Expand Down Expand Up @@ -37,133 +37,86 @@ const handleErrorResponse = (error, customErrMessage, expectedErrStatus, default
* @param {*} objectType
* @returns
*/
const objectExists = async (id, Config, objectType) => {
const objectExists = async (id, Config, objectType, metadata) => {
let url = `${ENDPOINTS.USERS_ENDPOINT}/${id}`;
let err = 'invalid response while searching user';

if (objectType === 'account') {
url = `${ENDPOINTS.ACCOUNTS_ENDPOINT}/${id}`;
err = 'invalid response while searching account';
}

let response;
try {
response = await myAxios.get(
url,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
},
{
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/accounts/accountId',
module: 'router',
},
);
if (response && response.status === 200) {
return { success: true, err: null };
}
const defStatus = 400;
const status = response ? response.status || defStatus : defStatus;
throw new NetworkError(
err,
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
const { httpResponse: res } = await handleHttpRequest(
'get',
url,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
response,
);
} catch (error) {
return handleErrorResponse(error, `error while fetching ${objectType}`, 404);
},
{
metadata,
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/accounts/accountId',
module: 'router',
},
);
if (res.success && res.response && res.response.status === 200) {
return { success: true, err: null };
}
return handleErrorResponse(res.response, `error while fetching ${objectType}`, 404);
};

const createAccount = async (payload, Config) => {
let response;
try {
response = await myAxios.post(
ENDPOINTS.ACCOUNTS_ENDPOINT,
payload,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
},
{
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'POST',
endpointPath: '/accounts',
module: 'router',
},
);
if (response && response.status === 201) {
return { success: true, err: null };
}

const defStatus = 400;
const status = response ? response.status || defStatus : defStatus;
throw new NetworkError(
'invalid response while creating account',
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
const createAccount = async (payload, Config, metadata) => {
const { httpResponse: res } = await handleHttpRequest(
'post',
ENDPOINTS.ACCOUNTS_ENDPOINT,
payload,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
response,
);
} catch (error) {
return handleErrorResponse(error, 'error while creating account', 400);
},
{
metadata,
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'POST',
endpointPath: '/accounts',
module: 'router',
},
);
if (res.success && res.response.status === 201) {
return { success: true, err: null };
}
return handleErrorResponse(res.response, 'error while creating account', 400);
};

const updateAccount = async (accountId, payload, Config) => {
let response;
try {
response = await myAxios.put(
`${ENDPOINTS.ACCOUNTS_ENDPOINT}/${accountId}`,
payload,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
},
{
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'PUT',
endpointPath: '/accounts/accountId',
module: 'router',
},
);
if (response && response.status === 204) {
return { success: true, err: null };
}
const defStatus = 400;
const status = response ? response.status || defStatus : defStatus;
throw new NetworkError(
'invalid response while updating account',
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
const updateAccount = async (accountId, payload, Config, metadata) => {
const { httpResponse: res } = await handleHttpRequest(
'put',
`${ENDPOINTS.ACCOUNTS_ENDPOINT}/${accountId}`,
payload,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
response,
);
} catch (error) {
// it will only occur if the user does not exist
if (
error.response?.status === 404 &&
error.response?.data?.externalapierror?.status === 'NOT_FOUND'
) {
return { success: false, err: null };
}
return handleErrorResponse(error, 'error while updating account', 400);
},
{
metadata,
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'PUT',
endpointPath: '/accounts/accountId',
module: 'router',
},
);
if (res.success && res.response.status === 204) {
return { success: true, err: null };
}
return handleErrorResponse(res.response, 'error while updating account', 400);
};

/**
Expand Down

0 comments on commit 98e9b00

Please sign in to comment.