@vendit-dev/thirdparty-adapters 0.3.31 → 0.4.0-beta.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.
@@ -1,494 +1,465 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __importDefault = (this && this.__importDefault) || function (mod) {
50
- return (mod && mod.__esModule) ? mod : { "default": mod };
51
- };
52
- Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.MutexLock = void 0;
54
- var node_fetch_1 = __importDefault(require("node-fetch"));
55
- var moment_1 = __importDefault(require("moment"));
56
- var uuid_1 = require("uuid");
57
- var sleep = function (sleepTime) { return new Promise(function (resolve) { return setTimeout(resolve, sleepTime); }); };
58
- var MutexLock = /** @class */ (function () {
59
- function MutexLock(_a) {
60
- var _this = this;
61
- var redis = _a.redis, hashPrefix = _a.hashPrefix, _b = _a.mutexLockInterval, mutexLockInterval = _b === void 0 ? 50 : _b;
62
- this.getMutexLockKey = function (id) { return "" + _this.hashPrefix + id; };
63
- this.tryMutexLock = function (streamId, lockHash) { return _this.redis.set(_this.getMutexLockKey(streamId), lockHash, 'EX', 60, 'NX'); };
64
- this.getMutexLock = function (streamId) { return __awaiter(_this, void 0, void 0, function () {
65
- var lockHash, lock;
66
- return __generator(this, function (_a) {
67
- switch (_a.label) {
68
- case 0:
69
- lockHash = uuid_1.v4();
70
- _a.label = 1;
71
- case 1:
72
- if (!!lock) return [3 /*break*/, 4];
73
- return [4 /*yield*/, this.tryMutexLock(streamId, lockHash)];
74
- case 2:
75
- lock = _a.sent(); // eslint-disable-line
76
- return [4 /*yield*/, sleep(this.mutexLockInterval)];
77
- case 3:
78
- _a.sent(); // eslint-disable-line
79
- return [3 /*break*/, 1];
80
- case 4: return [2 /*return*/, lockHash];
81
- }
82
- });
83
- }); };
84
- this.unlockMutexLock = function (streamId, lockHash) { return __awaiter(_this, void 0, void 0, function () {
85
- var existingLockHash;
86
- return __generator(this, function (_a) {
87
- switch (_a.label) {
88
- case 0: return [4 /*yield*/, this.redis.get(this.getMutexLockKey(streamId))];
89
- case 1:
90
- existingLockHash = _a.sent();
91
- if (!(existingLockHash === lockHash)) return [3 /*break*/, 3];
92
- return [4 /*yield*/, this.redis.del(this.getMutexLockKey(streamId))];
93
- case 2:
94
- _a.sent();
95
- return [2 /*return*/, existingLockHash];
96
- case 3: return [2 /*return*/, null];
97
- }
98
- });
99
- }); };
100
- this.redis = redis;
101
- this.hashPrefix = hashPrefix;
102
- this.mutexLockInterval = mutexLockInterval;
103
- }
104
- return MutexLock;
105
- }());
106
- exports.MutexLock = MutexLock;
107
- var SmartAccessAdapter = /** @class */ (function () {
108
- function SmartAccessAdapter(_a) {
109
- var _this = this;
110
- var authenticator = _a.authenticator, redis = _a.redis, apiHost = _a.apiHost;
111
- this.PROVIDER_KEY = 'SamsungSmartAccess';
112
- this.parseJSONSafe = function (str) {
113
- try {
114
- return JSON.parse(str);
115
- }
116
- catch (e) {
117
- console.log('parseJSONSafe', e);
118
- return false;
119
- }
120
- };
121
- this.checkSmartAccessIntegration = function (_a) {
122
- var accommodationId = _a.accommodationId;
123
- return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_b) {
124
- switch (_b.label) {
125
- case 0: return [4 /*yield*/, this.getAccommodationAuthInfo({ accommodationId: accommodationId })];
126
- case 1: return [2 /*return*/, !!(_b.sent())];
127
- }
128
- }); });
129
- };
130
- this.getAccommodationAuthInfo = function (_a) {
131
- var accommodationId = _a.accommodationId;
132
- return __awaiter(_this, void 0, void 0, function () {
133
- var existingAuth, lockId, lockHash, newAuth, parsedCredential, err_1;
134
- return __generator(this, function (_b) {
135
- switch (_b.label) {
136
- case 0: return [4 /*yield*/, this.redis.get(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }))];
137
- case 1:
138
- existingAuth = _b.sent();
139
- if (existingAuth) {
140
- if (existingAuth === 'NOT_FOUND') {
141
- return [2 /*return*/, false];
142
- }
143
- return [2 /*return*/, this.parseJSONSafe(existingAuth)];
144
- }
145
- lockId = "SMARTACCESS-HKEY|" + accommodationId;
146
- return [4 /*yield*/, this.mutexLock.getMutexLock(lockId)];
147
- case 2:
148
- lockHash = _b.sent();
149
- return [4 /*yield*/, this.redis.get(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }))];
150
- case 3:
151
- existingAuth = _b.sent();
152
- _b.label = 4;
153
- case 4:
154
- _b.trys.push([4, 9, 10, 12]);
155
- if (existingAuth) {
156
- return [2 /*return*/, this.parseJSONSafe(existingAuth)];
157
- }
158
- return [4 /*yield*/, this.authenticator.getThirdPartyCredential({ accommodationId: accommodationId, provider: this.PROVIDER_KEY })];
159
- case 5:
160
- newAuth = _b.sent();
161
- if (!(!newAuth || !(newAuth === null || newAuth === void 0 ? void 0 : newAuth.credential))) return [3 /*break*/, 7];
162
- return [4 /*yield*/, this.redis.set(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }), 'NOT_FOUND', 'EX', 60 * 5)];
163
- case 6:
164
- _b.sent();
165
- return [2 /*return*/, false];
166
- case 7:
167
- parsedCredential = this.parseJSONSafe(newAuth.credential);
168
- return [4 /*yield*/, this.redis.set(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }), newAuth.credential, 'EX', 60 * 5)];
169
- case 8:
170
- _b.sent();
171
- return [2 /*return*/, parsedCredential];
172
- case 9:
173
- err_1 = _b.sent();
174
- console.log(err_1);
175
- throw err_1;
176
- case 10: return [4 /*yield*/, this.mutexLock.unlockMutexLock(lockId, lockHash)];
177
- case 11:
178
- _b.sent();
179
- return [7 /*endfinally*/];
180
- case 12: return [2 /*return*/];
181
- }
182
- });
183
- });
184
- };
185
- this.getAccommodationAccessToken = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
186
- var authInfo;
187
- return __generator(this, function (_a) {
188
- switch (_a.label) {
189
- case 0: return [4 /*yield*/, this.getAccommodationAuthInfo({ accommodationId: accommodationId })];
190
- case 1:
191
- authInfo = _a.sent();
192
- if (!(authInfo === null || authInfo === void 0 ? void 0 : authInfo.accessToken))
193
- throw new Error('THIRDPARTH_AUTH_NOT_FOUND');
194
- return [2 /*return*/, authInfo.accessToken];
195
- }
196
- });
197
- }); };
198
- this.callSmartAccessApi = function (_a) {
199
- var route = _a.route, accommodationId = _a.accommodationId, params = _a.params, _b = _a.method, method = _b === void 0 ? 'post' : _b, headers = _a.headers;
200
- return __awaiter(_this, void 0, void 0, function () {
201
- var requestArgs, _c, _d, _e, _f, _g, _h, response, jsonResponse;
202
- return __generator(this, function (_j) {
203
- switch (_j.label) {
204
- case 0:
205
- _c = [__assign({ method: method }, (method !== 'get' && ({
206
- body: JSON.stringify(__assign({}, params)),
207
- })))];
208
- _d = {};
209
- _e = [{ 'Content-Type': 'application/json' }];
210
- _f = accommodationId;
211
- if (!_f) return [3 /*break*/, 2];
212
- _g = {};
213
- _h = "Bearer ";
214
- return [4 /*yield*/, this.getAccommodationAccessToken(accommodationId)];
215
- case 1:
216
- _f = (_g.Authorization = _h + (_j.sent()), _g);
217
- _j.label = 2;
218
- case 2:
219
- requestArgs = __assign.apply(void 0, _c.concat([(_d.headers = __assign.apply(void 0, [__assign.apply(void 0, _e.concat([(_f)])), headers]), _d)]));
220
- return [4 /*yield*/, node_fetch_1.default(this.API_HOST + "/sa/oauth/" + route, requestArgs)];
221
- case 3:
222
- response = _j.sent();
223
- return [4 /*yield*/, response.json()];
224
- case 4:
225
- jsonResponse = _j.sent();
226
- return [2 /*return*/, jsonResponse];
227
- }
228
- });
229
- });
230
- };
231
- this.getRoomInfo = function (_a) {
232
- var accommodationId = _a.accommodationId, roomId = _a.roomId;
233
- return __awaiter(_this, void 0, void 0, function () {
234
- var roomResponse;
235
- return __generator(this, function (_b) {
236
- switch (_b.label) {
237
- case 0: return [4 /*yield*/, this.callSmartAccessApi({
238
- accommodationId: accommodationId,
239
- method: 'get',
240
- route: "v1/room/" + roomId + "/roominfo",
241
- })];
242
- case 1:
243
- roomResponse = _b.sent();
244
- return [2 /*return*/, roomResponse];
245
- }
246
- });
247
- });
248
- };
249
- this.getRooms = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
250
- var roomResponse;
251
- return __generator(this, function (_a) {
252
- switch (_a.label) {
253
- case 0: return [4 /*yield*/, this.callSmartAccessApi({
254
- accommodationId: accommodationId,
255
- method: 'get',
256
- route: 'v1/room/list',
257
- })];
258
- case 1:
259
- roomResponse = _a.sent();
260
- return [2 /*return*/, roomResponse.roomList];
261
- }
262
- });
263
- }); };
264
- this.getDevices = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
265
- var deviceResponse;
266
- return __generator(this, function (_a) {
267
- switch (_a.label) {
268
- case 0: return [4 /*yield*/, this.callSmartAccessApi({
269
- accommodationId: accommodationId,
270
- method: 'get',
271
- route: 'v1/device/list',
272
- })];
273
- case 1:
274
- deviceResponse = _a.sent();
275
- return [2 /*return*/, deviceResponse.lockList];
276
- }
277
- });
278
- }); };
279
- this.getRoomsWithDevices = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
280
- var rooms, devices, devicesRoomIdMap, roomsWithDevices;
281
- return __generator(this, function (_a) {
282
- switch (_a.label) {
283
- case 0: return [4 /*yield*/, this.getRooms(accommodationId)];
284
- case 1:
285
- rooms = _a.sent();
286
- return [4 /*yield*/, this.getDevices(accommodationId)];
287
- case 2:
288
- devices = _a.sent();
289
- devicesRoomIdMap = devices.reduce(function (acc, device) {
290
- if (acc[device.roomId]) {
291
- acc[device.roomId].push(device);
292
- }
293
- else {
294
- acc[device.roomId] = [device];
295
- }
296
- return acc;
297
- }, {});
298
- roomsWithDevices = rooms.map(function (room) { return (__assign(__assign({}, room), { devices: devicesRoomIdMap[room.roomId] ? devicesRoomIdMap[room.roomId] : [] })); });
299
- return [2 /*return*/, roomsWithDevices];
300
- }
301
- });
302
- }); };
303
- this.createPinCodeForRoom = function (_a) {
304
- var roomId = _a.roomId, accommodationId = _a.accommodationId, userType = _a.userType, keyType = _a.keyType, startDate = _a.startDate, endDate = _a.endDate;
305
- return __awaiter(_this, void 0, void 0, function () {
306
- var foundProperty, data, parsedProperty, pinCodeResult, keyId, pinCodeQuery;
307
- return __generator(this, function (_b) {
308
- switch (_b.label) {
309
- case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
310
- accommodationId: accommodationId,
311
- provider: this.PROVIDER_KEY, relatedId: roomId, type: 'room',
312
- })];
313
- case 1:
314
- foundProperty = _b.sent();
315
- if (!(foundProperty === null || foundProperty === void 0 ? void 0 : foundProperty.result)) return [3 /*break*/, 4];
316
- data = foundProperty.data;
317
- parsedProperty = JSON.parse(data);
318
- return [4 /*yield*/, this.callSmartAccessApi({
319
- accommodationId: accommodationId,
320
- route: 'v1/key/pincode',
321
- params: {
322
- userType: userType,
323
- keyType: keyType,
324
- pinCode: null,
325
- roomId: parsedProperty.thirdPartyId,
326
- accessStartDate: moment_1.default(startDate).add(9, 'hours').format('YYYYMMDDHHmm'),
327
- accessEndDate: moment_1.default(endDate).add(9, 'hours').format('YYYYMMDDHHmm'),
328
- },
329
- })];
330
- case 2:
331
- pinCodeResult = _b.sent();
332
- keyId = pinCodeResult.keyId;
333
- return [4 /*yield*/, this.callSmartAccessApi({
334
- accommodationId: accommodationId,
335
- method: 'get',
336
- route: "v1/key/" + keyId + "/pincode",
337
- })];
338
- case 3:
339
- pinCodeQuery = _b.sent();
340
- return [2 /*return*/, __assign(__assign({}, pinCodeQuery.lockList[0]), { keyId: keyId })];
341
- case 4: return [2 /*return*/];
342
- }
343
- });
344
- });
345
- };
346
- this.modifyPinCodeByKeyId = function (_a) {
347
- var keyId = _a.keyId, accommodationId = _a.accommodationId, userType = _a.userType, keyType = _a.keyType, startDate = _a.startDate, endDate = _a.endDate;
348
- return __awaiter(_this, void 0, void 0, function () {
349
- var pinCodeResult, pinCodeQuery;
350
- return __generator(this, function (_b) {
351
- switch (_b.label) {
352
- case 0: return [4 /*yield*/, this.callSmartAccessApi({
353
- accommodationId: accommodationId,
354
- method: 'put',
355
- route: "v1/key/" + keyId + "/pincode",
356
- params: {
357
- userType: userType,
358
- keyType: keyType,
359
- accessStartDate: moment_1.default(startDate).add(9, 'hours').format('YYYYMMDDHHmm'),
360
- accessEndDate: moment_1.default(endDate).add(9, 'hours').format('YYYYMMDDHHmm'),
361
- },
362
- })];
363
- case 1:
364
- pinCodeResult = _b.sent();
365
- return [4 /*yield*/, this.callSmartAccessApi({
366
- accommodationId: accommodationId,
367
- method: 'get',
368
- route: "v1/key/" + keyId + "/pincode",
369
- })];
370
- case 2:
371
- pinCodeQuery = _b.sent();
372
- return [2 /*return*/, __assign(__assign({}, pinCodeQuery.lockList[0]), { keyId: keyId })];
373
- }
374
- });
375
- });
376
- };
377
- this.deletePinCodeByKeyId = function (_a) {
378
- var keyId = _a.keyId, accommodationId = _a.accommodationId;
379
- return __awaiter(_this, void 0, void 0, function () {
380
- var pinCodeResult;
381
- return __generator(this, function (_b) {
382
- switch (_b.label) {
383
- case 0: return [4 /*yield*/, this.callSmartAccessApi({
384
- accommodationId: accommodationId,
385
- method: 'put',
386
- route: "v1/key/" + keyId + "/delete",
387
- })];
388
- case 1:
389
- pinCodeResult = _b.sent();
390
- return [2 /*return*/, pinCodeResult];
391
- }
392
- });
393
- });
394
- };
395
- this.openLockByRoomId = function (_a) {
396
- var accommodationId = _a.accommodationId, roomId = _a.roomId, keyId = _a.keyId;
397
- return __awaiter(_this, void 0, void 0, function () {
398
- var foundRoomProperty, foundDoorlockProperty, data, parsedProperty, deviceId, res;
399
- return __generator(this, function (_b) {
400
- switch (_b.label) {
401
- case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
402
- accommodationId: accommodationId,
403
- provider: this.PROVIDER_KEY, relatedId: roomId, type: 'room',
404
- })];
405
- case 1:
406
- foundRoomProperty = _b.sent();
407
- if (!(foundRoomProperty === null || foundRoomProperty === void 0 ? void 0 : foundRoomProperty.result))
408
- return [2 /*return*/, false];
409
- return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
410
- accommodationId: accommodationId,
411
- provider: this.PROVIDER_KEY, relatedId: JSON.parse(foundRoomProperty.data).thirdPartyId, type: 'doorlock',
412
- })];
413
- case 2:
414
- foundDoorlockProperty = _b.sent();
415
- if (!(foundDoorlockProperty === null || foundDoorlockProperty === void 0 ? void 0 : foundDoorlockProperty.result)) return [3 /*break*/, 4];
416
- data = foundDoorlockProperty.data;
417
- parsedProperty = JSON.parse(data);
418
- deviceId = parsedProperty.thirdPartyId;
419
- return [4 /*yield*/, this.callSmartAccessApi({
420
- accommodationId: accommodationId,
421
- route: "v1/device/" + deviceId + "/open",
422
- params: {
423
- keyId: keyId,
424
- },
425
- })];
426
- case 3:
427
- res = _b.sent();
428
- return [2 /*return*/, true];
429
- case 4: return [2 /*return*/];
430
- }
431
- });
432
- });
433
- };
434
- this.getAccommodationCallbacks = function (_a) {
435
- var accommodationId = _a.accommodationId;
436
- return __awaiter(_this, void 0, void 0, function () {
437
- var response;
438
- return __generator(this, function (_b) {
439
- switch (_b.label) {
440
- case 0: return [4 /*yield*/, this.callSmartAccessApi({
441
- accommodationId: accommodationId,
442
- method: 'get',
443
- route: 'v1/callback/access',
444
- })];
445
- case 1:
446
- response = _b.sent();
447
- return [2 /*return*/, true];
448
- }
449
- });
450
- });
451
- };
452
- this.getRoomByThirdPartyId = function (_a) {
453
- var accommodationId = _a.accommodationId, thirdPartyId = _a.thirdPartyId;
454
- return __awaiter(_this, void 0, void 0, function () {
455
- var foundRoomProperty;
456
- return __generator(this, function (_b) {
457
- switch (_b.label) {
458
- case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByThirdPartyId({
459
- accommodationId: accommodationId,
460
- provider: this.PROVIDER_KEY,
461
- thirdPartyId: thirdPartyId,
462
- type: 'room',
463
- })];
464
- case 1:
465
- foundRoomProperty = _b.sent();
466
- if (!(foundRoomProperty === null || foundRoomProperty === void 0 ? void 0 : foundRoomProperty.result))
467
- return [2 /*return*/, false];
468
- return [2 /*return*/, JSON.parse(foundRoomProperty.data)];
469
- }
470
- });
471
- });
472
- };
473
- this.authenticator = authenticator;
474
- this.API_HOST = apiHost;
475
- this.redis = redis;
476
- this.mutexLock = new MutexLock({ hashPrefix: 'RES_SMARTACCESS_MUTEX_', mutexLockInterval: 50, redis: redis });
477
- }
478
- SmartAccessAdapter.indexAccommodation = function (_a) {
479
- var accommodationId = _a.accommodationId;
480
- return "AUTH|SMARTACCESS-API|" + accommodationId;
481
- };
482
- SmartAccessAdapter.refineRawTokenSet = function (rawTokenSet) {
483
- var refreshToken = rawTokenSet.refresh_token, accessToken = rawTokenSet.access_token, expiresIn = rawTokenSet.expires_in, tokenType = rawTokenSet.token_type;
484
- var tokenSet = {
485
- refreshToken: refreshToken,
486
- accessToken: accessToken,
487
- expiresIn: expiresIn,
488
- tokenType: tokenType,
489
- };
490
- return tokenSet;
491
- };
492
- return SmartAccessAdapter;
493
- }());
494
- exports.default = SmartAccessAdapter;
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var moment_1 = __importDefault(require("moment"));
54
+ var node_fetch_1 = __importDefault(require("node-fetch"));
55
+ var mutex_1 = __importDefault(require("../utils/mutex"));
56
+ var SmartAccessAdapter = /** @class */ (function () {
57
+ function SmartAccessAdapter(_a) {
58
+ var _this = this;
59
+ var authenticator = _a.authenticator, redis = _a.redis, apiHost = _a.apiHost;
60
+ this.PROVIDER_KEY = 'SamsungSmartAccess';
61
+ this.parseJSONSafe = function (str) {
62
+ try {
63
+ return JSON.parse(str);
64
+ }
65
+ catch (e) {
66
+ console.log('parseJSONSafe', e);
67
+ return false;
68
+ }
69
+ };
70
+ this.checkSmartAccessIntegration = function (_a) {
71
+ var accommodationId = _a.accommodationId;
72
+ return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_b) {
73
+ switch (_b.label) {
74
+ case 0: return [4 /*yield*/, this.getAccommodationAuthInfo({ accommodationId: accommodationId })];
75
+ case 1: return [2 /*return*/, !!(_b.sent())];
76
+ }
77
+ }); });
78
+ };
79
+ this.getAccommodationAuthInfo = function (_a) {
80
+ var accommodationId = _a.accommodationId;
81
+ return __awaiter(_this, void 0, void 0, function () {
82
+ var existingAuth, lockId, lockHash, newAuth, parsedCredential, err_1;
83
+ return __generator(this, function (_b) {
84
+ switch (_b.label) {
85
+ case 0: return [4 /*yield*/, this.redis.get(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }))];
86
+ case 1:
87
+ existingAuth = _b.sent();
88
+ if (existingAuth) {
89
+ if (existingAuth === 'NOT_FOUND') {
90
+ return [2 /*return*/, false];
91
+ }
92
+ return [2 /*return*/, this.parseJSONSafe(existingAuth)];
93
+ }
94
+ lockId = "SMARTACCESS-HKEY|" + accommodationId;
95
+ return [4 /*yield*/, this.mutexLock.getMutexLock(lockId)];
96
+ case 2:
97
+ lockHash = _b.sent();
98
+ return [4 /*yield*/, this.redis.get(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }))];
99
+ case 3:
100
+ existingAuth = _b.sent();
101
+ _b.label = 4;
102
+ case 4:
103
+ _b.trys.push([4, 9, 10, 12]);
104
+ if (existingAuth) {
105
+ return [2 /*return*/, this.parseJSONSafe(existingAuth)];
106
+ }
107
+ return [4 /*yield*/, this.authenticator.getThirdPartyCredential({
108
+ accommodationId: accommodationId,
109
+ provider: this.PROVIDER_KEY,
110
+ })];
111
+ case 5:
112
+ newAuth = _b.sent();
113
+ if (!(!newAuth || !(newAuth === null || newAuth === void 0 ? void 0 : newAuth.credential))) return [3 /*break*/, 7];
114
+ return [4 /*yield*/, this.redis.set(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }), 'NOT_FOUND', 'EX', 60 * 5)];
115
+ case 6:
116
+ _b.sent();
117
+ return [2 /*return*/, false];
118
+ case 7:
119
+ parsedCredential = this.parseJSONSafe(newAuth.credential);
120
+ return [4 /*yield*/, this.redis.set(SmartAccessAdapter.indexAccommodation({ accommodationId: accommodationId }), newAuth.credential, 'EX', 60 * 5)];
121
+ case 8:
122
+ _b.sent();
123
+ return [2 /*return*/, parsedCredential];
124
+ case 9:
125
+ err_1 = _b.sent();
126
+ console.log(err_1);
127
+ throw err_1;
128
+ case 10: return [4 /*yield*/, this.mutexLock.unlockMutexLock(lockId, lockHash)];
129
+ case 11:
130
+ _b.sent();
131
+ return [7 /*endfinally*/];
132
+ case 12: return [2 /*return*/];
133
+ }
134
+ });
135
+ });
136
+ };
137
+ this.getAccommodationAccessToken = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
138
+ var authInfo;
139
+ return __generator(this, function (_a) {
140
+ switch (_a.label) {
141
+ case 0: return [4 /*yield*/, this.getAccommodationAuthInfo({
142
+ accommodationId: accommodationId,
143
+ })];
144
+ case 1:
145
+ authInfo = (_a.sent());
146
+ if (!(authInfo === null || authInfo === void 0 ? void 0 : authInfo.accessToken))
147
+ throw new Error('THIRDPARTH_AUTH_NOT_FOUND');
148
+ return [2 /*return*/, authInfo.accessToken];
149
+ }
150
+ });
151
+ }); };
152
+ this.callSmartAccessApi = function (_a) {
153
+ var route = _a.route, accommodationId = _a.accommodationId, params = _a.params, _b = _a.method, method = _b === void 0 ? 'post' : _b, headers = _a.headers;
154
+ return __awaiter(_this, void 0, void 0, function () {
155
+ var requestArgs, _c, _d, _e, _f, _g, _h, response, jsonResponse;
156
+ return __generator(this, function (_j) {
157
+ switch (_j.label) {
158
+ case 0:
159
+ _c = [__assign({ method: method }, (method !== 'get' && {
160
+ body: JSON.stringify(__assign({}, params)),
161
+ }))];
162
+ _d = {};
163
+ _e = [{ 'Content-Type': 'application/json' }];
164
+ _f = accommodationId;
165
+ if (!_f) return [3 /*break*/, 2];
166
+ _g = {};
167
+ _h = "Bearer ";
168
+ return [4 /*yield*/, this.getAccommodationAccessToken(accommodationId)];
169
+ case 1:
170
+ _f = (_g.Authorization = _h + (_j.sent()),
171
+ _g);
172
+ _j.label = 2;
173
+ case 2:
174
+ requestArgs = __assign.apply(void 0, _c.concat([(_d.headers = __assign.apply(void 0, [__assign.apply(void 0, _e.concat([(_f)])), headers]), _d)]));
175
+ return [4 /*yield*/, node_fetch_1.default(this.API_HOST + "/sa/oauth/" + route, requestArgs)];
176
+ case 3:
177
+ response = _j.sent();
178
+ return [4 /*yield*/, response.json()];
179
+ case 4:
180
+ jsonResponse = _j.sent();
181
+ return [2 /*return*/, jsonResponse];
182
+ }
183
+ });
184
+ });
185
+ };
186
+ this.getRoomInfo = function (_a) {
187
+ var accommodationId = _a.accommodationId, roomId = _a.roomId;
188
+ return __awaiter(_this, void 0, void 0, function () {
189
+ var roomResponse;
190
+ return __generator(this, function (_b) {
191
+ switch (_b.label) {
192
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
193
+ accommodationId: accommodationId,
194
+ method: 'get',
195
+ route: "v1/room/" + roomId + "/roominfo",
196
+ })];
197
+ case 1:
198
+ roomResponse = _b.sent();
199
+ return [2 /*return*/, roomResponse];
200
+ }
201
+ });
202
+ });
203
+ };
204
+ this.getRooms = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
205
+ var roomResponse;
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
209
+ accommodationId: accommodationId,
210
+ method: 'get',
211
+ route: 'v1/room/list',
212
+ })];
213
+ case 1:
214
+ roomResponse = _a.sent();
215
+ return [2 /*return*/, roomResponse.roomList];
216
+ }
217
+ });
218
+ }); };
219
+ this.getDevices = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
220
+ var deviceResponse;
221
+ return __generator(this, function (_a) {
222
+ switch (_a.label) {
223
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
224
+ accommodationId: accommodationId,
225
+ method: 'get',
226
+ route: 'v1/device/list',
227
+ })];
228
+ case 1:
229
+ deviceResponse = _a.sent();
230
+ return [2 /*return*/, deviceResponse.lockList];
231
+ }
232
+ });
233
+ }); };
234
+ this.getRoomsWithDevices = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
235
+ var rooms, devices, devicesRoomIdMap, roomsWithDevices;
236
+ return __generator(this, function (_a) {
237
+ switch (_a.label) {
238
+ case 0: return [4 /*yield*/, this.getRooms(accommodationId)];
239
+ case 1:
240
+ rooms = _a.sent();
241
+ return [4 /*yield*/, this.getDevices(accommodationId)];
242
+ case 2:
243
+ devices = _a.sent();
244
+ devicesRoomIdMap = devices.reduce(function (acc, device) {
245
+ if (acc[device.roomId]) {
246
+ acc[device.roomId].push(device);
247
+ }
248
+ else {
249
+ acc[device.roomId] = [device];
250
+ }
251
+ return acc;
252
+ }, {});
253
+ roomsWithDevices = rooms.map(function (room) { return (__assign(__assign({}, room), { devices: devicesRoomIdMap[room.roomId]
254
+ ? devicesRoomIdMap[room.roomId]
255
+ : [] })); });
256
+ return [2 /*return*/, roomsWithDevices];
257
+ }
258
+ });
259
+ }); };
260
+ this.createPinCodeForRoom = function (_a) {
261
+ var roomId = _a.roomId, accommodationId = _a.accommodationId, userType = _a.userType, keyType = _a.keyType, startDate = _a.startDate, endDate = _a.endDate;
262
+ return __awaiter(_this, void 0, void 0, function () {
263
+ var foundProperty, data, parsedProperty, pinCodeResult, keyId, pinCodeQuery;
264
+ return __generator(this, function (_b) {
265
+ switch (_b.label) {
266
+ case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
267
+ accommodationId: accommodationId,
268
+ provider: this.PROVIDER_KEY,
269
+ relatedId: roomId,
270
+ type: 'room',
271
+ })];
272
+ case 1:
273
+ foundProperty = _b.sent();
274
+ if (!(foundProperty === null || foundProperty === void 0 ? void 0 : foundProperty.result)) return [3 /*break*/, 4];
275
+ data = foundProperty.data;
276
+ parsedProperty = JSON.parse(data);
277
+ return [4 /*yield*/, this.callSmartAccessApi({
278
+ accommodationId: accommodationId,
279
+ route: 'v1/key/pincode',
280
+ params: {
281
+ userType: userType,
282
+ keyType: keyType,
283
+ pinCode: null,
284
+ roomId: parsedProperty.thirdPartyId,
285
+ accessStartDate: moment_1.default(startDate)
286
+ .add(9, 'hours')
287
+ .format('YYYYMMDDHHmm'),
288
+ accessEndDate: moment_1.default(endDate).add(9, 'hours').format('YYYYMMDDHHmm'),
289
+ },
290
+ })];
291
+ case 2:
292
+ pinCodeResult = _b.sent();
293
+ keyId = pinCodeResult.keyId;
294
+ return [4 /*yield*/, this.callSmartAccessApi({
295
+ accommodationId: accommodationId,
296
+ method: 'get',
297
+ route: "v1/key/" + keyId + "/pincode",
298
+ })];
299
+ case 3:
300
+ pinCodeQuery = _b.sent();
301
+ return [2 /*return*/, __assign(__assign({}, pinCodeQuery.lockList[0]), { keyId: keyId })];
302
+ case 4: return [2 /*return*/];
303
+ }
304
+ });
305
+ });
306
+ };
307
+ this.modifyPinCodeByKeyId = function (_a) {
308
+ var keyId = _a.keyId, accommodationId = _a.accommodationId, userType = _a.userType, keyType = _a.keyType, startDate = _a.startDate, endDate = _a.endDate;
309
+ return __awaiter(_this, void 0, void 0, function () {
310
+ var pinCodeResult, pinCodeQuery;
311
+ return __generator(this, function (_b) {
312
+ switch (_b.label) {
313
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
314
+ accommodationId: accommodationId,
315
+ method: 'put',
316
+ route: "v1/key/" + keyId + "/pincode",
317
+ params: {
318
+ userType: userType,
319
+ keyType: keyType,
320
+ accessStartDate: moment_1.default(startDate)
321
+ .add(9, 'hours')
322
+ .format('YYYYMMDDHHmm'),
323
+ accessEndDate: moment_1.default(endDate).add(9, 'hours').format('YYYYMMDDHHmm'),
324
+ },
325
+ })];
326
+ case 1:
327
+ pinCodeResult = _b.sent();
328
+ return [4 /*yield*/, this.callSmartAccessApi({
329
+ accommodationId: accommodationId,
330
+ method: 'get',
331
+ route: "v1/key/" + keyId + "/pincode",
332
+ })];
333
+ case 2:
334
+ pinCodeQuery = _b.sent();
335
+ return [2 /*return*/, __assign(__assign({}, pinCodeQuery.lockList[0]), { keyId: keyId })];
336
+ }
337
+ });
338
+ });
339
+ };
340
+ this.deletePinCodeByKeyId = function (_a) {
341
+ var keyId = _a.keyId, accommodationId = _a.accommodationId;
342
+ return __awaiter(_this, void 0, void 0, function () {
343
+ var pinCodeResult;
344
+ return __generator(this, function (_b) {
345
+ switch (_b.label) {
346
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
347
+ accommodationId: accommodationId,
348
+ method: 'put',
349
+ route: "v1/key/" + keyId + "/delete",
350
+ })];
351
+ case 1:
352
+ pinCodeResult = _b.sent();
353
+ return [2 /*return*/, pinCodeResult];
354
+ }
355
+ });
356
+ });
357
+ };
358
+ this.openLockByRoomId = function (_a) {
359
+ var accommodationId = _a.accommodationId, roomId = _a.roomId, keyId = _a.keyId;
360
+ return __awaiter(_this, void 0, void 0, function () {
361
+ var foundRoomProperty, foundDoorlockProperty, data, parsedProperty, deviceId, res;
362
+ return __generator(this, function (_b) {
363
+ switch (_b.label) {
364
+ case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
365
+ accommodationId: accommodationId,
366
+ provider: this.PROVIDER_KEY,
367
+ relatedId: roomId,
368
+ type: 'room',
369
+ })];
370
+ case 1:
371
+ foundRoomProperty = _b.sent();
372
+ if (!(foundRoomProperty === null || foundRoomProperty === void 0 ? void 0 : foundRoomProperty.result))
373
+ return [2 /*return*/, false];
374
+ return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
375
+ accommodationId: accommodationId,
376
+ provider: this.PROVIDER_KEY,
377
+ relatedId: JSON.parse(foundRoomProperty.data).thirdPartyId,
378
+ type: 'doorlock',
379
+ })];
380
+ case 2:
381
+ foundDoorlockProperty = _b.sent();
382
+ if (!(foundDoorlockProperty === null || foundDoorlockProperty === void 0 ? void 0 : foundDoorlockProperty.result)) return [3 /*break*/, 4];
383
+ data = foundDoorlockProperty.data;
384
+ parsedProperty = JSON.parse(data);
385
+ deviceId = parsedProperty.thirdPartyId;
386
+ return [4 /*yield*/, this.callSmartAccessApi({
387
+ accommodationId: accommodationId,
388
+ route: "v1/device/" + deviceId + "/open",
389
+ params: {
390
+ keyId: keyId,
391
+ },
392
+ })];
393
+ case 3:
394
+ res = _b.sent();
395
+ return [2 /*return*/, true];
396
+ case 4: return [2 /*return*/];
397
+ }
398
+ });
399
+ });
400
+ };
401
+ this.getAccommodationCallbacks = function (_a) {
402
+ var accommodationId = _a.accommodationId;
403
+ return __awaiter(_this, void 0, void 0, function () {
404
+ var response;
405
+ return __generator(this, function (_b) {
406
+ switch (_b.label) {
407
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
408
+ accommodationId: accommodationId,
409
+ method: 'get',
410
+ route: 'v1/callback/access',
411
+ })];
412
+ case 1:
413
+ response = _b.sent();
414
+ return [2 /*return*/, true];
415
+ }
416
+ });
417
+ });
418
+ };
419
+ this.getRoomByThirdPartyId = function (_a) {
420
+ var accommodationId = _a.accommodationId, thirdPartyId = _a.thirdPartyId;
421
+ return __awaiter(_this, void 0, void 0, function () {
422
+ var foundRoomProperty;
423
+ return __generator(this, function (_b) {
424
+ switch (_b.label) {
425
+ case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByThirdPartyId({
426
+ accommodationId: accommodationId,
427
+ provider: this.PROVIDER_KEY,
428
+ thirdPartyId: thirdPartyId,
429
+ type: 'room',
430
+ })];
431
+ case 1:
432
+ foundRoomProperty = _b.sent();
433
+ if (!(foundRoomProperty === null || foundRoomProperty === void 0 ? void 0 : foundRoomProperty.result))
434
+ return [2 /*return*/, false];
435
+ return [2 /*return*/, JSON.parse(foundRoomProperty.data)];
436
+ }
437
+ });
438
+ });
439
+ };
440
+ this.authenticator = authenticator;
441
+ this.API_HOST = apiHost;
442
+ this.redis = redis;
443
+ this.mutexLock = new mutex_1.default({
444
+ hashPrefix: 'RES_SMARTACCESS_MUTEX_',
445
+ mutexLockInterval: 50,
446
+ redis: redis,
447
+ });
448
+ }
449
+ SmartAccessAdapter.indexAccommodation = function (_a) {
450
+ var accommodationId = _a.accommodationId;
451
+ return "AUTH|SMARTACCESS-API|" + accommodationId;
452
+ };
453
+ SmartAccessAdapter.refineRawTokenSet = function (rawTokenSet) {
454
+ var refreshToken = rawTokenSet.refresh_token, accessToken = rawTokenSet.access_token, expiresIn = rawTokenSet.expires_in, tokenType = rawTokenSet.token_type;
455
+ var tokenSet = {
456
+ refreshToken: refreshToken,
457
+ accessToken: accessToken,
458
+ expiresIn: expiresIn,
459
+ tokenType: tokenType,
460
+ };
461
+ return tokenSet;
462
+ };
463
+ return SmartAccessAdapter;
464
+ }());
465
+ exports.default = SmartAccessAdapter;