/var/www/greso.tech/server/nsm/controllers/company/condition
Edit: /var/www/greso.tech/server/nsm/controllers/company/condition/edit.js (1432B)
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, key } = 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(key) || isEmpty(key) || !minLength(key, 1)) {
throw new BadRequest({
code: 2005,
lang: req.user?.languageCode
})
}
const condition = await db.conditions.findOne({
where: { id: id }
})
if (!condition) {
throw new NotFound({ lang: req.user?.languageCode })
}
condition.name = name
condition.companyId = companyId
condition.key = key
await condition.save()
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: condition.id, time })
} catch (err) {
next(err)
}
}
}