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 tune destination #3795

Merged
merged 11 commits into from
Oct 22, 2024
71 changes: 71 additions & 0 deletions src/v0/destinations/tune/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { defaultRequestConfig, simpleProcessRouterDest } = require('../../util');

const responseBuilder = (message, { Config }) => {
const { tuneEvents } = Config; // Extract tuneEvents from config
const { properties, event: messageEvent } = message; // Destructure properties and event from message

// Find the relevant tune event based on the message's event name
const tuneEvent = tuneEvents.find((event) => event.eventName === messageEvent);
aanshi07 marked this conversation as resolved.
Show resolved Hide resolved

if (tuneEvent) {
const params = {};

// Map the properties to their corresponding destination keys
tuneEvent.eventsMapping.forEach((mapping) => {
if (properties && properties[mapping.from] !== undefined) {
params[mapping.to] = properties[mapping.from]; // Map the property to the destination key

Check warning on line 17 in src/v0/destinations/tune/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/tune/transform.js#L17

Added line #L17 was not covered by tests
yashasvibajpai marked this conversation as resolved.
Show resolved Hide resolved
}
});

// Prepare the response
const response = defaultRequestConfig();
response.params = params; // Set only the mapped params
response.endpoint = tuneEvent.url; // Use the user-defined URL

// Add query parameters from the URL to params
const urlParams = new URLSearchParams(new URL(tuneEvent.url).search);
urlParams.forEach((value, key) => {
params[key] = value; // Add each query parameter to params
});
aanshi07 marked this conversation as resolved.
Show resolved Hide resolved

// Include the event name in the response, not in params
response.event = tuneEvent.eventName;

return response;
}

throw new InstrumentationError('No matching tune event found for the provided event.', 400);

Check warning on line 38 in src/v0/destinations/tune/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/tune/transform.js#L38

Added line #L38 was not covered by tests
};

const processEvent = (message, destination) => {
// Validate message type
if (!message.type) {
aanshi07 marked this conversation as resolved.
Show resolved Hide resolved
throw new InstrumentationError('Message Type is not present. Aborting message.', 400);

Check warning on line 44 in src/v0/destinations/tune/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/tune/transform.js#L44

Added line #L44 was not covered by tests
}
const messageType = message.type.toLowerCase();
aanshi07 marked this conversation as resolved.
Show resolved Hide resolved

// Initialize response variable
let response;

// Process 'track' messages using the responseBuilder
if (messageType === 'track') {
response = responseBuilder(message, destination);
} else {
throw new InstrumentationError('Message type not supported. Only "track" is allowed.', 400);

Check warning on line 55 in src/v0/destinations/tune/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/tune/transform.js#L54-L55

Added lines #L54 - L55 were not covered by tests
yashasvibajpai marked this conversation as resolved.
Show resolved Hide resolved
}

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,
};
181 changes: 181 additions & 0 deletions test/integrations/destinations/tune/router/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
export const data = [
{
name: 'tune',
description: 'Track call',
feature: 'router',
module: 'destination',
version: 'v0',
input: {
request: {
body: {
input: [
{
message: {
type: 'track',
event: 'Product added',
context: {
traits: {
customProperty1: 'customValue',
firstName: 'David',
logins: 2,
},
},
anonymousId: 'david_bowie_anonId',
rudderId: '27e0a4fc-db65-47b7-ab3e-692e4e3c6b6b',
messageId: '7359e92b-b2d9-4147-9c8c-c0e7f060cbcc',
},
metadata: { jobId: 1, userId: 'u1' },
destination: {
secretConfig: {},
Config: {
blacklistedEvents: [
{
eventName: '',
},
],
whitelistedEvents: [
{
eventName: '',
},
],
eventFilteringOption: 'disable',
connectionMode: {
web: 'cloud',
},
consentManagement: {},
tuneEvents: [
{
url: 'https://demo.go2cloud.org/aff_l?offer_id=45&aff_id=1029',
eventName: 'Product added',
eventsMapping: [
{
to: 'adv_sub',
from: 'advSub',
},
{
to: 'aff_id',
from: 'affId',
},
{
to: 'goal_id',
from: 'goalId',
},
{
to: 'revenue',
from: 'revenue',
},
{
to: 'security_token',
from: 'securityToken',
},
{
to: 'status',
from: 'status',
},
{
to: 'transaction_id',
from: 'transactionId',
},
],
},
],
},
},
},
],
destType: 'tune',
},
method: 'POST',
},
},
output: {
response: {
status: 200,
body: {
output: [
{
batchedRequest: {
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'https://demo.go2cloud.org/aff_l?offer_id=45&aff_id=1029',
yashasvibajpai marked this conversation as resolved.
Show resolved Hide resolved
event: 'Product added',
headers: {},
params: {
offer_id: '45',
aff_id: '1029',
},
body: {
JSON: {},
JSON_ARRAY: {},
XML: {},
FORM: {},
},
files: {},
},
metadata: [{ jobId: 1, userId: 'u1' }],
batched: false,
statusCode: 200,
destination: {
secretConfig: {},
Config: {
blacklistedEvents: [
{
eventName: '',
},
],
whitelistedEvents: [
{
eventName: '',
},
],
eventFilteringOption: 'disable',
connectionMode: {
web: 'cloud',
},
consentManagement: {},
tuneEvents: [
{
url: 'https://demo.go2cloud.org/aff_l?offer_id=45&aff_id=1029',
eventName: 'Product added',
eventsMapping: [
{
to: 'adv_sub',
from: 'advSub',
},
{
to: 'aff_id',
from: 'affId',
},
{
to: 'goal_id',
from: 'goalId',
},
{
to: 'revenue',
from: 'revenue',
},
{
to: 'security_token',
from: 'securityToken',
},
{
to: 'status',
from: 'status',
},
{
to: 'transaction_id',
from: 'transactionId',
},
],
},
],
},
},
},
],
},
},
},
},
];
Loading