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.ts
197 lines (158 loc) · 4.32 KB
/
user.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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";
export type UserData = models.APIData["getUser"];
export class User implements models.User {
#api: models.API;
#client: Creator;
#data: UserData;
constructor(init: BasicObjectInit<UserData>) {
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 "User" as const;
}
get id() {
return this.#data.id;
}
get email() {
return this.#data.email;
}
get username() {
return this.#data.username;
}
get domain() {
return this.#data.domain;
}
get name() {
return this.#data.name;
}
get firstName() {
return this.#data.firstName;
}
get lastName() {
return this.#data.lastName;
}
get notes() {
return this.#data.notes;
}
get isTrashed() {
return this.#data.status === "Trash";
}
get isExcludedFromRestrictions() {
return this.#data.exclude;
}
get locationId() {
return this.#data.locationId;
}
async update() {
// This shouldn't be wrapped in try/catch because its failure is an error
// the user should know about.
this.#data = await this.#api.getUser(this.#data.id);
}
async getDevices(): Promise<models.Device[]> {
let devices;
try {
devices = await this.#api.getDevices({ ownerId: this.#data.id });
} catch (e: unknown) {
return suppressAPIError([], e);
}
return devices.map((device) => this.#client.createDevice(device));
}
async getGroups(): Promise<models.UserGroup[]> {
let userGroupData;
try {
userGroupData = await this.#api.getUserGroups();
} catch (e: unknown) {
return suppressAPIError([], e);
}
const groupIds = new Set(this.#data.groupIds);
const myGroups = userGroupData.filter((group) => groupIds.has(group.id));
return myGroups.map((group) => this.#client.createUserGroup(group));
}
async getClasses(): Promise<models.UserGroup[]> {
let userGroupData;
try {
userGroupData = await this.#api.getUserGroups();
} catch (e: unknown) {
return suppressAPIError([], e);
}
const groupIds = new Set(this.#data.teacherGroups);
const myClasses = userGroupData.filter((group) => groupIds.has(group.id));
return myClasses.map((group) => this.#client.createUserGroup(group));
}
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 setUsername(username: string) {
if (this.#data.username !== username) {
await this.#api.updateUser(this.#data.id, { username });
}
}
async setDomain(domain: string) {
if (this.#data.domain !== domain) {
await this.#api.updateUser(this.#data.id, { domain });
}
}
async setFirstName(name: string) {
if (this.#data.firstName !== name) {
await this.#api.updateUser(this.#data.id, { firstName: name });
}
}
async setLastName(name: string) {
if (this.#data.lastName !== name) {
await this.#api.updateUser(this.#data.id, { lastName: name });
}
}
async setPassword(password: string) {
await this.#api.updateUser(this.#data.id, { password });
}
async setEmail(email: string) {
if (this.#data.email !== email) {
await this.#api.updateUser(this.#data.id, { email });
}
}
async setGroups(groups: { id: number }[]) {
await this.#api.updateUser(this.#data.id, {
memberOf: groups.map((group) => group.id),
});
}
async setClasses(groups: { id: number }[]) {
await this.#api.updateUser(this.#data.id, {
teacher: groups.map((group) => group.id),
});
}
async setChildren(users: { id: number }[]) {
await this.#api.updateUser(this.#data.id, {
children: users.map((user) => user.id),
});
}
async setLocation(location: { id: number }) {
if (this.#data.locationId !== location.id) {
await this.#api.moveUser(this.#data.id, location.id);
}
}
async restartDevices() {
const devices = await this.#api.getDevices({ ownerId: this.#data.id });
const promises = devices.map((device) => this.#api.restartDevice(device.UDID));
await Promise.allSettled(promises);
}
}