Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move gainsight_px from myaxios to httpget,put and post #3464

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/**
* 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 @@
'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 @@
* 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 @@
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);

Check warning on line 148 in src/v0/destinations/gainsight_px/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight_px/transform.js#L148

Added line #L148 was not covered by tests
if (!createSuccess) {
throw new ConfigurationError(`failed to create account for group: ${error}`);
}
Expand All @@ -172,13 +172,13 @@
/**
* 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 @@
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 @@

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);

Check warning on line 220 in src/v0/destinations/gainsight_px/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight_px/transform.js#L220

Added line #L220 was not covered by tests
if (!createSuccess) {
throw new ConfigurationError(`failed to create account for group: ${err}`);
}
Expand Down Expand Up @@ -279,7 +279,7 @@
* 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 @@
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 @@
* @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(

Check warning on line 71 in src/v0/destinations/gainsight_px/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight_px/util.js#L71

Added line #L71 was not covered by tests
'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 };

Check warning on line 91 in src/v0/destinations/gainsight_px/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight_px/util.js#L91

Added line #L91 was not covered by tests
}
return handleErrorResponse(res.response, 'error while creating account', 400);

Check warning on line 93 in src/v0/destinations/gainsight_px/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight_px/util.js#L93

Added line #L93 was not covered by tests
};

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);

Check warning on line 119 in src/v0/destinations/gainsight_px/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight_px/util.js#L119

Added line #L119 was not covered by tests
};

/**
Expand Down
Loading