add pagination and fix bug
This commit is contained in:
@@ -51,8 +51,25 @@ module.exports.importExel = async (req, res) => {
|
||||
|
||||
module.exports.getAll = [
|
||||
async (req, res) => {
|
||||
const data = await Device.find().sort({ created_at: -1 })
|
||||
res.status(200).json(data)
|
||||
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 Device.find().sort({ created_at: -1 }).skip(page).limit(rows);
|
||||
const total = Math.ceil((await Device.estimatedDocumentCount()))
|
||||
res.status(200).json({ data, total })
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@@ -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 = [
|
||||
[
|
||||
@@ -35,14 +36,50 @@ module.exports.deviceRegistration = [
|
||||
|
||||
|
||||
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)
|
||||
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)
|
||||
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 })
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -35,8 +35,26 @@ module.exports.gpsRequest = [
|
||||
|
||||
module.exports.getAllRequest = [
|
||||
async (req, res) => {
|
||||
const data = await RequestGPS.find({}).sort({ status: 1 }).populate('userId', 'shopName shopNumber shopAddress birthDate score first_name last_name profilePic')
|
||||
res.status(200).json(data)
|
||||
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 RequestGPS.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 RequestGPS.estimatedDocumentCount()))
|
||||
res.status(200).json({ data, total })
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// const { body, param, validationResult } = require('express-validator')
|
||||
// const { _sr } = require('../plugins/serverResponses')
|
||||
// const { checkValidations } = require('../plugins/controllersHelperFunctions')
|
||||
const Score = require('../models/GPS.Score')
|
||||
const User = require('../models/User')
|
||||
|
||||
|
||||
|
||||
module.exports.getAll = 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 score = await Score.find({ userId: req.user_id }).skip(page).limit(rows);
|
||||
const total = Math.ceil((await score.estimatedDocumentCount()))
|
||||
|
||||
const user = await User.findById(req.user_id).select('score')
|
||||
res.status(200).json({ data: score, score: user.score, total })
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -24,6 +24,10 @@ const RenewalSchema = mongoose.Schema({
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
userId: {
|
||||
type: String,
|
||||
ref: 'User'
|
||||
},
|
||||
})
|
||||
|
||||
module.exports = mongoose.model('Renewal', RenewalSchema)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const ScoreSchema = mongoose.Schema({
|
||||
deviceId: {
|
||||
type: String,
|
||||
ref: 'Device'
|
||||
},
|
||||
score: {
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
userId: {
|
||||
type: String,
|
||||
ref: 'User'
|
||||
},
|
||||
})
|
||||
|
||||
module.exports = mongoose.model('Score', ScoreSchema)
|
||||
@@ -3,6 +3,7 @@ const router = Router()
|
||||
const bankAccount = require('../controllers/GPS.BankAccount')
|
||||
const withdraw = require('../controllers/GPS.Withdraw')
|
||||
const installedDevice = require('../controllers/GPS.InstalledDevice')
|
||||
const score = require('../controllers/GPS.Score')
|
||||
|
||||
/// ///////////////// Bank card
|
||||
router.post('/bank-card', bankAccount.addCard)
|
||||
@@ -16,4 +17,7 @@ router.get('/withdraw', withdraw.getAllForUser)
|
||||
router.post('/register-device', installedDevice.deviceRegistration)
|
||||
router.get('/register-device', installedDevice.getAllForUser)
|
||||
|
||||
/// ///////////////// Score
|
||||
router.get('/score', score.getAll)
|
||||
|
||||
module.exports = router
|
||||
|
||||
Reference in New Issue
Block a user