New serial reader system

This commit is contained in:
Mr Swift
2024-10-16 19:25:51 +03:30
parent 27ac6eec76
commit 6dd63fc206
4 changed files with 36 additions and 31 deletions
+34 -30
View File
@@ -30,9 +30,9 @@ module.exports.addExel = [
const { type, g } = req.body const { type, g } = req.body
try { try {
const exelFile = await readXlsxFile(Buffer.from(file.data)); const exelFile = await readXlsxFile(Buffer.from(file.data));
const data = await creator(exelFile, type, g) const data = await creator(exelFile, type, g, file.name)
const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
const files = await fs.readdirSync(path) const files = await fs.readdirSync(path)
for await (const item of files) { for await (const item of files) {
if (file.name === item) return res.status(422).json({ validation: { file: { msg: 'نام فایل تکراریست' } } }) if (file.name === item) return res.status(422).json({ validation: { file: { msg: 'نام فایل تکراریست' } } })
@@ -57,7 +57,7 @@ module.exports.getAllExels = [
if (!type || !g) { if (!type || !g) {
res500(res, 'choose type') res500(res, 'choose type')
} }
const validValues = ['guaranteeSerial', 'orginality'] const validValues = ['guaranteeSerial', 'orginality']
if (!validValues.includes(type)) return res500(res, 'choose type') if (!validValues.includes(type)) return res500(res, 'choose type')
const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
@@ -74,7 +74,7 @@ module.exports.getAllExels = [
module.exports.removeExel = [ module.exports.removeExel = [
(req, res) => { async (req, res) => {
const type = req.query?.type const type = req.query?.type
const g = req.query?.g const g = req.query?.g
@@ -82,6 +82,8 @@ module.exports.removeExel = [
const validValues = ['guaranteeSerial', 'orginality'] const validValues = ['guaranteeSerial', 'orginality']
if (!validValues.includes(type)) return res500(res, 'choose type') if (!validValues.includes(type)) return res500(res, 'choose type')
const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}` const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
await Serial.deleteMany({ fileName: req.params.name })
fs.unlink(`${path}/${req.params.name}`, err => { fs.unlink(`${path}/${req.params.name}`, err => {
if (err) return res404(res, _faSr.not_found.item_id) if (err) return res404(res, _faSr.not_found.item_id)
else return res.json({ message: _faSr.response.success_remove }) else return res.json({ message: _faSr.response.success_remove })
@@ -114,10 +116,10 @@ module.exports.checkSerial = [
type, type,
kind: g kind: g
}) })
if(!serialStatus){ if (!serialStatus) {
res404(res, 'این کد در سامانه وجود ندارد') res404(res, 'این کد در سامانه وجود ندارد')
}else{ } else {
res.status(200).json({message:serialStatus.message}) res.status(200).json({ message: serialStatus.message })
} }
} catch (e) { } catch (e) {
@@ -127,38 +129,40 @@ module.exports.checkSerial = [
} }
] ]
const creator = (exel, type, g) => { const creator = (exel, type, g, name) => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
for (const row of exel) { for (const row of exel) {
// eslint-disable-next-line no-constant-condition // eslint-disable-next-line no-constant-condition
const code = row[1].toString().trim().toLowerCase() const code = row[1].toString().trim().toLowerCase()
const serial = await Serial.findOne({ code }) const serial = await Serial.findOne({ code })
if (serial) { if (serial) {
console.info("Repeated serial ", row[1]) console.info("Repeated serial ", row[1])
serial.model = row[0]
serial.message = row[2]
serial.type = type
serial.kind = g
serial.fileName = name
serial.save()
} else {
await Serial.create(
{
code,
model: row[0],
message: row[2],
type,
fileName: name,
kind: g
}
)
}
serial.model = row[0]
serial.message = row[2]
serial.type = type
serial.kind = g
serial.save()
} else {
await Serial.create(
{
code,
model: row[0],
message: row[2],
type,
kind: g
}
)
}
} }
resolve("end") resolve("end")
} catch (error) { } catch (error) {
console.log(error); console.log(error);
reject(error.message) reject(error.message)
} }
}) })
+2 -1
View File
@@ -12,7 +12,8 @@ const SerialSchema = mongoose.Schema({
kind:{ kind:{
type:Number, type:Number,
enum:[2,1] enum:[2,1]
} },
fileName:String
}) })