fix: honor table search q param on users and related lists
Dashboard sends q, but users/classes/payments ignored it; accept q/search and normalize Persian digits.
This commit is contained in:
+29
-5
@@ -22,7 +22,27 @@ const parsePaginationAndSort = (query, defaultSortBy = 'createdAt', defaultSortO
|
||||
};
|
||||
};
|
||||
|
||||
const buildFilterQuery = (query, searchFields = [], excludedKeys = ['page', 'limit', 'sortBy', 'sortOrder', 'q', 'lang']) => {
|
||||
const escapeRegex = (value) => String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
|
||||
const toLatinDigits = (value) => {
|
||||
if (value == null) return '';
|
||||
const persian = '۰۱۲۳۴۵۶۷۸۹';
|
||||
const arabic = '٠١٢٣٤٥٦٧٨٩';
|
||||
return String(value).replace(/[۰-۹٠-٩]/g, (ch) => {
|
||||
const p = persian.indexOf(ch);
|
||||
if (p >= 0) return String(p);
|
||||
const a = arabic.indexOf(ch);
|
||||
return a >= 0 ? String(a) : ch;
|
||||
});
|
||||
};
|
||||
|
||||
const getSearchTerm = (query) => {
|
||||
const raw = query?.q || query?.search;
|
||||
if (raw == null || String(raw).trim() === '') return '';
|
||||
return toLatinDigits(String(raw).trim());
|
||||
};
|
||||
|
||||
const buildFilterQuery = (query, searchFields = [], excludedKeys = ['page', 'limit', 'sortBy', 'sortOrder', 'q', 'search', 'lang']) => {
|
||||
const filter = {};
|
||||
|
||||
// Build field-based exact or boolean filters
|
||||
@@ -41,9 +61,10 @@ const buildFilterQuery = (query, searchFields = [], excludedKeys = ['page', 'lim
|
||||
}
|
||||
});
|
||||
|
||||
// Build regex search for ?q= across defined text fields
|
||||
if (query.q && searchFields.length > 0) {
|
||||
const searchRegex = new RegExp(query.q, 'i');
|
||||
// Build regex search for ?q= or ?search= across defined text fields
|
||||
const searchTerm = getSearchTerm(query);
|
||||
if (searchTerm && searchFields.length > 0) {
|
||||
const searchRegex = new RegExp(escapeRegex(searchTerm), 'i');
|
||||
filter.$or = searchFields.map((field) => ({
|
||||
[field]: searchRegex
|
||||
}));
|
||||
@@ -65,5 +86,8 @@ const calculateMeta = (totalCount, page, limit) => {
|
||||
module.exports = {
|
||||
parsePaginationAndSort,
|
||||
buildFilterQuery,
|
||||
calculateMeta
|
||||
calculateMeta,
|
||||
escapeRegex,
|
||||
toLatinDigits,
|
||||
getSearchTerm
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user