installed device : add pagination and fix search bug
This commit is contained in:
@@ -182,34 +182,53 @@ module.exports.getAllForUser = async (req, res) => {
|
||||
|
||||
module.exports.getAllForAdmin = async (req, res) => {
|
||||
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 search = req.query.search || ''
|
||||
const filterType = req.query.filter || 'all'
|
||||
const page = parseInt(req.query.page) || 1
|
||||
const rows = parseInt(req.query.rows) || 10
|
||||
const skip = (page - 1) * rows
|
||||
|
||||
// const skip = (page - 1) * rows;
|
||||
// Build status filter
|
||||
const filter = {}
|
||||
if (filterType !== 'all') {
|
||||
let statusValue
|
||||
if (filterType === 'rej') statusValue = 2
|
||||
else if (filterType === 'accept') statusValue = 1
|
||||
else if (filterType === 'pend') statusValue = 0
|
||||
|
||||
if (statusValue !== undefined) {
|
||||
filter.status = statusValue
|
||||
}
|
||||
}
|
||||
|
||||
// const filter = {};
|
||||
// if (search) {
|
||||
// filter.$or = [
|
||||
// { IMEI: new RegExp(search, 'i') },
|
||||
// { 'userId.shopName': new RegExp(search, 'i') },
|
||||
// { 'userId.national_code': new RegExp(search, 'i') },
|
||||
// ];
|
||||
// }
|
||||
|
||||
const data = await InstalledDevice.find()
|
||||
// Fetch data with status filter (search will be done after populate)
|
||||
let data = await InstalledDevice.find(filter)
|
||||
.sort({ created_at: -1 })
|
||||
.populate('deviceId')
|
||||
.populate('userId', 'shopName national_code first_name last_name mobile_number city_name province_name')
|
||||
// .skip(skip)
|
||||
// .limit(rows);
|
||||
|
||||
// const totalDocuments = await InstalledDevice.countDocuments(filter);
|
||||
// const totalPages = Math.ceil(totalDocuments / rows);
|
||||
// Filter by IMEI and deviceId.model if search is provided (needs to be done after populate)
|
||||
if (search) {
|
||||
const searchLower = search.toLowerCase()
|
||||
data = data.filter(item => {
|
||||
const imeiMatch = item.IMEI && item.IMEI.toLowerCase().includes(searchLower)
|
||||
const modelMatch = item.deviceId && item.deviceId.model && item.deviceId.model.toLowerCase().includes(searchLower)
|
||||
return imeiMatch || modelMatch
|
||||
})
|
||||
}
|
||||
|
||||
res.status(200).json({ data })
|
||||
// Calculate pagination
|
||||
const totalDocuments = data.length
|
||||
const totalPages = Math.ceil(totalDocuments / rows)
|
||||
|
||||
// Apply pagination
|
||||
const paginatedData = data.slice(skip, skip + rows)
|
||||
|
||||
res.status(200).json({
|
||||
data: paginatedData,
|
||||
totalDocuments,
|
||||
totalPages
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message)
|
||||
res.status(500).json({ message: 'Server Error', error: error.message })
|
||||
|
||||
Reference in New Issue
Block a user