Installed Device
This commit is contained in:
@@ -5,14 +5,96 @@ const User = require('../models/User');
|
|||||||
const InstalledDevice = require('../models/GPS.InstalledDevice');
|
const InstalledDevice = require('../models/GPS.InstalledDevice');
|
||||||
const Device = require('../models/GPS.Device');
|
const Device = require('../models/GPS.Device');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get all installed device for user
|
||||||
|
* get all installed device for admin
|
||||||
|
* patch a installed device by id and type
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
module.exports.createRequest = [
|
module.exports.deviceRegistration = [
|
||||||
[
|
[
|
||||||
|
body('IMEI').notEmpty().isString().withMessage('لطفا IMEI را وارد یا تصحیح کنید')
|
||||||
],
|
],
|
||||||
checkValidations(validationResult),
|
checkValidations(validationResult),
|
||||||
async (req, res) => {
|
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 قبلا ثبت شده'})
|
||||||
|
|
||||||
|
}else{
|
||||||
|
const data = {
|
||||||
|
deviceId: device.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})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
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.getAllForAdmin = async(req, res)=>{
|
||||||
|
const data = await InstalledDevice.find({}).sort({status: 1}).populate('deviceId userId')
|
||||||
|
res.status(200).json(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.updateInstalled = [
|
||||||
|
[
|
||||||
|
param('id').notEmpty().isMongoId().withMessage('لطفا ایدی را وارد یا تصحیح کنید'),
|
||||||
|
param('type').notEmpty().isString().withMessage('لطفا وضعیت را وارد یا تصحیح کنید'),
|
||||||
|
],
|
||||||
|
checkValidations(validationResult),
|
||||||
|
async (req, res) => {
|
||||||
|
const installedDevice = await InstalledDevice.findById(req.params.id)
|
||||||
|
if (!installedDevice) {
|
||||||
|
throw res.status(404).json({msg:'این ایدی وجود ندارد'})
|
||||||
|
} else {
|
||||||
|
const user = await User.findById(installedDevice.userID)
|
||||||
|
const device = await Device.findById(installedDevice.deviceId)
|
||||||
|
switch (req.params.type) {
|
||||||
|
case "accept": {
|
||||||
|
installedDevice.status = 1;
|
||||||
|
installedDevice.installationDate = new Date();
|
||||||
|
installedDevice.save()
|
||||||
|
device.status = 1;
|
||||||
|
device.save()
|
||||||
|
user.walletBalance += device.profit
|
||||||
|
user.dollarBalance += device.dollarPrice
|
||||||
|
user.score += device.score
|
||||||
|
user.save()
|
||||||
|
res.status(201).json({ msg: _sr.fa.response.success_save, data: installedDevice })
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "reject": {
|
||||||
|
installedDevice.status = 2;
|
||||||
|
installedDevice.save()
|
||||||
|
device.status = 0;
|
||||||
|
device.save()
|
||||||
|
res.status(201).json({ msg: _sr.fa.response.success_save, data: installedDevice })
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
res.status(404).json({ msg: "استاتوس اشتباه است" })
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,25 @@ const deviceSchema = mongoose.Schema({
|
|||||||
type:String,
|
type:String,
|
||||||
unique:true
|
unique:true
|
||||||
},
|
},
|
||||||
|
dollarPrice:{
|
||||||
|
type:Number,
|
||||||
|
min:0,
|
||||||
|
default:0
|
||||||
|
},
|
||||||
|
tomanPrice:{
|
||||||
|
type:Number,
|
||||||
|
min:0,
|
||||||
|
default:0
|
||||||
|
},
|
||||||
|
profit:{
|
||||||
|
type:Number,
|
||||||
|
min:0,
|
||||||
|
default:0
|
||||||
|
},
|
||||||
|
score:{
|
||||||
|
type:Number,
|
||||||
|
min:0
|
||||||
|
},
|
||||||
status:{
|
status:{
|
||||||
type:Number,
|
type:Number,
|
||||||
enum:[
|
enum:[
|
||||||
|
|||||||
@@ -11,15 +11,8 @@ const InstalledDeviceSchema = mongoose.Schema({
|
|||||||
0, // Pending
|
0, // Pending
|
||||||
1, // Accepted
|
1, // Accepted
|
||||||
2, // Rejected
|
2, // Rejected
|
||||||
]
|
],
|
||||||
},
|
default:0
|
||||||
dollarProfit:{
|
|
||||||
type:Number,
|
|
||||||
min:0
|
|
||||||
},
|
|
||||||
score:{
|
|
||||||
type:Number,
|
|
||||||
min:0
|
|
||||||
},
|
},
|
||||||
registerDate:Date,
|
registerDate:Date,
|
||||||
installationDate:Date,
|
installationDate:Date,
|
||||||
|
|||||||
Reference in New Issue
Block a user