/var/www/greso.tech/server/nsm/controllers/company/package
Edit: /var/www/greso.tech/server/nsm/controllers/company/package/edit.js (1829B)
module.exports = function ({ db, logger }) {
const { NotFound, BadRequest } = require('../../../responseModels/errors')
const { isSet, isEmail, isPhone, isEmpty, minLength, isNumber } = require('../../../utils/validator.util')
return async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
let t1 = performance.now()
try {
const { id, companyId, name, itemPrice, packingPrice, unpackingPrice } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ code: 400, lang: req.user?.languageCode })
}
if (!isSet(companyId) || isEmpty(companyId) || !isNumber(companyId)) {
throw new BadRequest({
code: 2003,
lang: req.user?.languageCode
})
}
if (!isSet(name) || isEmpty(name) || !minLength(name, 1)) {
throw new BadRequest({
code: 2005,
lang: req.user?.languageCode
})
}
if (isSet(itemPrice) && isEmpty(itemPrice)) {
throw new BadRequest({
code: 2033,
lang: req.user?.languageCode
})
}
if (isSet(packingPrice) && isEmpty(packingPrice)) {
throw new BadRequest({
code: 2033,
lang: req.user?.languageCode
})
}
if (isSet(unpackingPrice) && isEmpty(unpackingPrice)) {
throw new BadRequest({
code: 2033,
lang: req.user?.languageCode
})
}
const package = await db.packages.findOne({
where: { id: id }
})
if (!package) {
throw new NotFound({ lang: req.user?.languageCode })
}
package.name = name
package.companyId = companyId
package.itemPrice = itemPrice
package.packingPrice = packingPrice
package.unpackingPrice = unpackingPrice
await package.save()
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: package.id, time })
} catch (err) {
next(err)
}
}
}