New serial reader system
This commit is contained in:
@@ -30,9 +30,9 @@ 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 path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
|
||||
const files = await fs.readdirSync(path)
|
||||
for await (const item of files) {
|
||||
if (file.name === item) return res.status(422).json({ validation: { file: { msg: 'نام فایل تکراریست' } } })
|
||||
@@ -57,7 +57,7 @@ module.exports.getAllExels = [
|
||||
|
||||
if (!type || !g) {
|
||||
res500(res, 'choose type')
|
||||
}
|
||||
}
|
||||
const validValues = ['guaranteeSerial', 'orginality']
|
||||
if (!validValues.includes(type)) return res500(res, 'choose type')
|
||||
const path = (type === 'guaranteeSerial' ? guaranteeSerialsPath : productSerialsPath) + `/${g}`
|
||||
@@ -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){
|
||||
res404(res, 'این کد در سامانه وجود ندارد')
|
||||
}else{
|
||||
res.status(200).json({message:serialStatus.message})
|
||||
if (!serialStatus) {
|
||||
res404(res, 'این کد در سامانه وجود ندارد')
|
||||
} else {
|
||||
res.status(200).json({ message: serialStatus.message })
|
||||
}
|
||||
|
||||
} 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) => {
|
||||
try {
|
||||
for (const row of exel) {
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
const code = row[1].toString().trim().toLowerCase()
|
||||
const serial = await Serial.findOne({ code })
|
||||
if (serial) {
|
||||
console.info("Repeated serial ", row[1])
|
||||
const serial = await Serial.findOne({ code })
|
||||
if (serial) {
|
||||
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")
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
|
||||
reject(error.message)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -12,7 +12,8 @@ const SerialSchema = mongoose.Schema({
|
||||
kind:{
|
||||
type:Number,
|
||||
enum:[2,1]
|
||||
}
|
||||
},
|
||||
fileName:String
|
||||
|
||||
})
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user