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