From 5feaa11b9b6c75c24a9358baf282c98281052d93 Mon Sep 17 00:00:00 2001 From: Swift Date: Sat, 10 Feb 2024 00:01:28 +0330 Subject: [PATCH] final --- server/controllers/serialsExelController.js | 3 +- server/plugins/asanServiceSerialChecker.js | 47 ++++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/server/controllers/serialsExelController.js b/server/controllers/serialsExelController.js index dd59f6a..c4782d3 100644 --- a/server/controllers/serialsExelController.js +++ b/server/controllers/serialsExelController.js @@ -55,7 +55,7 @@ module.exports.getAllExels = [ const validValues = ['guaranteeSerial', 'orginality'] if (!validValues.includes(type)) return res500(res, 'choose type') const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` - const downloadAblePath = type === 'guaranteeSerial' ? '/uploads/serials/' : '/uploads/serials_products/' + const downloadAblePath = (type === 'guaranteeSerial' ? '/uploads/serials' : '/uploads/serials_products') + `/${g}` fs.readdir(path, (err, files) => { if (err) return res500(res, err) const filesWithURL = files.map(item => { @@ -113,6 +113,7 @@ module.exports.checkSerial = [ if (serialStatus) return res.json({ message: okMsg }) else return res404(res, errMsg) } catch (e) { + console.log(e) return res500(res, `there is an error here --- ${e}`) } } diff --git a/server/plugins/asanServiceSerialChecker.js b/server/plugins/asanServiceSerialChecker.js index 2315f55..c408745 100644 --- a/server/plugins/asanServiceSerialChecker.js +++ b/server/plugins/asanServiceSerialChecker.js @@ -1,13 +1,13 @@ const fs = require('fs') const readXlsxFile = require('read-excel-file/node') -const guaranteeSerialsPath = './static/uploads/serials/' -const productSerialsPath = './static/uploads/serials_products/' +const guaranteeSerialsPath = './static/uploads/serials' +const productSerialsPath = './static/uploads/serials_products' const serialChecker = async (serial, type = 'guaranteeSerial', g = 1) => { let exelsPath - if (type === 'guaranteeSerial') exelsPath = guaranteeSerialsPath + g - if (type === 'orginality') exelsPath = productSerialsPath + g + 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 @@ -31,26 +31,31 @@ const serialChecker = async (serial, type = 'guaranteeSerial', g = 1) => { // push each exel file data to exelFilesData array let n0 = 0 for await (const file of serialFiles) { - // reading all exel files one by one - const exelFile = await readXlsxFile(`${exelsPath}/${file}`) - exelFilesData.push(exelFile) + - // flat exelFilesData and filter and lowerCase it's items - if (n0 === serialFiles.length - 1) { - const flattedArray = exelFilesData.flat(10000) - const lowerCaseFlattedArray = await toLowerCaseArray(flattedArray) - - // loop throw lowerCased items to find exact match item - let n1 = 0 - for await (const item of lowerCaseFlattedArray) { - if (item === lowerSerial) return true - if (n1 === lowerCaseFlattedArray.length - 1) return false - n1++ - } - } - n0++ + // reading all exel files one by one + const exelFile = await readXlsxFile(`${exelsPath}/${file}`) + exelFilesData.push(exelFile) + + // flat exelFilesData and filter and lowerCase it's items + if (n0 === serialFiles.length - 1) { + const flattedArray = exelFilesData.flat(10000) + const lowerCaseFlattedArray = await toLowerCaseArray(flattedArray) + + // loop throw lowerCased items to find exact match item + let n1 = 0 + for await (const item of lowerCaseFlattedArray) { + if (item === lowerSerial) return true + if (n1 === lowerCaseFlattedArray.length - 1) return false + n1++ + } + } + + + n0++ } } catch (e) { + console.log(e) throw new Error(e) } }