From 7438a951664bce3ef27e6d9d2bf05d559cc9251d Mon Sep 17 00:00:00 2001 From: Swift Date: Fri, 9 Feb 2024 22:04:14 +0330 Subject: [PATCH] Add custom serials --- components/admin/TheSidebar.vue | 22 +++++++++++++++---- pages/admin/serials-exel/index.vue | 18 ++++++++++----- pages/index.vue | 4 +++- server/controllers/serialsExelController.js | 20 ++++++++++------- server/plugins/asanServiceSerialChecker.js | 6 ++--- static/uploads/serials/{ => 1}/readme.md | 0 .../{serials_products => serials/2}/readme.md | 0 static/uploads/serials_products/1/readme.md | 0 static/uploads/serials_products/2/readme.md | 0 9 files changed, 48 insertions(+), 22 deletions(-) rename static/uploads/serials/{ => 1}/readme.md (100%) rename static/uploads/{serials_products => serials/2}/readme.md (100%) create mode 100644 static/uploads/serials_products/1/readme.md create mode 100644 static/uploads/serials_products/2/readme.md diff --git a/components/admin/TheSidebar.vue b/components/admin/TheSidebar.vue index e37106d..48254a1 100644 --- a/components/admin/TheSidebar.vue +++ b/components/admin/TheSidebar.vue @@ -254,15 +254,29 @@ /> + + diff --git a/pages/admin/serials-exel/index.vue b/pages/admin/serials-exel/index.vue index c499ae1..a89ebf2 100644 --- a/pages/admin/serials-exel/index.vue +++ b/pages/admin/serials-exel/index.vue @@ -30,17 +30,17 @@ download target="_blank" title="برای دانلود کلیک کنید" - :href="scope.row.url + scope.row.name" + :href="scope?.row.url + scope?.row.name" style="color: blue; display: block; direction: ltr !important; text-align: right" > - {{ scope.row.name }} + {{ scope?.row.name }} - + @@ -60,9 +60,11 @@ export default { async asyncData({ $axios, error, query }) { try { const type = query?.type + const g = query?.g + if (!type) throw new Error('invalid type') const files = await $axios.get('/api/admin/serialExels', { - params: { type } + params: { type, g } }) return { files: files.data @@ -74,7 +76,7 @@ export default { }, data() { return { - files: null, + files: [], validation: {} } }, @@ -87,6 +89,9 @@ export default { type() { return this.$route.query?.type }, + g() { + return this.$route.query?.g + }, title() { if (this.type === 'guaranteeSerial') return 'فایل اکسل شماره سریال گارانتی' else return 'فایل اکسل شماره سریال کالا' @@ -102,6 +107,7 @@ export default { this.validation = {} const data = new FormData() data.append('type', this.type) + data.append('g', this.g) data.append('file', this.$refs.file.files[0]) this.$axios .post('/api/admin/addExel', data) diff --git a/pages/index.vue b/pages/index.vue index c1ced5d..bdb1de1 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -132,6 +132,7 @@ export default { inquiryType: 'guaranteeSerial', checkingSerial: false, brands:[], + g: 1 } }, head() { @@ -178,7 +179,8 @@ export default { this.checkingSerial = true const data = { serial: this.serial, - type: this.inquiryType + type: this.inquiryType, + g: this.g } const loading = this.$loading({ lock: true, diff --git a/server/controllers/serialsExelController.js b/server/controllers/serialsExelController.js index cfa5648..dd59f6a 100644 --- a/server/controllers/serialsExelController.js +++ b/server/controllers/serialsExelController.js @@ -27,9 +27,9 @@ module.exports.addExel = [ 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 } = req.body + const { type, g } = req.body try { - const path = type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath + const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` const files = await fs.readdirSync(path) for await (const item of files) { if (file.name === item) return res.status(422).json({ validation: { file: { msg: 'نام فایل تکراریست' } } }) @@ -47,12 +47,14 @@ module.exports.addExel = [ module.exports.getAllExels = [ (req, res) => { const type = req.query?.type - if (!type) { + const g = req.query?.g + + if (!type || !g) { res500(res, 'choose type') } const validValues = ['guaranteeSerial', 'orginality'] if (!validValues.includes(type)) return res500(res, 'choose type') - const path = type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath + const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` const downloadAblePath = type === 'guaranteeSerial' ? '/uploads/serials/' : '/uploads/serials_products/' fs.readdir(path, (err, files) => { if (err) return res500(res, err) @@ -67,10 +69,12 @@ module.exports.getAllExels = [ module.exports.removeExel = [ (req, res) => { const type = req.query?.type - if (!type) return res500(res, 'choose type') + const g = req.query?.g + + if (!type || !g) return res500(res, 'choose type') const validValues = ['guaranteeSerial', 'orginality'] if (!validValues.includes(type)) return res500(res, 'choose type') - const path = type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath + const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` fs.unlink(`${path}/${req.params.name}`, err => { if (err) return res404(res, _faSr.not_found.item_id) else return res.json({ message: _faSr.response.success_remove }) @@ -95,8 +99,8 @@ module.exports.checkSerial = [ checkValidations(validationResult), async (req, res) => { try { - const { serial, type } = req.body - const serialStatus = await serialChecker(serial, type) + const { serial, type, g } = req.body + const serialStatus = await serialChecker(serial, type, g) let okMsg if (type === 'guaranteeSerial') okMsg = 'شماره سریال گارانتی معتبر میباشد.' diff --git a/server/plugins/asanServiceSerialChecker.js b/server/plugins/asanServiceSerialChecker.js index 03a295d..82a6a66 100644 --- a/server/plugins/asanServiceSerialChecker.js +++ b/server/plugins/asanServiceSerialChecker.js @@ -4,10 +4,10 @@ const readXlsxFile = require('read-excel-file/node') const guaranteeSerialsPath = './static/uploads/serials' const productSerialsPath = './static/uploads/serials_products' -const serialChecker = async (serial, type = 'guaranteeSerial') => { +const serialChecker = async (serial, type = 'guaranteeSerial', g = 1) => { let exelsPath - if (type === 'guaranteeSerial') exelsPath = guaranteeSerialsPath - if (type === 'orginality') exelsPath = productSerialsPath + if (type === 'guaranteeSerial') exelsPath = guaranteeSerialsPath + g + if (type === 'orginality') exelsPath = productSerialsPath + g const lowerSerial = serial.toLowerCase() // a promise to lowercase items and filter null or DateObjects diff --git a/static/uploads/serials/readme.md b/static/uploads/serials/1/readme.md similarity index 100% rename from static/uploads/serials/readme.md rename to static/uploads/serials/1/readme.md diff --git a/static/uploads/serials_products/readme.md b/static/uploads/serials/2/readme.md similarity index 100% rename from static/uploads/serials_products/readme.md rename to static/uploads/serials/2/readme.md diff --git a/static/uploads/serials_products/1/readme.md b/static/uploads/serials_products/1/readme.md new file mode 100644 index 0000000..e69de29 diff --git a/static/uploads/serials_products/2/readme.md b/static/uploads/serials_products/2/readme.md new file mode 100644 index 0000000..e69de29