Device Controller
This commit is contained in:
Generated
+4
-3
@@ -38,6 +38,7 @@
|
|||||||
"nuxt-leaflet": "^0.0.27",
|
"nuxt-leaflet": "^0.0.27",
|
||||||
"read-excel-file": "^5.2.2",
|
"read-excel-file": "^5.2.2",
|
||||||
"socket.io": "^4.4.1",
|
"socket.io": "^4.4.1",
|
||||||
|
"validator": "^13.12.0",
|
||||||
"vue-persian-datetime-picker": "^2.10.3"
|
"vue-persian-datetime-picker": "^2.10.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -20668,9 +20669,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/validator": {
|
"node_modules/validator": {
|
||||||
"version": "13.11.0",
|
"version": "13.12.0",
|
||||||
"resolved": "https://registry.npmjs.org/validator/-/validator-13.11.0.tgz",
|
"resolved": "https://registry.npmjs.org/validator/-/validator-13.12.0.tgz",
|
||||||
"integrity": "sha512-Ii+sehpSfZy+At5nPdnyMhx78fEoPDkR2XW/zimHEL3MyGJQOCQ7WeP20jPYRz7ZCpcKLB21NxuXHF3bxjStBQ==",
|
"integrity": "sha512-c1Q0mCiPlgdTVVVIJIrBuxNicYE+t/7oKeI9MWLj3fh/uq2Pxh/3eeWbVZ4OcGW1TUf53At0njHw5SMdA3tmMg==",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.10"
|
"node": ">= 0.10"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,7 @@
|
|||||||
"nuxt-leaflet": "^0.0.27",
|
"nuxt-leaflet": "^0.0.27",
|
||||||
"read-excel-file": "^5.2.2",
|
"read-excel-file": "^5.2.2",
|
||||||
"socket.io": "^4.4.1",
|
"socket.io": "^4.4.1",
|
||||||
|
"validator": "^13.12.0",
|
||||||
"vue-persian-datetime-picker": "^2.10.3"
|
"vue-persian-datetime-picker": "^2.10.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,5 +1,136 @@
|
|||||||
|
const readXlsxFile = require('read-excel-file/node')
|
||||||
|
const validator = require('validator')
|
||||||
const { body, param, validationResult } = require('express-validator');
|
const { body, param, validationResult } = require('express-validator');
|
||||||
const { _sr } = require('../plugins/serverResponses');
|
const { _sr } = require('../plugins/serverResponses');
|
||||||
const { checkValidations } = require('../plugins/controllersHelperFunctions');
|
const { checkValidations, res500 } = require('../plugins/controllersHelperFunctions');
|
||||||
const User = require('../models/User');
|
|
||||||
const Device = require('../models/GPS.Device');
|
const Device = require('../models/GPS.Device');
|
||||||
|
|
||||||
|
module.exports.addCode = [
|
||||||
|
[
|
||||||
|
body('model').notEmpty().isString().withMessage(_sr.fa.required.name),
|
||||||
|
body('IMEI').notEmpty().isIMEI().withMessage('لطفا IMEI را وارد یا تصحیح کنید').custom(async (v) => {
|
||||||
|
const device = await Device.findOne({ IMEI: v })
|
||||||
|
if (device) {
|
||||||
|
return Promise.reject(_sr.fa.duplicated.IMEI)
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
body('tomanPrice').notEmpty().isInt({ min: 0 }).withMessage("قیمت دستگاه را به تومتن وارد کنید"),
|
||||||
|
body('dollarProfit').notEmpty().isInt({ min: 0 }).withMessage("سود نصاب را به دلار وارد کنید"),
|
||||||
|
body('profit').notEmpty().isInt({ min: 0 }).withMessage("سود نصاب را به تومان وارد کنید"),
|
||||||
|
body('score').notEmpty().isInt({ min: 0 }).withMessage("امتیاز دریافتی نصاب را وارد کنید"),
|
||||||
|
body('renewalProfit').notEmpty().isInt({ min: 0 }).withMessage("سود دریافتی نصاب را از تمدید اشتراک وارد کنید"),
|
||||||
|
],
|
||||||
|
checkValidations(validationResult),
|
||||||
|
async (req, res) => {
|
||||||
|
const data = await Device.create(req.body)
|
||||||
|
res.status(201).json({ msg: _sr.fa.response.success_save, data })
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.importExel = async (req, res) => {
|
||||||
|
try {
|
||||||
|
if (!req.files?.file) return res.status(422).json({ validation: { file: { msg: 'فایل را اضافه کنید' } } })
|
||||||
|
if (!req.files?.file.name.split('.').some(item => ['xlsx', 'xltx', 'xlsm', 'xlsb'].includes(item))) return res.status(422).json({ validation: { file: { msg: 'فرمت فایل درست نمیباشد' } } })
|
||||||
|
const file = req.files.file
|
||||||
|
const exelFile = await readXlsxFile(file);
|
||||||
|
|
||||||
|
const data = await creator(exelFile)
|
||||||
|
res.status(201).json({ msg: _sr.fa.response.success_save, data })
|
||||||
|
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
res500(res, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.getAll = [
|
||||||
|
async (req, res) => {
|
||||||
|
const data = await Device.find().sort({ created_at: -1 })
|
||||||
|
res.status(200).json(data)
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
module.exports.getOne = [
|
||||||
|
[
|
||||||
|
param('id').notEmpty().isMongoId().withMessage('ایدی اشتباه است')
|
||||||
|
],
|
||||||
|
checkValidations(validationResult),
|
||||||
|
async (req, res) => {
|
||||||
|
const data = await Device.findById(req.params.id).sort({ created_at: -1 })
|
||||||
|
if (!data) {
|
||||||
|
res.status(404).json(_sr.fa.not_found.item_id)
|
||||||
|
} else {
|
||||||
|
res.status(200).json(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.updateDevice = [
|
||||||
|
[
|
||||||
|
param('id').notEmpty().isMongoId().withMessage('ایدی اشتباه است').custom(async (v) => {
|
||||||
|
const device = await Device.findById(v)
|
||||||
|
if (device) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return Promise.reject(_sr.fa.not_found.item_id)
|
||||||
|
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
body('model').notEmpty().isString().withMessage(_sr.fa.required.name),
|
||||||
|
body('tomanPrice').notEmpty().isInt({ min: 0 }).withMessage("قیمت دستگاه را به تومتن وارد کنید"),
|
||||||
|
body('dollarProfit').notEmpty().isInt({ min: 0 }).withMessage("سود نصاب را به دلار وارد کنید"),
|
||||||
|
body('profit').notEmpty().isInt({ min: 0 }).withMessage("سود نصاب را به تومان وارد کنید"),
|
||||||
|
body('score').notEmpty().isInt({ min: 0 }).withMessage("امتیاز دریافتی نصاب را وارد کنید"),
|
||||||
|
body('renewalProfit').notEmpty().isInt({ min: 0 }).withMessage("سود دریافتی نصاب را از تمدید اشتراک وارد کنید"),
|
||||||
|
|
||||||
|
],
|
||||||
|
checkValidations(validationResult),
|
||||||
|
async (req, res) => {
|
||||||
|
const { model, tomanPrice, dollarProfit, profit, score, renewalProfit } = req.body;
|
||||||
|
const data = await Device.findByIdAndUpdate(
|
||||||
|
req.params.id,
|
||||||
|
{
|
||||||
|
model, tomanPrice, dollarProfit, profit, score, renewalProfit
|
||||||
|
},
|
||||||
|
{ new: true }
|
||||||
|
)
|
||||||
|
res.status(200).json({ msg: _sr.fa.response.success_save, data })
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
const creator = (exel)=>{
|
||||||
|
return new Promise (async(resolve, reject)=>{
|
||||||
|
for (const row of exel) {
|
||||||
|
if (validator.isIMEI(row[0])) {
|
||||||
|
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 {
|
||||||
|
console.warn("Wrong IMEI ", row[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resolve("end")
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -68,7 +68,7 @@ module.exports.updateInstalled = [
|
|||||||
device.status = 1;
|
device.status = 1;
|
||||||
device.save()
|
device.save()
|
||||||
user.walletBalance += device.profit
|
user.walletBalance += device.profit
|
||||||
user.dollarBalance += device.dollarPrice
|
user.dollarBalance += device.dollarProfit
|
||||||
user.score += device.score
|
user.score += device.score
|
||||||
user.save()
|
user.save()
|
||||||
res.status(201).json({ msg: _sr.fa.response.success_save, data: installedDevice })
|
res.status(201).json({ msg: _sr.fa.response.success_save, data: installedDevice })
|
||||||
|
|||||||
@@ -4,14 +4,14 @@ const deviceSchema = mongoose.Schema({
|
|||||||
model:String,
|
model:String,
|
||||||
IMEI:{
|
IMEI:{
|
||||||
type:String,
|
type:String,
|
||||||
unique:true
|
unique:[true,"این ایدی در سامانه موجود است"]
|
||||||
},
|
},
|
||||||
dollarPrice:{
|
tomanPrice:{
|
||||||
type:Number,
|
type:Number,
|
||||||
min:0,
|
min:0,
|
||||||
default:0
|
default:0
|
||||||
},
|
},
|
||||||
tomanPrice:{
|
dollarProfit:{
|
||||||
type:Number,
|
type:Number,
|
||||||
min:0,
|
min:0,
|
||||||
default:0
|
default:0
|
||||||
@@ -25,6 +25,10 @@ const deviceSchema = mongoose.Schema({
|
|||||||
type:Number,
|
type:Number,
|
||||||
min:0
|
min:0
|
||||||
},
|
},
|
||||||
|
renewalProfit:{
|
||||||
|
type:Number,
|
||||||
|
min:0
|
||||||
|
},
|
||||||
status:{
|
status:{
|
||||||
type:Number,
|
type:Number,
|
||||||
enum:[
|
enum:[
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
const mongoose = require('mongoose')
|
const mongoose = require('mongoose')
|
||||||
|
|
||||||
const ScoreSchema = mongoose.Schema({
|
const RenewalSchema = mongoose.Schema({
|
||||||
deviceId:{
|
deviceId:{
|
||||||
type:String,
|
type:String,
|
||||||
ref:'Device'
|
ref:'Device'
|
||||||
@@ -26,4 +26,4 @@ const ScoreSchema = mongoose.Schema({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = mongoose.model('Score', ScoreSchema)
|
module.exports = mongoose.model('Renewal', RenewalSchema)
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const readXlsxFile = require('read-excel-file/node')
|
const readXlsxFile = require('read-excel-file/node')
|
||||||
|
|
||||||
const guaranteeSerialsPath = './static/uploads/serials'
|
const guaranteeSerialsPath = './static/uploads/serials'
|
||||||
const productSerialsPath = './static/uploads/serials_products'
|
const productSerialsPath = './static/uploads/serials_products'
|
||||||
|
|
||||||
|
|||||||
@@ -108,7 +108,8 @@ module.exports._sr = {
|
|||||||
email: 'این ایمیل از قبل وجود دارد',
|
email: 'این ایمیل از قبل وجود دارد',
|
||||||
name: 'نام تکراری است',
|
name: 'نام تکراری است',
|
||||||
title: 'عنوان تکراری است',
|
title: 'عنوان تکراری است',
|
||||||
phone_number: 'این شماره تماس از قبل وجود دارد'
|
phone_number: 'این شماره تماس از قبل وجود دارد',
|
||||||
|
IMEI:'این IMEI تکراری است'
|
||||||
},
|
},
|
||||||
response: {
|
response: {
|
||||||
logged_in: 'شما وارد سیستم شدید',
|
logged_in: 'شما وارد سیستم شدید',
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ const brandController = require('../controllers/brandController')
|
|||||||
const requestController = require('../controllers/GPS.Request')
|
const requestController = require('../controllers/GPS.Request')
|
||||||
const withdraw = require('../controllers/GPS.Withdraw')
|
const withdraw = require('../controllers/GPS.Withdraw')
|
||||||
const installedDevice = require('../controllers/GPS.InstalledDevice')
|
const installedDevice = require('../controllers/GPS.InstalledDevice')
|
||||||
|
const device = require('../controllers/GPS.Device')
|
||||||
|
|
||||||
|
|
||||||
/// //////////////////////////////////////////////////////// routes
|
/// //////////////////////////////////////////////////////// routes
|
||||||
@@ -49,6 +50,13 @@ router.patch('/gps/withdraw/:id/:type',hasPermission('gps'), withdraw.updateReq)
|
|||||||
router.patch('/register-device/:id/:type', installedDevice.updateInstalled)
|
router.patch('/register-device/:id/:type', installedDevice.updateInstalled)
|
||||||
router.get('/register-device', installedDevice.getAllForAdmin)
|
router.get('/register-device', installedDevice.getAllForAdmin)
|
||||||
|
|
||||||
|
/// ////////////// Device
|
||||||
|
router.post('/gps/device',hasPermission('gps'), device.addCode)
|
||||||
|
router.post('/gps/device/exel',hasPermission('gps'), device.importExel)
|
||||||
|
router.get('/gps/device',hasPermission('gps'), device.getAll)
|
||||||
|
router.get('/gps/device/:id',hasPermission('gps'), device.getOne)
|
||||||
|
router.put('/gps/device/:id',hasPermission('gps'), device.updateDevice)
|
||||||
|
|
||||||
/// /////////////////////////////////////////////////////////
|
/// /////////////////////////////////////////////////////////
|
||||||
/// ///////////// survey
|
/// ///////////// survey
|
||||||
router.get('/surveys',hasPermission('surveys'), surveyController.getAllForAdmin)
|
router.get('/surveys',hasPermission('surveys'), surveyController.getAllForAdmin)
|
||||||
|
|||||||
Reference in New Issue
Block a user