add pagination and fix bug

This commit is contained in:
Mr Swift
2024-08-07 15:42:53 +03:30
parent 13f991eab2
commit db6c8d6681
13 changed files with 322 additions and 156 deletions
+44 -27
View File
@@ -38,8 +38,8 @@ module.exports.importExel = async (req, res) => {
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 })
const data = await creator(exelFile)
res.status(201).json({ msg: _sr.fa.response.success_save, data })
} catch (error) {
@@ -51,8 +51,25 @@ module.exports.importExel = async (req, res) => {
module.exports.getAll = [
async (req, res) => {
const data = await Device.find().sort({ created_at: -1 })
res.status(200).json(data)
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 })
}
]
@@ -107,31 +124,31 @@ module.exports.updateDevice = [
]
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])
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 {
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
}
)
console.warn("Wrong IMEI ", row[0])
}
} else {
console.warn("Wrong IMEI ", row[0])
}
}
resolve("end")
resolve("end")
})
}