Fix ws and device page

This commit is contained in:
Mr Swift
2024-08-23 23:30:45 +03:30
parent b974644989
commit d91bc5beba
12 changed files with 322 additions and 30 deletions
+55 -24
View File
@@ -1,10 +1,9 @@
const readXlsxFile = require('read-excel-file/node')
const validator = require('validator')
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),
@@ -55,25 +54,48 @@ module.exports.importExel = async (req, res) => {
module.exports.getAll = [
async (req, res) => {
let page;
let rows;
if (!req.query?.rows) {
rows = 10
} else {
rows = req.query.rows
const data = await Device.find().sort({ created_at: -1 })
res.status(200).json(data)
}
]
module.exports.downloadAll = [
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 settings = {
writeOptions: {
type: "buffer",
bookType: "csv",
},
RTL: true
}
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 })
setTimeout(() => {
const buffer = xlsx(data, settings)
res.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-disposition": "attachment; filename=device.csv",
})
res.end(buffer)
}, 2000);
}
]
@@ -99,7 +121,7 @@ module.exports.getOneByIMEI = [
],
checkValidations(validationResult),
async (req, res) => {
const data = await Device.findOne({IMEI:req.params.imei}).sort({ created_at: -1 })
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 {
@@ -151,10 +173,18 @@ 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) {
if (/^[0-9]{15}$/gm.test(row[0].toString().trim())) {
const device = await Device.findOne({ IMEI: row[0] })
if (device) {
console.warn("Repeated IMEI ", row[0])
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(
{
@@ -174,4 +204,5 @@ const creator = (exel) => {
}
resolve("end")
})
}
}
+1
View File
@@ -71,6 +71,7 @@ router.get('/gps/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/exel',hasPermission('gps'), device.downloadAll)
router.get('/gps/device',hasPermission('gps'), device.getAll)
router.get('/gps/device/imei/:imei',hasPermission('gps'), device.getOneByIMEI)
router.get('/gps/device/id/:id',hasPermission('gps'), device.getOne)