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: onboard courier destination (#1844) (#1883) #1899

Merged
merged 1 commit into from
Feb 27, 2023
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
2 changes: 1 addition & 1 deletion features.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
"CRITEO_AUDIENCE": true,
"CUSTOMERIO": true
}
}
}
5 changes: 5 additions & 0 deletions src/constants/destinationCanonicalNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ const DestCanonicalNames = {
'Optimizely_Fullstack',
'optimizely_fullstack',
],
courier: [
'Courier',
'courier',
'COURIER',
],
};

module.exports = { DestHandlerMap, DestCanonicalNames };
5 changes: 5 additions & 0 deletions src/v0/destinations/courier/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const API_URL = 'https://api.courier.com/inbound/rudderstack';

module.exports = {
API_URL,
};
58 changes: 58 additions & 0 deletions src/v0/destinations/courier/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { EventType } = require('../../../constants');
const {
defaultRequestConfig,
defaultPostRequestConfig,
removeUndefinedAndNullValues,
simpleProcessRouterDest,
} = require('../../util');
const {
TransformationError,
InstrumentationError,
ConfigurationError,
} = require('../../util/errorTypes');
const { API_URL } = require('./config');

const responseBuilder = (payload, endpoint, destination) => {
if (payload) {
const response = defaultRequestConfig();
const { apiKey } = destination.Config;
response.endpoint = endpoint;
response.headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
};
response.method = defaultPostRequestConfig.requestMethod;
response.body.JSON = removeUndefinedAndNullValues(payload);
return response;
}
throw new TransformationError('Something went wrong while constructing the payload');
};

const processEvent = (message, destination) => {
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
if (!destination.Config.apiKey) {
throw new ConfigurationError('apiKey is required');
}
const messageType = message.type.toLowerCase();
let response;
switch (messageType) {
case EventType.IDENTIFY:
case EventType.TRACK:
response = responseBuilder(message, API_URL, destination);
break;
default:
throw new InstrumentationError(`Message type ${messageType} is not supported`);
}
return response;
};

const process = (event) => processEvent(event.message, event.destination);

const processRouterDest = async (inputs, reqMetadata) => {
const respList = await simpleProcessRouterDest(inputs, process, reqMetadata);
return respList;
};

module.exports = { process, processRouterDest };
7 changes: 6 additions & 1 deletion test/__mocks__/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
} = require("./freshsales.mock");
const { sendgridGetRequestHandler } = require("./sendgrid.mock");
const { sendinblueGetRequestHandler } = require("./sendinblue.mock");
const { courierGetRequestHandler } = require("./courier.mock");

const urlDirectoryMap = {
"api.hubapi.com": "hs",
Expand All @@ -50,7 +51,8 @@ const urlDirectoryMap = {
"ruddertest2.mautic.net": "mautic",
"api.sendgrid.com": "sendgrid",
"api.sendinblue.com": "sendinblue",
"api.criteo.com": "criteo_audience"
"api.criteo.com": "criteo_audience",
"api.courier.com": "courier"
};

const fs = require("fs");
Expand Down Expand Up @@ -152,6 +154,9 @@ function get(url, options) {
if (url.includes("https://api.sendinblue.com/v3/contacts/")) {
return Promise.resolve(sendinblueGetRequestHandler(url, mockData));
}
if (url.includes("https://api.courier.com")) {
return Promise.resolve(courierGetRequestHandler(url, mockData));
}
return new Promise((resolve, reject) => {
if (mockData) {
resolve({ data: mockData, status: 200 });
Expand Down
13 changes: 13 additions & 0 deletions test/__mocks__/courier.mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const MOCK_DATA = {
messageId: "MOCK_MESSAGE_ID"
};

const courierGetRequestHandler = url => {
if (url === "https://api.courier.com/inbound/rudderstack") {
return { data: MOCK_DATA, status: 202 };
}

return Promise.resolve({ error: "Request failed", status: 404 });
};

module.exports = { courierGetRequestHandler };
46 changes: 46 additions & 0 deletions test/__tests__/courier.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const integration = "courier";
const name = "Courier";

const fs = require("fs");
const path = require("path");

const version = "v0";

const transformer = require(`../../src/${version}/destinations/${integration}/transform`);

// Processor Test files
const testDataFile = fs.readFileSync(
path.resolve(__dirname, `./data/${integration}.json`)
);
const testData = JSON.parse(testDataFile);

// Router Test files
const routerTestDataFile = fs.readFileSync(
path.resolve(__dirname, `./data/${integration}_router.json`)
);
const routerTestData = JSON.parse(routerTestDataFile);

describe(`${name} Tests`, () => {
describe("Processor", () => {
testData.forEach(async (dataPoint, index) => {
it(`${index}. ${integration} - ${dataPoint.description}`, async () => {
try {
const output = await transformer.process(dataPoint.input);
expect(output).toEqual(dataPoint.output);
} catch (error) {
expect(error.message).toEqual(dataPoint.output.error);
}
});
});
});

describe("Router Tests", () => {
routerTestData.forEach(dataPoint => {
it("Payload", async () => {
const output = await transformer.processRouterDest(dataPoint.input);
expect(output).toEqual(dataPoint.output);
});
});
});

});
133 changes: 133 additions & 0 deletions test/__tests__/data/courier.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
[
{
"description": "Invalid Configuration (missing api key)",
"input": {
"message": {
"type": "track",
"channel": "web",
"event": "Product Added",
"properties": {},
"context": {},
"rudderId": "8f8fa6b5-8e24-489c-8e22-61f23f2e364f",
"messageId": "2116ef8c-efc3-4ca4-851b-02ee60dad6ff",
"anonymousId": "97c46c81-3140-456d-b2a9-690d70aaca35"
},
"destination": {
"Config": {}
}
},
"output": {
"error": "apiKey is required"
}
},
{
"description": "Identify call",
"input": {
"message": {
"context": {
"ip": "8.8.8.8"
},
"traits": {
"name": "Joe Doe",
"email": "joe@example.com",
"plan": "basic",
"age": 27
},
"type": "identify",
"userId": "userIdTest",
"originalTimestamp": "2022-10-17T15:53:10.566+05:30",
"messageId": "8d04cc30-fc15-49bd-901f-c5c3f72a7d82"
},
"destination": {
"Config": {
"apiKey": "test-api-key"
}
}
},
"output": {
"body": {
"FORM": {},
"JSON": {
"context": {
"ip": "8.8.8.8"
},
"traits": {
"name": "Joe Doe",
"email": "joe@example.com",
"plan": "basic",
"age": 27
},
"type": "identify",
"userId": "userIdTest",
"originalTimestamp": "2022-10-17T15:53:10.566+05:30",
"messageId": "8d04cc30-fc15-49bd-901f-c5c3f72a7d82"
},
"JSON_ARRAY": {},
"XML": {}
},
"endpoint": "https://api.courier.com/inbound/rudderstack",
"files": {},
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer test-api-key"
},
"method": "POST",
"params": {},
"type": "REST",
"version": "1"
}
},
{
"description": "Track call",
"input": {
"message": {
"context": {
"ip": "8.8.8.8"
},
"event": "trackTest",
"properties": {
"activity": "checkout"
},
"userId": "userIdTest",
"type": "track",
"messageId": "3c0abc14-96a2-4aed-9dfc-ee463832cc24",
"originalTimestamp": "2022-10-17T15:32:44.202+05:30"
},
"destination": {
"Config": {
"apiKey": "test-api-key"
}
}
},
"output": {
"body": {
"FORM": {},
"JSON": {
"context": {
"ip": "8.8.8.8"
},
"event": "trackTest",
"properties": {
"activity": "checkout"
},
"userId": "userIdTest",
"type": "track",
"messageId": "3c0abc14-96a2-4aed-9dfc-ee463832cc24",
"originalTimestamp": "2022-10-17T15:32:44.202+05:30"
},
"JSON_ARRAY": {},
"XML": {}
},
"endpoint": "https://api.courier.com/inbound/rudderstack",
"files": {},
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer test-api-key"
},
"method": "POST",
"params": {},
"type": "REST",
"version": "1"
}
}
]
Loading