44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { dataSource } from "../data-source";
|
|
import { rolePermission } from "../entity/role_permission";
|
|
import { userPermission } from "../entity/user_permission";
|
|
import InternalException from "../exceptions/internalException";
|
|
|
|
export default abstract class RolePermissionService {
|
|
/**
|
|
* @description get permission by role
|
|
* @param roleId number
|
|
* @returns {Promise<Array<rolePermission>>}
|
|
*/
|
|
static async getByRole(roleId: number): Promise<Array<rolePermission>> {
|
|
return await dataSource
|
|
.getRepository(rolePermission)
|
|
.createQueryBuilder("permission")
|
|
.where("permission.roleId = :roleId", { roleId: roleId })
|
|
.getMany()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new InternalException("permissions not found by role");
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @description get permission by roles
|
|
* @param roleIds Array<number>
|
|
* @returns {Promise<Array<rolePermission>>}
|
|
*/
|
|
static async getByRoles(roleIds: Array<number>): Promise<Array<rolePermission>> {
|
|
return await dataSource
|
|
.getRepository(rolePermission)
|
|
.createQueryBuilder("permission")
|
|
.where("permission.roleId IN (:...roleIds)", { roleIds: roleIds })
|
|
.getMany()
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
throw new InternalException("permissions not found by roles");
|
|
});
|
|
}
|
|
}
|