|
| 1 | +import { joinPaths } from '../../util/path'; |
| 2 | +import type { DeletedObject, Role } from '../resources'; |
| 3 | +import { AbstractAPI } from './AbstractApi'; |
| 4 | + |
| 5 | +const basePath = '/organizations_roles'; |
| 6 | + |
| 7 | +type GetRoleListParams = { |
| 8 | + limit?: number; |
| 9 | + offset?: number; |
| 10 | + query?: string; |
| 11 | + order_by?: string; |
| 12 | +}; |
| 13 | + |
| 14 | +type CreateParams = { |
| 15 | + /** |
| 16 | + * A name of a role in a readable friendly format. |
| 17 | + * F.e. `Teacher` or `Administrator` |
| 18 | + */ |
| 19 | + name: string; |
| 20 | + |
| 21 | + /** |
| 22 | + * A unique identifier that represents the role. |
| 23 | + * F.e. `org:administrator` |
| 24 | + */ |
| 25 | + key: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * A brief description of what the role represents or its intended use. |
| 29 | + */ |
| 30 | + description: string; |
| 31 | + |
| 32 | + /** |
| 33 | + * An array of permission ids that will be assigned to this role. |
| 34 | + */ |
| 35 | + permissions: string[]; |
| 36 | +}; |
| 37 | + |
| 38 | +type GetOrganizationRoleParams = { roleId: string }; |
| 39 | + |
| 40 | +type UpdateParams = { |
| 41 | + /** |
| 42 | + * A name of a role in a readable friendly format. |
| 43 | + * F.e. `Teacher` or `Administrator` |
| 44 | + * Passing undefined has no effect to the existing value. |
| 45 | + */ |
| 46 | + name?: string; |
| 47 | + |
| 48 | + /** |
| 49 | + * A unique identifier that represents the role. |
| 50 | + * F.e. `org:administrator` |
| 51 | + * Passing undefined has no effect to the existing value. |
| 52 | + */ |
| 53 | + key?: string; |
| 54 | + |
| 55 | + /** |
| 56 | + * A brief description of what the role represents or its intended use. |
| 57 | + * Passing undefined has no effect to the existing value. |
| 58 | + */ |
| 59 | + description?: string; |
| 60 | + |
| 61 | + /** |
| 62 | + * An array of permission ids that will be assigned to this role. |
| 63 | + * Passing undefined has no effect to the permission that already exist. |
| 64 | + * Passing an empty array will override the existing permissions. |
| 65 | + */ |
| 66 | + permissions?: string[]; |
| 67 | +}; |
| 68 | + |
| 69 | +type RemovePermissionParams = { |
| 70 | + permissionId: string; |
| 71 | + roleId: string; |
| 72 | +}; |
| 73 | + |
| 74 | +type AssignPermissionParams = RemovePermissionParams; |
| 75 | + |
| 76 | +export class OrganizationRoleAPI extends AbstractAPI { |
| 77 | + public async getOrganizationRoleList(params?: GetRoleListParams) { |
| 78 | + return this.request<Role[]>({ |
| 79 | + method: 'GET', |
| 80 | + path: basePath, |
| 81 | + queryParams: params, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + public async createOrganizationRole(params: CreateParams) { |
| 86 | + return this.request<Role>({ |
| 87 | + method: 'POST', |
| 88 | + path: basePath, |
| 89 | + bodyParams: params, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public async getOrganizationRole(params: GetOrganizationRoleParams) { |
| 94 | + this.requireId(params.roleId); |
| 95 | + |
| 96 | + return this.request<Role>({ |
| 97 | + method: 'GET', |
| 98 | + path: joinPaths(basePath, params.roleId), |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + public async updateOrganizationRole(roleId: string, params: UpdateParams) { |
| 103 | + this.requireId(roleId); |
| 104 | + return this.request<Role>({ |
| 105 | + method: 'PATCH', |
| 106 | + path: joinPaths(basePath, roleId), |
| 107 | + bodyParams: params, |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + public async deleteOrganizationRole(roleId: string) { |
| 112 | + this.requireId(roleId); |
| 113 | + return this.request<DeletedObject>({ |
| 114 | + method: 'DELETE', |
| 115 | + path: joinPaths(basePath, roleId), |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + public async assignPermissionToRole(params: AssignPermissionParams) { |
| 120 | + const { roleId, permissionId } = params; |
| 121 | + this.requireId(roleId); |
| 122 | + this.requireId(permissionId); |
| 123 | + return this.request<Role>({ |
| 124 | + method: 'POST', |
| 125 | + path: joinPaths(basePath, roleId, 'permission', permissionId), |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + public async removePermissionFromRole(params: RemovePermissionParams) { |
| 130 | + const { roleId, permissionId } = params; |
| 131 | + this.requireId(roleId); |
| 132 | + this.requireId(permissionId); |
| 133 | + return this.request<Role>({ |
| 134 | + method: 'DELETE', |
| 135 | + path: joinPaths(basePath, roleId, 'permission', permissionId), |
| 136 | + }); |
| 137 | + } |
| 138 | +} |
0 commit comments