-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Space.ts
113 lines (100 loc) · 2.73 KB
/
Space.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
import { QlikSaaSClient } from "qlik-rest-api";
import { Actions } from "../types/types";
import { Assignment, IAssignment, IClassAssignment } from "./Assignment";
export interface ISpace {
id: string;
type: string;
ownerId: string;
tenantId: string;
name: string;
description: string;
createdAt: string;
createdBy: string;
updatedAt: string;
meta: {
actions: Actions[];
roles: string[];
assignableRoles: string[];
};
links: {
self: {
href: string;
};
assignments: {
href: string;
};
};
}
export interface ISpaceUpdate {
name?: string;
description?: string;
ownerId?: string;
}
export interface IAssignmentCreate {
type?: string;
assigneeId?: string;
roles?: string[];
}
export interface IClassSpace {
details: ISpace;
remove(): Promise<number>;
update(arg: ISpaceUpdate): Promise<number>;
assignments(): Promise<IClassAssignment[]>;
}
export class Space implements IClassSpace {
private id: string;
private saasClient: QlikSaaSClient;
details: ISpace;
constructor(saasClient: QlikSaaSClient, id: string, details?: ISpace) {
if (!id) throw new Error(`app.get: "id" parameter is required`);
this.id = id;
this.saasClient = saasClient;
if (details) this.details = details;
}
async init() {
if (!this.details) {
this.details = await this.saasClient
.Get(`spaces/${this.id}`)
.then((res) => res.data as ISpace);
}
}
async remove() {
return await this.saasClient
.Delete(`spaces/${this.id}`)
.then((res) => res.status);
}
async update(arg: ISpaceUpdate) {
let data: { [k: string]: any } = {};
if (arg.name) data["name"] = arg.name;
if (arg.description) data["description"] = arg.description;
if (arg.ownerId) data["ownerId"] = arg.ownerId;
return await this.saasClient.Put(`spaces/${this.id}`, data).then((res) => {
this.details = res.data;
return res.status;
});
}
async assignments() {
return await this.saasClient
.Delete(`spaces/${this.id}/assignments`)
.then((res) => res.data as IAssignment[])
.then((assignments) =>
assignments.map(
(assignment) =>
new Assignment(this.saasClient, assignment.id, this.id, assignment)
)
);
}
async assignmentCreate(arg: IAssignmentCreate) {
let data: { [k: string]: any } = {};
if (arg.type) data["type"] = arg.type;
if (arg.type) data["type"] = arg.type;
if (arg.roles) data["roles"] = arg.roles;
return await this.saasClient
.Post(`spaces/${this.id}/assignments`, data)
.then((res) => res.data as IAssignment)
.then(
(assignment) =>
new Assignment(this.saasClient, assignment.id, this.id, assignment)
);
}
}