/var/www/greso.tech/server/nsm/controllers/order/inventory
Edit: /var/www/greso.tech/server/nsm/controllers/order/inventory/edit.js (8194B)
module.exports = function ({ db, logger }) {
const { NotFound, BadRequest } = require('../../../responseModels/errors')
const { isSet, isEmail, isPhone, isEmpty, minLength, isNumber, parseBool } = require('../../../utils/validator.util')
return async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
let t1 = performance.now()
try {
var {
id,
orderId,
localRateId,
pickupFrom,
pickupTo,
pickupHours,
drivingFrom,
drivingTo,
drivingHours,
deliveryFrom,
deliveryTo,
deliveryHours,
doubleDriving,
rate,
rate_multiply,
estimate_type,
storage,
valuation,
weight_brutto,
weight_tare,
items_weight,
items_volume,
items_count,
items_total,
bulky_count,
bulky_total,
extras_count,
extras_total,
packages_count,
packages_total,
fuel_percent,
fuel_total,
edited_total,
custom_total,
edited_packages,
custom_packages,
custom_unpackage,
edited_weight,
custom_weight,
edited_volume,
custom_volume,
discount_comment,
discount_cash,
total,
paid,
due,
} = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ code: 400, lang: req.user?.languageCode })
}
if (!isSet(orderId) || isEmpty(orderId)) {
throw new BadRequest({ code: 400, lang: req.user?.languageCode })
}
// Check params
if (isSet(localRateId)){
if(isEmpty(localRateId)) {
throw new BadRequest({
code: 2108,
lang: req.user?.languageCode
})
}
}
if (isSet(pickupFrom)){
if(isEmpty(pickupFrom)) {
throw new BadRequest({
code: 2109,
lang: req.user?.languageCode
})
}
}
if (isSet(pickupTo)){
if(isEmpty(pickupTo)) {
throw new BadRequest({
code: 2110,
lang: req.user?.languageCode
})
}
}
if (isSet(pickupHours)){
if(isEmpty(pickupHours)) {
throw new BadRequest({
code: 2111,
lang: req.user?.languageCode
})
}
}
if (isSet(drivingFrom)){
if(isEmpty(drivingFrom)) {
throw new BadRequest({
code: 2112,
lang: req.user?.languageCode
})
}
}
if (isSet(drivingTo)){
if(isEmpty(drivingTo)) {
throw new BadRequest({
code: 2113,
lang: req.user?.languageCode
})
}
}
if (isSet(drivingHours)){
if(isEmpty(drivingHours)) {
throw new BadRequest({
code: 2114,
lang: req.user?.languageCode
})
}
}
if (isSet(deliveryFrom)){
if(isEmpty(deliveryFrom)) {
throw new BadRequest({
code: 2115,
lang: req.user?.languageCode
})
}
}
if (isSet(deliveryTo)){
if(isEmpty(deliveryTo)) {
throw new BadRequest({
code: 2116,
lang: req.user?.languageCode
})
}
}
if (isSet(deliveryHours)){
if(isEmpty(deliveryHours)) {
throw new BadRequest({
code: 2117,
lang: req.user?.languageCode
})
}
}
if (isSet(doubleDriving)){
if(isEmpty(doubleDriving)) {
throw new BadRequest({
code: 2120,
lang: req.user?.languageCode
})
}
}
if (isSet(rate)){
if(isEmpty(rate)) {
throw new BadRequest({
code: 2027,
lang: req.user?.languageCode
})
}
}
if (isSet(rate_multiply)){
if(isEmpty(rate_multiply)) {
throw new BadRequest({
code: 2057,
lang: req.user?.languageCode
})
}
}
if (isSet(estimate_type)){
if(isEmpty(estimate_type)) {
throw new BadRequest({
code: 2104,
lang: req.user?.languageCode
})
}
}
if (isSet(storage)){
if(isEmpty(storage)) {
throw new BadRequest({
code: 2100,
lang: req.user?.languageCode
})
}
}
if (isSet(valuation)){
if(isEmpty(valuation)) {
throw new BadRequest({
code: 2085,
lang: req.user?.languageCode
})
}
}
if (isSet(edited_packages)){
if(isEmpty(edited_packages)) {
throw new BadRequest({
code: 2078,
lang: req.user?.languageCode
})
}else{
edited_packages = parseBool(edited_packages.toString())
}
}
if (isSet(custom_packages)){
if(isEmpty(custom_packages)) {
throw new BadRequest({
code: 2079,
lang: req.user?.languageCode
})
}
}
if (isSet(custom_unpackage)){
if(isEmpty(custom_unpackage)) {
throw new BadRequest({
code: 2079,
lang: req.user?.languageCode
})
}
}
if (isSet(edited_weight)){
if(isEmpty(edited_weight)) {
throw new BadRequest({
code: 2070,
lang: req.user?.languageCode
})
}else{
edited_weight = parseBool(edited_weight.toString())
}
}
if (isSet(custom_weight)){
if(isEmpty(custom_weight)) {
throw new BadRequest({
code: 2071,
lang: req.user?.languageCode
})
}
}
if (isSet(edited_volume)){
if(isEmpty(edited_volume)) {
throw new BadRequest({
code: 2072,
lang: req.user?.languageCode
})
}else{
edited_volume = parseBool(edited_volume.toString())
}
}
if (isSet(custom_volume)){
if(isEmpty(custom_volume)) {
throw new BadRequest({
code: 2073,
lang: req.user?.languageCode
})
}
}
if (isSet(edited_total)){
if(isEmpty(edited_total)) {
throw new BadRequest({
code: 2061,
lang: req.user?.languageCode
})
}else{
edited_total = parseBool(edited_total.toString())
}
}
if (isSet(custom_total)){
if(isEmpty(custom_total)) {
throw new BadRequest({
code: 2062,
lang: req.user?.languageCode
})
}
}
if (isSet(fuel_total)){
if(isEmpty(fuel_total)) {
throw new BadRequest({
code: 2086,
lang: req.user?.languageCode
})
}
}
if (isSet(discount_cash)){
if(isEmpty(discount_cash)) {
throw new BadRequest({
code: 2056,
lang: req.user?.languageCode
})
}
}
const inventory = await db.orderInventory.findOne({
// include: [{ model: db.role }],
where: { id: id }
})
if (!inventory) {
throw new NotFound({ lang: req.user?.languageCode })
}
inventory.localRateId = localRateId
inventory.pickupFrom = pickupFrom
inventory.pickupTo = pickupTo
inventory.pickupHours = pickupHours
inventory.drivingFrom = drivingFrom
inventory.drivingTo = drivingTo
inventory.drivingHours = drivingHours
inventory.deliveryFrom = deliveryFrom
inventory.deliveryTo = deliveryTo
inventory.deliveryHours = deliveryHours
inventory.doubleDriving = doubleDriving
inventory.rate = rate
inventory.rate_multiply = rate_multiply
inventory.estimate_type = estimate_type
inventory.storage = storage
inventory.valuation = JSON.stringify(valuation)
inventory.weight_brutto = weight_brutto
inventory.weight_tare = weight_tare
inventory.items_weight = items_weight
inventory.items_volume = items_volume
inventory.items_count = items_count
inventory.items_total = items_total
inventory.bulky_count = bulky_count
inventory.bulky_total = bulky_total
inventory.extras_count = extras_count
inventory.extras_total = extras_total
inventory.packages_count = packages_count
inventory.packages_total = packages_total
inventory.fuel_percent = fuel_percent
inventory.fuel_total = fuel_total
inventory.edited_total = edited_total
inventory.custom_total = custom_total
inventory.edited_packages = edited_packages
inventory.custom_packages = custom_packages
inventory.custom_unpackage = custom_unpackage
inventory.edited_weight = edited_weight
inventory.custom_weight = custom_weight
inventory.edited_volume = edited_volume
inventory.custom_volume = custom_volume
inventory.discount_comment = discount_comment
inventory.discount_cash = discount_cash
inventory.total = total
inventory.paid = paid
inventory.due = due
await inventory.save()
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: inventory.id, time })
} catch (err) {
next(err)
}
}
}