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:
2026-08-15 02:57:19 +03:30
parent 4603b45208
commit 7394829d49
5 changed files with 66 additions and 15 deletions
+20 -1
View File
@@ -5,7 +5,8 @@ const Payment = require('./paymentModel');
const AppError = require('../../utils/AppError');
const eventEmitter = require('../../events/eventEmitter');
const EVENT_NAMES = require('../../constants/eventNames');
const { calculateMeta } = require('../../utils/pagination');
const { calculateMeta, escapeRegex, getSearchTerm } = require('../../utils/pagination');
const User = require('../users/userModel');
const getAllPayments = async (query) => {
const page = parseInt(query.page) || 1;
@@ -16,6 +17,24 @@ const getAllPayments = async (query) => {
if (query.userId) filter.user = query.userId;
if (query.status) filter.status = query.status;
const searchTerm = getSearchTerm(query);
if (searchTerm) {
const searchRegex = new RegExp(escapeRegex(searchTerm), 'i');
const matchedUsers = await User.find({
$or: [
{ name: searchRegex },
{ phoneNumber: searchRegex },
{ nationalIdCode: searchRegex }
]
}).select('_id').lean();
filter.$or = [
{ notes: searchRegex },
{ status: searchRegex },
{ user: { $in: matchedUsers.map((u) => u._id) } }
];
}
const [items, total] = await Promise.all([
Payment.find(filter)
.populate({ path: 'user', select: 'name' })