/var/www/greso.tech/server/nsm/controllers/branch
Edit: /var/www/greso.tech/server/nsm/controllers/branch/create.js (2298B)
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 { agent, companyId, addressId, name, short_name, dba, phone, email, site, description, usdot, mc } = req.body
// required
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.create({
agent,
companyId,
addressId,
name,
short_name,
dba,
phone,
email,
site,
description,
usdot,
mc,
})
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: branch.id, time })
} catch (err) {
next(err)
}
}
}