Fix search bug

This commit is contained in:
Mr Swift
2024-08-26 00:07:19 +03:30
parent 11d8507e3c
commit 01a2871103
3 changed files with 57 additions and 27 deletions
+21 -10
View File
@@ -66,18 +66,29 @@ module.exports.getAllForUser = async (req, res) => {
page = req.query.page
page = page * rows - rows
}
// const search = req.query?.search ? req.query.search : 0
const search = req.query?.search ? req.query.search : null
let sort
if(req.query?.sort){
sort= req.query?.sort
}else{
sort= { created_at: -1 }
}
const data = await Withdraw.find({
const filter = {
userId: req.user_id,
// $or: [
// { amount: { $regex: search } },
// { dollarAmount: { $regex: search } },
// { trackingNumber: { $regex: search } },
// ],
}).skip(page).limit(rows).sort({ created_at: -1 });
const total = Math.ceil((await Withdraw.countDocuments({ userId: req.user_id })))
}
if(search){
filter.$or = [
{ amount: search },
{ dollarAmount: search },
{ trackingNumber: { $regex: search } },
]
}
const data = await Withdraw.find(filter).skip(page).limit(rows).sort(sort);
const total = Math.ceil((await Withdraw.countDocuments(filter)))
res.status(200).json({ data, total })
}