154 lines
6.0 KiB
JavaScript
154 lines
6.0 KiB
JavaScript
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, 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().trim().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(Buffer.from(file.data));
|
|
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) => {
|
|
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 Device.find().sort({ created_at: -1 }).skip(page).limit(rows);
|
|
const total = Math.ceil((await Device.estimatedDocumentCount()))
|
|
res.status(200).json({ data, total })
|
|
}
|
|
]
|
|
|
|
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) {
|
|
// eslint-disable-next-line no-constant-condition
|
|
if (validator.isIMEI(row[0].toString().trim()) || true) {
|
|
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")
|
|
})
|
|
} |