diff --git a/server/controllers/serialsExelController.js b/server/controllers/serialsExelController.js index 1e3cff7..08bb750 100644 --- a/server/controllers/serialsExelController.js +++ b/server/controllers/serialsExelController.js @@ -2,9 +2,10 @@ const fs = require('fs') const { body, validationResult } = require('express-validator') const { res404, res500, checkValidations } = require('../plugins/controllersHelperFunctions') const { _sr } = require('../plugins/serverResponses') -const { serialChecker } = require('../plugins/asanServiceSerialChecker') 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 productSerialsPath = './static/uploads/serials_products' @@ -24,11 +25,13 @@ module.exports.addExel = [ checkValidations(validationResult), async (req, res) => { 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: 'فرمت فایل درست نمیباشد' } } }) + 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 { type, g } = req.body try { + const exelFile = await readXlsxFile(Buffer.from(file.data)); + const data = await creator(exelFile, type, g) + const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` const files = await fs.readdirSync(path) for await (const item of files) { @@ -37,7 +40,10 @@ module.exports.addExel = [ 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) { return res500(res, err) } @@ -66,6 +72,7 @@ module.exports.getAllExels = [ } ] + module.exports.removeExel = [ (req, res) => { const type = req.query?.type @@ -100,22 +107,59 @@ module.exports.checkSerial = [ async (req, res) => { try { const { serial, type, g } = req.body - const serialStatus = await serialChecker(serial, type, g) - console.log(serialStatus); + const code = serial.toString().trim().toLowerCase() - let okMsg - if (type === 'guaranteeSerial') okMsg = serialStatus - else okMsg = serialStatus - - let errMsg - if (type === 'guaranteeSerial') errMsg = 'شماره سریال گارانتی معتبر نیست.' - else errMsg = 'شماره سریال کالا معتبر نیست.' + const serialStatus = await Serial.findOne({ + code, + type, + kind: g + }) + if(!serialStatus){ + res404(res, 'این کد در سامانه وجود ندارد') + }else{ + res.status(200).json({message:serialStatus.message}) + } - if (serialStatus) return res.json({ message: okMsg }) - else return res404(res, errMsg) } catch (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) + } + }) +} \ No newline at end of file diff --git a/server/models/Serials.js b/server/models/Serials.js index a06db2d..2ee6568 100644 --- a/server/models/Serials.js +++ b/server/models/Serials.js @@ -4,7 +4,15 @@ const SerialSchema = mongoose.Schema({ code:String, model:String, - message:String + message:String, + type:{ + type:String, + enum:['guaranteeSerial', 'orginality'] + }, + kind:{ + type:Number, + enum:[2,1] + } }) diff --git a/server/plugins/asanServiceSerialChecker.js b/server/plugins/asanServiceSerialChecker.js index 359be64..344bfec 100644 --- a/server/plugins/asanServiceSerialChecker.js +++ b/server/plugins/asanServiceSerialChecker.js @@ -48,3 +48,4 @@ const serialChecker = (serial, type = 'guaranteeSerial', g = 1) => { }; module.exports.serialChecker = serialChecker + diff --git a/static/uploads/serials/1/code-guaranty-19-2-1403-site.xlsx b/static/uploads/serials/1/code-guaranty-19-2-1403-site.xlsx new file mode 100644 index 0000000..706ff46 Binary files /dev/null and b/static/uploads/serials/1/code-guaranty-19-2-1403-site.xlsx differ