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 10 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: ValueOf<typeof CONST.SEARCH.SYNTAX_FILTER_KEYS>;
filters: QueryFilter[];
}>;

type SearchQueryString = string;

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("operator");
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 @@ -848,15 +848,6 @@ function peg$parse(input, options) {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e22); }
}
if (s1 === peg$FAILED) {
if (input.substr(peg$currPos, 2) === peg$c11) {
s1 = peg$c11;
peg$currPos += 2;
} else {
s1 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e14); }
}
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/libs/SearchParser/searchParser.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ key "key"
/ "cardID"
/ "from"
/ "expenseType"
/ "in"
)

defaultKey "default key"
Expand All @@ -137,7 +136,7 @@ identifier
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
91 changes: 54 additions & 37 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ function sanitizeString(str: string) {
* 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 @@ -510,12 +510,8 @@ 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
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
const filterArray = filters[nodeKey] ?? [];
const filterArray = [];
if (!Array.isArray(node.right)) {
filterArray.push({
operator: node.operator,
Expand All @@ -529,6 +525,7 @@ function getFilters(queryJSON: SearchQueryJSON) {
});
});
}
filters.push({key: nodeKey, filters: filterArray});
}

if (queryJSON.filters) {
Expand Down Expand Up @@ -572,13 +569,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 = buildFilterString(filterKey, queryFilter);
queryParts.push(filterValueString);
}
for (const filter of filters) {
const filterValueString = buildFilterString(filter.key, filter.filters);
queryParts.push(filterValueString);
}

return queryParts.join(' ');
Expand Down Expand Up @@ -670,32 +663,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 @@ -704,20 +700,21 @@ function buildFilterFormValuesFromQuery(
.filter((item) => !!item)
.map((tagList) => getTagNamesFromTagsLists(tagList ?? {}))
.flat();
filtersForm[filterKey] = filters[filterKey]?.map((tag) => tag.value.toString()).filter((name) => tags.includes(name));
const uniqueTags = new Set(tags);
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((xd) => Object.values(xd ?? {}).map((category) => category.name))
.map((categoryList) => Object.values(categoryList ?? {}).map((category) => category.name))
.flat();
filtersForm[filterKey] = filters[filterKey]?.map((category) => category.value.toString()).filter((name) => categories.includes(name));
const uniqueCategories = new Set(categories);
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 @@ -726,12 +723,16 @@ 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_AFTER] = filterList.find((filter) => filter.operator === 'gt' && ValidationUtils.isValidDate(filter.value.toString()))?.value.toString();
}
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();
filtersForm[FILTER_KEYS.LESS_THAN] = filterList
.find((filter) => filter.operator === 'lt' && validateAmount(filter.value.toString(), 0, CONST.IOU.AMOUNT_MAX_LENGTH + 2))
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
?.value.toString();
filtersForm[FILTER_KEYS.GREATER_THAN] = filterList
.find((filter) => filter.operator === 'gt' && validateAmount(filter.value.toString(), 0, CONST.IOU.AMOUNT_MAX_LENGTH + 2))
?.value.toString();
}
}

Expand Down Expand Up @@ -779,6 +780,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 Down Expand Up @@ -814,8 +819,10 @@ function getSearchHeaderTitle(

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 @@ -839,7 +846,7 @@ function getSearchHeaderTitle(
}));
}
title += buildFilterString(key, displayQueryFilters);
});
}

return title;
}
Expand Down Expand Up @@ -921,6 +928,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 Down
Loading