169 lines
5.5 KiB
JavaScript
169 lines
5.5 KiB
JavaScript
const { body, validationResult } = require('express-validator')
|
|
const TransactionDraft = require('../models/TransactionDraft')
|
|
const { VerityStockAreaId, PanatechStockAreaId } = require('../_env')
|
|
const { res404, res406, res500, checkValidations } = require('../plugins/controllersHelperFunctions')
|
|
const { notifyUser } = require('../WebSocket/controllers/user_EventsController')
|
|
|
|
module.exports.add_transaction_draft = [
|
|
[
|
|
body('db_name')
|
|
.notEmpty()
|
|
.withMessage('db param is required')
|
|
.bail()
|
|
.custom((value, { req }) => {
|
|
if (value === 'verity' || value === 'panatech') return Promise.resolve()
|
|
else return Promise.reject(new Error('incorrect db value'))
|
|
})
|
|
],
|
|
checkValidations(validationResult),
|
|
(req, res) => {
|
|
const { user_id } = req
|
|
if (!user_id) return res404(res, 'invalid userID')
|
|
const { db_name, description } = req.body
|
|
const data = { user_id, db_name, description }
|
|
|
|
const draft = new TransactionDraft(data)
|
|
draft.save((err, data) => {
|
|
if (err) return res500(res, err)
|
|
notifyUser('updateDraftsCount', user_id)
|
|
return res.json({ draftId: data._id })
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.update_draft_description = [
|
|
(req, res) => {
|
|
const draftId = req.params?.draftId
|
|
const { description } = req.body
|
|
TransactionDraft.findByIdAndUpdate(draftId, { description }, (err, oldData) => {
|
|
if (err || !oldData) return res404(res, err)
|
|
else return res.json({ message: 'توضیحات فرم با موفقیت بروزرسانی شد' })
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.get_user_drafts = [
|
|
async (req, res) => {
|
|
try {
|
|
const { user_id } = req
|
|
const drafts = await TransactionDraft.find({ user_id })
|
|
return res.json(drafts)
|
|
} catch (e) {
|
|
return res.status(500).json({ message: e })
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.get_one_user_draft = [
|
|
(req, res) => {
|
|
TransactionDraft.findById(req.params?.draftId, (err, data) => {
|
|
if (err || !data) return res404(res, err)
|
|
else return res.json(data)
|
|
})
|
|
}
|
|
]
|
|
|
|
module.exports.add_stuff_to_draft = [
|
|
async (req, res) => {
|
|
const userId = req.user_id
|
|
const draftId = req.params?.draftId
|
|
const { ItemID, ItemCode, MjQty, SerialNo1, GStartDate, GEndDate } = req.body
|
|
|
|
try {
|
|
// custom validations
|
|
if (!Number(MjQty) || !Number.isInteger(Number(MjQty))) return res406(res, 'تعداد باید از جنس عدد صحیح باشد')
|
|
|
|
if (!ItemID?.length || !ItemCode?.length) return res406(res, 'کالا را انتخاب کنید')
|
|
|
|
if (Number(MjQty) < 1) return res406(res, 'تعداد نباید کمتر از 1 باشد')
|
|
|
|
if (SerialNo1?.length && Number(MjQty) !== 1)
|
|
return res406(res, 'تعداد کالاهایی که شماره سریال اختصاصی دارند نمیتواند بیشتر از یکی باشد')
|
|
|
|
const draft = await TransactionDraft.findOne({ user_id: userId, _id: draftId })
|
|
|
|
if (!draft) return res404(res, 'this transaction does not exist or not belongs to you')
|
|
|
|
// check if item already exists
|
|
for (let i = 0; i < draft.items.length; i++) {
|
|
if ((await draft.items[i].SerialNo1) !== '') {
|
|
if ((await draft.items[i].SerialNo1) === SerialNo1) return res406(res, 'این شماره سریال قبلا افزوده شده')
|
|
} else {
|
|
// eslint-disable-next-line no-lonely-if
|
|
if ((await draft.items[i].ItemID) === ItemID) {
|
|
draft.items[i].MjQty = Number(draft.items[i].MjQty) + Number(MjQty)
|
|
draft.markModified('items')
|
|
await draft.save()
|
|
return res.json({ message: 'این کالا به تعداد کالای مشابه داخل لیست اضافه گردید' })
|
|
}
|
|
}
|
|
}
|
|
|
|
function stockAreaID(db) {
|
|
if (db === 'verity') return VerityStockAreaId
|
|
else if (db === 'panatech') return PanatechStockAreaId
|
|
else return null
|
|
}
|
|
|
|
const item = {
|
|
ItemID,
|
|
ItemCode,
|
|
MjQty: Number(MjQty),
|
|
SerialNo1,
|
|
GStartDate,
|
|
GEndDate,
|
|
StockAreaID: stockAreaID(draft.db_name),
|
|
TransLineID: 'null',
|
|
Price: 0,
|
|
DiscountAmount: 0,
|
|
DiscountPercent: 0,
|
|
TaxAmount: 0,
|
|
TollAmount: 0
|
|
}
|
|
|
|
draft.items.push(item)
|
|
await draft.save()
|
|
return res.json({ message: 'کالا با موفقیت به فرم افزوده شد' })
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.remove_stuff_from_draft = [
|
|
async (req, res) => {
|
|
const draftId = req.params?.draftId
|
|
const { user_id } = req
|
|
const { itemId, itemSerial } = req.body
|
|
|
|
try {
|
|
const draft = await TransactionDraft.findOne({ user_id, _id: draftId })
|
|
if (!draft) return res404(res, 'this transaction does not exist or not belongs to you')
|
|
draft.items = await draft.items.filter(item => {
|
|
if (itemSerial === '') {
|
|
return item.ItemID !== itemId
|
|
} else {
|
|
return item.SerialNo1 !== itemSerial
|
|
}
|
|
})
|
|
draft.markModified('items')
|
|
await draft.save()
|
|
return res.json({ message: 'آیتم با موفقیت از لیست حذف شد' })
|
|
} catch (e) {
|
|
return res500(res, e)
|
|
}
|
|
}
|
|
]
|
|
|
|
module.exports.remove_draft = [
|
|
(req, res) => {
|
|
const draftId = req.params?.draftId
|
|
|
|
TransactionDraft.findByIdAndDelete(draftId, (err, data) => {
|
|
if (err || !data) return res404(res, err)
|
|
notifyUser('updateDraftsCount', data.user_id)
|
|
return res.json({ message: 'پیشنویس با موفقیت حذف شد' })
|
|
})
|
|
}
|
|
]
|