This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_group.ts
139 lines (115 loc) · 2.93 KB
/
user_group.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type * as models from "../models/mod.ts";
import type { BasicObjectInit, Creator } from "./client.ts";
import { suppressAPIError } from "./api_error.ts";
import { customInspect } from "./custom_inspect.ts";
function convertStatusToACL(status: boolean | null): "allow" | "deny" | "inherit" {
switch (status) {
case true:
return "allow";
case false:
return "deny";
case null:
return "inherit";
default:
throw new Error("unreachable");
}
}
function convertACLToStatus(acl: "allow" | "deny" | "inherit"): boolean | null {
switch (acl) {
case "allow":
return true;
case "deny":
return false;
case "inherit":
return null;
default:
throw new Error("unreachable");
}
}
export type UserGroupData = models.APIData["getUserGroups"][number];
export class UserGroup implements models.UserGroup {
#api: models.API;
#client: Creator;
#data: UserGroupData;
constructor(init: BasicObjectInit<UserGroupData>) {
this.#api = init.api;
this.#client = init.client;
this.#data = init.data;
}
toJSON() {
return this.#data;
}
toString() {
return this.#data.name;
}
[Symbol.for("Deno.customInspect")]() {
return customInspect(this);
}
get type() {
return "UserGroup" as const;
}
get id() {
return this.#data.id;
}
get name() {
return this.#data.name;
}
get description() {
return this.#data.description;
}
get isParentGroup() {
return convertACLToStatus(this.#data.acl.parent);
}
get isTeacherGroup() {
return convertACLToStatus(this.#data.acl.teacher);
}
get locationId() {
return this.#data.locationId;
}
async update() {
this.#data = await this.#api.getUserGroup(this.#data.id);
}
async getUsers(): Promise<models.User[]> {
let allUsers;
try {
allUsers = await this.#api.getUsers();
} catch (e: unknown) {
return suppressAPIError([], e);
}
const myUsers = allUsers.filter((userData) =>
userData.groupIds.includes(this.#data.id)
);
return myUsers.map((user) => this.#client.createUser(user));
}
async getLocation() {
let locationData;
try {
locationData = await this.#api.getLocation(this.#data.locationId);
} catch (e: unknown) {
return suppressAPIError(null, e);
}
return this.#client.createLocation(locationData);
}
async setName(name: string) {
if (this.#data.name !== name) {
await this.#api.updateUserGroup(this.#data.id, { name });
}
}
async setDescription(text: string) {
if (this.#data.description !== text) {
await this.#api.updateUserGroup(this.#data.id, { description: text });
}
}
async setParentGroup(status: boolean | null) {
const parent = convertStatusToACL(status);
if (this.#data.acl.parent !== parent) {
await this.#api.updateUserGroup(this.#data.id, { acl: { parent } });
}
}
async setTeacherGroup(status: boolean | null) {
const teacher = convertStatusToACL(status);
if (this.#data.acl.teacher !== teacher) {
await this.#api.updateUserGroup(this.#data.id, { acl: { teacher } });
}
}
}