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
+10 -6
View File
@@ -30,7 +30,7 @@ module.exports.addExel = [
const { type, g } = req.body
try {
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 files = await fs.readdirSync(path)
@@ -74,7 +74,7 @@ module.exports.getAllExels = [
module.exports.removeExel = [
(req, res) => {
async (req, res) => {
const type = req.query?.type
const g = req.query?.g
@@ -82,6 +82,8 @@ module.exports.removeExel = [
const validValues = ['guaranteeSerial', 'orginality']
if (!validValues.includes(type)) return res500(res, 'choose type')
const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
await Serial.deleteMany({ fileName: req.params.name })
fs.unlink(`${path}/${req.params.name}`, err => {
if (err) return res404(res, _faSr.not_found.item_id)
else return res.json({ message: _faSr.response.success_remove })
@@ -114,10 +116,10 @@ module.exports.checkSerial = [
type,
kind: g
})
if(!serialStatus){
if (!serialStatus) {
res404(res, 'این کد در سامانه وجود ندارد')
}else{
res.status(200).json({message:serialStatus.message})
} else {
res.status(200).json({ message: serialStatus.message })
}
} catch (e) {
@@ -127,7 +129,7 @@ module.exports.checkSerial = [
}
]
const creator = (exel, type, g) => {
const creator = (exel, type, g, name) => {
return new Promise(async (resolve, reject) => {
try {
for (const row of exel) {
@@ -141,6 +143,7 @@ const creator = (exel, type, g) => {
serial.message = row[2]
serial.type = type
serial.kind = g
serial.fileName = name
serial.save()
} else {
await Serial.create(
@@ -149,6 +152,7 @@ const creator = (exel, type, g) => {
model: row[0],
message: row[2],
type,
fileName: name,
kind: g
}
)
+2 -1
View File
@@ -12,7 +12,8 @@ const SerialSchema = mongoose.Schema({
kind:{
type:Number,
enum:[2,1]
}
},
fileName:String
})