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

updated nested field logic #445

Merged
merged 4 commits into from
Sep 5, 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
19 changes: 6 additions & 13 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,9 +72,9 @@
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();

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

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

nonPurchaseEvents.forEach((event) => {
eventsToProcess.push(event);
Expand All @@ -83,7 +83,7 @@
return eventsToProcess;
}

private getEventsWithCartItems(): any[] {

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

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const processedEvents: any[] = [];

this.localStoredEventList.forEach((localEventData) => {
Expand Down Expand Up @@ -351,21 +351,14 @@

if (field.includes('.')) {
const fields = field.split('.');
if (Array.isArray(eventData[fields[0]])) {
return eventData[fields[0]]?.every((item: any) => {
const firstElement = eventData?.[fields[0]];
if (Array.isArray(firstElement)) {
return firstElement?.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 @@ -782,3 +782,54 @@ export const ARRAY_MATCHREGEX_CRITERIA = {
}
]
};

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);
});
});
Loading