diff --git a/src/v0/destinations/tune/transform.js b/src/v0/destinations/tune/transform.js index 79ef9c0f34..4c41c0b4c9 100644 --- a/src/v0/destinations/tune/transform.js +++ b/src/v0/destinations/tune/transform.js @@ -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 @@ -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 + 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 params = { + ...mapPropertiesWithNestedSupport(message, standardHashMap), + ...mapPropertiesWithNestedSupport(message, advSubIdHashMap), + ...mapPropertiesWithNestedSupport(message, advUniqueIdHashMap), + }; // Prepare the response const response = defaultRequestConfig(); diff --git a/src/v0/destinations/tune/transform.test.js b/src/v0/destinations/tune/transform.test.js index 688339d649..3d3a2a5747 100644 --- a/src/v0/destinations/tune/transform.test.js +++ b/src/v0/destinations/tune/transform.test.js @@ -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', + }); }); }); @@ -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: {},