@juhuu/sdk-ts 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,989 @@
1
+ // src/index.service.ts
2
+ import io from "socket.io-client";
3
+ var Service = class {
4
+ constructor(config) {
5
+ this.environment = config.environment;
6
+ this.getAccessToken = config.getAccessToken;
7
+ this.onException = config.onException;
8
+ this.setAccessToken = config.setAccessToken;
9
+ this.getAccessToken = config.getAccessToken;
10
+ this.setRefreshToken = config.setRefreshToken;
11
+ this.getRefreshToken = config.getRefreshToken;
12
+ this.clientVersion = config.clientVersion;
13
+ switch (config.environment) {
14
+ case "development":
15
+ this.httpBaseUrl = "https://api.juhuu.dev/v1/";
16
+ this.wssBaseUrl = "wss://api.juhuu.dev/v1/";
17
+ break;
18
+ default:
19
+ this.httpBaseUrl = "https://api.juhuu.app/v1/";
20
+ this.wssBaseUrl = "wss://api.juhuu.app/v1/";
21
+ break;
22
+ }
23
+ }
24
+ environment;
25
+ httpBaseUrl;
26
+ wssBaseUrl;
27
+ clientVersion;
28
+ onException;
29
+ getAccessToken;
30
+ setAccessToken;
31
+ getRefreshToken;
32
+ setRefreshToken;
33
+ async sendRequest({
34
+ url,
35
+ method,
36
+ body = void 0,
37
+ useAuthentication
38
+ }, options = {}) {
39
+ if (options.triggerOnException === void 0) {
40
+ options.triggerOnException = true;
41
+ }
42
+ if (options.refreshTokensIfNecessary === void 0) {
43
+ options.refreshTokensIfNecessary = true;
44
+ }
45
+ let token = null;
46
+ if (useAuthentication === true && options.accessToken === void 0) {
47
+ token = await this.getAccessToken();
48
+ if (token === null) {
49
+ console.error(
50
+ "endpoint",
51
+ url,
52
+ "should use authentication but no token was found"
53
+ );
54
+ return {
55
+ ok: false,
56
+ data: null
57
+ };
58
+ }
59
+ } else if (useAuthentication === true && options.accessToken !== void 0) {
60
+ token = options.accessToken;
61
+ }
62
+ const uri = this.httpBaseUrl + url;
63
+ let response = null;
64
+ let responseObject = null;
65
+ try {
66
+ switch (method) {
67
+ case "GET": {
68
+ response = await this.sendGetRequest(token, uri);
69
+ break;
70
+ }
71
+ case "POST": {
72
+ response = await this.sendPostRequest(token, uri, body);
73
+ break;
74
+ }
75
+ case "PATCH": {
76
+ response = await this.sendPatchRequest(token, uri, body);
77
+ break;
78
+ }
79
+ }
80
+ if (response.ok === false) {
81
+ throw new Error(response.statusText);
82
+ }
83
+ responseObject = {
84
+ ok: response.ok,
85
+ data: await response.json(),
86
+ statusText: response.statusText,
87
+ status: response.status
88
+ };
89
+ } catch (error) {
90
+ console.error(error);
91
+ responseObject = {
92
+ ok: false,
93
+ data: await response?.json(),
94
+ statusText: response?.statusText ?? "no statusText",
95
+ status: response?.status ?? 0
96
+ };
97
+ if (responseObject.status === 403 && options.refreshTokensIfNecessary === true) {
98
+ console.log("refreshing tokens...");
99
+ const oldRefreshToken = await this.getRefreshToken();
100
+ console.log("old refresh token", oldRefreshToken);
101
+ if (oldRefreshToken === null) {
102
+ console.log("no old refresh token found");
103
+ return responseObject;
104
+ }
105
+ console.log("sending request to refresh tokens...");
106
+ const query = await this.sendRequest(
107
+ {
108
+ method: "GET",
109
+ url: "auth/refresh",
110
+ body: void 0,
111
+ useAuthentication: true
112
+ },
113
+ {
114
+ accessToken: oldRefreshToken,
115
+ // use old refresh token instead of access token
116
+ triggerOnException: false,
117
+ refreshTokensIfNecessary: false
118
+ }
119
+ );
120
+ console.log("query for new tokens", query);
121
+ if (query.ok === false) {
122
+ return responseObject;
123
+ }
124
+ const accessToken = query.data.accessToken;
125
+ const refreshToken = query.data.refreshToken;
126
+ await Promise.all([
127
+ this.setRefreshToken(refreshToken),
128
+ this.setAccessToken(accessToken)
129
+ ]);
130
+ console.log("retrying original request...");
131
+ const retryResponse = await this.sendRequest(
132
+ {
133
+ url,
134
+ method,
135
+ body,
136
+ useAuthentication
137
+ },
138
+ {
139
+ accessToken,
140
+ refreshTokensIfNecessary: false
141
+ }
142
+ );
143
+ console.log("retry response", retryResponse);
144
+ if (retryResponse.ok === true) {
145
+ return retryResponse;
146
+ } else {
147
+ return responseObject;
148
+ }
149
+ }
150
+ if (options.triggerOnException === true) {
151
+ await this.onException(responseObject);
152
+ }
153
+ } finally {
154
+ console.log(
155
+ method + ": " + uri + " (body: " + JSON.stringify(body, null, 2) + ") => " + JSON.stringify(responseObject, null, 2)
156
+ );
157
+ }
158
+ return responseObject;
159
+ }
160
+ async sendGetRequest(token, uri) {
161
+ return await fetch(uri, {
162
+ method: "GET",
163
+ headers: {
164
+ "Content-Type": "application/json",
165
+ "Client-Version": this.clientVersion,
166
+ Authorization: token ? `Bearer ${token}` : ""
167
+ }
168
+ });
169
+ }
170
+ async sendPostRequest(token, uri, body) {
171
+ return await fetch(uri, {
172
+ method: "POST",
173
+ headers: {
174
+ "Content-Type": "application/json",
175
+ "Client-Version": this.clientVersion,
176
+ Authorization: token ? `Bearer ${token}` : ""
177
+ },
178
+ body: JSON.stringify(body)
179
+ });
180
+ }
181
+ async sendPatchRequest(token, uri, body) {
182
+ return await fetch(uri, {
183
+ method: "PATCH",
184
+ headers: {
185
+ "Content-Type": "application/json",
186
+ "Client-Version": this.clientVersion,
187
+ Authorization: token ? `Bearer ${token}` : ""
188
+ },
189
+ body: JSON.stringify(body)
190
+ });
191
+ }
192
+ connectToWebsocket({ url }) {
193
+ const uri = this.wssBaseUrl + url;
194
+ console.log("connecting to websocket", uri);
195
+ const socket = io(uri, { transports: ["websocket"] });
196
+ socket.on("connect", () => {
197
+ console.log("connected to websocket", uri);
198
+ });
199
+ socket.on("connect_error", (error) => {
200
+ console.error("Connection error:", error);
201
+ });
202
+ return socket;
203
+ }
204
+ disconnectFromWebsocket(socket) {
205
+ socket.disconnect();
206
+ }
207
+ };
208
+
209
+ // src/sessions/sessions.service.ts
210
+ var SessionService = class extends Service {
211
+ constructor(config) {
212
+ super(config);
213
+ }
214
+ async create(SessionCreateParams, SessionCreateOptions) {
215
+ return await super.sendRequest({
216
+ method: "POST",
217
+ url: "sessions",
218
+ body: {
219
+ locationId: SessionCreateParams.locationId,
220
+ tariffId: SessionCreateParams.tariffId,
221
+ autoRenew: SessionCreateParams.autoRenew,
222
+ type: SessionCreateParams.sessionType,
223
+ isOffSession: SessionCreateParams.isOffSession,
224
+ idempotencyKey: SessionCreateParams.idempotencyKey,
225
+ userId: SessionCreateParams.userId
226
+ },
227
+ useAuthentication: true
228
+ });
229
+ }
230
+ async retrieve(SessionRetrieveParams, SessionRetrieveOptions) {
231
+ const queryArray = [];
232
+ if (SessionRetrieveOptions?.expand !== void 0) {
233
+ queryArray.push("expand=" + SessionRetrieveOptions.expand.join(","));
234
+ }
235
+ return await super.sendRequest({
236
+ method: "GET",
237
+ url: "sessions/" + SessionRetrieveParams.sessionId + "?" + queryArray.join("&"),
238
+ body: void 0,
239
+ useAuthentication: true
240
+ });
241
+ }
242
+ async list(SessionListParams, SessionListOptions) {
243
+ const queryArray = [];
244
+ if (SessionListParams.userId !== void 0) {
245
+ queryArray.push("userId=" + SessionListParams.userId);
246
+ }
247
+ if (SessionListParams.propertyId !== void 0) {
248
+ queryArray.push("propertyId=" + SessionListParams.propertyId);
249
+ }
250
+ if (SessionListParams.statusArray !== void 0) {
251
+ queryArray.push("statusArray=" + SessionListParams.statusArray.join(","));
252
+ }
253
+ if (SessionListParams.managementUserId !== void 0) {
254
+ queryArray.push("managementUserId=" + SessionListParams.managementUserId);
255
+ }
256
+ return await super.sendRequest(
257
+ {
258
+ method: "GET",
259
+ url: "sessions?" + queryArray.join("&"),
260
+ body: void 0,
261
+ useAuthentication: true
262
+ },
263
+ SessionListOptions
264
+ );
265
+ }
266
+ async update(SessionUpdateParams, SessionUpdateOptions) {
267
+ return await super.sendRequest(
268
+ {
269
+ method: "PATCH",
270
+ url: "sessions/" + SessionUpdateParams.sessionId,
271
+ body: {
272
+ autoRenew: SessionUpdateParams?.autoRenew
273
+ },
274
+ useAuthentication: true
275
+ },
276
+ SessionUpdateOptions
277
+ );
278
+ }
279
+ async terminate(SessionTerminateParams, SessionTerminateOptions) {
280
+ return await super.sendRequest(
281
+ {
282
+ method: "PATCH",
283
+ url: "sessions/" + SessionTerminateParams.sessionId + "/terminate",
284
+ body: {
285
+ isOffSession: SessionTerminateParams.isOffSession
286
+ },
287
+ useAuthentication: true
288
+ },
289
+ SessionTerminateOptions
290
+ );
291
+ }
292
+ async attachLocation(SessionTerminateParams, SessionTerminateOptions) {
293
+ return await super.sendRequest(
294
+ {
295
+ method: "PATCH",
296
+ url: "sessions/" + SessionTerminateParams.sessionId + "/attachLocation",
297
+ body: {
298
+ locationId: SessionTerminateParams.locationId
299
+ },
300
+ useAuthentication: true
301
+ },
302
+ SessionTerminateOptions
303
+ );
304
+ }
305
+ };
306
+
307
+ // src/links/links.service.ts
308
+ var LinkService = class extends Service {
309
+ constructor(config) {
310
+ super(config);
311
+ }
312
+ async create() {
313
+ }
314
+ async retrieve(LinkRetrieveParams, LinkRetrieveOptions) {
315
+ const queryArray = [];
316
+ return await super.sendRequest({
317
+ method: "GET",
318
+ url: "links/" + LinkRetrieveParams.linkId + "?" + queryArray.join("&"),
319
+ body: void 0,
320
+ useAuthentication: false
321
+ });
322
+ }
323
+ async list(LinkListParams, LinkListOptions) {
324
+ const queryArray = [];
325
+ if (LinkListParams.propertyId !== void 0) {
326
+ queryArray.push("propertyId=" + LinkListParams.propertyId);
327
+ }
328
+ if (LinkListParams.fiveLetterQr !== void 0) {
329
+ queryArray.push("fiveLetterQr=" + LinkListParams.fiveLetterQr);
330
+ }
331
+ return await super.sendRequest(
332
+ {
333
+ method: "GET",
334
+ url: "links?" + queryArray.join("&"),
335
+ body: void 0,
336
+ useAuthentication: true
337
+ },
338
+ LinkListOptions
339
+ );
340
+ }
341
+ async update() {
342
+ }
343
+ async delete() {
344
+ }
345
+ async terminate() {
346
+ }
347
+ };
348
+
349
+ // src/users/users.service.ts
350
+ var UsersService = class extends Service {
351
+ constructor(config) {
352
+ super(config);
353
+ }
354
+ async create() {
355
+ }
356
+ async retrieve(UserRetrieveParams, UserRetrieveOptions) {
357
+ const queryArray = [];
358
+ return await super.sendRequest(
359
+ {
360
+ method: "GET",
361
+ url: "users/" + UserRetrieveParams.userId + "?" + queryArray.join("&"),
362
+ body: void 0,
363
+ useAuthentication: true
364
+ },
365
+ UserRetrieveOptions
366
+ );
367
+ }
368
+ async exists(UserExistsParams, UserExistsOptions) {
369
+ return await super.sendRequest(
370
+ {
371
+ method: "GET",
372
+ url: "users/exists?email=" + UserExistsParams.email,
373
+ body: void 0,
374
+ useAuthentication: false
375
+ },
376
+ UserExistsOptions
377
+ );
378
+ }
379
+ async registerEmailPassword(UserRegisterEmailPasswordParams, UserRegisterEmailPasswordOptions) {
380
+ return await super.sendRequest(
381
+ {
382
+ method: "POST",
383
+ url: "auth/emailPassword/register",
384
+ body: {
385
+ email: UserRegisterEmailPasswordParams.email,
386
+ password: UserRegisterEmailPasswordParams.password
387
+ },
388
+ useAuthentication: false
389
+ },
390
+ UserRegisterEmailPasswordOptions
391
+ );
392
+ }
393
+ async loginEmailPassword(UserLoginEmailPasswordParams, UserLoginEmailPasswordOptions) {
394
+ return await super.sendRequest(
395
+ {
396
+ method: "POST",
397
+ url: "auth/emailPassword/login",
398
+ body: {
399
+ email: UserLoginEmailPasswordParams.email,
400
+ password: UserLoginEmailPasswordParams.password
401
+ },
402
+ useAuthentication: false
403
+ },
404
+ UserLoginEmailPasswordOptions
405
+ );
406
+ }
407
+ async paymentMethodTokens(UserPaymentMethodTokensParams, UserPaymentMethodTokensOptions) {
408
+ return await super.sendRequest(
409
+ {
410
+ method: "GET",
411
+ url: "users/" + UserPaymentMethodTokensParams.userId + "/paymentMethodTokens",
412
+ body: void 0,
413
+ useAuthentication: true
414
+ },
415
+ UserPaymentMethodTokensOptions
416
+ );
417
+ }
418
+ async refreshAccessToken(UserRefreshAccessTokenParams, UserRefreshAccessTokenOptions) {
419
+ return await super.sendRequest(
420
+ {
421
+ method: "GET",
422
+ url: "auth/refresh",
423
+ body: void 0,
424
+ useAuthentication: true
425
+ },
426
+ {
427
+ accessToken: UserRefreshAccessTokenParams?.refreshToken,
428
+ ...UserRefreshAccessTokenOptions
429
+ }
430
+ );
431
+ }
432
+ async list(UserListParams, UserListOptions) {
433
+ const queryArray = [];
434
+ if (UserListParams.managementUserId !== void 0) {
435
+ queryArray.push("managementUserId=" + UserListParams.managementUserId);
436
+ }
437
+ return await super.sendRequest(
438
+ {
439
+ method: "GET",
440
+ url: "users?" + queryArray.join("&"),
441
+ body: void 0,
442
+ useAuthentication: true
443
+ },
444
+ UserListOptions
445
+ );
446
+ }
447
+ async update(UserUpdateParams, UserUpdateOptions) {
448
+ return await super.sendRequest(
449
+ {
450
+ method: "PATCH",
451
+ url: "users/" + UserUpdateParams.userId,
452
+ body: {
453
+ name: UserUpdateParams?.name,
454
+ licenseArray: UserUpdateParams?.licenseArray,
455
+ platform: UserUpdateParams?.platform,
456
+ languageCode: UserUpdateParams?.languageCode,
457
+ appVersion: UserUpdateParams?.appVersion,
458
+ billingAddress: UserUpdateParams?.billingAddress,
459
+ vat: UserUpdateParams?.vat,
460
+ acceptedTermIdArray: UserUpdateParams?.acceptedTermIdArray
461
+ },
462
+ useAuthentication: true
463
+ },
464
+ UserUpdateOptions
465
+ );
466
+ }
467
+ async inviteEmployee(UserInviteEmployeeParams, UserInviteEmployeeOptions) {
468
+ return await super.sendRequest(
469
+ {
470
+ method: "POST",
471
+ url: "users/" + UserInviteEmployeeParams.userId + "/inviteEmployee",
472
+ body: {
473
+ userId: UserInviteEmployeeParams?.userIdToInvite,
474
+ email: UserInviteEmployeeParams?.email
475
+ },
476
+ useAuthentication: true
477
+ },
478
+ UserInviteEmployeeOptions
479
+ );
480
+ }
481
+ async removeEmployee(UserRemoveEmployeeParams, UserRemoveEmployeeOptions) {
482
+ return await super.sendRequest(
483
+ {
484
+ method: "PATCH",
485
+ url: "users/" + UserRemoveEmployeeParams.userId + "/removeEmployee",
486
+ body: {
487
+ userId: UserRemoveEmployeeParams.userIdToRemove
488
+ },
489
+ useAuthentication: true
490
+ },
491
+ UserRemoveEmployeeOptions
492
+ );
493
+ }
494
+ };
495
+
496
+ // src/payments/payments.service.ts
497
+ var PaymentsService = class extends Service {
498
+ constructor(config) {
499
+ super(config);
500
+ }
501
+ async create() {
502
+ }
503
+ async retrieve(PaymentRetrieveParams, PaymentRetrieveOptions) {
504
+ const queryArray = [];
505
+ if (PaymentRetrieveOptions?.expand !== void 0) {
506
+ queryArray.push("expand=" + PaymentRetrieveOptions.expand.join(","));
507
+ }
508
+ return await super.sendRequest({
509
+ method: "GET",
510
+ url: "payments/" + PaymentRetrieveParams.paymentId + "?" + queryArray.join("&"),
511
+ body: void 0,
512
+ useAuthentication: true
513
+ });
514
+ }
515
+ async tokens(PaymentTokensParams, PaymentTokensOptions) {
516
+ return await super.sendRequest(
517
+ {
518
+ method: "GET",
519
+ url: "payments/" + PaymentTokensParams.paymentId + "/tokens",
520
+ body: void 0,
521
+ useAuthentication: true
522
+ },
523
+ PaymentTokensOptions
524
+ );
525
+ }
526
+ async update() {
527
+ }
528
+ async delete() {
529
+ }
530
+ async retrieveInvoiceUrl(PaymentRetrieveInvoiceUrlParams, PaymentRetrieveInvoiceUrlOptions) {
531
+ const queryArray = [];
532
+ return await super.sendRequest({
533
+ method: "GET",
534
+ url: "payments/" + PaymentRetrieveInvoiceUrlParams.paymentId + "/invoiceUrl",
535
+ body: void 0,
536
+ useAuthentication: true
537
+ });
538
+ }
539
+ };
540
+
541
+ // src/properties/properties.service.ts
542
+ var PropertiesService = class extends Service {
543
+ constructor(config) {
544
+ super(config);
545
+ }
546
+ async create() {
547
+ }
548
+ async retrieve(PropertyRetrieveParams, PropertyRetrieveOptions) {
549
+ const queryArray = [];
550
+ return await super.sendRequest({
551
+ method: "GET",
552
+ url: "properties/" + PropertyRetrieveParams.propertyId + "?" + queryArray.join("&"),
553
+ body: void 0,
554
+ useAuthentication: false
555
+ });
556
+ }
557
+ async list(PropertyListParams, PropertyListOptions) {
558
+ const queryArray = [];
559
+ return await super.sendRequest(
560
+ {
561
+ method: "GET",
562
+ url: "properties?" + queryArray.join("&"),
563
+ body: void 0,
564
+ useAuthentication: false
565
+ },
566
+ PropertyListOptions
567
+ );
568
+ }
569
+ async update() {
570
+ }
571
+ async delete() {
572
+ }
573
+ async terminate() {
574
+ }
575
+ };
576
+
577
+ // src/points/points.service.ts
578
+ var PointsService = class extends Service {
579
+ constructor(config) {
580
+ super(config);
581
+ }
582
+ async map(PointListParams, PointListOptions) {
583
+ const queryArray = [
584
+ "modalityArray=" + PointListParams.modalityArray.join(","),
585
+ "categoryArray=" + PointListParams.categoryArray.join(","),
586
+ "sectorArray=" + PointListParams.sectorArray.join(","),
587
+ "longitudeTopLeft=" + PointListParams.longitudeTopLeft,
588
+ "latitudeTopLeft=" + PointListParams.latitudeTopLeft,
589
+ "longitudeBottomRight=" + PointListParams.longitudeBottomRight,
590
+ "latitudeBottomRight=" + PointListParams.latitudeBottomRight
591
+ ];
592
+ return await super.sendRequest(
593
+ {
594
+ method: "GET",
595
+ url: "points/map?" + queryArray.join("&"),
596
+ body: void 0,
597
+ useAuthentication: false
598
+ },
599
+ PointListOptions
600
+ );
601
+ }
602
+ toRadians(degrees) {
603
+ return degrees * Math.PI / 180;
604
+ }
605
+ calculateDistance(lat1, lon1, lat2, lon2) {
606
+ var R = 6371e3;
607
+ var \u03C61 = this.toRadians(lat1);
608
+ var \u03C62 = this.toRadians(lat2);
609
+ var \u0394\u03C6 = this.toRadians(lat2 - lat1);
610
+ var \u0394\u03BB = this.toRadians(lon2 - lon1);
611
+ var a = Math.sin(\u0394\u03C6 / 2) * Math.sin(\u0394\u03C6 / 2) + Math.cos(\u03C61) * Math.cos(\u03C62) * Math.sin(\u0394\u03BB / 2) * Math.sin(\u0394\u03BB / 2);
612
+ var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
613
+ return R * c;
614
+ }
615
+ calculateAltitudeForTopDownView(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon) {
616
+ var diagonalDistance = this.calculateDistance(
617
+ topLeftLat,
618
+ topLeftLon,
619
+ bottomRightLat,
620
+ bottomRightLon
621
+ );
622
+ const diagonalDistanceHalf = Math.round(diagonalDistance / 2);
623
+ if (diagonalDistanceHalf < 0) {
624
+ return 0;
625
+ }
626
+ if (diagonalDistanceHalf > 1e6) {
627
+ return 1e6;
628
+ }
629
+ return diagonalDistanceHalf;
630
+ }
631
+ };
632
+
633
+ // src/devices/devices.service.ts
634
+ var DevicesService = class extends Service {
635
+ constructor(config) {
636
+ super(config);
637
+ }
638
+ async create() {
639
+ }
640
+ async retrieve(DeviceRetrieveParams, DeviceRetrieveOptions) {
641
+ const queryArray = [];
642
+ if (DeviceRetrieveParams?.source !== void 0) {
643
+ queryArray.push("source=" + DeviceRetrieveParams.source);
644
+ }
645
+ if (DeviceRetrieveOptions?.expand !== void 0) {
646
+ queryArray.push("expand=" + DeviceRetrieveOptions.expand.join(","));
647
+ }
648
+ return await super.sendRequest(
649
+ {
650
+ method: "GET",
651
+ url: "devices/" + DeviceRetrieveParams.deviceId + "?" + queryArray.join("&"),
652
+ body: void 0,
653
+ useAuthentication: false
654
+ },
655
+ DeviceRetrieveOptions
656
+ );
657
+ }
658
+ async list(DeviceListParams, DeviceListOptions) {
659
+ const queryArray = [];
660
+ if (DeviceListParams?.statusArray !== void 0) {
661
+ queryArray.push("statusArray=" + DeviceListParams.statusArray.join(","));
662
+ }
663
+ if (DeviceListParams?.rentable !== void 0) {
664
+ queryArray.push("rentable=" + DeviceListParams.rentable);
665
+ }
666
+ if (DeviceListParams?.propertyId !== void 0) {
667
+ queryArray.push("propertyId=" + DeviceListParams.propertyId);
668
+ }
669
+ if (DeviceListParams?.visible !== void 0) {
670
+ queryArray.push("visible=" + DeviceListParams.visible);
671
+ }
672
+ if (DeviceListParams?.deviceTemplateId !== void 0) {
673
+ queryArray.push("deviceTemplateId=" + DeviceListParams.deviceTemplateId);
674
+ }
675
+ if (DeviceListParams?.termId !== void 0) {
676
+ queryArray.push("termId=" + DeviceListParams.termId);
677
+ }
678
+ return await super.sendRequest(
679
+ {
680
+ method: "GET",
681
+ url: "devices?" + queryArray.join("&"),
682
+ body: void 0,
683
+ useAuthentication: false
684
+ },
685
+ DeviceListOptions
686
+ );
687
+ }
688
+ async update() {
689
+ }
690
+ async delete() {
691
+ }
692
+ listen(DeviceRealtimeParams, DeviceRealtimeOptions) {
693
+ const socket = super.connectToWebsocket({
694
+ url: "devices/" + DeviceRealtimeParams.deviceId + "/websocket"
695
+ });
696
+ const onUpdated = (onUpdatedCallback) => {
697
+ socket.on("updated", (message) => {
698
+ onUpdatedCallback(message);
699
+ });
700
+ };
701
+ return {
702
+ onUpdated,
703
+ close: () => {
704
+ console.log("closing websocket connection");
705
+ socket.close();
706
+ }
707
+ };
708
+ }
709
+ async message(DeviceMessageParams, DeviceMessageOptions) {
710
+ return await super.sendRequest(
711
+ {
712
+ method: "POST",
713
+ url: "devices?" + DeviceMessageParams.deviceId + "/message",
714
+ body: {
715
+ message: DeviceMessageParams.message
716
+ },
717
+ useAuthentication: true
718
+ },
719
+ DeviceMessageOptions
720
+ );
721
+ }
722
+ async parameterUpdate(DeviceParameterParams, DeviceParameterOptions) {
723
+ return await super.sendRequest(
724
+ {
725
+ method: "PATCH",
726
+ url: "devices/" + DeviceParameterParams.deviceId + "/parameter/" + DeviceParameterParams.parameterName,
727
+ body: {
728
+ value: DeviceParameterParams.value
729
+ },
730
+ useAuthentication: true
731
+ },
732
+ DeviceParameterOptions
733
+ );
734
+ }
735
+ async commandExecute(DeviceCommandExecuteParams, DeviceCommandExecuteOptions) {
736
+ return await super.sendRequest(
737
+ {
738
+ method: "POST",
739
+ url: "devices/" + DeviceCommandExecuteParams.deviceId + "/command/" + DeviceCommandExecuteParams.commandName,
740
+ body: {},
741
+ useAuthentication: true
742
+ },
743
+ DeviceCommandExecuteOptions
744
+ );
745
+ }
746
+ };
747
+
748
+ // src/deviceTemplates/deviceTemplates.service.ts
749
+ var DeviceTemplatesService = class extends Service {
750
+ constructor(config) {
751
+ super(config);
752
+ }
753
+ async create() {
754
+ }
755
+ async retrieve(DeviceTemplateRetrieveParams, DeviceTemplateRetrieveOptions) {
756
+ const queryArray = [];
757
+ if (DeviceTemplateRetrieveParams?.source !== void 0) {
758
+ queryArray.push("source=" + DeviceTemplateRetrieveParams.source);
759
+ }
760
+ return await super.sendRequest(
761
+ {
762
+ method: "GET",
763
+ url: "deviceTemplates/" + DeviceTemplateRetrieveParams.deviceTemplateId + "?" + queryArray.join("&"),
764
+ body: void 0,
765
+ useAuthentication: false
766
+ },
767
+ DeviceTemplateRetrieveOptions
768
+ );
769
+ }
770
+ async update() {
771
+ }
772
+ async delete() {
773
+ }
774
+ async terminate() {
775
+ }
776
+ };
777
+
778
+ // src/locations/locations.service.ts
779
+ var LocationsService = class extends Service {
780
+ constructor(config) {
781
+ super(config);
782
+ }
783
+ async retrieve(LocationRetrieveParams, LocationRetrieveOptions) {
784
+ const queryArray = [];
785
+ if (LocationRetrieveOptions?.expand !== void 0) {
786
+ queryArray.push("expand=" + LocationRetrieveOptions.expand.join(","));
787
+ }
788
+ return await super.sendRequest(
789
+ {
790
+ method: "GET",
791
+ url: "locations/" + LocationRetrieveParams.locationId + "?" + queryArray.join("&"),
792
+ body: void 0,
793
+ useAuthentication: false
794
+ },
795
+ LocationRetrieveOptions
796
+ );
797
+ }
798
+ async list(LocationListParams, LocationListOptions) {
799
+ const queryArray = [];
800
+ if (LocationListParams?.rentableDeviceGroupLocationId !== void 0) {
801
+ queryArray.push(
802
+ "rentableDeviceGroupLocationId=" + LocationListParams.rentableDeviceGroupLocationId
803
+ );
804
+ }
805
+ return await super.sendRequest(
806
+ {
807
+ method: "GET",
808
+ url: "locations?" + queryArray.join("&"),
809
+ body: void 0,
810
+ useAuthentication: false
811
+ },
812
+ LocationListOptions
813
+ );
814
+ }
815
+ };
816
+
817
+ // src/terms/terms.service.ts
818
+ var TermsService = class extends Service {
819
+ constructor(config) {
820
+ super(config);
821
+ }
822
+ async retrieve(TermRetrieveParams, TermRetrieveOptions) {
823
+ const queryArray = [];
824
+ if (TermRetrieveOptions?.expand !== void 0) {
825
+ queryArray.push("expand=" + TermRetrieveOptions.expand.join(","));
826
+ }
827
+ return await super.sendRequest(
828
+ {
829
+ method: "GET",
830
+ url: "terms/" + TermRetrieveParams.termId + "?" + queryArray.join("&"),
831
+ body: void 0,
832
+ useAuthentication: false
833
+ },
834
+ TermRetrieveOptions
835
+ );
836
+ }
837
+ async accept(TermAcceptParams, TermAcceptOptions) {
838
+ return await super.sendRequest(
839
+ {
840
+ method: "PATCH",
841
+ url: "terms/" + TermAcceptParams.termId + "/accept",
842
+ body: {
843
+ userId: TermAcceptParams.userId
844
+ },
845
+ useAuthentication: true
846
+ },
847
+ TermAcceptOptions
848
+ );
849
+ }
850
+ };
851
+
852
+ // src/tariffs/tariffs.service.ts
853
+ var TariffsService = class extends Service {
854
+ constructor(config) {
855
+ super(config);
856
+ }
857
+ async create() {
858
+ }
859
+ // async retrieve(
860
+ // TariffRetrieveParams: JUHUU.Tariff.Retrieve.Params,
861
+ // TariffRetrieveOptions?: JUHUU.Tariff.Retrieve.Options,
862
+ // ): Promise<JUHUU.HttpResponse<JUHUU.Tariff.Retrieve.Response>> {
863
+ // const queryArray: string[] = [];
864
+ // return await super.sendRequest<JUHUU.Tariff.Retrieve.Response>({
865
+ // method: "GET",
866
+ // url:
867
+ // "tariffs/" +
868
+ // TariffRetrieveParams.propertyId +
869
+ // "?" +
870
+ // queryArray.join("&"),
871
+ // body: undefined,
872
+ // useAuthentication: false,
873
+ // });
874
+ // }
875
+ // async list(
876
+ // TariffListParams: JUHUU.Tariff.List.Params,
877
+ // TariffListOptions?: JUHUU.Tariff.List.Options,
878
+ // ): Promise<JUHUU.HttpResponse<JUHUU.Tariff.List.Response>> {
879
+ // const queryArray: string[] = [];
880
+ // return await super.sendRequest<JUHUU.Tariff.List.Response>(
881
+ // {
882
+ // method: "GET",
883
+ // url: "tariffs?" + queryArray.join("&"),
884
+ // body: undefined,
885
+ // useAuthentication: false,
886
+ // },
887
+ // TariffListOptions,
888
+ // );
889
+ // }
890
+ async update() {
891
+ }
892
+ async delete() {
893
+ }
894
+ async terminate() {
895
+ }
896
+ /**
897
+ * Checks if the tariff is free
898
+ */
899
+ isFree(tariff) {
900
+ if (tariff.amount.every((amount) => amount === 0) === true && tariff.continue === 0) {
901
+ return true;
902
+ }
903
+ return false;
904
+ }
905
+ /**
906
+ * Returns the amount of seconds the tariff needs to be active before the amount will not change anymore
907
+ */
908
+ getAmountFinalizationDuration(tariff) {
909
+ if (this.isFree(tariff) === true) {
910
+ return 0;
911
+ }
912
+ if (tariff.interval === 0) {
913
+ return 0;
914
+ }
915
+ if (tariff.continue === 0) {
916
+ return tariff.interval * tariff.amount.length;
917
+ } else {
918
+ return tariff.duration;
919
+ }
920
+ }
921
+ /**
922
+ * Calculates the amount for a give rent time in seconds
923
+ */
924
+ calculateAmount(tariff, rentTimeSeconds) {
925
+ if (this.isFree(tariff) === true) {
926
+ return 0;
927
+ }
928
+ if (tariff.interval === 0) {
929
+ return tariff.amount[0];
930
+ }
931
+ if (rentTimeSeconds > tariff.duration) {
932
+ console.warn("rentTimeS is greater than duration");
933
+ rentTimeSeconds = tariff.duration;
934
+ }
935
+ let sum = 0;
936
+ const startedIntervals = Math.ceil(rentTimeSeconds / tariff.interval);
937
+ for (let i = 0; i < startedIntervals; i += 1) {
938
+ if (i < tariff.amount.length) {
939
+ sum += tariff.amount[i];
940
+ } else {
941
+ sum += tariff.continue;
942
+ }
943
+ }
944
+ let serviceFee = sum * tariff.serviceFeePercentage / 100;
945
+ if (serviceFee < tariff.serviceFeeMin) {
946
+ serviceFee = tariff.serviceFeeMin;
947
+ } else if (serviceFee > tariff.serviceFeeMax) {
948
+ serviceFee = tariff.serviceFeeMax;
949
+ }
950
+ return sum + serviceFee;
951
+ }
952
+ calculateMaximumAmount(tariff) {
953
+ return this.calculateAmount(tariff, tariff.duration);
954
+ }
955
+ };
956
+
957
+ // src/index.ts
958
+ var JUHUU = class {
959
+ constructor(config) {
960
+ this.sessions = new SessionService(config);
961
+ this.links = new LinkService(config);
962
+ this.users = new UsersService(config);
963
+ this.payments = new PaymentsService(config);
964
+ this.properties = new PropertiesService(config);
965
+ this.points = new PointsService(config);
966
+ this.devices = new DevicesService(config);
967
+ this.deviceTemplates = new DeviceTemplatesService(config);
968
+ this.locations = new LocationsService(config);
969
+ this.terms = new TermsService(config);
970
+ this.tariffs = new TariffsService(config);
971
+ }
972
+ /**
973
+ * Top Level Resources
974
+ */
975
+ sessions;
976
+ links;
977
+ users;
978
+ payments;
979
+ properties;
980
+ points;
981
+ devices;
982
+ deviceTemplates;
983
+ locations;
984
+ terms;
985
+ tariffs;
986
+ };
987
+ export {
988
+ JUHUU as default
989
+ };