This commit is contained in:
Mr Swift
2024-10-01 15:31:05 +03:30
parent 283ba25a9d
commit 418ca4e0cf
12 changed files with 361 additions and 318 deletions
+82 -51
View File
@@ -49,71 +49,102 @@ module.exports.createRequest = [
module.exports.getAllForUser = async (req, res) => {
let page;
let rows;
if (!req.query?.rows || req.query?.rows <= 5) {
rows = 10
} else {
rows = parseInt(req.query.rows)
const rows = (!req.query?.rows || req.query?.rows <= 5) ? 10 : parseInt(req.query.rows);
let page = 0;
if (req.query?.page && req.query.page > 1) {
page = (req.query.page - 1) * rows;
}
if (!req.query?.page) {
page = 0
// eslint-disable-next-line eqeqeq
} else if (req.query.page <= 1) {
page = 0
} else {
page = req.query.page
page = page * rows - rows
const search = req.query?.search || null;
let sort = {};
if (req.query?.created_at) {
sort.created_at = parseInt(req.query.created_at);
}
const search = req.query?.search ? req.query.search : null
let sort
if(req.query?.sort){
sort= req.query?.sort
}else{
sort= { created_at: -1 }
if (req.query?.updated_at) {
sort.updated_at = parseInt(req.query.updated_at);
}
if (req.query?.amount) {
sort.amount = parseInt(req.query.amount);
}
if (req.query?.status) {
sort.status = parseInt(req.query.status);
}
if (Object.keys(sort).length === 0) {
sort = { created_at: 1 };
}
const filter = {
userId: req.user_id,
}
};
if(search){
filter.$or = [
if (search) {
filter.$or = [
{ amount: search },
{ dollarAmount: search },
{ trackingNumber: { $regex: search } },
]
{ trackingNumber: { $regex: search, $options: 'i' } },
];
}
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 })
}
try {
const data = await Withdraw.find(filter).skip(page).limit(rows).sort(sort);
const total = await Withdraw.countDocuments(filter);
res.status(200).json({ data, total });
} catch (error) {
res.status(500).json({ message: 'خطای سرور', error });
}
};
module.exports.getAllForAdmin = async (req, res) => {
// let page;
// let rows;
// if (!req.query?.rows || req.query?.rows <= 5) {
// rows = 10
// } else {
// rows = parseInt(req.query.rows)
// }
try {
const page = parseInt(req.query.page) || 1;
const rows = parseInt(req.query.rows) || 10;
const search = req.query.search || "";
const sortField = req.query.sortField || "status";
const sortOrder = req.query.sortOrder === "desc" ? -1 : 1;
const skip = (page - 1) * rows;
// if (!req.query?.page) {
// page = 0
// // eslint-disable-next-line eqeqeq
// } else if (req.query.page <= 1) {
// page = 0
// } else {
// page = req.query.page
// page = page * rows - rows
// }
const data = await Withdraw.find().sort({ status: 1 })
.populate('userId', 'shopName national_code first_name last_name mobile_number city_name province_name')
const total = Math.ceil((await Withdraw.estimatedDocumentCount()))
res.status(200).json({ data, total })
const matchStage = search ? {
$or: [
{ IMEI: new RegExp(search, 'i') },
{ trackingNumber: new RegExp(search, 'i') }
]
} : {};
const aggregationPipeline = [
{ $match: matchStage },
{
$lookup: {
from: 'users',
localField: 'userId',
foreignField: '_id',
as: 'userId'
}
},
{ $unwind: "$userId" },
{ $sort: { [sortField]: sortOrder } },
{ $skip: skip },
{ $limit: rows }
];
const [data, totalDocuments] = await Promise.all([
Withdraw.aggregate(aggregationPipeline),
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 });
}
}