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

feat: add tags to a company in intercom #3434

Merged
merged 10 commits into from
Jun 19, 2024
48 changes: 29 additions & 19 deletions src/cdk/v2/destinations/intercom/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const md5 = require('md5');
const get = require('get-value');
const { NetworkError } = require('@rudderstack/integrations-lib');
const {
NetworkError,
ConfigurationError,
InstrumentationError,
} = require('@rudderstack/integrations-lib');
const tags = require('../../../../v0/util/tags');
const { httpPOST } = require('../../../../adapters/network');
const {
Expand Down Expand Up @@ -30,6 +34,27 @@ const {
TAGS_ENDPOINT,
} = require('./config');

const intercomErrorHandler = (message, processedResponse) => {
const errorMessages = JSON.stringify(processedResponse?.response?.errors);
manish339k marked this conversation as resolved.
Show resolved Hide resolved
if (processedResponse?.status === 400) {
throw new InstrumentationError(`${message} : ${errorMessages}`);
manish339k marked this conversation as resolved.
Show resolved Hide resolved
}
if (processedResponse?.status === 401) {
throw new ConfigurationError(`${message} : ${errorMessages}`);
}
if (processedResponse?.status === 404) {
throw new InstrumentationError(`${message} : ${errorMessages}`);
}
throw new NetworkError(
`${message} : ${errorMessages}`,
processedResponse?.status,
manish339k marked this conversation as resolved.
Show resolved Hide resolved
{
[tags]: getDynamicErrorType(processedResponse?.status),
},
processedResponse,
);
};

/**
* Returns destination request headers
* @param {*} destination
Expand Down Expand Up @@ -405,16 +430,7 @@ const attachContactToCompany = async (payload, endpoint, destination) => {

const processedResponse = processAxiosResponse(response);
if (!isHttpStatusSuccess(processedResponse.status)) {
throw new NetworkError(
`Unable to attach Contact or User to Company due to : ${JSON.stringify(
processedResponse?.response?.errors,
)}`,
processedResponse?.status,
{
[tags]: getDynamicErrorType(processedResponse?.status),
},
processedResponse,
);
intercomErrorHandler('Unable to attach Contact or User to Company due to', processedResponse);
}
};

Expand Down Expand Up @@ -461,14 +477,8 @@ const addOrUpdateTagsToCompany = async (message, destination, id) => {
);
const processedResponse = processAxiosResponse(response);
if (!isHttpStatusSuccess(processedResponse.status)) {
throw new NetworkError(
`Unable to Add or Update the Tag to Company due to : ${JSON.stringify(
processedResponse?.response?.errors,
)}`,
processedResponse?.status,
{
[tags]: getDynamicErrorType(processedResponse?.status),
},
intercomErrorHandler(
'Unable to Add or Update the Tag to Company due to',
processedResponse,
);
}
Expand Down
73 changes: 69 additions & 4 deletions src/cdk/v2/destinations/intercom/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ describe('attachContactToCompany utility test', () => {
);
});

it('Should throw a network error during attachment', async () => {
it('Should throw error for invalid company during attachment', async () => {
const payload = {
id: 'company123',
};
Expand Down Expand Up @@ -868,6 +868,36 @@ describe('attachContactToCompany utility test', () => {
);
}
});

it('Should throw error for faulty payload during attachment', async () => {
const payload = {};
const endpoint = 'https://api.intercom.io/contacts/contact123/companies';
const destination = { Config: { apiKey: 'testApiKey', apiServer: 'us', apiVersion: 'v2' } };

axios.post.mockRejectedValue({
response: {
status: 400,
data: {
type: 'error.list',
request_id: '123',
errors: [
{
code: 'parameter_not_found',
message: 'company not specified',
},
],
},
},
});

try {
await attachContactToCompany(payload, endpoint, destination);
} catch (error) {
expect(error.message).toEqual(
'Unable to attach Contact or User to Company due to : [{"code":"parameter_not_found","message":"company not specified"}]',
);
}
});
});

describe('addOrUpdateTagsToCompany utility test', () => {
Expand Down Expand Up @@ -926,10 +956,10 @@ describe('addOrUpdateTagsToCompany utility test', () => {
const id = 'companyId';

axios.post.mockRejectedValue({
status: 400,
status: 401,
data: {
type: 'error.list',
request_id: 'request_400',
request_id: 'request_401',
errors: [
{
code: 'unauthorized',
Expand All @@ -944,7 +974,42 @@ describe('addOrUpdateTagsToCompany utility test', () => {
await addOrUpdateTagsToCompany(message, destination, id);
} catch (error) {
expect(error.message).toEqual(
`Unable to Add or Update the Tag to Company due to : [{"code":"unauthorized","message":"Access Token Invalid"}]`,
`Unable to Add or Update the Tag to Company due to : [{\"code\":\"unauthorized\",\"message\":\"Access Token Invalid\"}]`,
);
}
});

it('Should throw a network error in case if axios calls returns an error', async () => {
const message = {
context: {
traits: {
tags: ['tag1'],
},
},
};
const destination = { Config: { apiKey: 'testApiKey', apiServer: 'us' } };
const id = 'companyId';

axios.post.mockRejectedValue({
status: 429,
data: {
type: 'error.list',
request_id: 'request_429',
errors: [
{
code: 'rate_limit_exceeded',
message: 'You have exceeded the rate limit. Please try again later.',
},
],
},
});

try {
axios.post.mockClear();
await addOrUpdateTagsToCompany(message, destination, id);
} catch (error) {
expect(error.message).toEqual(
`Unable to Add or Update the Tag to Company due to : [{\"code\":\"rate_limit_exceeded\",\"message\":\"You have exceeded the rate limit. Please try again later.\"}]`,
);
}
});
Expand Down
Loading