generated from amelspahic/typescript-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addonCall.ts
345 lines (313 loc) · 7.19 KB
/
addonCall.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { Request } from 'express';
import {
AddonLogDto,
KitsasBookInterface,
KitsasConnectionInterface,
LanguageString,
LogStatus,
Notification,
NotificationType,
OfficeType,
} from 'kitsas-library';
import { AddonSession } from './addonSession.interface';
import { KitsasAddon } from './kitsasAddon';
/**
* Call information of addon
*
* This class is used to get information about the call, the user, the book etc. and to operate with Kitsas Server.
*
* Usually you get an instance of this class in the very first line of the route handlers.
*/
export class AddonCall {
private request: Request;
private session: AddonSession;
private myConnection: KitsasConnectionInterface;
/**
* Constructor for AddonCall
*
* @param request - Request object from express
*/
constructor(request: Request) {
this.request = request;
this.session = request.session as AddonSession;
const addon: KitsasAddon = request.app.get('Addon');
this.myConnection = addon.getConnection();
}
/**
* Get connection to Kitsas Server
* @returns Connection to Kitsas Server
*/
public connection(): KitsasConnectionInterface {
return this.myConnection;
}
/**
* Username
*
* @returns Name of the user
*/
public userName(): string {
return this.session.call?.user.name ?? '';
}
/**
* User Id
*
* @returns Id of the user
*/
public userId(): string {
return this.session.call?.user.id ?? '';
}
/**
* User email
*
* @returns Email of the user
*/
public userEmail(): string {
return this.session.call?.user.email ?? '';
}
/**
*
* User rights
*
* @returns Rights of the user
*/
public userRights(): string[] {
return this.session.call?.user.rights ?? [];
}
/**
* Organization (book) name
*
* @returns Name of the organization
*/
public organizationName(): string {
return this.session.call?.organization.name ?? '';
}
/**
* Organization (book) Id
*
* @returns Id of the organization
*/
public organizationId(): string {
return this.session.call?.organization.id ?? '';
}
/**
* Organization (book) businessId
*
* @returns businessId of the organization
*/
public businessId(): string {
return this.session.call?.organization.businessId ?? '';
}
/**
* Office Id
*
* @returns Id of the office
*/
public officeId(): string {
return this.session.call?.office?.id ?? '';
}
/**
* Office name
*
* @returns Name of the office
*/
public officeName(): string {
return this.session.call?.office?.name ?? '';
}
/**
* Office businessId
*
* @returns Business id of the office
*/
public officeBusinessId(): string {
return this.session.call?.office?.businessId ?? '';
}
/**
* Office type
*
* @returns Type of the office
*/
public officeType(): OfficeType {
return this.session.call?.office?.type ?? OfficeType.KITSAS;
}
/**
*
* Rights of this sessios
*
* @returns Rights of this session
*/
public rigths(): string[] {
return this.session.call?.rights ?? [];
}
/**
* Is the addon active i.e. does it have rights
*
* @returns True if the addon is active
*/
public isActive(): boolean {
return (
(this.session.call?.rights && this.session.call?.rights?.length > 0) ||
false
);
}
/**
* Log information
*
* @returns Log information
*/
public logInfo() {
return {
user: this.userName(),
userId: this.userId(),
organization: this.organizationName(),
organizationId: this.organizationId(),
};
}
/**
* Get session stored custom data
* @param key Key of the data
* @returns Data or undefined if not found
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get(key: string): any {
return this.session.data?.[key] || undefined;
}
/**
* Store custom data to session
* @param key Key of the data
* @param value Data value
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set(key: string, value: any) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const oldObject: { [key: string]: any } = this.session.data || {};
oldObject[key] = value;
this.session.data = oldObject;
}
/**
* Get language of the call
* @returns Language code
*/
language(): string {
return this.session.language || 'fi';
}
/**
* Weite to the log
* @param status Status of the action
* @param message Log message
* @param data Additional data
*/
async log(status: LogStatus, message: string, data?: object): Promise<void> {
await this.connection().writeAddonLog(
this.organizationId(),
status,
message,
data
);
}
/**
* Read the log
*
* @returns Log entries
*/
async getLogs(): Promise<AddonLogDto[]> {
return await this.connection().getAddonLog(this.organizationId());
}
/**
* Store data to the server
*
* @param key key of the data
* @param data data value
*/
async saveData(key: string, data: object): Promise<void> {
await this.connection().saveData(this.organizationId(), key, data);
}
/**
*
* Fetch data stored on the server
*
* @param key key of the data
* @returns data value
*/
async getData(key: string): Promise<object> {
return await this.connection().getData(this.organizationId(), key);
}
/**
* Add a notify
*
* @param type Notification type
* @param title Title, in languages
* @param text Text, in languages
* @param category Category of the notification
*/
async notify(
type: NotificationType,
title: LanguageString,
text: LanguageString,
category?: string
): Promise<void> {
await this.connection().addNotification(
this.organizationId(),
type,
title,
text,
category
);
}
/**
* Add or replace notification with same type
*
* @param type Notification type
* @param title Title, in languages
* @param text Text, in languages
* @param category Category of the notification
*/
async replaceNotification(
type: NotificationType,
title: LanguageString,
text: LanguageString,
category: string
): Promise<void> {
await this.connection().replaceNotification(
this.organizationId(),
type,
title,
text,
category
);
}
/**
* List notifications
*
* @returns Notifications
*/
async notifications(): Promise<Notification[]> {
return await this.connection().getNotifications(this.organizationId());
}
/**
* Delete notifications
*
* @param category Category of the notification (optional)
*/
async deleteNotifications(category?: string): Promise<void> {
await this.connection().deleteNotifications(
this.organizationId(),
category
);
}
/**
* Get the book object to interact with the book
*
* @returns Book object
*/
async getBook(): Promise<KitsasBookInterface> {
return await this.connection().getBook(this.organizationId());
}
/**
* Get the base URL of the service
*
* @returns Base URL of the service
*/
baseUrl(): string {
return this.request.app.locals.baseUrl;
}
}