This commit is contained in:
Swift
2024-02-13 20:54:56 +03:30
parent f0040c1748
commit 7a38f7a9e6
5 changed files with 41 additions and 59 deletions
+27 -49
View File
@@ -5,59 +5,37 @@ 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}`
return new Promise(async (reso, rej) => {
let exelsPath;
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
const toLowerCaseArray = array => {
return new Promise((resolve, reject) => {
const newArray = []
array.forEach((item, index) => {
if (item !== null && typeof item === 'string') newArray.push(item.toLowerCase())
if (item !== null && typeof item === 'number') newArray.push(item.toString())
if (index === array.length - 1) return resolve(newArray)
})
})
}
const lowerSerial = serial.toLowerCase();
// create an array of all exel files data
const exelFilesData = []
try {
// get a list of all exel files
const serialFiles = await fs.readdirSync(exelsPath)
try {
const serialFiles = await fs.readdirSync(exelsPath);
// push each exel file data to exelFilesData array
let n0 = 0
for await (const file of serialFiles) {
serialFiles.forEach(async(file, i)=>{
const exelFile = await readXlsxFile(`${exelsPath}/${file}`);
// 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++
for (const row of exelFile) {
for (const col of row) {
if (col == lowerSerial) {
reso(row[2]);
return; // Serial found, exit loop
}
}
}
if((serialFiles.length - 1) <= i){
rej(undefined)
}
})
} catch (e) {
console.log(e);
rej(new Error(e));
}
} catch (e) {
console.log(e)
throw new Error(e)
}
}
});
};
module.exports.serialChecker = serialChecker