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
+5 -2
View File
@@ -102,14 +102,17 @@ module.exports.checkSerial = [
const { serial, type, g } = req.body const { serial, type, g } = req.body
const serialStatus = await serialChecker(serial, type, g) const serialStatus = await serialChecker(serial, type, g)
console.log(serialStatus)
let okMsg let okMsg
if (type === 'guaranteeSerial') okMsg = 'شماره سریال گارانتی معتبر میباشد.' if (type === 'guaranteeSerial') okMsg = serialStatus
else okMsg = 'شماره سریال کالا معتبر میباشد.' else okMsg = serialStatus
let errMsg let errMsg
if (type === 'guaranteeSerial') errMsg = 'شماره سریال گارانتی معتبر نیست.' if (type === 'guaranteeSerial') errMsg = 'شماره سریال گارانتی معتبر نیست.'
else errMsg = 'شماره سریال کالا معتبر نیست.' else errMsg = 'شماره سریال کالا معتبر نیست.'
if (serialStatus) return res.json({ message: okMsg }) if (serialStatus) return res.json({ message: okMsg })
else return res404(res, errMsg) else return res404(res, errMsg)
} catch (e) { } catch (e) {
+9 -8
View File
@@ -1,14 +1,7 @@
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
const mongoose = require('mongoose') const mongoose = require('mongoose')
mongoose.plugin(schema => {
schema.set('toObject', { getters: true })
schema.set('toJSON', { getters: true })
schema.set('timestamps', {
createdAt: 'created_at',
updatedAt: 'updated_at'
})
})
// mongodb database connection string. change it as per your needs. here "mydb" is the name of the database. You don't need to create DB from mongodb terminal. mongoose create the database automatically. // mongodb database connection string. change it as per your needs. here "mydb" is the name of the database. You don't need to create DB from mongodb terminal. mongoose create the database automatically.
const inHostUrl = 'mongodb://asanserv_admin:admin1234@localhost:27017/asanserv_database' const inHostUrl = 'mongodb://asanserv_admin:admin1234@localhost:27017/asanserv_database'
@@ -22,6 +15,14 @@ mongoose.connect(outHostUrl, {
useCreateIndex: true useCreateIndex: true
}) })
mongoose.plugin(schema => {
schema.set('toObject', { getters: true })
schema.set('toJSON', { getters: true })
schema.set('timestamps', {
createdAt: 'created_at',
updatedAt: 'updated_at'
})
})
const database = mongoose.connection const database = mongoose.connection
database.on('error', console.error.bind(console, 'connection error:')) database.on('error', console.error.bind(console, 'connection error:'))
database.once('open', function callback() { database.once('open', function callback() {
+28 -50
View File
@@ -5,59 +5,37 @@ const guaranteeSerialsPath = './static/uploads/serials'
const productSerialsPath = './static/uploads/serials_products' const productSerialsPath = './static/uploads/serials_products'
const serialChecker = async (serial, type = 'guaranteeSerial', g = 1) => { const serialChecker = async (serial, type = 'guaranteeSerial', g = 1) => {
let exelsPath return new Promise(async (reso, rej) => {
if (type === 'guaranteeSerial') exelsPath = guaranteeSerialsPath + `/${g}` let exelsPath;
if (type === 'orginality') exelsPath = productSerialsPath + `/${g}` if (type === 'guaranteeSerial') exelsPath = guaranteeSerialsPath + `/${g}`;
if (type === 'orginality') exelsPath = productSerialsPath + `/${g}`;
const lowerSerial = serial.toLowerCase() const lowerSerial = serial.toLowerCase();
// a promise to lowercase items and filter null or DateObjects
const toLowerCaseArray = array => { try {
return new Promise((resolve, reject) => { const serialFiles = await fs.readdirSync(exelsPath);
const newArray = []
array.forEach((item, index) => { serialFiles.forEach(async(file, i)=>{
if (item !== null && typeof item === 'string') newArray.push(item.toLowerCase()) const exelFile = await readXlsxFile(`${exelsPath}/${file}`);
if (item !== null && typeof item === 'number') newArray.push(item.toString())
if (index === array.length - 1) return resolve(newArray) 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)
}
}) })
})
}
// create an array of all exel files data } catch (e) {
const exelFilesData = [] console.log(e);
try { rej(new Error(e));
// get a list of all exel files
const serialFiles = await fs.readdirSync(exelsPath)
// 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++
} }
} catch (e) { });
console.log(e) };
throw new Error(e)
}
}
module.exports.serialChecker = serialChecker module.exports.serialChecker = serialChecker
View File