/var/www/greso.tech/server/nsm/controllers/branch
Edit: /var/www/greso.tech/server/nsm/controllers/branch/edit.js (2744B)
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, agent, companyId, addressId, name, short_name, dba, phone, email, site, description, usdot, mc } = req.body
// required
if (!isSet(id) || isEmpty(id) || !minLength(id, 32)) {
throw new BadRequest({
code: 2007,
lang: req.user?.languageCode
})
}
if (!isSet(companyId) || isEmpty(companyId) || !minLength(companyId, 32)) {
throw new BadRequest({
code: 2003,
lang: req.user?.languageCode
})
}
if (!isSet(name) || isEmpty(name) || !minLength(name, 2)) {
throw new BadRequest({
code: 2005,
lang: req.user?.languageCode
})
}
// not required
if (isSet(addressId) && !isEmpty(addressId) && !minLength(addressId, 4)) {
throw new BadRequest({
code: 2004,
lang: req.user?.languageCode
})
}
if (isSet(phone) && !isEmpty(phone)&& !minLength(phone, 10)) {
throw new BadRequest({
code: 1004,
lang: req.user?.languageCode
})
}
if (isSet(email) && !isEmpty(email) && !isEmail(email)) {
throw new BadRequest({
code: 1003,
lang: req.user?.languageCode
})
}
if (isSet(site) && !isEmpty(site) && !minLength(site, 4)) {
throw new BadRequest({
code: 2006,
lang: req.user?.languageCode
})
}
if (isSet(description) && !isEmpty(description) && !minLength(description, 4)) {
throw new BadRequest({
code: 2002,
lang: req.user?.languageCode
})
}
if (isSet(usdot) && !isEmpty(usdot) && !minLength(usdot, 1)) {
throw new BadRequest({
code: 2042,
lang: req.user?.languageCode
})
}
if (isSet(mc) && !isEmpty(mc) && !minLength(mc, 1)) {
throw new BadRequest({
code: 2043,
lang: req.user?.languageCode
})
}
const branch = await db.branch.findOne({
where: { id: id }
})
if (!branch) {
throw new NotFound({ lang: req.user?.languageCode })
}
branch.agent = agent
branch.companyId = companyId
branch.addressId = addressId
branch.name = name
branch.short_name = short_name
branch.dba = dba
branch.phone = phone
branch.email = email
branch.site = site
branch.description = description
branch.usdot = usdot
branch.mc = mc
await branch.save()
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: branch.id, time })
} catch (err) {
next(err)
}
}
}