Skip to content

Commit

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L24 was not covered by tests
if (data) {
newParams[value] = data; // Map to the corresponding destination key

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L26 was not covered by tests
}
} 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 params = {
...mapPropertiesWithNestedSupport(message, standardHashMap),
...mapPropertiesWithNestedSupport(message, advSubIdHashMap),
...mapPropertiesWithNestedSupport(message, advUniqueIdHashMap),
};

// Prepare the response
const response = defaultRequestConfig();
Expand Down
101 changes: 65 additions & 36 deletions src/v0/destinations/tune/transform.test.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,91 @@
const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { responseBuilder, processEvent } = require('./transform');

describe('responseBuilder', () => {
// Correctly maps properties to destination keys based on eventsMapping
it('should map properties to destination keys when eventsMapping is provided', () => {
// Correctly maps properties to params using standard, advSubId, and advUniqueId mappings
it('should construct response with default config when tune event found', () => {
const message = {
event: 'product list viewed',
properties: {
platform: 'meta',
conversions: 1,
ad_unit_id: 221187,
ad_interaction_time: '1652826278',
},
properties: { key1: 'value1', key2: 'value2' },
event: 'testEvent',
};
const Config = {
tuneEvents: [
{
eventName: 'product list viewed',
eventsMapping: [
{ from: 'platform', to: 'destinationPlatform' },
{ from: 'conversions', to: 'destinationConversions' },
],
url: 'https://example.com/event',
eventName: 'testEvent',
standardMapping: [{ from: 'key1', to: 'mappedKey1' }],
advSubIdMapping: [{ from: 'key2', to: 'mappedKey2' }],
advUniqueIdMapping: [{ from: 'key3', to: 'mappedKey3' }],
url: 'https://example.com/api',
},
],
};
const expectedParams = {
destinationPlatform: 'meta',
destinationConversions: 1,

const response = responseBuilder(message, { Config });

expect(response).toEqual({
version: '1',
type: 'REST',
method: 'POST',
endpoint: 'https://example.com/api',
headers: {},
params: { mappedKey1: 'value1', mappedKey2: 'value2' },
body: {
JSON: {},
JSON_ARRAY: {},
XML: {},
FORM: {},
},
files: {},
event: 'testEvent',
});
});

it('should extract and use tuneEvents from Config', () => {
const message = {
properties: { key1: 'value1', key2: 'value2' },
event: 'testEvent',
};
const Config = {
tuneEvents: [
{
eventName: 'testEvent',
standardMapping: [{ from: 'key1', to: 'mappedKey1' }],
advSubIdMapping: [{ from: 'key2', to: 'mappedKey2' }],
advUniqueIdMapping: [{ from: 'key3', to: 'mappedKey3' }],
url: 'https://example.com/api',
},
],
};

const response = responseBuilder(message, { Config });
expect(response.params).toEqual(expectedParams);
expect(response.endpoint).toBe('https://example.com/event');

expect(response).toBeDefined();
});

// Handles empty properties object without errors
it('should handle empty properties object without throwing errors', () => {
it('should extract parameters from the provided URL', () => {
const message = {
event: 'product list viewed',
properties: {},
event: 'testEvent',
};
const Config = {
tuneEvents: [
{
eventName: 'product list viewed',
eventsMapping: [
{ from: 'platform', to: 'destinationPlatform' },
{ from: 'conversions', to: 'destinationConversions' },
],
url: 'https://example.com/event',
eventName: 'testEvent',
standardMapping: [{ from: 'key1', to: 'mappedKey1' }],
advSubIdMapping: [{ from: 'key2', to: 'mappedKey2' }],
advUniqueIdMapping: [{ from: 'key3', to: 'mappedKey3' }],
url: 'https://demo.go2cloud.org/aff_l?offer_id=45&aff_id=1029',
},
],
};

const response = responseBuilder(message, { Config });
expect(response.params).toEqual({});
expect(response.endpoint).toBe('https://example.com/event');

expect(response).toBeDefined();
expect(response.endpoint).toEqual('https://demo.go2cloud.org/aff_l?offer_id=45&aff_id=1029');
expect(response.params).toEqual({
offer_id: '45',
aff_id: '1029',
});
});
});

Expand Down Expand Up @@ -91,10 +123,7 @@ describe('processEvent', () => {
JSON_ARRAY: {},
XML: {},
},
params: {
platform_key: 'meta',
conversions_key: 1,
},
params: {},
endpoint: 'https://example.com/track',
event: 'product list viewed',
files: {},
Expand Down

0 comments on commit 7878e3b

Please sign in to comment.