/var/www/qr4y.com/server/delivery/api/controllers
Edit: /var/www/qr4y.com/server/delivery/api/controllers/companyTypes.controller.js (3134B)
const logger = require('../../logger')({ service: 'CompanyTypesController' })
const checkPermission = require('../utils/checkPermission.util')
const { NotFound, BadRequest } = require('../responseModels/errors')
const { isSet, isEmpty, minLength, isPhone, isEmail } = require('../utils/validator.util')
module.exports = function ({ db }) {
this.List = async (req, res, next) => {
logger.child({ path: req.path, request: req.user?.id }).info()
try {
// checkPermission(req, 'company_types_list')
const company_types = await db.companyType.findAll()
if (!company_types || company_types.length == 0) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(company_types)
} catch (err) {
next(err)
}
}
this.Details = async (req, res, next) => {
logger.child({ path: req.path, request: req.user.id }).info()
try {
checkPermission(req, 'company_types_list')
const { id } = req.params
const company_type = await db.companyType.findOne({ where: { id: id } })
if (!company_type) {
throw new NotFound({ lang: req.user?.languageCode })
}
return res.status(200).json(company_type)
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.user.id }).info()
try {
checkPermission(req, 'company_types_edit')
const { name, desc, image, countryCodeId, rootTypeId, adult } = req.body
const company_type = await db.companyType.create({ name, desc, image, countryCodeId, rootTypeId, adult })
return res.status(200).json({
status: 'success',
id: company_type.id
})
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.user.id }).info()
try {
checkPermission(req, 'company_types_edit')
const { id, name, desc, image, countryCodeId, rootTypeId, adult } = req.body
const company_type = await db.companyType.findOne({ where: { id: id } })
if (!company_type) {
throw new NotFound({ lang: req.user?.languageCode })
}
company_type.name = name
company_type.desc = desc
company_type.image = image
company_type.countryCodeId = countryCodeId
company_type.rootTypeId = rootTypeId
company_type.adult = adult ?? false
await company_type.save()
return res.status(200).json(company_type)
} catch (err) {
next(err)
}
}
this.Delete = async (req, res, next) => {
logger.child({ path: req.path, request: req.user.id }).info()
try {
checkPermission(req, 'company_types_edit')
const { id } = req.body
const company_types = await db.companyType.findAll(
{ where: { rootTypeId: id } }
)
if (company_types && company_types.length > 0) {
throw new BadRequest({ code: 1063, lang: req.user?.languageCode })
}
const company_type = await db.companyType.findOne({ where: { id: id } })
if (!company_type) {
throw new NotFound({ lang: req.user?.languageCode })
}
await db.companyType.destroy({ where: { id: id } })
return res.status(200).json({ status: 'success', id: company_type.id })
} catch (err) {
next(err)
}
}
}