|
1 | | -import { IRolePrivilegeRepo } from 'database/repository.interfaces/role.privilege.repo.interface'; |
2 | | -import { RolePrivilegeDto } from 'domain.types/role/role.privilege.dto'; |
| 1 | +import { IRolePrivilegeRepo } from '../../../repository.interfaces/role.privilege.repo.interface'; |
| 2 | +import RolePrivilege from '../models/role.privilege.model'; |
| 3 | +import { RolePrivilegeDto } from '../../../../domain.types/role/role.privilege.dto'; |
| 4 | +import { Logger } from '../../../../common/logger'; |
| 5 | +import { ApiError } from '../../../../common/api.error'; |
| 6 | +import { Op } from 'sequelize'; |
| 7 | + |
| 8 | +/////////////////////////////////////////////////////////////////////// |
3 | 9 |
|
4 | 10 | export class RolePrivilegeRepo implements IRolePrivilegeRepo { |
5 | | - create(entity: any): Promise<RolePrivilegeDto> { |
6 | | - throw new Error('Method not implemented.'); |
7 | | - } |
| 11 | + create = async (object: any): Promise<RolePrivilegeDto> => { |
| 12 | + try { |
| 13 | + const entity = { |
| 14 | + RoleId: object.RoleId, |
| 15 | + Privilege: object.Privilege, |
| 16 | + }; |
| 17 | + const rolePrivilege = await RolePrivilege.create(entity); |
| 18 | + const dto: RolePrivilegeDto = { |
| 19 | + id: rolePrivilege.id, |
| 20 | + RoleId: rolePrivilege.RoleId, |
| 21 | + Privilege: rolePrivilege.Privilege, |
| 22 | + }; |
| 23 | + return dto; |
| 24 | + } catch (error) { |
| 25 | + Logger.instance().log(error.message); |
| 26 | + throw new ApiError(500, error.message); |
| 27 | + } |
| 28 | + }; |
8 | 29 |
|
9 | | - getById(id: string): Promise<RolePrivilegeDto> { |
10 | | - throw new Error('Method not implemented.'); |
11 | | - } |
| 30 | + getById = async (id: string): Promise<RolePrivilegeDto> => { |
| 31 | + try { |
| 32 | + const rolePrivilege = await RolePrivilege.findByPk(id); |
| 33 | + const dto: RolePrivilegeDto = { |
| 34 | + id: rolePrivilege.id, |
| 35 | + RoleId: rolePrivilege.RoleId, |
| 36 | + Privilege: rolePrivilege.Privilege, |
| 37 | + }; |
| 38 | + return dto; |
| 39 | + } catch (error) { |
| 40 | + Logger.instance().log(error.message); |
| 41 | + throw new ApiError(500, error.message); |
| 42 | + } |
| 43 | + }; |
12 | 44 |
|
13 | | - search(): Promise<RolePrivilegeDto[]> { |
14 | | - throw new Error('Method not implemented.'); |
15 | | - } |
| 45 | + search = async (): Promise<RolePrivilegeDto[]> => { |
| 46 | + try { |
| 47 | + const rolePrivileges = await RolePrivilege.findAll(); |
| 48 | + const dtos: RolePrivilegeDto[] = []; |
| 49 | + for (let i = 0; i < rolePrivileges.length; i++) { |
| 50 | + const rp = rolePrivileges[i]; |
| 51 | + const dto: RolePrivilegeDto = { |
| 52 | + id: rp.id, |
| 53 | + RoleId: rp.RoleId, |
| 54 | + Privilege: rp.Privilege, |
| 55 | + }; |
| 56 | + dtos.push(dto); |
| 57 | + } |
| 58 | + return dtos; |
| 59 | + } catch (error) { |
| 60 | + Logger.instance().log(error.message); |
| 61 | + throw new ApiError(500, error.message); |
| 62 | + } |
| 63 | + }; |
16 | 64 |
|
17 | | - getPrivilegesForRole(roleId: string): Promise<RolePrivilegeDto[]> { |
18 | | - throw new Error('Method not implemented.'); |
19 | | - } |
| 65 | + getPrivilegesForRole = async (roleId: string): Promise<RolePrivilegeDto[]> => { |
| 66 | + try { |
| 67 | + const rolePrivileges = await RolePrivilege.findAll({ |
| 68 | + where: { |
| 69 | + RoleId: roleId, |
| 70 | + }, |
| 71 | + }); |
| 72 | + const dtos: RolePrivilegeDto[] = []; |
| 73 | + for (let i = 0; i < rolePrivileges.length; i++) { |
| 74 | + const rp = rolePrivileges[i]; |
| 75 | + const dto: RolePrivilegeDto = { |
| 76 | + id: rp.id, |
| 77 | + RoleId: rp.RoleId, |
| 78 | + Privilege: rp.Privilege, |
| 79 | + }; |
| 80 | + dtos.push(dto); |
| 81 | + } |
| 82 | + return dtos; |
| 83 | + } catch (error) { |
| 84 | + Logger.instance().log(error.message); |
| 85 | + throw new ApiError(500, error.message); |
| 86 | + } |
| 87 | + }; |
20 | 88 |
|
21 | | - hasPrivilegeForRole(roleId: string, privilege: string): Promise<boolean> { |
22 | | - throw new Error('Method not implemented.'); |
23 | | - } |
| 89 | + hasPrivilegeForRole = async (roleId: string, privilege: string): Promise<boolean> => { |
| 90 | + try { |
| 91 | + const rolePrivileges = await RolePrivilege.findAll({ |
| 92 | + where: { |
| 93 | + RoleId: roleId, |
| 94 | + Privilege: { [Op.like]: '%' + privilege + '%' }, |
| 95 | + }, |
| 96 | + }); |
| 97 | + return rolePrivileges.length > 0; |
| 98 | + } catch (error) { |
| 99 | + Logger.instance().log(error.message); |
| 100 | + throw new ApiError(500, error.message); |
| 101 | + } |
| 102 | + }; |
24 | 103 |
|
25 | | - delete(id: string): Promise<boolean> { |
26 | | - throw new Error('Method not implemented.'); |
27 | | - } |
| 104 | + delete = async (id: string): Promise<boolean> => { |
| 105 | + try { |
| 106 | + await RolePrivilege.destroy({ where: { id: id } }); |
| 107 | + return true; |
| 108 | + } catch (error) { |
| 109 | + Logger.instance().log(error.message); |
| 110 | + throw new ApiError(500, error.message); |
| 111 | + } |
| 112 | + }; |
28 | 113 | } |
0 commit comments