add pagination and fix bug
This commit is contained in:
@@ -30,17 +30,48 @@ module.exports.createRequest = [
|
||||
|
||||
|
||||
module.exports.getAllForUser = async (req, res) => {
|
||||
|
||||
const requests = await Withdraw.find({ userId: req.user_id }).sort({ created_at: -1 })
|
||||
|
||||
res.status(201).json(requests)
|
||||
let page;
|
||||
let rows;
|
||||
if (!req.query?.rows) {
|
||||
rows = 10
|
||||
} else {
|
||||
rows = req.query.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({ userId: req.user_id }).sort({ created_at: -1 }).skip(page).limit(rows);
|
||||
const total = Math.ceil((await Withdraw.countDocuments({ userId: req.user_id })))
|
||||
res.status(201).json({ data, total })
|
||||
}
|
||||
|
||||
module.exports.getAllForAdmin = async (req, res) => {
|
||||
|
||||
const requests = await Withdraw.find().sort({ status: 1 }).populate('userId', 'shopName shopNumber shopAddress birthDate score first_name last_name profilePic')
|
||||
|
||||
res.status(201).json(requests)
|
||||
let page;
|
||||
let rows;
|
||||
if (!req.query?.rows) {
|
||||
rows = 10
|
||||
} else {
|
||||
rows = req.query.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 }).skip(page).limit(rows)
|
||||
.populate('userId', 'shopName shopNumber shopAddress birthDate score first_name last_name profilePic')
|
||||
const total = Math.ceil((await Withdraw.estimatedDocumentCount()))
|
||||
res.status(201).json({ data, total })
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user