/var/www/greso.tech/server/nsm/controllers/company
Edit: /var/www/greso.tech/server/nsm/controllers/company/edit.js (3676B)
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 {
id,
name,
short_name,
dba,
description,
addressId,
prefix,
phone,
email,
site,
usdot,
mc,
config,
address
} = req.body
if (!isSet(id) || isEmpty(id)) {
throw new BadRequest({ code: 400, lang: req.user?.languageCode })
}
if (!isSet(name) || isEmpty(name) || !minLength(name, 2)) {
throw new BadRequest({
code: 1037,
lang: req.user?.languageCode
})
}
if (!isSet(short_name) || isEmpty(short_name) || !minLength(short_name, 2)) {
throw new BadRequest({
code: 2105,
lang: req.user?.languageCode
})
}
if (!isSet(dba) || isEmpty(dba)) {
dba = ''
}
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.findOne({
where: { id: address.id }
})
if (!addr) {
const newAddr = await db.address.create(address)
if (newAddr) {
addressId = addr.id
}
} else {
addr.city = address.city
addr.state = address.state
addr.street = address.street
addr.street_number = address.street_number
addr.number = address.number
addr.zip = address.zip
addr.comment = address.comment
addr.doorbell = address.doorbell
addr.floor = address.floor
addr.latitude = address.latitude
addr.longitude = address.longitude
await addr.save()
}
}
const company = await db.company.findOne({
where: { id: id }
})
if (!company) {
throw new NotFound({ lang: req.user?.languageCode })
}
company.name = name
company.short_name = short_name
company.dba = dba
company.description = description
company.addressId = addressId
company.prefix = prefix
company.phone = phone
company.email = email
company.site = site
company.usdot = usdot
company.mc = mc
company.config = config
await company.save()
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: company.id, time })
} catch (err) {
next(err)
}
}
}