2025-01-05 14:14:00 +01:00
|
|
|
import { dataSource } from "../../data-source";
|
|
|
|
import { role } from "../../entity/user/role";
|
|
|
|
import InternalException from "../../exceptions/internalException";
|
2024-08-27 17:54:59 +02:00
|
|
|
|
|
|
|
export default abstract class RoleService {
|
2024-08-28 20:41:16 +02:00
|
|
|
/**
|
|
|
|
* @description get roles
|
|
|
|
* @returns {Promise<Array<role>>}
|
|
|
|
*/
|
|
|
|
static async getAll(): Promise<Array<role>> {
|
|
|
|
return await dataSource
|
|
|
|
.getRepository(role)
|
|
|
|
.createQueryBuilder("role")
|
2024-09-01 14:55:05 +02:00
|
|
|
.leftJoinAndSelect("role.permissions", "role_permissions")
|
2025-01-12 18:17:54 +01:00
|
|
|
.orderBy("role", "ASC")
|
2024-08-28 20:41:16 +02:00
|
|
|
.getMany()
|
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2024-09-06 10:08:19 +02:00
|
|
|
throw new InternalException("roles not found", err);
|
2024-08-28 20:41:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-27 17:54:59 +02:00
|
|
|
/**
|
|
|
|
* @description get role by id
|
|
|
|
* @param id number
|
|
|
|
* @returns {Promise<role>}
|
|
|
|
*/
|
|
|
|
static async getById(id: number): Promise<role> {
|
|
|
|
return await dataSource
|
|
|
|
.getRepository(role)
|
|
|
|
.createQueryBuilder("role")
|
2024-09-01 14:55:05 +02:00
|
|
|
.leftJoinAndSelect("role.permissions", "role_permissions")
|
2024-08-27 17:54:59 +02:00
|
|
|
.where("role.id = :id", { id: id })
|
|
|
|
.getOneOrFail()
|
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2024-09-06 10:08:19 +02:00
|
|
|
throw new InternalException("role not found by id", err);
|
2024-08-27 17:54:59 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|