ff-admin-server/src/command/rolePermissionCommandHandler.ts

49 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-08-27 17:54:59 +02:00
import { dataSource } from "../data-source";
import { rolePermission } from "../entity/role_permission";
import InternalException from "../exceptions/internalException";
import RoleService from "../service/roleService";
import { CreateRolePermissionCommand, DeleteRolePermissionCommand } from "./rolePermissionCommand";
export default abstract class UserPermissionCommandHandler {
/**
* @description grant permission to user
* @param CreateRolePermissionCommand
* @returns {Promise<number>}
*/
static async create(createPermission: CreateRolePermissionCommand): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
.into(rolePermission)
.values({
permission: createPermission.permission,
role: await RoleService.getById(createPermission.roleId),
})
.execute()
.then((result) => {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed saving role permission");
});
}
/**
* @description remove permission from role
* @param DeleteRolePermissionCommand
* @returns {Promise<any>}
*/
2024-08-28 20:41:16 +02:00
static async delete(deletePermission: DeleteRolePermissionCommand): Promise<any> {
2024-08-27 17:54:59 +02:00
return await dataSource
.createQueryBuilder()
.delete()
.from(rolePermission)
.where("permission.id = :id", { id: deletePermission.id })
.execute()
.then((res) => {})
.catch((err) => {
throw new InternalException("failed role permission removal");
});
}
}