/var/www/greso.tech/server/nsm/controllers/order/inventory/extra
Edit: /var/www/greso.tech/server/nsm/controllers/order/inventory/extra/add.js (1462B)
module.exports = function ({ db, logger }) {
const { NotFound, BadRequest } = require('../../../../responseModels/errors')
const { isSet, isEmail, isPhone, isEmpty, minLength, isNumber } = require('../../../../utils/validator.util')
const uuid = require('uuid');
return async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
let t1 = performance.now()
try {
var {
id,
extraId,
inventoryId,
name,
description,
type,
direction,
price,
value,
minSumm,
comment
} = req.body
// check id
if (!isSet(id) || isEmpty(id)) {
id = uuid.v4()
}
var extra = await db.inventoryExtras.findOne({
where: { id: id }
})
if(extra){
extra.name = name
extra.description = description
extra.extraId = extraId
extra.orderInventoryId = inventoryId
extra.type = type
extra.direction = direction
extra.price = price
extra.value = value
extra.minSumm = minSumm
extra.comment = comment
await extra.save()
}else{
extra = await db.inventoryExtras.create({
id,
extraId,
orderInventoryId: inventoryId,
name,
description,
type,
direction,
price,
value,
minSumm,
comment
})
}
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: extra.id, time })
} catch (err) {
next(err)
}
}
}