Device Controller
This commit is contained in:
@@ -1,5 +1,136 @@
|
||||
const readXlsxFile = require('read-excel-file/node')
|
||||
const validator = require('validator')
|
||||
const { body, param, validationResult } = require('express-validator');
|
||||
const { _sr } = require('../plugins/serverResponses');
|
||||
const { checkValidations } = require('../plugins/controllersHelperFunctions');
|
||||
const User = require('../models/User');
|
||||
const { checkValidations, res500 } = require('../plugins/controllersHelperFunctions');
|
||||
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")
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user