Files
asan-service/server/plugins/asanServiceSerialChecker.js
T
2024-02-13 20:59:18 +03:30

42 lines
1.2 KiB
JavaScript

const fs = require('fs')
const readXlsxFile = require('read-excel-file/node')
const guaranteeSerialsPath = './static/uploads/serials'
const productSerialsPath = './static/uploads/serials_products'
const serialChecker = async (serial, type = 'guaranteeSerial', g = 1) => {
return new Promise(async (reso, rej) => {
let exelsPath;
if (type === 'guaranteeSerial') exelsPath = guaranteeSerialsPath + `/${g}`;
if (type === 'orginality') exelsPath = productSerialsPath + `/${g}`;
const lowerSerial = serial.toLowerCase();
try {
const serialFiles = await fs.readdirSync(exelsPath);
serialFiles.forEach(async(file, i)=>{
const exelFile = await readXlsxFile(`${exelsPath}/${file}`);
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("این کد در سامانه موجود نیست")
}
})
} catch (e) {
console.log(e);
rej(new Error(e));
}
});
};
module.exports.serialChecker = serialChecker