add pagination and fix bug
This commit is contained in:
@@ -4,6 +4,7 @@ const { checkValidations } = require('../plugins/controllersHelperFunctions');
|
||||
const User = require('../models/User');
|
||||
const InstalledDevice = require('../models/GPS.InstalledDevice');
|
||||
const Device = require('../models/GPS.Device');
|
||||
const Score = require('../models/GPS.Score');
|
||||
|
||||
module.exports.deviceRegistration = [
|
||||
[
|
||||
@@ -11,38 +12,74 @@ module.exports.deviceRegistration = [
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const device = await Device.find({IMEI:req.body.IMEI});
|
||||
if(!device){
|
||||
throw res.status(404).json({msg:'این IMEI وجود ندارد'})
|
||||
// eslint-disable-next-line eqeqeq
|
||||
}else if(device.status == 1){
|
||||
throw res.status(404).json({msg:'این IMEI قبلا ثبت شده'})
|
||||
const device = await Device.find({ IMEI: req.body.IMEI });
|
||||
if (!device) {
|
||||
throw res.status(404).json({ msg: 'این IMEI وجود ندارد' })
|
||||
// eslint-disable-next-line eqeqeq
|
||||
} else if (device.status == 1) {
|
||||
throw res.status(404).json({ msg: 'این IMEI قبلا ثبت شده' })
|
||||
|
||||
}else{
|
||||
} else {
|
||||
const data = {
|
||||
deviceId: device.id,
|
||||
status:0,
|
||||
registerDate:new Date(),
|
||||
userId:req.user_id
|
||||
status: 0,
|
||||
registerDate: new Date(),
|
||||
userId: req.user_id
|
||||
}
|
||||
const newRequest = await InstalledDevice.create(data);
|
||||
device.status = 1;
|
||||
device.save()
|
||||
res.status(201).json({msg:_sr.fa.response.success_save, data:newRequest})
|
||||
res.status(201).json({ msg: _sr.fa.response.success_save, data: newRequest })
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
module.exports.getAllForUser = async(req, res)=>{
|
||||
const data = await InstalledDevice.find({userId:req.user_id}).sort({created_at: -1}).populate('deviceId')
|
||||
res.status(200).json(data)
|
||||
|
||||
module.exports.getAllForUser = async (req, res) => {
|
||||
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 InstalledDevice.find({ userId: req.user_id }).sort({ created_at: -1 }).populate('deviceId').skip(page).limit(rows);
|
||||
const total = Math.ceil((await InstalledDevice.countDocuments({ userId: req.user_id })))
|
||||
res.status(200).json({ data, total })
|
||||
|
||||
}
|
||||
|
||||
module.exports.getAllForAdmin = async(req, res)=>{
|
||||
const data = await InstalledDevice.find({}).sort({status: 1}).populate('deviceId').populate('userId', 'shopName shopNumber shopAddress birthDate score first_name last_name profilePic')
|
||||
res.status(200).json(data)
|
||||
module.exports.getAllForAdmin = async (req, res) => {
|
||||
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 InstalledDevice.find({}).sort({ status: 1 }).skip(page).limit(rows)
|
||||
.populate('deviceId')
|
||||
.populate('userId', 'shopName shopNumber shopAddress birthDate score first_name last_name profilePic')
|
||||
const total = Math.ceil((await InstalledDevice.estimatedDocumentCount()))
|
||||
res.status(200).json({ data, total })
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +93,7 @@ module.exports.updateInstalled = [
|
||||
async (req, res) => {
|
||||
const installedDevice = await InstalledDevice.findById(req.params.id)
|
||||
if (!installedDevice) {
|
||||
throw res.status(404).json({msg:'این ایدی وجود ندارد'})
|
||||
throw res.status(404).json({ msg: 'این ایدی وجود ندارد' })
|
||||
} else {
|
||||
const user = await User.findById(installedDevice.userID)
|
||||
const device = await Device.findById(installedDevice.deviceId)
|
||||
@@ -71,6 +108,11 @@ module.exports.updateInstalled = [
|
||||
user.dollarBalance += device.dollarProfit
|
||||
user.score += device.score
|
||||
user.save()
|
||||
await Score.create({
|
||||
deviceId: device.id,
|
||||
score: device.score,
|
||||
userId: installedDevice.userID
|
||||
})
|
||||
res.status(201).json({ msg: _sr.fa.response.success_save, data: installedDevice })
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user