feat: add route to update and add cardbank and fix
This commit is contained in:
+174
-182
@@ -1,212 +1,204 @@
|
||||
const readXlsxFile = require('read-excel-file/node')
|
||||
const { body, param, validationResult } = require('express-validator');
|
||||
const xlsx = require("json-as-xlsx")
|
||||
const { _sr } = require('../plugins/serverResponses');
|
||||
const { checkValidations, res500 } = require('../plugins/controllersHelperFunctions');
|
||||
const Device = require('../models/GPS.Device');
|
||||
const { body, param, validationResult } = require('express-validator')
|
||||
const xlsx = require('json-as-xlsx')
|
||||
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 })
|
||||
res.status(201).json(data)
|
||||
|
||||
}
|
||||
[
|
||||
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 })
|
||||
res.status(201).json(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 })
|
||||
res.status(201).json(data)
|
||||
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res500(res, error)
|
||||
}
|
||||
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 })
|
||||
res.status(201).json(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)
|
||||
}
|
||||
async (req, res) => {
|
||||
const data = await Device.find().sort({ created_at: -1 })
|
||||
res.status(200).json(data)
|
||||
}
|
||||
]
|
||||
|
||||
module.exports.downloadAll = [
|
||||
async (req, res) => {
|
||||
async (req, res) => {
|
||||
const device = await Device.find().sort({ created_at: -1 })
|
||||
const data = [
|
||||
{
|
||||
sheet: 'device',
|
||||
columns: [
|
||||
{ label: 'IMEI', value: 'IMEI' },
|
||||
{ label: 'مدل', value: 'model' },
|
||||
{ label: 'قیمت به تومان', value: 'tomanPrice' },
|
||||
{ label: 'سود به دلار', value: 'dollarProfit' },
|
||||
{ label: 'سود به تومان', value: 'profit' },
|
||||
{ label: 'امتیاز', value: 'score' },
|
||||
{ label: 'سود از تمدید', value: 'renewalProfit' },
|
||||
{ label: 'وضعیت نصب', value: 'status' }
|
||||
],
|
||||
content: device
|
||||
}
|
||||
]
|
||||
|
||||
const device = await Device.find().sort({ created_at: -1 })
|
||||
const data = [
|
||||
{
|
||||
sheet: "device",
|
||||
columns: [
|
||||
{ label: "IMEI", value: "IMEI" },
|
||||
{ label: "مدل", value: "model" },
|
||||
{ label: "قیمت به تومان", value: "tomanPrice" },
|
||||
{ label: "سود به دلار", value: "dollarProfit" },
|
||||
{ label: "سود به تومان", value: "profit" },
|
||||
{ label: "امتیاز", value: "score" },
|
||||
{ label: "سود از تمدید", value: "renewalProfit" },
|
||||
{ label: "وضعیت نصب", value: "status" },
|
||||
],
|
||||
content: device,
|
||||
}
|
||||
]
|
||||
|
||||
const settings = {
|
||||
writeOptions: {
|
||||
type: "buffer",
|
||||
bookType: "csv",
|
||||
},
|
||||
RTL: true
|
||||
}
|
||||
setTimeout(() => {
|
||||
const buffer = xlsx(data, settings)
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-disposition": "attachment; filename=device.csv",
|
||||
})
|
||||
res.end(buffer)
|
||||
}, 2000);
|
||||
const settings = {
|
||||
writeOptions: {
|
||||
type: 'buffer',
|
||||
bookType: 'csv'
|
||||
},
|
||||
RTL: true
|
||||
}
|
||||
setTimeout(() => {
|
||||
const buffer = xlsx(data, settings)
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'Content-disposition': 'attachment; filename=device.csv'
|
||||
})
|
||||
res.end(buffer)
|
||||
}, 2000)
|
||||
}
|
||||
]
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
[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.getOneByIMEI = [
|
||||
[
|
||||
param('imei').notEmpty().isString().withMessage('ایدی اشتباه است')
|
||||
],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const data = await Device.findOne({ IMEI: req.params.imei }).sort({ created_at: -1 })
|
||||
if (!data) {
|
||||
res.status(404).json(_sr.fa.not_found.item_id)
|
||||
} else {
|
||||
res.status(200).json(data)
|
||||
}
|
||||
|
||||
[param('imei').notEmpty().isString().withMessage('ایدی اشتباه است')],
|
||||
checkValidations(validationResult),
|
||||
async (req, res) => {
|
||||
const data = await Device.findOne({ IMEI: req.params.imei }).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 })
|
||||
res.status(200).json(data)
|
||||
|
||||
}
|
||||
[
|
||||
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 })
|
||||
res.status(200).json(data)
|
||||
}
|
||||
]
|
||||
|
||||
const creator = exel => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
for (const row of exel) {
|
||||
console.log(row)
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
if (/^[0-9]{15}$/gm.test(row?.[0]?.toString()?.trim())) {
|
||||
const device = await Device.findOne({ IMEI: row?.[0]?.toString()?.trim() })
|
||||
if (device) {
|
||||
console.info('Repeated IMEI ', row[0])
|
||||
|
||||
const creator = (exel) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
for (const row of exel) {
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
if (/^[0-9]{15}$/gm.test(row[0].toString().trim())) {
|
||||
const device = await Device.findOne({ IMEI: row[0].trim() })
|
||||
if (device) {
|
||||
console.info("Repeated IMEI ", row[0])
|
||||
|
||||
device.model = row[1]
|
||||
device.tomanPrice = row[2]
|
||||
device.dollarProfit = row[3] || 1
|
||||
device.profit = row[4] || 1
|
||||
device.score = row[5] || 1
|
||||
device.renewalProfit = row[6] || 1
|
||||
device.save()
|
||||
} else {
|
||||
await Device.create(
|
||||
{
|
||||
IMEI: row[0].trim(),
|
||||
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")
|
||||
} catch (error) {
|
||||
reject(error.message)
|
||||
device.model = row[1]
|
||||
device.tomanPrice = row[2]
|
||||
device.dollarProfit = row[3] || 1
|
||||
device.profit = row[4] || 1
|
||||
device.score = row[5] || 1
|
||||
device.renewalProfit = row[6] || 1
|
||||
device.save()
|
||||
} else {
|
||||
await Device.create({
|
||||
IMEI: row[0].toString().trim(),
|
||||
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')
|
||||
} catch (error) {
|
||||
reject(error.message)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user