Skip to content

Commit

Permalink
chore: updated transform.js
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Oct 21, 2024
1 parent 7878e3b commit eff5182
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 396 deletions.
68 changes: 30 additions & 38 deletions src/v0/destinations/tune/transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
const get = require('get-value');
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { defaultRequestConfig, simpleProcessRouterDest, getHashFromArray } = require('../../util');
const {
defaultRequestConfig,
simpleProcessRouterDest,
getHashFromArray,
isDefinedAndNotNull,
} = require('../../util');

const mapPropertiesWithNestedSupport = (msg, properties, mappings) => {
const mappedObj = {}; // Create a new object for parameters
Object.entries(mappings).forEach(([key, value]) => {
const keyStr = `${key}`;
const args = { object: properties, key: keyStr };
if (key.split('.').length > 1) {
// Handle nested keys
args.object = msg; // This line modifies the object property of args
}
const data = get(args.object, args.key);
if (isDefinedAndNotNull(data)) {
mappedObj[value] = data; // Map to the corresponding destination key
}
});
return mappedObj; // Return the new params object
};

const responseBuilder = (message, { Config }) => {
const { tuneEvents } = Config; // Extract tuneEvents from config
Expand All @@ -10,50 +32,20 @@ const responseBuilder = (message, { Config }) => {
const tuneEvent = tuneEvents.find((event) => event.eventName === messageEvent);

if (tuneEvent) {
const standardHashMap = getHashFromArray(tuneEvent.standardMapping);
const advSubIdHashMap = getHashFromArray(tuneEvent.advSubIdMapping);
const advUniqueIdHashMap = getHashFromArray(tuneEvent.advUniqueIdMapping);

const mapPropertiesWithNestedSupport = (msg, mappings) => {
const newParams = {}; // Create a new object for parameters
Object.entries(mappings).forEach(([key, value]) => {
let data; // Declare data variable

if (key.split('.').length > 1) {
// Handle nested keys
data = get(msg, key); // Use `get` to retrieve nested data
if (data) {
newParams[value] = data; // Map to the corresponding destination key
}
} else {
// Handle non-nested keys
data = get(properties, key); // Retrieve data from properties directly
if (data) {
newParams[value] = data; // Map to the corresponding destination key
}
}
});
return newParams; // Return the new params object
};
const standardHashMap = getHashFromArray(tuneEvent.standardMapping, 'from', 'to', false);
const advSubIdHashMap = getHashFromArray(tuneEvent.advSubIdMapping, 'from', 'to', false);
const advUniqueIdHashMap = getHashFromArray(tuneEvent.advUniqueIdMapping, 'from', 'to', false);

const params = {
...mapPropertiesWithNestedSupport(message, standardHashMap),
...mapPropertiesWithNestedSupport(message, advSubIdHashMap),
...mapPropertiesWithNestedSupport(message, advUniqueIdHashMap),
...mapPropertiesWithNestedSupport(message, properties, standardHashMap),
...mapPropertiesWithNestedSupport(message, properties, advSubIdHashMap),
...mapPropertiesWithNestedSupport(message, properties, advUniqueIdHashMap),
};

// 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
});

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

return response;
Expand All @@ -64,7 +56,7 @@ const responseBuilder = (message, { Config }) => {

const processEvent = (message, destination) => {
// Validate message type
if (!message.type) {
if (!isDefinedAndNotNull(message.type)) {
throw new InstrumentationError('Message Type is not present. Aborting message.', 400);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L60 was not covered by tests
}
const messageType = message.type.toLowerCase();
Expand Down
177 changes: 0 additions & 177 deletions src/v0/destinations/tune/transform.test.js

This file was deleted.

3 changes: 3 additions & 0 deletions test/integrations/destinations/tune/processor/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { trackTestdata } from './trackTestData';

export const data = [...trackTestdata];
104 changes: 104 additions & 0 deletions test/integrations/destinations/tune/processor/trackTestData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { ProcessorTestData } from '../../../testTypes';
import {
generateMetadata,
generateSimplifiedTrackPayload,
transformResultBuilder,
} from '../../../testUtils';

export const trackTestdata: ProcessorTestData[] = [
{
id: 'Test 1',
name: 'tune',
description: 'Track call with standard properties mapping',
scenario: 'Business',
successCriteria:
'The response should have a status code of 200 and correctly map the properties to the specified parameters.',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
message: generateSimplifiedTrackPayload({
type: 'track',
event: 'Product added',
properties: {
securityToken: '1123',
mytransactionId: 'test-123',
},
context: {
traits: {
customProperty1: 'customValue',
firstName: 'David',
logins: 2,
},
},
anonymousId: 'david_bowie_anonId',
}),
metadata: generateMetadata(1),
destination: {
ID: '123',
Name: 'tune',
DestinationDefinition: {
ID: '123',
Name: 'tune',
DisplayName: 'tune',
Config: {},
},
Config: {
connectionMode: {
web: 'cloud',
},
consentManagement: {},
oneTrustCookieCategories: {},
ketchConsentPurposes: {},
tuneEvents: [
{
url: 'https://demo.go2cloud.org/aff_l?offer_id=45&aff_id=1029',
eventName: 'Product added',
standardMapping: [
{ to: 'aff_id', from: 'affId' },
{ to: 'promo_code', from: 'promoCode' },
{ to: 'security_token', from: 'securityToken' },
{ to: 'status', from: 'status' },
{ to: 'transaction_id', from: 'mytransactionId' },
],
advSubIdMapping: [{ from: 'context.ip', to: 'adv_sub2' }],
advUniqueIdMapping: [],
},
],
},
Enabled: true,
WorkspaceID: '123',
Transformations: [],
},
},
],
},
},
output: {
response: {
status: 200,
body: [
{
output: transformResultBuilder({
method: 'POST',
endpoint: 'https://demo.go2cloud.org/aff_l?offer_id=45&aff_id=1029',
event: 'Product added',
headers: {},
params: {
security_token: '1123',
transaction_id: 'test-123',
},
userId: '',
JSON: {},
}),
metadata: generateMetadata(1),
statusCode: 200,
},
],
},
},
},
];
Loading

0 comments on commit eff5182

Please sign in to comment.