add pagination and fix bug
This commit is contained in:
@@ -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 })
|
||||
}
|
||||
]
|
||||
@@ -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")
|
||||
})
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
|
||||
|
||||
+26
-26
@@ -1,41 +1,41 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const deviceSchema = mongoose.Schema({
|
||||
model:String,
|
||||
IMEI:{
|
||||
type:String,
|
||||
unique:[true,"این ایدی در سامانه موجود است"]
|
||||
model: String,
|
||||
IMEI: {
|
||||
type: String,
|
||||
unique: [true, "این ایدی در سامانه موجود است"]
|
||||
},
|
||||
tomanPrice:{
|
||||
type:Number,
|
||||
min:0,
|
||||
default:0
|
||||
tomanPrice: {
|
||||
type: Number,
|
||||
min: 0,
|
||||
default: 0
|
||||
},
|
||||
dollarProfit:{
|
||||
type:Number,
|
||||
min:0,
|
||||
default:0
|
||||
dollarProfit: {
|
||||
type: Number,
|
||||
min: 0,
|
||||
default: 0
|
||||
},
|
||||
profit:{
|
||||
type:Number,
|
||||
min:0,
|
||||
default:0
|
||||
profit: {
|
||||
type: Number,
|
||||
min: 0,
|
||||
default: 0
|
||||
},
|
||||
score:{
|
||||
type:Number,
|
||||
min:0
|
||||
score: {
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
renewalProfit:{
|
||||
type:Number,
|
||||
min:0
|
||||
renewalProfit: {
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
status:{
|
||||
type:Number,
|
||||
enum:[
|
||||
status: {
|
||||
type: Number,
|
||||
enum: [
|
||||
0, // not installed
|
||||
1 // installed
|
||||
],
|
||||
default:0
|
||||
default: 0
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const InstalledDeviceSchema = mongoose.Schema({
|
||||
deviceId:{
|
||||
type:String,
|
||||
ref:"Device"
|
||||
deviceId: {
|
||||
type: String,
|
||||
ref: "Device"
|
||||
},
|
||||
status:{
|
||||
type:Number,
|
||||
enum:[
|
||||
status: {
|
||||
type: Number,
|
||||
enum: [
|
||||
0, // Pending
|
||||
1, // Accepted
|
||||
2, // Rejected
|
||||
],
|
||||
default:0
|
||||
default: 0
|
||||
},
|
||||
registerDate:Date,
|
||||
installationDate:Date,
|
||||
userId:{
|
||||
type:String,
|
||||
ref:'User'
|
||||
registerDate: Date,
|
||||
installationDate: Date,
|
||||
userId: {
|
||||
type: String,
|
||||
ref: 'User'
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const RenewalSchema = mongoose.Schema({
|
||||
deviceId:{
|
||||
type:String,
|
||||
ref:'Device'
|
||||
deviceId: {
|
||||
type: String,
|
||||
ref: 'Device'
|
||||
},
|
||||
renewDate:Date,
|
||||
renewType:{
|
||||
type:String,
|
||||
renewDate: Date,
|
||||
renewType: {
|
||||
type: String,
|
||||
},
|
||||
price:{
|
||||
type:Number,
|
||||
min:0
|
||||
price: {
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
profit:{
|
||||
type:Number,
|
||||
min:0,
|
||||
unique:true,
|
||||
default:0,
|
||||
required:true
|
||||
profit: {
|
||||
type: Number,
|
||||
min: 0,
|
||||
unique: true,
|
||||
default: 0,
|
||||
required: true
|
||||
},
|
||||
dollarProfit:{
|
||||
type:Number,
|
||||
min:0
|
||||
dollarProfit: {
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
userId: {
|
||||
type: String,
|
||||
ref: 'User'
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
const mongoose = require('mongoose')
|
||||
|
||||
const RequestSchema = mongoose.Schema({
|
||||
userId:{
|
||||
type:String,
|
||||
ref:'User'
|
||||
userId: {
|
||||
type: String,
|
||||
ref: 'User'
|
||||
},
|
||||
shopName:String,
|
||||
shopNumber:String,
|
||||
shopAddress:String,
|
||||
birthDate:String,
|
||||
status:{
|
||||
type:Number,
|
||||
enum:[
|
||||
shopName: String,
|
||||
shopNumber: String,
|
||||
shopAddress: String,
|
||||
birthDate: String,
|
||||
status: {
|
||||
type: Number,
|
||||
enum: [
|
||||
0, // Pending
|
||||
1, // Accepted
|
||||
2 // Rejected
|
||||
],
|
||||
default:0
|
||||
default: 0
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -2,40 +2,40 @@ const mongoose = require('mongoose')
|
||||
|
||||
|
||||
const BankSchema = mongoose.Schema({
|
||||
holderName:String,
|
||||
PAN:{
|
||||
type:Number,
|
||||
holderName: String,
|
||||
PAN: {
|
||||
type: Number,
|
||||
},
|
||||
IBAN:{
|
||||
type:Number,
|
||||
IBAN: {
|
||||
type: Number,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
/// /////////////////////////////////////////////////////
|
||||
const WithdrawSchema = mongoose.Schema({
|
||||
card:BankSchema,
|
||||
amount:{
|
||||
type:Number,
|
||||
min:0
|
||||
card: BankSchema,
|
||||
amount: {
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
dollarAmount:{
|
||||
type:Number,
|
||||
min:0
|
||||
dollarAmount: {
|
||||
type: Number,
|
||||
min: 0
|
||||
},
|
||||
userId:{
|
||||
type:String,
|
||||
ref:'User'
|
||||
userId: {
|
||||
type: String,
|
||||
ref: 'User'
|
||||
},
|
||||
status:{
|
||||
type:Number,
|
||||
enum:[
|
||||
status: {
|
||||
type: Number,
|
||||
enum: [
|
||||
0, // Pending
|
||||
1, // Accepted
|
||||
2, // deposited
|
||||
]
|
||||
},
|
||||
depositDate:Date,
|
||||
trackingNumber:String
|
||||
depositDate: Date,
|
||||
trackingNumber: String
|
||||
})
|
||||
|
||||
module.exports = mongoose.model('Withdraw', WithdrawSchema)
|
||||
|
||||
@@ -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