Skip to content

Commit

Permalink
fix: event fix and added utility (#3142)
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishmalik authored Feb 29, 2024
1 parent 0e2588e commit 9b705b7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/v0/destinations/adobe_analytics/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
getIntegrationsObj,
removeUndefinedAndNullValues,
simpleProcessRouterDest,
validateEventAndLowerCaseConversion,
} = require('../../util');

const {
Expand Down Expand Up @@ -307,7 +308,7 @@ const processTrackEvent = (message, adobeEventName, destinationConfig, extras =
destinationConfig;
const { event: rawMessageEvent, properties } = message;
const { overrideEventString, overrideProductString } = properties;
const event = rawMessageEvent.toLowerCase();
const event = validateEventAndLowerCaseConversion(rawMessageEvent, true, true);
const adobeEventArr = adobeEventName ? adobeEventName.split(',') : [];
// adobeEventArr is an array of events which is defined as
// ["eventName", "mapped Adobe Event=mapped merchproperty's value", "mapped Adobe Event=mapped merchproperty's value", . . .]
Expand Down
20 changes: 20 additions & 0 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,25 @@ const combineBatchRequestsWithSameJobIds = (inputBatches) => {
return combineBatches(combineBatches(inputBatches));
};

/**
* This function validates the event and return it as string.
* @param {*} isMandatory The event is a required field.
* @param {*} convertToLowerCase The event should be converted to lower-case.
* @returns {string} Event name converted to string.
*/
const validateEventAndLowerCaseConversion = (event, isMandatory, convertToLowerCase) => {
if (!isDefined(event) || typeof event === 'object' || typeof event === 'boolean') {
throw new InstrumentationError('Event should not be a object, NaN, boolean or undefined');
}

// handling 0 as it is a valid value
if (isMandatory && !event && event !== 0) {
throw new InstrumentationError('Event is a required field');
}

return convertToLowerCase ? event.toString().toLowerCase() : event.toString();
};

// ========================================================================
// EXPORTS
// ========================================================================
Expand Down Expand Up @@ -2317,4 +2336,5 @@ module.exports = {
findExistingBatch,
removeDuplicateMetadata,
combineBatchRequestsWithSameJobIds,
validateEventAndLowerCaseConversion,
};
54 changes: 52 additions & 2 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { TAG_NAMES } = require('@rudderstack/integrations-lib');
const { TAG_NAMES, InstrumentationError } = require('@rudderstack/integrations-lib');
const utilities = require('.');
const { getFuncTestData } = require('../../../test/testHelper');
const { FilteredEventsError } = require('./errorTypes');
Expand All @@ -7,6 +7,7 @@ const {
flattenJson,
generateExclusionList,
combineBatchRequestsWithSameJobIds,
validateEventAndLowerCaseConversion,
} = require('./index');

// Names of the utility functions to test
Expand Down Expand Up @@ -36,7 +37,6 @@ describe('Utility Functions Tests', () => {
test.each(funcTestData)('$description', async ({ description, input, output }) => {
try {
let result;

// This is to allow sending multiple arguments to the function
if (Array.isArray(input)) {
result = utilities[funcName](...input);
Expand Down Expand Up @@ -456,3 +456,53 @@ describe('Unit test cases for combineBatchRequestsWithSameJobIds', () => {
expect(combineBatchRequestsWithSameJobIds(input)).toEqual(expectedOutput);
});
});

describe('validateEventAndLowerCaseConversion Tests', () => {
it('should return string conversion of number types', () => {
const ev = 0;
expect(validateEventAndLowerCaseConversion(ev, false, true)).toBe('0');
expect(validateEventAndLowerCaseConversion(ev, true, false)).toBe('0');
});

it('should convert string types to lowercase', () => {
const ev = 'Abc';
expect(validateEventAndLowerCaseConversion(ev, true, true)).toBe('abc');
});

it('should throw error if event is object type', () => {
expect(() => {
validateEventAndLowerCaseConversion({}, true, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion([1, 2], false, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion({ a: 1 }, true, true);
}).toThrow(InstrumentationError);
});

it('should convert string to lowercase', () => {
expect(validateEventAndLowerCaseConversion('Abc', true, true)).toBe('abc');
expect(validateEventAndLowerCaseConversion('ABC', true, false)).toBe('ABC');
expect(validateEventAndLowerCaseConversion('abc55', false, true)).toBe('abc55');
expect(validateEventAndLowerCaseConversion(123, false, true)).toBe('123');
});

it('should throw error for null and undefined', () => {
expect(() => {
validateEventAndLowerCaseConversion(null, true, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion(undefined, false, true);
}).toThrow(InstrumentationError);
});

it('should throw error for boolean values', () => {
expect(() => {
validateEventAndLowerCaseConversion(true, true, true);
}).toThrow(InstrumentationError);
expect(() => {
validateEventAndLowerCaseConversion(false, false, false);
}).toThrow(InstrumentationError);
});
});

0 comments on commit 9b705b7

Please sign in to comment.