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

Isset purchase update fix #416

Merged
merged 21 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
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 @@
UPDATE_USER,
KEY_EVENT_NAME,
UPDATECART_ITEM_PREFIX,
PURCHASE_ITEM_PREFIX
PURCHASE_ITEM_PREFIX,
PURCHASE_ITEM
} from '../constants';

interface SearchQuery {
Expand All @@ -33,7 +34,7 @@
}

class CriteriaCompletionChecker {
private localStoredEventList: any[];

Check warning on line 37 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 @@ -69,7 +70,7 @@
return matchingCriteria ? matchingCriteria.criteriaId : null;
}

private prepareEventsToProcess(): any[] {

Check warning on line 73 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();
const nonPurchaseEvents: any[] = this.getNonCartEvents();

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

if (localEventData.dataFields) {
Expand Down Expand Up @@ -277,9 +278,16 @@
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 @@
}
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 @@
}

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 @@
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 @@
}
}

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
Loading