fix withdraw page search bug
This commit is contained in:
@@ -131,48 +131,118 @@ module.exports.getAllForAdmin = async (req, res) => {
|
||||
const page = parseInt(req.query.page) || 1
|
||||
const rows = parseInt(req.query.rows) || 10
|
||||
const search = req.query.search || ''
|
||||
const status = req.query.status !== undefined ? parseInt(req.query.status) : undefined
|
||||
const sortField = req.query.sortField || 'status'
|
||||
const sortOrder = req.query.sortOrder === 'desc' ? -1 : 1
|
||||
const skip = (page - 1) * rows
|
||||
|
||||
const matchStage = search
|
||||
? {
|
||||
$or: [{ IMEI: new RegExp(search, 'i') }, { trackingNumber: new RegExp(search, 'i') }]
|
||||
}
|
||||
: {}
|
||||
// If search is provided, use aggregation to search in user fields
|
||||
if (search) {
|
||||
const matchStage = {}
|
||||
|
||||
if (status !== undefined) {
|
||||
matchStage.status = status
|
||||
}
|
||||
|
||||
// const aggregationPipeline = [
|
||||
// { $match: matchStage },
|
||||
// {
|
||||
// $lookup: {
|
||||
// from: 'usergps',
|
||||
// localField: 'userId',
|
||||
// foreignField: '_id',
|
||||
// as: 'userId'
|
||||
// }
|
||||
// },
|
||||
// { $unwind: '$userId' },
|
||||
// { $sort: { [sortField]: sortOrder } },
|
||||
// { $skip: skip },
|
||||
// { $limit: rows }
|
||||
// ]
|
||||
// const data = await Withdraw.aggregate(aggregationPipeline)
|
||||
const data = await Withdraw.find(matchStage)
|
||||
.sort({ [sortField]: sortOrder })
|
||||
.skip(skip)
|
||||
.limit(rows)
|
||||
.populate('userId')
|
||||
// Build search conditions
|
||||
const searchConditions = [
|
||||
{ 'userId.mobile_number': new RegExp(search, 'i') },
|
||||
{ 'userId.national_code': new RegExp(search, 'i') },
|
||||
{ 'userId.first_name': new RegExp(search, 'i') },
|
||||
{ 'userId.last_name': new RegExp(search, 'i') },
|
||||
{ 'userId.shopName': new RegExp(search, 'i') },
|
||||
{ trackingNumber: new RegExp(search, 'i') }
|
||||
]
|
||||
|
||||
// Add amount search if search is a number
|
||||
if (!isNaN(search) && search.trim() !== '') {
|
||||
searchConditions.push({ amount: parseInt(search) })
|
||||
}
|
||||
|
||||
const totalDocuments = await Withdraw.countDocuments(matchStage)
|
||||
const aggregationPipeline = [
|
||||
{ $match: matchStage },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'usergps',
|
||||
let: { userIdStr: '$userId' },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$eq: ['$_id', { $toObjectId: '$$userIdStr' }]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
as: 'userId'
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: '$userId', preserveNullAndEmptyArrays: false } },
|
||||
{
|
||||
$match: {
|
||||
$or: searchConditions
|
||||
}
|
||||
},
|
||||
{ $sort: { [sortField]: sortOrder } },
|
||||
{ $skip: skip },
|
||||
{ $limit: rows }
|
||||
]
|
||||
|
||||
// const [data, totalDocuments] = await Promise.all([
|
||||
// Withdraw.aggregate(aggregationPipeline),
|
||||
// Withdraw.countDocuments(matchStage)
|
||||
// ])
|
||||
const countPipeline = [
|
||||
{ $match: matchStage },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'usergps',
|
||||
let: { userIdStr: '$userId' },
|
||||
pipeline: [
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$eq: ['$_id', { $toObjectId: '$$userIdStr' }]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
as: 'userId'
|
||||
}
|
||||
},
|
||||
{ $unwind: { path: '$userId', preserveNullAndEmptyArrays: false } },
|
||||
{
|
||||
$match: {
|
||||
$or: searchConditions
|
||||
}
|
||||
},
|
||||
{ $count: 'total' }
|
||||
]
|
||||
|
||||
const totalPages = Math.ceil(totalDocuments / rows)
|
||||
const [data, countResult] = await Promise.all([
|
||||
Withdraw.aggregate(aggregationPipeline),
|
||||
Withdraw.aggregate(countPipeline)
|
||||
])
|
||||
|
||||
res.status(200).json({ data, totalDocuments, totalPages })
|
||||
const totalDocuments = countResult[0]?.total || 0
|
||||
const totalPages = Math.ceil(totalDocuments / rows)
|
||||
|
||||
res.status(200).json({ data, totalDocuments, totalPages })
|
||||
} else {
|
||||
// No search - use simple find with populate
|
||||
const matchStage = {}
|
||||
|
||||
if (status !== undefined) {
|
||||
matchStage.status = status
|
||||
}
|
||||
|
||||
const data = await Withdraw.find(matchStage)
|
||||
.sort({ [sortField]: sortOrder })
|
||||
.skip(skip)
|
||||
.limit(rows)
|
||||
.populate('userId')
|
||||
|
||||
const totalDocuments = await Withdraw.countDocuments(matchStage)
|
||||
const totalPages = Math.ceil(totalDocuments / rows)
|
||||
|
||||
res.status(200).json({ data, totalDocuments, totalPages })
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'An error occurred, please try again.', error: error.message })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user