136 lines
4.8 KiB
JavaScript
136 lines
4.8 KiB
JavaScript
const { body, param, validationResult } = require('express-validator');
|
|
const { _sr } = require('../plugins/serverResponses');
|
|
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 = [
|
|
[
|
|
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) => {
|
|
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) => {
|
|
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 })
|
|
}
|
|
|
|
|
|
|
|
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.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;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
|