Skip to content

Commit

Permalink
fix: merging common properties to a single config json
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Feb 15, 2024
1 parent 9e97f4c commit 2fb5cba
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 104 deletions.
8 changes: 6 additions & 2 deletions src/cdk/v2/destinations/bluecore/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const CONFIG_CATEGORIES = {
name: 'bluecoreTrackConfig',
type: 'track',
},
COMMON: {
name: 'bluecoreCommonConfig',
type: 'common',
},
};

const EVENT_NAME_MAPPING = [
Expand Down Expand Up @@ -40,13 +44,13 @@ const EVENT_NAME_MAPPING = [
},
];

const BLUECORE_EXCLUSION_FIELDS = ['query','order_id','total']
const BLUECORE_EXCLUSION_FIELDS = ['query', 'order_id', 'total'];

const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname);
module.exports = {
CONFIG_CATEGORIES,
MAPPING_CONFIG,
EVENT_NAME_MAPPING,
BASE_URL,
BLUECORE_EXCLUSION_FIELDS
BLUECORE_EXCLUSION_FIELDS,
};
52 changes: 52 additions & 0 deletions src/cdk/v2/destinations/bluecore/data/bluecoreCommonConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"destKey": "properties.customer.name",
"sourceKeys": "name",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.first_name",
"sourceKeys": "firstName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.last_name",
"sourceKeys": "lastName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.age",
"sourceKeys": ["context.traits.age", "traits.age"],
"required": false
},
{
"destKey": "properties.customer.sex",
"sourceKeys": ["traits.gender", "context.traits.gender", "traits.sex", "context.traits.sex"],
"required": false
},
{
"destKey": "properties.customer.address",
"sourceKeys": "address",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.email",
"sourceKeys": "email",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.client",
"sourceKeys": "context.app.version",
"required": false
},
{
"destKey": "properties.device",
"sourceKeys": "context.device.model",
"required": false
}
]
50 changes: 0 additions & 50 deletions src/cdk/v2/destinations/bluecore/data/bluecoreIdentifyConfig.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,7 @@
[
{
"destKey": "properties.customer.name",
"sourceKeys": "name",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.first_name",
"sourceKeys": "firstName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.last_name",
"sourceKeys": "lastName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.age",
"sourceKeys": ["context.traits.age", "traits.age"],
"required": false
},
{
"destKey": "event",
"sourceKeys": ["traits.action", "context.traits.action"],
"required": false
},
{
"destKey": "properties.customer.sex",
"sourceKeys": ["traits.gender", "context.traits.gender", "traits.sex", "context.traits.sex"],
"required": false
},
{
"destKey": "properties.customer.address",
"sourceKeys": "address",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.email",
"sourceKeys": "email",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.client",
"sourceKeys": "context.app.version",
"required": false
},
{
"destKey": "properties.device",
"sourceKeys": "context.device.model",
"required": false
}
]
50 changes: 0 additions & 50 deletions src/cdk/v2/destinations/bluecore/data/bluecoreTrackConfig.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,4 @@
[
{
"destKey": "properties.customer.name",
"sourceKeys": "name",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.first_name",
"sourceKeys": "firstName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.last_name",
"sourceKeys": "lastName",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.age",
"sourceKeys": ["context.traits.age", "traits.age"],
"required": false
},
{
"destKey": "properties.customer.sex",
"sourceKeys": ["traits.gender", "context.traits.gender", "traits.sex", "context.traits.sex"],
"required": false
},
{
"destKey": "properties.customer.address",
"sourceKeys": "address",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.customer.email",
"sourceKeys": "email",
"required": false,
"sourceFromGenericMap": true
},
{
"destKey": "properties.search_term",
"sourceKeys": "properties.query",
Expand All @@ -54,16 +14,6 @@
"sourceKeys": "properties.total",
"required": false
},
{
"destKey": "properties.client",
"sourceKeys": "context.app.version",
"required": false
},
{
"destKey": "properties.device",
"sourceKeys": "context.device.model",
"required": false
},
{
"destKey": "properties.products",
"sourceKeys": ["properties.products"],
Expand Down
37 changes: 35 additions & 2 deletions src/cdk/v2/destinations/bluecore/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,48 @@ const addProductArray = (products) => {
return finalProductArray;
};

function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}

/**
* Recursively merges multiple objects into a single object.
*
* @param {Object} target - The target object to merge into.
* @param {...Object} sources - The source objects to merge from.
* @returns {Object} - The merged object.
*/
function deepMerge(target, ...sources) {
if (sources.length === 0) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
deepMerge(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
});
}

return deepMerge(target, ...sources);
}

/**
* Constructs properties based on the given message.
*
* @param {object} message - The message object.
* @returns {object} - The constructed properties object.
*/
const constructProperties = (message) => {
const commonCategory = CONFIG_CATEGORIES.COMMON;
const commonPayload = constructPayload(message, MAPPING_CONFIG[commonCategory.name]);
const category = CONFIG_CATEGORIES[message.type.toUpperCase()];
const payload = constructPayload(message, MAPPING_CONFIG[category.name]);
return payload;
const typeSpecificPayload = constructPayload(message, MAPPING_CONFIG[category.name]);
const finalPayload = deepMerge({}, commonPayload, typeSpecificPayload);
return finalPayload;
};

/**
Expand Down Expand Up @@ -220,4 +252,5 @@ module.exports = {
constructProperties,
createProductForStandardEcommEvent,
populateAccurateDistinctId,
deepMerge,
};
50 changes: 50 additions & 0 deletions src/cdk/v2/destinations/bluecore/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
isStandardBluecoreEvent,
deduceTrackEventName,
populateAccurateDistinctId,
deepMerge,
} = require('./utils');
const { InstrumentationError } = require('@rudderstack/integrations-lib');

Expand Down Expand Up @@ -328,4 +329,53 @@ describe('populateAccurateDistinctId', () => {
const distinctId = populateAccurateDistinctId(payload, message);
expect(distinctId).toBe('54321');
});

describe('deepMerge', () => {
// Should merge two objects with non-overlapping properties
it('should merge two objects with non-overlapping properties', () => {
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const result = deepMerge(obj1, obj2);
expect(result).toEqual({ a: 1, b: 2, c: 3, d: 4 });
});

// Should merge two objects with overlapping properties
it('should merge two objects with overlapping properties', () => {
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const result = deepMerge(obj1, obj2);
expect(result).toEqual({ a: 1, b: 3, c: 4 });
});

// Should merge multiple objects with non-overlapping properties
it('should merge multiple objects with non-overlapping properties', () => {
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const obj3 = { c: 3 };
const result = deepMerge(obj1, obj2, obj3);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

// Should return an empty object when no arguments are passed
it('should return an empty object when no arguments are passed', () => {
const result = deepMerge();
expect(result).toEqual(undefined);
});

// Should return the target object when no sources are passed
it('should return the target object when no sources are passed', () => {
const obj = { a: 1, b: 2 };
const result = deepMerge(obj);
expect(result).toBe(obj);
});

// Should handle non-object values as the target object
it('target must be an object', () => {
const target = 'hello';
const obj = { a: 1, b: 2 };
const result = deepMerge(target, obj);
expect(result).not.toBe({ a: 1, b: 2 });
expect(result).toBe('hello');
});
});
});

0 comments on commit 2fb5cba

Please sign in to comment.