/var/www/qr4y.com/server/delivery/api/controllers
Edit: /var/www/qr4y.com/server/delivery/api/controllers/role.controller.js (3982B)
const logger = require('../../logger')({ service: 'api' })
const { RoleModel } = require('../responseModels')
const { NotFound, BadRequest } = require('../responseModels/errors')
const checkPermission = require('../utils/checkPermission.util')
module.exports = function ({ db }) {
this.List = async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
try {
checkPermission(req, 'role_list')
const roles = await db.role.findAll()
if (roles.length == 0 || !roles) {
throw new NotFound({ lang: req.user.languageCode })
}
return res.status(200).json(roles)
} catch (err) {
next(err)
}
}
this.Details = async (req, res, next) => {
logger.child({ path: req.path, request: req.params }).info()
try {
checkPermission(req, 'role_list')
const { id } = req.params
const role = await db.role.findOne({
include: { model: db.permission, through: { attributes: [] } },
where: { id: id }
})
if (!role) {
throw new NotFound({ lang: req.user.languageCode })
}
return res.status(200).json(new RoleModel(role))
} catch (err) {
next(err)
}
}
this.Create = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'role_edit')
const { name, key, image } = req.body
const role = await db.role.create({ name, key, image })
return res.status(200).json({
status: 'success',
id: role.id
})
} catch (err) {
next(err)
}
}
this.Edit = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'role_edit')
const { id, name, key, image } = req.body
let role = await db.role.findOne({
where: { id: id }
})
if (!role) {
throw new NotFound({ lang: req.user.languageCode })
}
role.name = name
role.key = key
role.image = image
await role.save()
return res.status(200).json(new RoleModel(role))
} catch (err) {
next(err)
}
}
this.Delete = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'role_edit')
const { id } = req.body
if (!id) {
throw new BadRequest({ lang: req.user.languageCode })
}
const role = await db.role.findOne({ where: { id: id } })
if (!role) {
throw new NotFound({ lang: req.user.languageCode })
}
await db.role.destroy({ where: { id: id } })
return res.status(200).json({ status: 'success', id: role.id })
} catch (err) {
next(err)
}
}
this.AddPermission = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'role_edit')
const { roleId, permissionId } = req.body
if (!roleId || !permissionId) {
throw new BadRequest({ lang: req.user.languageCode })
}
let role = await db.role.findOne({
where: { id: roleId }
})
const permission = await db.permission.findOne({
where: { id: permissionId }
})
if (!role) {
throw new NotFound({ lang: req.user.languageCode })
}
if (!permission) {
throw new NotFound({ lang: req.user.languageCode })
}
role.addPermission(permission)
await role.save()
return res.status(200).json({ status: 'success' })
} catch (err) {
next(err)
}
}
this.RemovePermission = async (req, res, next) => {
logger.child({ path: req.path, request: req.body }).info()
try {
checkPermission(req, 'role_edit')
const { roleId, permissionId } = req.body
let role = await db.role.findOne({ where: { id: roleId } })
const permission = await db.permission.findOne({
where: { id: permissionId }
})
if (!role) {
throw new NotFound({ lang: req.user.languageCode })
}
if (!permission) {
throw new NotFound({ lang: req.user.languageCode })
}
role.removePermission(permission)
await role.save()
return res.status(200).json({ status: 'success' })
} catch (err) {
next(err)
}
}
}