/var/www/greso.tech/server/nsm/controllers/company
NameSizeModeActions
category/-0755rm
client/-0755rm
condition/-0755rm
document/-0755rm
email_template/-0755rm
extra/-0755rm
items/-0755rm
lead_distribution/-0755rm
merchant/-0755rm
package/-0755rm
pay_type/-0755rm
provider/-0755rm
rate_area/-0755rm
rate_item/-0755rm
rate_local/-0755rm
truck/-0755rm
user/-0755rm
valuation/-0755rm
warehouse/-0755rm
zip_radius/-0755rm
create.js24810644editdlrm
delete.js8380644editdlrm
details.js4630644editdlrm
edit.js36760644editdlrm
list.js11750644editdlrm
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) } } }