/var/www/qr4y.gr/server/delivery/api/controllers
Edit: /var/www/qr4y.gr/server/delivery/api/controllers/companyTypes.controller.js (2722B)
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 { uid } = req.params
const company_type = await db.companyType.findOne({ where: { uid: uid } })
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, image } = req.body
const company_type = await db.companyType.create({ name, image })
return res.status(200).json({
status: 'success',
uid: company_type.uid
})
} 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 { uid, name, image } = req.body
const company_type = await db.companyType.findOne({ where: { uid: uid } })
if (!company_type) {
throw new NotFound({ lang: req.user?.languageCode })
}
;(company_type.name = name), (company_type.image = image)
await db.companyType.update(
{ name, image },
{
where: { uid: uid }
}
)
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 { uid } = req.body
const company_type = await db.companyType.findOne({ where: { uid: uid } })
if (!company_type) {
throw new NotFound({ lang: req.user?.languageCode })
}
await db.companyType.destroy({ where: { uid: uid } })
return res.status(200).json({ status: 'success', uid: company_type.uid })
} catch (err) {
next(err)
}
}
}