Skip to content

Commit

Permalink
updated nested field logic
Browse files Browse the repository at this point in the history
  • Loading branch information
darshan-iterable committed Sep 2, 2024
1 parent 0218166 commit 1da1586
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 120 deletions.
19 changes: 6 additions & 13 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,21 +351,14 @@ class CriteriaCompletionChecker {

if (field.includes('.')) {
const fields = field.split('.');
if (Array.isArray(eventData[fields[0]])) {
return eventData[fields[0]]?.every((item: any) => {
const firtElement = eventData?.[fields[0]];
if (Array.isArray(firtElement)) {
return firtElement?.some((item: any) => {
const data = {
[fields[0]]: item,
eventType: query?.eventType
...eventData,
[fields[0]]: item
};
const valueFromObj = this.getFieldValue(data, field);
if (valueFromObj) {
return this.evaluateComparison(
query.comparatorType,
valueFromObj,
query.value ? query.value : ''
);
}
return false;
return this.evaluateFieldLogic(searchQueries, data);
});
}

Expand Down
51 changes: 51 additions & 0 deletions src/anonymousUserTracking/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,54 @@ export const DATA_TYPE_COMPARATOR_IS_SET = {
}
]
};

export const NESTED_CRITERIA = {
count: 1,
criterias: [
{
criteriaId: '168',
name: 'nested testing',
createdAt: 1721251169153,
updatedAt: 1723488175352,
searchQuery: {
combinator: 'And',
searchQueries: [
{
combinator: 'And',
searchQueries: [
{
dataType: 'user',
searchCombo: {
combinator: 'And',
searchQueries: [
{
dataType: 'user',
field: 'furniture',
comparatorType: 'IsSet',
value: '',
fieldType: 'nested'
},
{
dataType: 'user',
field: 'furniture.furnitureType',
comparatorType: 'Equals',
value: 'Sofa',
fieldType: 'string'
},
{
dataType: 'user',
field: 'furniture.furnitureColor',
comparatorType: 'Equals',
value: 'White',
fieldType: 'string'
}
]
}
}
]
}
]
}
}
]
};
113 changes: 6 additions & 107 deletions src/anonymousUserTracking/tests/nestedTesting.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SHARED_PREFS_EVENT_LIST_KEY } from '../../constants';
import CriteriaCompletionChecker from '../criteriaCompletionChecker';
import { NESTED_CRITERIA } from './constants';

const localStorageMock = {
getItem: jest.fn(),
Expand Down Expand Up @@ -27,7 +28,7 @@ describe('nestedTesting', () => {
widthInches: 60
},
{
furnitureType: 'Sofa',
furnitureType: 'table',
furnitureColor: 'Gray',
lengthInches: 20,
widthInches: 30
Expand All @@ -48,58 +49,7 @@ describe('nestedTesting', () => {
const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify({
count: 1,
criterias: [
{
criteriaId: '168',
name: 'nested testing',
createdAt: 1721251169153,
updatedAt: 1723488175352,
searchQuery: {
combinator: 'And',
searchQueries: [
{
combinator: 'And',
searchQueries: [
{
dataType: 'user',
searchCombo: {
combinator: 'And',
searchQueries: [
{
dataType: 'user',
field: 'furniture',
comparatorType: 'IsSet',
value: '',
fieldType: 'nested'
},
{
dataType: 'user',
field: 'furniture.furnitureColor',
comparatorType: 'IsSet',
value: '',
fieldType: 'string'
},
{
dataType: 'user',
field: 'furniture.furnitureType',
comparatorType: 'Equals',
value: 'Sofa',
fieldType: 'string'
}
]
}
}
]
}
]
}
}
]
})
);
const result = checker.getMatchedCriteria(JSON.stringify(NESTED_CRITERIA));
expect(result).toEqual('168');
});

Expand All @@ -113,13 +63,13 @@ describe('nestedTesting', () => {
furniture: [
{
furnitureType: 'Sofa',
furnitureColor: 'White',
furnitureColor: 'Gray',
lengthInches: 40,
widthInches: 60
},
{
furnitureType: 'table',
furnitureColor: 'Gray',
furnitureColor: 'White',
lengthInches: 20,
widthInches: 30
}
Expand All @@ -139,58 +89,7 @@ describe('nestedTesting', () => {
const checker = new CriteriaCompletionChecker(
localStoredEventList === null ? '' : localStoredEventList
);
const result = checker.getMatchedCriteria(
JSON.stringify({
count: 1,
criterias: [
{
criteriaId: '168',
name: 'nested testing',
createdAt: 1721251169153,
updatedAt: 1723488175352,
searchQuery: {
combinator: 'And',
searchQueries: [
{
combinator: 'And',
searchQueries: [
{
dataType: 'user',
searchCombo: {
combinator: 'And',
searchQueries: [
{
dataType: 'user',
field: 'furniture',
comparatorType: 'IsSet',
value: '',
fieldType: 'nested'
},
{
dataType: 'user',
field: 'furniture.furnitureColor',
comparatorType: 'IsSet',
value: '',
fieldType: 'string'
},
{
dataType: 'user',
field: 'furniture.furnitureType',
comparatorType: 'Equals',
value: 'Sofa',
fieldType: 'string'
}
]
}
}
]
}
]
}
}
]
})
);
const result = checker.getMatchedCriteria(JSON.stringify(NESTED_CRITERIA));
expect(result).toEqual(null);
});
});

0 comments on commit 1da1586

Please sign in to comment.