/var/www/verywell.gr/server/delivery/api/controllers
Edit: /var/www/verywell.gr/server/delivery/api/controllers/role.controller.js (4012B)
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 { uid } = req.params
const role = await db.role.findOne({
include: { model: db.permission, through: { attributes: [] } },
where: { uid: uid }
})
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',
uid: role.uid
})
} 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 { uid, name, key, image } = req.body
let role = await db.role.findOne({
where: { uid: uid }
})
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 { uid } = req.body
if (!uid) {
throw new BadRequest({ lang: req.user.languageCode })
}
const role = await db.role.findOne({ where: { uid: uid } })
if (!role) {
throw new NotFound({ lang: req.user.languageCode })
}
await db.role.destroy({ where: { uid: uid } })
return res.status(200).json({ status: 'success', uid: role.uid })
} 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 { roleUid, permissionUid } = req.body
if (!roleUid || !permissionUid) {
throw new BadRequest({ lang: req.user.languageCode })
}
let role = await db.role.findOne({
where: { uid: roleUid }
})
const permission = await db.permission.findOne({
where: { uid: permissionUid }
})
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 { roleUid, permissionUid } = req.body
let role = await db.role.findOne({ where: { uid: roleUid } })
const permission = await db.permission.findOne({
where: { uid: permissionUid }
})
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)
}
}
}