Skip to content

Commit

Permalink
Isset purchase update fix (#416)
Browse files Browse the repository at this point in the history
* Fixed single item matches code

* Fixed comments

* added more unit tests, bug fixes

* Not combinator implemented

* evaluateEvent added not combinator

* minMatch changes revert

* minMatch implemented

* complex criteria tests, not combinator, evaluateFieldLogic fix

* Isset fix and unit tests

* update cart and custom event fix, unit tests

* little changes

* doesItemMatchQueries fix

* isset fail & min-max tests,

* Replace for loop with array methods

* added sampleTest1

* Set type annotations for issetCheck

---------

Co-authored-by: hardikmashru <[email protected]>
  • Loading branch information
hardikmashru and hardikmashru authored Jul 12, 2024
1 parent 41ffd79 commit 06c55d9
Show file tree
Hide file tree
Showing 4 changed files with 2,374 additions and 9 deletions.
58 changes: 50 additions & 8 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
UPDATE_USER,
KEY_EVENT_NAME,
UPDATECART_ITEM_PREFIX,
PURCHASE_ITEM_PREFIX
PURCHASE_ITEM_PREFIX,
PURCHASE_ITEM
} from '../constants';

interface SearchQuery {
Expand Down Expand Up @@ -99,7 +100,7 @@ class CriteriaCompletionChecker {
});
return updatItem;
});
updatedItem[KEY_ITEMS] = items;
updatedItem[PURCHASE_ITEM] = items;
}

if (localEventData.dataFields) {
Expand Down Expand Up @@ -277,9 +278,16 @@ class CriteriaCompletionChecker {
private evaluateFieldLogic(searchQueries: any[], eventData: any): boolean {
const localDataKeys = Object.keys(eventData);
let itemMatchedResult = false;
let key_item = null;
if (localDataKeys.includes(KEY_ITEMS)) {
key_item = KEY_ITEMS;
} else if (localDataKeys.includes(PURCHASE_ITEM)) {
key_item = PURCHASE_ITEM;
}

if (key_item !== null) {
// scenario of items inside purchase and updateCart Events
const items = eventData[KEY_ITEMS];
const items = eventData[key_item];
const result = items.some((item: any) => {
return this.doesItemMatchQueries(item, searchQueries);
});
Expand Down Expand Up @@ -307,6 +315,19 @@ class CriteriaCompletionChecker {
}
const matchResult = filteredSearchQueries.every((query: any) => {
const field = query.field;
if (
query.dataType === TRACK_EVENT &&
query.fieldType === 'object' &&
query.comparatorType === 'IsSet'
) {
const eventName = eventData[KEY_EVENT_NAME];
if (eventName === UPDATE_CART && field === eventName) {
return true;
}
if (field === eventName) {
return true;
}
}
const eventKeyItems = filteredLocalDataKeys.filter(
(keyItem) => keyItem === field
);
Expand All @@ -323,10 +344,21 @@ class CriteriaCompletionChecker {
}

private doesItemMatchQueries(item: any, searchQueries: any[]): boolean {
const filteredSearchQueries = searchQueries.filter((searchQuery) =>
Object.keys(item).includes(searchQuery.field)
);
if (filteredSearchQueries.length === 0) {
let shouldReturn = false;
const filteredSearchQueries = searchQueries.filter((searchQuery) => {
if (
searchQuery.field.startsWith(UPDATECART_ITEM_PREFIX) ||
searchQuery.field.startsWith(PURCHASE_ITEM_PREFIX)
) {
if (!Object.keys(item).includes(searchQuery.field)) {
shouldReturn = true;
return false;
}
return true;
}
return false;
});
if (filteredSearchQueries.length === 0 || shouldReturn) {
return false;
}
return filteredSearchQueries.every((query: any) => {
Expand Down Expand Up @@ -356,7 +388,7 @@ class CriteriaCompletionChecker {
case 'DoesNotEquals':
return !this.compareValueEquality(matchObj, valueToCompare);
case 'IsSet':
return matchObj !== '';
return this.issetCheck(matchObj);
case 'GreaterThan':
case 'LessThan':
case 'GreaterThanOrEqualTo':
Expand Down Expand Up @@ -439,6 +471,16 @@ class CriteriaCompletionChecker {
}
}

private issetCheck(matchObj: string | object | any[]): boolean {
if (Array.isArray(matchObj)) {
return matchObj.length > 0;
} else if (typeof matchObj === 'object' && matchObj !== null) {
return Object.keys(matchObj).length > 0;
} else {
return matchObj !== '';
}
}

private handleException(e: any) {
console.error('Exception occurred', e.toString());
}
Expand Down
Loading

0 comments on commit 06c55d9

Please sign in to comment.