Skip to content

Commit

Permalink
chore: refactor code to remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSudip committed Jun 21, 2024
1 parent 4691c80 commit 185b772
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/v0/destinations/gainsight/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ const { JSON_MIME_TYPE } = require('../../util/constant');
const { handleHttpRequest } = require('../../../adapters/network');
const { isHttpStatusSuccess } = require('../../util');

const throwNetworkError = (errMsg, status, response) => {
throw new NetworkError(
errMsg,
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
},
response,
);
};

const throwRetryableError = (errMsg, response) => {
throw new RetryableError(errMsg, 500, response);

Check warning on line 26 in src/v0/destinations/gainsight/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight/util.js#L26

Added line #L26 was not covered by tests
};

const searchGroup = async (groupName, Config) => {
const { processedResponse } = await handleHttpRequest(
'post',
Expand All @@ -32,18 +47,15 @@ const searchGroup = async (groupName, Config) => {
);

if (!isHttpStatusSuccess(processedResponse.status)) {
throw new NetworkError(
throwNetworkError(
`failed to search group ${JSON.stringify(processedResponse.response)}`,
processedResponse.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponse.status),
},
processedResponse,
);
}

if (!processedResponse?.response || processedResponse.status !== 200) {
throw new RetryableError('failed to search group', 500, processedResponse);
throwRetryableError('failed to search group', processedResponse);

Check warning on line 58 in src/v0/destinations/gainsight/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight/util.js#L58

Added line #L58 was not covered by tests
}

return processedResponse.response;
Expand Down Expand Up @@ -72,18 +84,15 @@ const createGroup = async (payload, Config) => {
);

if (!isHttpStatusSuccess(processedResponse.status)) {
throw new NetworkError(
throwNetworkError(

Check warning on line 87 in src/v0/destinations/gainsight/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight/util.js#L87

Added line #L87 was not covered by tests
`failed to create group ${JSON.stringify(processedResponse.response)}`,
processedResponse.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponse.status),
},
processedResponse,
);
}

if (!processedResponse?.response || processedResponse.status !== 200) {
throw new RetryableError('failed to create group', 500, processedResponse);
throwRetryableError('failed to create group', processedResponse);

Check warning on line 95 in src/v0/destinations/gainsight/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight/util.js#L95

Added line #L95 was not covered by tests
}

return processedResponse.response.data.records[0].Gsid;
Expand Down Expand Up @@ -115,18 +124,15 @@ const updateGroup = async (payload, Config) => {
);

if (!isHttpStatusSuccess(processedResponse.status)) {
throw new NetworkError(
throwNetworkError(
`failed to update group ${JSON.stringify(processedResponse.response)}`,
processedResponse.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedResponse.status),
},
processedResponse,
);
}

if (!processedResponse?.response || processedResponse.status !== 200) {
throw new RetryableError('failed to update group', 500, processedResponse);
throwRetryableError('failed to update group', processedResponse);

Check warning on line 135 in src/v0/destinations/gainsight/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/gainsight/util.js#L135

Added line #L135 was not covered by tests
}

return processedResponse.response.data.records[0].Gsid;
Expand Down

0 comments on commit 185b772

Please sign in to comment.