/var/www/greso.tech/server/nsm/controllers/company
Edit: /var/www/greso.tech/server/nsm/controllers/company/create.js (2481B)
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 {
var {
name,
short_name,
dba,
description,
addressId,
prefix,
phone,
email,
site,
usdot,
mc,
config,
address
} = req.body
if (!isSet(name) || isEmpty(name) || !minLength(name, 2)) {
throw new BadRequest({
code: 1037,
lang: req.user?.languageCode
})
}
if (isSet(description) && !isEmpty(description) && !minLength(description, 4)) {
throw new BadRequest({
code: 2002,
lang: req.user?.languageCode
})
}
if (isSet(prefix) && !isEmpty(prefix) && !minLength(prefix, 1)) {
throw new BadRequest({
code: 2026,
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(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
})
}
// not required
if (isSet(addressId) && !isEmpty(addressId) && !minLength(addressId, 4)) {
throw new BadRequest({
code: 2004,
lang: req.user?.languageCode
})
}
if (isSet(address) && !isEmpty(address)) {
const addr = await db.address.create(address)
if (addr) {
addressId = addr.id
}
}
const company = await db.company.create({
name,
short_name,
dba,
description,
addressId,
prefix,
phone,
email,
site,
usdot,
mc,
config,
})
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: company.id, time })
} catch (err) {
next(err)
}
}
}