Set new structure
This commit is contained in:
@@ -2,9 +2,10 @@ const fs = require('fs')
|
|||||||
const { body, validationResult } = require('express-validator')
|
const { body, validationResult } = require('express-validator')
|
||||||
const { res404, res500, checkValidations } = require('../plugins/controllersHelperFunctions')
|
const { res404, res500, checkValidations } = require('../plugins/controllersHelperFunctions')
|
||||||
const { _sr } = require('../plugins/serverResponses')
|
const { _sr } = require('../plugins/serverResponses')
|
||||||
const { serialChecker } = require('../plugins/asanServiceSerialChecker')
|
|
||||||
const _faSr = _sr.fa
|
const _faSr = _sr.fa
|
||||||
|
// eslint-disable-next-line import/order
|
||||||
|
const readXlsxFile = require('read-excel-file/node')
|
||||||
|
const Serial = require('../models/Serials')
|
||||||
const guaranteeSerialsPath = './static/uploads/serials'
|
const guaranteeSerialsPath = './static/uploads/serials'
|
||||||
const productSerialsPath = './static/uploads/serials_products'
|
const productSerialsPath = './static/uploads/serials_products'
|
||||||
|
|
||||||
@@ -24,11 +25,13 @@ module.exports.addExel = [
|
|||||||
checkValidations(validationResult),
|
checkValidations(validationResult),
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
if (!req.files?.file) return res.status(422).json({ validation: { file: { msg: 'فایل را اضافه کنید' } } })
|
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)))
|
if (!req.files?.file.name.split('.').some(item => ['xlsx', 'xltx', 'xlsm', 'xlsb'].includes(item))) return res.status(422).json({ validation: { file: { msg: 'فرمت فایل درست نمیباشد' } } })
|
||||||
return res.status(422).json({ validation: { file: { msg: 'فرمت فایل درست نمیباشد' } } })
|
|
||||||
const file = req.files.file
|
const file = req.files.file
|
||||||
const { type, g } = req.body
|
const { type, g } = req.body
|
||||||
try {
|
try {
|
||||||
|
const exelFile = await readXlsxFile(Buffer.from(file.data));
|
||||||
|
const data = await creator(exelFile, type, g)
|
||||||
|
|
||||||
const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
|
const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
|
||||||
const files = await fs.readdirSync(path)
|
const files = await fs.readdirSync(path)
|
||||||
for await (const item of files) {
|
for await (const item of files) {
|
||||||
@@ -37,7 +40,10 @@ module.exports.addExel = [
|
|||||||
|
|
||||||
await file.mv(`${path}/${file.name}`)
|
await file.mv(`${path}/${file.name}`)
|
||||||
|
|
||||||
return res.json({ message: _faSr.response.success_save })
|
// res.status(201).json({ msg: _sr.fa.response.success_save, data })
|
||||||
|
res.status(201).json(data)
|
||||||
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res500(res, err)
|
return res500(res, err)
|
||||||
}
|
}
|
||||||
@@ -66,6 +72,7 @@ module.exports.getAllExels = [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
module.exports.removeExel = [
|
module.exports.removeExel = [
|
||||||
(req, res) => {
|
(req, res) => {
|
||||||
const type = req.query?.type
|
const type = req.query?.type
|
||||||
@@ -100,22 +107,59 @@ module.exports.checkSerial = [
|
|||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { serial, type, g } = req.body
|
const { serial, type, g } = req.body
|
||||||
const serialStatus = await serialChecker(serial, type, g)
|
const code = serial.toString().trim().toLowerCase()
|
||||||
console.log(serialStatus);
|
|
||||||
|
|
||||||
let okMsg
|
const serialStatus = await Serial.findOne({
|
||||||
if (type === 'guaranteeSerial') okMsg = serialStatus
|
code,
|
||||||
else okMsg = serialStatus
|
type,
|
||||||
|
kind: g
|
||||||
let errMsg
|
})
|
||||||
if (type === 'guaranteeSerial') errMsg = 'شماره سریال گارانتی معتبر نیست.'
|
if(!serialStatus){
|
||||||
else errMsg = 'شماره سریال کالا معتبر نیست.'
|
res404(res, 'این کد در سامانه وجود ندارد')
|
||||||
|
}else{
|
||||||
|
res.status(200).json({message:serialStatus.message})
|
||||||
|
}
|
||||||
|
|
||||||
if (serialStatus) return res.json({ message: okMsg })
|
|
||||||
else return res404(res, errMsg)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
return res404(res, e)
|
return res500(res, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
const creator = (exel, type, g) => {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
for (const row of exel) {
|
||||||
|
// eslint-disable-next-line no-constant-condition
|
||||||
|
const code = row[1].toString().trim().toLowerCase()
|
||||||
|
const serial = await Serial.findOne({ code })
|
||||||
|
if (serial) {
|
||||||
|
console.info("Repeated serial ", row[1])
|
||||||
|
|
||||||
|
serial.model = row[0]
|
||||||
|
serial.message = row[2]
|
||||||
|
serial.type = type
|
||||||
|
serial.kind = g
|
||||||
|
serial.save()
|
||||||
|
} else {
|
||||||
|
await Serial.create(
|
||||||
|
{
|
||||||
|
code,
|
||||||
|
model: row[0],
|
||||||
|
message: row[2],
|
||||||
|
type,
|
||||||
|
kind: g
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
resolve("end")
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
|
||||||
|
reject(error.message)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -4,7 +4,15 @@ const SerialSchema = mongoose.Schema({
|
|||||||
|
|
||||||
code:String,
|
code:String,
|
||||||
model:String,
|
model:String,
|
||||||
message:String
|
message:String,
|
||||||
|
type:{
|
||||||
|
type:String,
|
||||||
|
enum:['guaranteeSerial', 'orginality']
|
||||||
|
},
|
||||||
|
kind:{
|
||||||
|
type:Number,
|
||||||
|
enum:[2,1]
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -48,3 +48,4 @@ const serialChecker = (serial, type = 'guaranteeSerial', g = 1) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports.serialChecker = serialChecker
|
module.exports.serialChecker = serialChecker
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user