Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOB-9652] support for nested JSON array #459

Merged
merged 6 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

class CriteriaCompletionChecker {
private localStoredEventList: any[];

Check warning on line 39 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

constructor(localStoredEventList: string) {
this.localStoredEventList = JSON.parse(localStoredEventList);
Expand Down Expand Up @@ -72,8 +72,8 @@
return matchingCriteria ? matchingCriteria.criteriaId : null;
}

private prepareEventsToProcess(): any[] {

Check warning on line 75 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const eventsToProcess: any[] = this.getEventsWithCartItems();

Check warning on line 76 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const nonPurchaseEvents: any[] = this.getNonCartEvents();

nonPurchaseEvents.forEach((event) => {
Expand Down Expand Up @@ -350,16 +350,41 @@
);

if (field.includes('.')) {
const fields = field.split('.');
const firstElement = eventData?.[fields[0]];
if (Array.isArray(firstElement)) {
return firstElement?.some((item: any) => {
const data = {
...eventData,
[fields[0]]: item
};
return this.evaluateFieldLogic(searchQueries, data);
});
const splitField = field.split('.') as string[];
const fields =
eventData?.eventType === TRACK_EVENT &&
eventData?.eventName === splitField[0]
? splitField.slice(1)
: splitField;

let fieldValue = eventData;
let isSubFieldArray = false;
let isSubMatch = false;

fields.forEach((subField) => {
const subFieldValue = fieldValue[subField];
if (Array.isArray(subFieldValue)) {
isSubFieldArray = true;
isSubMatch = subFieldValue.some((item: any) => {
const data = fields.reduceRight((acc: any, key) => {
if (key === subField) {
return { [key]: item };
}
return { [key]: acc };
}, {});

return this.evaluateFieldLogic(searchQueries, {
...eventData,
...data
});
});
} else {
fieldValue = subFieldValue;
}
});

if (isSubFieldArray) {
return isSubMatch;
}

const valueFromObj = this.getFieldValue(eventData, field);
Expand Down
253 changes: 253 additions & 0 deletions src/anonymousUserTracking/tests/complexCriteria.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { SHARED_PREFS_EVENT_LIST_KEY } from '../../constants';
import CriteriaCompletionChecker from '../criteriaCompletionChecker';
import {
COMPLEX_CRITERIA_1,
COMPLEX_CRITERIA_2,
COMPLEX_CRITERIA_3
} from './constants';

const localStorageMock = {
getItem: jest.fn(),
Expand Down Expand Up @@ -1598,4 +1603,252 @@ describe('complexCriteria', () => {
);
expect(result).toEqual(null);
});

// MARK: Complex criteria #1
it('should return criteriaId 290 if (1 OR 2 OR 3) AND (4 AND 5) AND (6 NOT 7) matched', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataFields: {
saved_cars: { color: 'black' },
'animal-found': { vaccinated: true },
eventName: 'birthday'
},
eventType: 'customEvent'
},
{
dataFields: { reason: 'testing', total: 30 },
eventType: 'purchase'
},
{
dataFields: { firstName: 'Adam' },
eventType: 'user'
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(COMPLEX_CRITERIA_1)
);
expect(result).toEqual('290');
});

it('should return criteriaId null if (1 OR 2 OR 3) AND (4 AND 5) AND (6 NOT 7) - No match', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'Alex'
}
},
{
eventType: 'customEvent',
eventName: 'saved_cars',
dataFields: {
color: ''
}
},
{
eventType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: true
}
},
{
eventType: 'purchase',
dataFields: {
total: 30,
reason: 'testing'
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(COMPLEX_CRITERIA_1)
);
expect(result).toEqual(null);
});

// MARK: Complex criteria #2
it('should return criteriaId 291 if (6 OR 7) OR (4 AND 5) OR (1 NOT 2 NOT 3) matched', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'xcode'
}
},
{
eventType: 'customEvent',
eventName: 'saved_cars',
dataFields: {
color: 'black'
}
},
{
eventType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: true
}
},
{
eventType: 'purchase',
dataFields: {
total: 110,
reason: 'testing'
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(COMPLEX_CRITERIA_2)
);
expect(result).toEqual('291');
});

it('should return criteriaId null if (6 OR 7) OR (4 AND 5) OR (1 NOT 2 NOT 3) - No match', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'Alex'
}
},
{
eventType: 'purchase',
dataFields: {
total: 10,
reason: 'null'
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(COMPLEX_CRITERIA_2)
);
expect(result).toEqual(null);
});

// MARK: Complex criteria #3
it('should return criteriaId 292 if (1 AND 2) NOR (3 OR 4 OR 5) NOR (6 NOR 7) matched', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
dataType: 'user',
dataFields: {
firstName: 'xcode',
lastName: 'ssr'
}
},
{
dataType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: true,
count: 10
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(COMPLEX_CRITERIA_3)
);
expect(result).toEqual('292');
});

it('should return criteriaId null if (1 AND 2) NOR (3 OR 4 OR 5) NOR (6 NOR 7) - No match', () => {
(localStorage.getItem as jest.Mock).mockImplementation((key) => {
if (key === SHARED_PREFS_EVENT_LIST_KEY) {
return JSON.stringify([
{
eventType: 'user',
dataFields: {
firstName: 'Alex',
lastName: 'Aris'
}
},
{
eventType: 'customEvent',
eventName: 'animal-found',
dataFields: {
vaccinated: false,
count: 4
}
}
]);
}
return null;
});

const localStoredEventList = localStorage.getItem(
SHARED_PREFS_EVENT_LIST_KEY
);

const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify(COMPLEX_CRITERIA_3)
);
expect(result).toEqual(null);
});
});
Loading
Loading