/var/www/greso.tech/server/nsm/controllers/config
Edit: /var/www/greso.tech/server/nsm/controllers/config/edit.js (1227B)
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, key, value, comment } = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({
code: 2007,
lang: req.user?.languageCode
})
}
if (!isSet(key) || isEmpty(key) || !minLength(key, 2)) {
throw new BadRequest({
code: 2037,
lang: req.user?.languageCode
})
}
if (!isSet(value) || isEmpty(value)) {
throw new BadRequest({
code: 2059,
lang: req.user?.languageCode
})
}
const config = await db.config.findOne({
where: { id: id }
})
if (!config) {
throw new NotFound({ lang: req.user?.languageCode })
}
config.key = key
config.value = value
config.comment = comment
await config.save()
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: config.id, time })
} catch (err) {
next(err)
}
}
}