/
Group.ts
74 lines (64 loc) · 1.63 KB
/
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
import { QlikSaaSClient } from "qlik-rest-api";
export interface IGroup {
id: string;
name: string;
idpid: string;
links: {
href: string;
};
status: "active" | "disabled";
tenantId: string;
createdAt: string;
assignedRoles: {
id: string;
name: string;
type: string;
level: "admin" | "user";
permissions: string[];
}[];
lastUpdatedAt: string;
}
export type IGroupUpdate =
| {
id: string;
}
| { name: string };
export class Group {
#id: string;
#saasClient: QlikSaaSClient;
details: IGroup;
constructor(saasClient: QlikSaaSClient, id: string, details?: IGroup) {
if (!id) throw new Error(`group.get: "id" parameter is required`);
this.details = details ?? ({} as IGroup);
this.#id = id;
this.#saasClient = saasClient;
}
async init(arg?: { force: true }) {
if (Object.keys(this.details).length == 0 || arg?.force == true) {
this.details = await this.#saasClient
.Get<IGroup>(`groups/${this.#id}`)
.then((res) => res.data);
}
}
async remove() {
return await this.#saasClient
.Delete(`groups/${this.#id}`)
.then((res) => res.status);
}
async update(arg: IGroupUpdate[]) {
if (!arg) throw new Error(`group.update: update arguments are missing`);
const data = arg.map((a) => ({
path: `/assignedRoles`,
value: a,
op: "replace",
}));
let updateStatus: number = -1;
return await this.#saasClient
.Patch(`groups/${this.#id}`, data)
.then((res) => {
updateStatus = res.status;
return this.init({ force: true });
})
.then(() => updateStatus);
}
}