add pagination and fix bug

This commit is contained in:
Mr Swift
2024-08-07 15:42:53 +03:30
parent 13f991eab2
commit db6c8d6681
13 changed files with 322 additions and 156 deletions
+12 -12
View File
@@ -13,22 +13,22 @@ module.exports.addCard = [
async (req, res) => {
const { holderName, PAN, IBAN } = req.body;
const userData = await User.findById(req.user_id).select('id cardBank')
if(userData?.cardBank && Array.isArray(userData?.cardBank)){
if (userData?.cardBank && Array.isArray(userData?.cardBank)) {
userData.cardBank.push({
holderName,
PAN,
IBAN
})
userData.save()
res.status(201).json({msg: 'با موفقیت ثبت شد ' })
}else{
})
userData.save()
res.status(201).json({ msg: 'با موفقیت ثبت شد ' })
} else {
userData.cardBank = [{
holderName,
PAN,
IBAN
}];
}];
userData.save()
res.status(201).json({msg: 'با موفقیت ثبت شد '})
res.status(201).json({ msg: 'با موفقیت ثبت شد ' })
}
}
]
@@ -50,15 +50,15 @@ module.exports.deleteCard = [
async (req, res) => {
const userData = await User.findOneAndUpdate(
{
_id:req.user_id,
"cardBank._id":req.params.id
_id: req.user_id,
"cardBank._id": req.params.id
},
{
$pull: {
cardBank: { _id: req.params.id }
$pull: {
cardBank: { _id: req.params.id }
}
}
).select('id cardBank')
res.status(200).json({msg: "با موفقیت حذف شد ", data:userData})
res.status(200).json({ msg: "با موفقیت حذف شد ", data: userData })
}
]
+44 -27
View File
@@ -38,8 +38,8 @@ module.exports.importExel = async (req, res) => {
const file = req.files.file
const exelFile = await readXlsxFile(Buffer.from(file.data));
const data = await creator(exelFile)
res.status(201).json({ msg: _sr.fa.response.success_save, data })
const data = await creator(exelFile)
res.status(201).json({ msg: _sr.fa.response.success_save, data })
} catch (error) {
@@ -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 })
}
]
@@ -107,31 +124,31 @@ module.exports.updateDevice = [
]
const creator = (exel)=>{
return new Promise (async(resolve, reject)=>{
for (const row of exel) {
// eslint-disable-next-line no-constant-condition
if (validator.isIMEI(row[0].toString().trim() )|| true) {
const device = await Device.findOne({ IMEI: row[0] })
if (device) {
console.warn("Repeated IMEI ", row[0])
const creator = (exel) => {
return new Promise(async (resolve, reject) => {
for (const row of exel) {
// eslint-disable-next-line no-constant-condition
if (validator.isIMEI(row[0].toString().trim()) || true) {
const device = await Device.findOne({ IMEI: row[0] })
if (device) {
console.warn("Repeated IMEI ", row[0])
} else {
await Device.create(
{
IMEI: row[0],
model: row[1],
tomanPrice: row[2],
dollarProfit: row[3] || 1,
profit: row[4] || 1,
score: row[5] || 1,
renewalProfit: row[6] || 1
}
)
}
} else {
await Device.create(
{
IMEI: row[0],
model: row[1],
tomanPrice: row[2],
dollarProfit: row[3] || 1,
profit: row[4] || 1,
score: row[5] || 1,
renewalProfit:row[6] || 1
}
)
console.warn("Wrong IMEI ", row[0])
}
} else {
console.warn("Wrong IMEI ", row[0])
}
}
resolve("end")
resolve("end")
})
}
+61 -19
View File
@@ -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;
}
+20 -2
View File
@@ -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 })
}
]
+32
View File
@@ -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 })
}
+39 -8
View File
@@ -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 })
}