From 8f17162b112b95f489131dd352c1fd6c2499c905 Mon Sep 17 00:00:00 2001 From: Mr Swift Date: Tue, 6 Aug 2024 19:03:52 +0330 Subject: [PATCH] Installed Device --- server/controllers/GPS.InstalledDevice.js | 86 ++++++++++++++++++++++- server/models/GPS.Device.js | 19 +++++ server/models/GPS.InstalledDevice.js | 11 +-- 3 files changed, 105 insertions(+), 11 deletions(-) diff --git a/server/controllers/GPS.InstalledDevice.js b/server/controllers/GPS.InstalledDevice.js index 0818d8d..b718f75 100644 --- a/server/controllers/GPS.InstalledDevice.js +++ b/server/controllers/GPS.InstalledDevice.js @@ -5,14 +5,96 @@ const User = require('../models/User'); const InstalledDevice = require('../models/GPS.InstalledDevice'); 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), 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; + } + } + } + } +] + diff --git a/server/models/GPS.Device.js b/server/models/GPS.Device.js index ca5c6b9..f9db1e3 100644 --- a/server/models/GPS.Device.js +++ b/server/models/GPS.Device.js @@ -6,6 +6,25 @@ const deviceSchema = mongoose.Schema({ type:String, 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:{ type:Number, enum:[ diff --git a/server/models/GPS.InstalledDevice.js b/server/models/GPS.InstalledDevice.js index e349cfe..74ee922 100644 --- a/server/models/GPS.InstalledDevice.js +++ b/server/models/GPS.InstalledDevice.js @@ -11,15 +11,8 @@ const InstalledDeviceSchema = mongoose.Schema({ 0, // Pending 1, // Accepted 2, // Rejected - ] - }, - dollarProfit:{ - type:Number, - min:0 - }, - score:{ - type:Number, - min:0 + ], + default:0 }, registerDate:Date, installationDate:Date,