41 lines
915 B
JavaScript
41 lines
915 B
JavaScript
const Role = require('./roleModel')
|
|
const AppError = require('../../globalErrorHandler/AppError')
|
|
|
|
module.exports.getAllRoles = async () => {
|
|
return Role.find({}, null, null)
|
|
}
|
|
|
|
module.exports.createRole = async (data) => {
|
|
return await Role.create(data)
|
|
}
|
|
|
|
module.exports.getRole = async (roleId) => {
|
|
const role = await Role.findById(roleId)
|
|
|
|
if (!role) {
|
|
throw new AppError('NoRoleFound', 404, {})
|
|
}
|
|
|
|
return role
|
|
}
|
|
|
|
module.exports.updateRole = async (roleId, data) => {
|
|
const role = await Role.findOneAndUpdate({ _id: roleId }, data, { new: true })
|
|
|
|
if (!role) {
|
|
throw new AppError('NoRoleFound', 404, {})
|
|
}
|
|
|
|
return role
|
|
}
|
|
|
|
module.exports.deleteRole = async (roleId) => {
|
|
const role = await Role.findByIdAndUpdate(roleId, { isActive: false }, { new: true })
|
|
|
|
if (!role) {
|
|
throw new AppError('NoRoleFound', 404, {})
|
|
}
|
|
|
|
return null
|
|
}
|