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

search router polish #50883

Merged
Show file tree
Hide file tree
Changes from 18 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
7 changes: 4 additions & 3 deletions src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ type QueryFilter = {

type AdvancedFiltersKeys = ValueOf<typeof CONST.SEARCH.SYNTAX_FILTER_KEYS>;

type QueryFilters = {
[K in AdvancedFiltersKeys]?: QueryFilter[];
};
type QueryFilters = Array<{
key: AdvancedFiltersKeys;
filters: QueryFilter[];
}>;

type SearchQueryString = string;

Expand Down
4 changes: 2 additions & 2 deletions src/libs/SearchParser/autocompleteParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function peg$parse(input, options) {

var peg$r0 = /^[:=]/;
var peg$r1 = /^[^"\r\n]/;
var peg$r2 = /^[A-Za-z0-9_@.\/#&+\-\\',;%]/;
var peg$r2 = /^[^ "\t\n\r]/;
var peg$r3 = /^[ \t\r\n]/;

var peg$e0 = peg$otherExpectation("key");
Expand All @@ -219,7 +219,7 @@ function peg$parse(input, options) {
var peg$e19 = peg$literalExpectation("\"", false);
var peg$e20 = peg$classExpectation(["\"", "\r", "\n"], true, false);
var peg$e21 = peg$otherExpectation("word");
var peg$e22 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", "@", ".", "/", "#", "&", "+", "-", "\\", "'", ",", ";", "%"], false, false);
var peg$e22 = peg$classExpectation([" ", "\"", "\t", "\n", "\r"], true, false);
var peg$e23 = peg$otherExpectation("whitespace");
var peg$e24 = peg$classExpectation([" ", "\t", "\r", "\n"], false, false);

Expand Down
2 changes: 1 addition & 1 deletion src/libs/SearchParser/baseRules.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ operator "operator"
quotedString "quote" = "\"" chars:[^"\r\n]* "\"" { return chars.join(""); }

alphanumeric "word"
= chars:[A-Za-z0-9_@./#&+\-\\',;%]+ {
= chars:[^ "\t\n\r]+ {
return chars.join("").trim().split(",").filter(Boolean);
}

Expand Down
13 changes: 2 additions & 11 deletions src/libs/SearchParser/searchParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function peg$parse(input, options) {

var peg$r0 = /^[:=]/;
var peg$r1 = /^[^"\r\n]/;
var peg$r2 = /^[A-Za-z0-9_@.\/#&+\-\\',;%]/;
var peg$r2 = /^[^ "\t\n\r]/;
var peg$r3 = /^[ \t\r\n]/;

var peg$e0 = peg$otherExpectation("key");
Expand Down Expand Up @@ -245,7 +245,7 @@ function peg$parse(input, options) {
var peg$e30 = peg$literalExpectation("\"", false);
var peg$e31 = peg$classExpectation(["\"", "\r", "\n"], true, false);
var peg$e32 = peg$otherExpectation("word");
var peg$e33 = peg$classExpectation([["A", "Z"], ["a", "z"], ["0", "9"], "_", "@", ".", "/", "#", "&", "+", "-", "\\", "'", ",", ";", "%"], false, false);
var peg$e33 = peg$classExpectation([" ", "\"", "\t", "\n", "\r"], true, false);
var peg$e34 = peg$otherExpectation("whitespace");
var peg$e35 = peg$classExpectation([" ", "\t", "\r", "\n"], false, false);

Expand Down Expand Up @@ -747,15 +747,6 @@ function peg$parse(input, options) {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e15); }
}
if (s1 === peg$FAILED) {
if (input.substr(peg$currPos, 2) === peg$c6) {
s1 = peg$c6;
peg$currPos += 2;
} else {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e7); }
}
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/libs/SearchParser/searchParser.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ key "key"
/ "cardID"
/ "from"
/ "expenseType"
/ "in"
)

defaultKey "default key"
Expand Down
119 changes: 68 additions & 51 deletions src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import cloneDeep from 'lodash/cloneDeep';
import type {OnyxCollection} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {AdvancedFiltersKeys, ASTNode, QueryFilter, QueryFilters, SearchQueryJSON, SearchQueryString, SearchStatus} from '@components/Search/types';
import type {ASTNode, QueryFilter, QueryFilters, SearchQueryJSON, SearchQueryString, SearchStatus} from '@components/Search/types';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {SearchAdvancedFiltersForm} from '@src/types/form';
import FILTER_KEYS from '@src/types/form/SearchAdvancedFiltersForm';
import type * as OnyxTypes from '@src/types/onyx';
import type {SearchDataTypes} from '@src/types/onyx/SearchResults';
import * as CurrencyUtils from './CurrencyUtils';
import localeCompare from './LocaleCompare';
import {validateAmount} from './MoneyRequestUtils';
import * as PersonalDetailsUtils from './PersonalDetailsUtils';
import {getTagNamesFromTagsLists} from './PolicyUtils';
Expand Down Expand Up @@ -115,7 +117,7 @@ function buildFilterValuesString(filterName: string, queryFilters: QueryFilter[]
* Traverses the AST and returns filters as a QueryFilters object.
*/
function getFilters(queryJSON: SearchQueryJSON) {
const filters = {} as QueryFilters;
const filters = [] as QueryFilters;
const filterKeys = Object.values(CONST.SEARCH.SYNTAX_FILTER_KEYS);

function traverse(node: ASTNode) {
Expand All @@ -136,12 +138,7 @@ function getFilters(queryJSON: SearchQueryJSON) {
return;
}

if (!filters[nodeKey]) {
filters[nodeKey] = [];
}

// the "?? []" is added only for typescript because otherwise TS throws an error, in newer TS versions this should be fixed
const filterArray = filters[nodeKey] ?? [];
const filterArray = [];
if (!Array.isArray(node.right)) {
filterArray.push({
operator: node.operator,
Expand All @@ -155,6 +152,7 @@ function getFilters(queryJSON: SearchQueryJSON) {
});
});
}
filters.push({key: nodeKey, filters: filterArray});
}

if (queryJSON.filters) {
Expand Down Expand Up @@ -200,6 +198,16 @@ function findIDFromDisplayValue(filterName: ValueOf<typeof CONST.SEARCH.SYNTAX_F
)
.flat();
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.AMOUNT) {
if (typeof filter === 'string') {
const backendAmount = CurrencyUtils.convertToBackendAmount(Number(filter));
return Number.isNaN(backendAmount) ? filter : backendAmount.toString();
}
return filter.map((amount) => {
const backendAmount = CurrencyUtils.convertToBackendAmount(Number(amount));
return Number.isNaN(backendAmount) ? amount : backendAmount.toString();
});
}
return filter;
}

Expand All @@ -218,18 +226,14 @@ function getQueryHash(query: SearchQueryJSON): number {
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_BY}:${query.sortBy}`;
orderedQuery += ` ${CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_ORDER}:${query.sortOrder}`;

Object.keys(query.flatFilters)
query.flatFilters.forEach((filter) => {
filter.filters.sort((a, b) => localeCompare(a.value.toString(), b.value.toString()));
});

query.flatFilters
.map((filter) => buildFilterValuesString(filter.key, filter.filters))
.sort()
.forEach((key) => {
const filterValues = query.flatFilters?.[key as AdvancedFiltersKeys];
const sortedFilterValues = filterValues?.sort((queryFilter1, queryFilter2) => {
if (queryFilter1.value > queryFilter2.value) {
return 1;
}
return -1;
});
orderedQuery += ` ${buildFilterValuesString(key, sortedFilterValues ?? [])}`;
});
.forEach((filterString) => (orderedQuery += ` ${filterString}`));

return UserUtils.hashText(orderedQuery, 2 ** 32);
}
Expand Down Expand Up @@ -281,13 +285,9 @@ function buildSearchQueryString(queryJSON?: SearchQueryJSON) {

const filters = queryJSON.flatFilters;

for (const [, filterKey] of Object.entries(CONST.SEARCH.SYNTAX_FILTER_KEYS)) {
const queryFilter = filters[filterKey];

if (queryFilter) {
const filterValueString = buildFilterValuesString(filterKey, queryFilter);
queryParts.push(filterValueString);
}
for (const filter of filters) {
const filterValueString = buildFilterValuesString(filter.key, filter.filters);
queryParts.push(filterValueString);
}

return queryParts.join(' ');
Expand Down Expand Up @@ -388,32 +388,35 @@ function buildFilterFormValuesFromQuery(
taxRates: Record<string, string[]>,
) {
const filters = queryJSON.flatFilters;
const filterKeys = Object.keys(filters);
const filtersForm = {} as Partial<SearchAdvancedFiltersForm>;
const policyID = queryJSON.policyID;
for (const filterKey of filterKeys) {
for (const queryFilter of filters) {
const filterKey = queryFilter.key;
const filterList = queryFilter.filters;
const filterValues = filterList.map((item) => item.value.toString());
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.REPORT_ID || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.MERCHANT || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.DESCRIPTION) {
filtersForm[filterKey] = filters[filterKey]?.[0]?.value.toString();
filtersForm[filterKey] = filterValues.at(0);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPENSE_TYPE) {
filtersForm[filterKey] = filters[filterKey]
?.map((expenseType) => expenseType.value.toString())
.filter((expenseType) => Object.values(CONST.SEARCH.TRANSACTION_TYPE).includes(expenseType as ValueOf<typeof CONST.SEARCH.TRANSACTION_TYPE>));
const validExpenseTypes = new Set(Object.values(CONST.SEARCH.TRANSACTION_TYPE));
filtersForm[filterKey] = filterValues.filter((expenseType) => validExpenseTypes.has(expenseType as ValueOf<typeof CONST.SEARCH.TRANSACTION_TYPE>));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID) {
filtersForm[filterKey] = filters[filterKey]?.map((card) => card.value.toString()).filter((card) => Object.keys(cardList).includes(card));
filtersForm[filterKey] = filterValues.filter((card) => cardList[card]);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE) {
filtersForm[filterKey] = filters[filterKey]?.map((tax) => tax.value.toString()).filter((tax) => [...Object.values(taxRates)].flat().includes(tax));
const allTaxRates = new Set(Object.values(taxRates).flat());
filtersForm[filterKey] = filterValues.filter((tax) => allTaxRates.has(tax));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) {
filtersForm[filterKey] = filters[filterKey]?.map((report) => report.value.toString()).filter((id) => reports?.[`${ONYXKEYS.COLLECTION.REPORT}${id}`]);
filtersForm[filterKey] = filterValues.filter((id) => reports?.[`${ONYXKEYS.COLLECTION.REPORT}${id}`]);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TO) {
filtersForm[filterKey] = filters[filterKey]?.map((id) => id.value.toString()).filter((id) => Object.keys(personalDetails).includes(id));
filtersForm[filterKey] = filterValues.filter((id) => personalDetails[id]);
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY) {
filtersForm[filterKey] = filters[filterKey]?.filter((currency) => Object.keys(currencyList).includes(currency.value.toString())).map((currency) => currency.value.toString());
const validCurrency = new Set(Object.keys(currencyList));
filtersForm[filterKey] = filterValues.filter((currency) => validCurrency.has(currency));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG) {
const tags = policyID
Expand All @@ -422,22 +425,23 @@ function buildFilterFormValuesFromQuery(
.filter((item) => !!item)
.map((tagList) => getTagNamesFromTagsLists(tagList ?? {}))
.flat();
tags.push(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filters[filterKey]?.map((tag) => tag.value.toString()).filter((name) => tags.includes(name));
const uniqueTags = new Set(tags);
uniqueTags.add(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filterValues.filter((name) => uniqueTags.has(name));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY) {
const categories = policyID
? Object.values(policyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`] ?? {}).map((category) => category.name)
: Object.values(policyCategories ?? {})
.map((item) => Object.values(item ?? {}).map((category) => category.name))
.flat();
categories.push(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filters[filterKey]?.map((category) => category.value.toString()).filter((name) => categories.includes(name));
const uniqueCategories = new Set(categories);
uniqueCategories.add(CONST.SEARCH.EMPTY_VALUE);
filtersForm[filterKey] = filterValues.filter((name) => uniqueCategories.has(name));
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.KEYWORD) {
filtersForm[filterKey] = filters[filterKey]
?.map((filter) => filter.value.toString())
.map((filter) => {
filtersForm[filterKey] = filterValues
?.map((filter) => {
if (filter.includes(' ')) {
return `"${filter}"`;
}
Expand All @@ -446,12 +450,19 @@ function buildFilterFormValuesFromQuery(
.join(' ');
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE) {
filtersForm[FILTER_KEYS.DATE_BEFORE] = filters[filterKey]?.find((filter) => filter.operator === 'lt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString();
filtersForm[FILTER_KEYS.DATE_AFTER] = filters[filterKey]?.find((filter) => filter.operator === 'gt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString();
filtersForm[FILTER_KEYS.DATE_BEFORE] =
filterList.find((filter) => filter.operator === 'lt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString() ?? filtersForm[FILTER_KEYS.DATE_BEFORE];
filtersForm[FILTER_KEYS.DATE_AFTER] =
filterList.find((filter) => filter.operator === 'gt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString() ?? filtersForm[FILTER_KEYS.DATE_AFTER];
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.AMOUNT) {
filtersForm[FILTER_KEYS.LESS_THAN] = filters[filterKey]?.find((filter) => filter.operator === 'lt' && validateAmount(filter.value.toString(), 2))?.value.toString();
filtersForm[FILTER_KEYS.GREATER_THAN] = filters[filterKey]?.find((filter) => filter.operator === 'gt' && validateAmount(filter.value.toString(), 2))?.value.toString();
// backend amount is an integer and is 2 digits longer than frontend amount
filtersForm[FILTER_KEYS.LESS_THAN] =
filterList.find((filter) => filter.operator === 'lt' && validateAmount(filter.value.toString(), 0, CONST.IOU.AMOUNT_MAX_LENGTH + 2))?.value.toString() ??
filtersForm[FILTER_KEYS.LESS_THAN];
filtersForm[FILTER_KEYS.GREATER_THAN] =
filterList.find((filter) => filter.operator === 'gt' && validateAmount(filter.value.toString(), 0, CONST.IOU.AMOUNT_MAX_LENGTH + 2))?.value.toString() ??
filtersForm[FILTER_KEYS.GREATER_THAN];
}
}

Expand Down Expand Up @@ -503,6 +514,10 @@ function getDisplayValue(filterName: string, filter: string, personalDetails: On
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) {
return ReportUtils.getReportName(reports?.[`${ONYXKEYS.COLLECTION.REPORT}${filter}`]) || filter;
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.AMOUNT) {
const frontendAmount = CurrencyUtils.convertToFrontendAmountAsInteger(Number(filter));
return Number.isNaN(frontendAmount) ? filter : frontendAmount.toString();
}
return filter;
}

Expand All @@ -524,8 +539,10 @@ function buildUserReadableQueryString(

let title = `type:${type} status:${status}`;

Object.keys(filters).forEach((key) => {
const queryFilter = filters[key as ValueOf<typeof CONST.SEARCH.SYNTAX_FILTER_KEYS>] ?? [];
for (const filterObject of filters) {
const key = filterObject.key;
const queryFilter = filterObject.filters;

let displayQueryFilters: QueryFilter[] = [];
if (key === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE) {
const taxRateIDs = queryFilter.map((filter) => filter.value.toString());
Expand All @@ -549,7 +566,7 @@ function buildUserReadableQueryString(
}));
}
title += buildFilterValuesString(key, displayQueryFilters);
});
}

return title;
}
Expand Down
Loading