Skip to content

Commit

Permalink
fix: custify user-regulation logic (#3076)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpj2292 authored Feb 16, 2024
1 parent 0f01524 commit 9683161
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 58 deletions.
55 changes: 30 additions & 25 deletions src/v0/destinations/custify/deleteUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,41 @@ const userDeletionHandler = async (userAttributes, config) => {
}

const { apiKey } = config;
const { userId } = userAttributes;

if (!apiKey) {
throw new ConfigurationError('api key for deletion not present', 400);
}
if (!userId) {
throw new InstrumentationError('User id for deletion not present', 400);
}
const requestUrl = `https://api.custify.com/people?user_id=${userId}`;
const requestOptions = {
headers: {
Authorization: `Bearer ${apiKey}`,
},
};
await Promise.all(
userAttributes.map(async (userAttr) => {
const { userId } = userAttr;
if (!userId) {
throw new InstrumentationError('User id for deletion not present', 400);
}
// Reference: https://docs.custify.com/#tag/People/paths/~1people/delete
const requestUrl = `https://api.custify.com/people?user_id=${userId}`;
const requestOptions = {
headers: {
Authorization: `Bearer ${apiKey}`,
},
};

const deletionResponse = await httpDELETE(requestUrl, requestOptions, {
destType: 'custify',
feature: 'deleteUsers',
});
const processedDeletionRequest = processAxiosResponse(deletionResponse);
if (processedDeletionRequest.status !== 200 && processedDeletionRequest.status !== 404) {
throw new NetworkError(
JSON.stringify(processedDeletionRequest.response) || 'Error while deleting user',
processedDeletionRequest.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedDeletionRequest.status),
},
deletionResponse,
);
}
const deletionResponse = await httpDELETE(requestUrl, requestOptions, {
destType: 'custify',
feature: 'deleteUsers',
});
const processedDeletionRequest = processAxiosResponse(deletionResponse);
if (processedDeletionRequest.status !== 200 && processedDeletionRequest.status !== 404) {
throw new NetworkError(
JSON.stringify(processedDeletionRequest.response) || 'Error while deleting user',
processedDeletionRequest.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(processedDeletionRequest.status),
},
deletionResponse,
);
}
}),
);

return { statusCode: 200, status: 'successful' };
};
Expand Down
160 changes: 160 additions & 0 deletions test/integrations/destinations/custify/deleteUsers/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
const destType = 'custify';
const commonData = {
name: destType,
feature: 'userDeletion',
module: 'destination',
version: 'v0',
};

export const data = [
{
description: 'Test 0: should fail when config is not being sent',
input: {
request: {
body: [
{
destType: destType.toUpperCase(),
userAttributes: [
{
userId: 'rudder1',
},
],
},
],
},
},
output: {
response: {
status: 400,
body: [
{
statusCode: 400,
error: 'Config for deletion not present',
},
],
},
},
},
{
description: 'Test 1: should fail when apiKey is not present in config',
input: {
request: {
body: [
{
destType: destType.toUpperCase(),
userAttributes: [
{
userId: 'rudder2',
},
],
config: {
apiToken: 'dummyApiKey',
},
},
],
},
},
output: {
response: {
status: 400,
body: [
{
statusCode: 400,
error: 'api key for deletion not present',
},
],
},
},
},

{
description: 'Test 2: should pass when one of the users is not present in destination',
input: {
request: {
body: [
{
destType: destType.toUpperCase(),
userAttributes: [
{
userId: 'rudder1',
},
{
userId: 'rudder2',
},
],
config: {
apiKey: 'dummyApiKey',
},
},
],
},
},
output: {
response: {
status: 200,
body: [{ statusCode: 200, status: 'successful' }],
},
},
},

{
description:
'Test 3: should fail when one of the users is returning with 4xx(not 404) from destination',
input: {
request: {
body: [
{
destType: destType.toUpperCase(),
userAttributes: [
{
userId: 'rudder1',
},
{
userId: 'rudder3',
},
],
config: {
apiKey: 'dummyApiKey',
},
},
],
},
},
output: {
response: {
status: 400,
body: [{ statusCode: 400, error: '{"error":"User: rudder3 has a problem"}' }],
},
},
},

{
description:
'Test 4: should fail when one of the userAttributes does not contain `userId`',
input: {
request: {
body: [
{
destType: destType.toUpperCase(),
userAttributes: [
{
userId: 'rudder1',
},
{
},
],
config: {
apiKey: 'dummyApiKey',
},
},
],
},
},
output: {
response: {
status: 400,
body: [{ statusCode: 400, error: 'User id for deletion not present' }],
},
},
},
].map((props) => ({ ...commonData, ...props }));
115 changes: 82 additions & 33 deletions test/integrations/destinations/custify/network.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,85 @@
export const networkCallsData = [
{
httpReq: {
url: 'https://api.custify.com/company',
method: 'POST',
{
httpReq: {
url: 'https://api.custify.com/company',
method: 'POST',
},
httpRes: {
data: {
company_id: '6',
name: 'Pizzeria Presto',
signed_up_at: '2019-05-30T12:00:00.000Z',
size: 15,
website: 'www.pizzeriapresto.com',
industry: 'Restaurant',
plan: 'Platinum',
monthly_revenue: 1234567,
churned: false,
owners_csm: '[email protected]',
owners_account: '[email protected]',
parent_companies: [
{
id: '5ec50c9829d3c17c7cf455f2',
},
{
id: '5ec50c9829d3c17c7cf457f2',
},
],
custom_attributes: {
restaurants: 5,
custom: 'template',
},
httpRes: {
data: {
"company_id": "6",
"name": "Pizzeria Presto",
"signed_up_at": "2019-05-30T12:00:00.000Z",
"size": 15,
"website": "www.pizzeriapresto.com",
"industry": "Restaurant",
"plan": "Platinum",
"monthly_revenue": 1234567,
"churned": false,
"owners_csm": "[email protected]",
"owners_account": "[email protected]",
"parent_companies": [
{
"id": "5ec50c9829d3c17c7cf455f2"
},
{
"id": "5ec50c9829d3c17c7cf457f2"
}
],
"custom_attributes": {
"restaurants": 5,
"custom": "template"
}
},
status: 200
},
}
},
status: 200,
},
},

{
httpReq: {
method: 'delete',
url: 'https://api.custify.com/people?user_id=rudder1',
headers: {
Authorization: 'Bearer dummyApiKey',
},
},
httpRes: {
data: {
msg: 'All users associated with rudder1 were successfully deleted',
code: 'Success',
params: null,
},
status: 200,
},
},
{
httpReq: {
method: 'delete',
url: 'https://api.custify.com/people?user_id=rudder2',
headers: {
Authorization: 'Bearer dummyApiKey',
},
},
httpRes: {
data: {
error: 'User: rudder2 not found',
},
status: 404,
},
},

{
httpReq: {
method: 'delete',
url: 'https://api.custify.com/people?user_id=rudder3',
headers: {
Authorization: 'Bearer dummyApiKey',
},
},
httpRes: {
data: {
error: 'User: rudder3 has a problem',
},
status: 400,
},
},
];

0 comments on commit 9683161

Please sign in to comment.