@vendit-dev/thirdparty-adapters 0.4.0 → 0.4.1-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,465 +1,500 @@
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;
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.getRoomsKey = function (accommodationId, roomId) { return __awaiter(_this, void 0, void 0, function () {
235
+ var foundProperty, data, parsedProperty, roomKeyResponse, error_1;
236
+ return __generator(this, function (_a) {
237
+ switch (_a.label) {
238
+ case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
239
+ accommodationId: accommodationId,
240
+ provider: this.PROVIDER_KEY,
241
+ relatedId: roomId,
242
+ type: 'room',
243
+ })];
244
+ case 1:
245
+ foundProperty = _a.sent();
246
+ if (!foundProperty)
247
+ throw new Error('THIRDPARTY_PROPERTY_NOT_FOUND');
248
+ _a.label = 2;
249
+ case 2:
250
+ _a.trys.push([2, 4, , 5]);
251
+ data = foundProperty.data;
252
+ parsedProperty = JSON.parse(data);
253
+ return [4 /*yield*/, this.callSmartAccessApi({
254
+ accommodationId: accommodationId,
255
+ method: 'get',
256
+ route: "v1/room/" + parsedProperty.thirdPartyId + "/keyinfo",
257
+ })];
258
+ case 3:
259
+ roomKeyResponse = _a.sent();
260
+ console.log('🚀 ~ file: smartAccess.ts:288 ~ SmartAccessAdapter ~ roomKeyResponse:', roomKeyResponse);
261
+ return [2 /*return*/, roomKeyResponse];
262
+ case 4:
263
+ error_1 = _a.sent();
264
+ return [2 /*return*/, { lockList: [] }];
265
+ case 5: return [2 /*return*/];
266
+ }
267
+ });
268
+ }); };
269
+ this.getRoomsWithDevices = function (accommodationId) { return __awaiter(_this, void 0, void 0, function () {
270
+ var rooms, devices, devicesRoomIdMap, roomsWithDevices;
271
+ return __generator(this, function (_a) {
272
+ switch (_a.label) {
273
+ case 0: return [4 /*yield*/, this.getRooms(accommodationId)];
274
+ case 1:
275
+ rooms = _a.sent();
276
+ return [4 /*yield*/, this.getDevices(accommodationId)];
277
+ case 2:
278
+ devices = _a.sent();
279
+ devicesRoomIdMap = devices.reduce(function (acc, device) {
280
+ if (acc[device.roomId]) {
281
+ acc[device.roomId].push(device);
282
+ }
283
+ else {
284
+ acc[device.roomId] = [device];
285
+ }
286
+ return acc;
287
+ }, {});
288
+ roomsWithDevices = rooms.map(function (room) { return (__assign(__assign({}, room), { devices: devicesRoomIdMap[room.roomId]
289
+ ? devicesRoomIdMap[room.roomId]
290
+ : [] })); });
291
+ return [2 /*return*/, roomsWithDevices];
292
+ }
293
+ });
294
+ }); };
295
+ this.createPinCodeForRoom = function (_a) {
296
+ var roomId = _a.roomId, accommodationId = _a.accommodationId, userType = _a.userType, keyType = _a.keyType, startDate = _a.startDate, endDate = _a.endDate;
297
+ return __awaiter(_this, void 0, void 0, function () {
298
+ var foundProperty, data, parsedProperty, pinCodeResult, keyId, pinCodeQuery;
299
+ return __generator(this, function (_b) {
300
+ switch (_b.label) {
301
+ case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
302
+ accommodationId: accommodationId,
303
+ provider: this.PROVIDER_KEY,
304
+ relatedId: roomId,
305
+ type: 'room',
306
+ })];
307
+ case 1:
308
+ foundProperty = _b.sent();
309
+ if (!(foundProperty === null || foundProperty === void 0 ? void 0 : foundProperty.result)) return [3 /*break*/, 4];
310
+ data = foundProperty.data;
311
+ parsedProperty = JSON.parse(data);
312
+ return [4 /*yield*/, this.callSmartAccessApi({
313
+ accommodationId: accommodationId,
314
+ route: 'v1/key/pincode',
315
+ params: {
316
+ userType: userType,
317
+ keyType: keyType,
318
+ pinCode: null,
319
+ roomId: parsedProperty.thirdPartyId,
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 2:
327
+ pinCodeResult = _b.sent();
328
+ keyId = pinCodeResult.keyId;
329
+ return [4 /*yield*/, this.callSmartAccessApi({
330
+ accommodationId: accommodationId,
331
+ method: 'get',
332
+ route: "v1/key/" + keyId + "/pincode",
333
+ })];
334
+ case 3:
335
+ pinCodeQuery = _b.sent();
336
+ return [2 /*return*/, __assign(__assign({}, pinCodeQuery.lockList[0]), { keyId: keyId })];
337
+ case 4: return [2 /*return*/];
338
+ }
339
+ });
340
+ });
341
+ };
342
+ this.modifyPinCodeByKeyId = function (_a) {
343
+ var keyId = _a.keyId, accommodationId = _a.accommodationId, userType = _a.userType, keyType = _a.keyType, startDate = _a.startDate, endDate = _a.endDate;
344
+ return __awaiter(_this, void 0, void 0, function () {
345
+ var pinCodeResult, pinCodeQuery;
346
+ return __generator(this, function (_b) {
347
+ switch (_b.label) {
348
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
349
+ accommodationId: accommodationId,
350
+ method: 'put',
351
+ route: "v1/key/" + keyId + "/pincode",
352
+ params: {
353
+ userType: userType,
354
+ keyType: keyType,
355
+ accessStartDate: moment_1.default(startDate)
356
+ .add(9, 'hours')
357
+ .format('YYYYMMDDHHmm'),
358
+ accessEndDate: moment_1.default(endDate).add(9, 'hours').format('YYYYMMDDHHmm'),
359
+ },
360
+ })];
361
+ case 1:
362
+ pinCodeResult = _b.sent();
363
+ return [4 /*yield*/, this.callSmartAccessApi({
364
+ accommodationId: accommodationId,
365
+ method: 'get',
366
+ route: "v1/key/" + keyId + "/pincode",
367
+ })];
368
+ case 2:
369
+ pinCodeQuery = _b.sent();
370
+ return [2 /*return*/, __assign(__assign({}, pinCodeQuery.lockList[0]), { keyId: keyId })];
371
+ }
372
+ });
373
+ });
374
+ };
375
+ this.deletePinCodeByKeyId = function (_a) {
376
+ var keyId = _a.keyId, accommodationId = _a.accommodationId;
377
+ return __awaiter(_this, void 0, void 0, function () {
378
+ var pinCodeResult;
379
+ return __generator(this, function (_b) {
380
+ switch (_b.label) {
381
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
382
+ accommodationId: accommodationId,
383
+ method: 'put',
384
+ route: "v1/key/" + keyId + "/delete",
385
+ })];
386
+ case 1:
387
+ pinCodeResult = _b.sent();
388
+ return [2 /*return*/, pinCodeResult];
389
+ }
390
+ });
391
+ });
392
+ };
393
+ this.openLockByRoomId = function (_a) {
394
+ var accommodationId = _a.accommodationId, roomId = _a.roomId, keyId = _a.keyId;
395
+ return __awaiter(_this, void 0, void 0, function () {
396
+ var foundRoomProperty, foundDoorlockProperty, data, parsedProperty, deviceId, res;
397
+ return __generator(this, function (_b) {
398
+ switch (_b.label) {
399
+ case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByRelatedId({
400
+ accommodationId: accommodationId,
401
+ provider: this.PROVIDER_KEY,
402
+ relatedId: roomId,
403
+ 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,
412
+ relatedId: JSON.parse(foundRoomProperty.data).thirdPartyId,
413
+ type: 'doorlock',
414
+ })];
415
+ case 2:
416
+ foundDoorlockProperty = _b.sent();
417
+ if (!(foundDoorlockProperty === null || foundDoorlockProperty === void 0 ? void 0 : foundDoorlockProperty.result)) return [3 /*break*/, 4];
418
+ data = foundDoorlockProperty.data;
419
+ parsedProperty = JSON.parse(data);
420
+ deviceId = parsedProperty.thirdPartyId;
421
+ return [4 /*yield*/, this.callSmartAccessApi({
422
+ accommodationId: accommodationId,
423
+ route: "v1/device/" + deviceId + "/open",
424
+ params: {
425
+ keyId: keyId,
426
+ },
427
+ })];
428
+ case 3:
429
+ res = _b.sent();
430
+ return [2 /*return*/, true];
431
+ case 4: return [2 /*return*/];
432
+ }
433
+ });
434
+ });
435
+ };
436
+ this.getAccommodationCallbacks = function (_a) {
437
+ var accommodationId = _a.accommodationId;
438
+ return __awaiter(_this, void 0, void 0, function () {
439
+ var response;
440
+ return __generator(this, function (_b) {
441
+ switch (_b.label) {
442
+ case 0: return [4 /*yield*/, this.callSmartAccessApi({
443
+ accommodationId: accommodationId,
444
+ method: 'get',
445
+ route: 'v1/callback/access',
446
+ })];
447
+ case 1:
448
+ response = _b.sent();
449
+ return [2 /*return*/, true];
450
+ }
451
+ });
452
+ });
453
+ };
454
+ this.getRoomByThirdPartyId = function (_a) {
455
+ var accommodationId = _a.accommodationId, thirdPartyId = _a.thirdPartyId;
456
+ return __awaiter(_this, void 0, void 0, function () {
457
+ var foundRoomProperty;
458
+ return __generator(this, function (_b) {
459
+ switch (_b.label) {
460
+ case 0: return [4 /*yield*/, this.authenticator.getThirdPartyPropertyByThirdPartyId({
461
+ accommodationId: accommodationId,
462
+ provider: this.PROVIDER_KEY,
463
+ thirdPartyId: thirdPartyId,
464
+ type: 'room',
465
+ })];
466
+ case 1:
467
+ foundRoomProperty = _b.sent();
468
+ if (!(foundRoomProperty === null || foundRoomProperty === void 0 ? void 0 : foundRoomProperty.result))
469
+ return [2 /*return*/, false];
470
+ return [2 /*return*/, JSON.parse(foundRoomProperty.data)];
471
+ }
472
+ });
473
+ });
474
+ };
475
+ this.authenticator = authenticator;
476
+ this.API_HOST = apiHost;
477
+ this.redis = redis;
478
+ this.mutexLock = new mutex_1.default({
479
+ hashPrefix: 'RES_SMARTACCESS_MUTEX_',
480
+ mutexLockInterval: 50,
481
+ redis: redis,
482
+ });
483
+ }
484
+ SmartAccessAdapter.indexAccommodation = function (_a) {
485
+ var accommodationId = _a.accommodationId;
486
+ return "AUTH|SMARTACCESS-API|" + accommodationId;
487
+ };
488
+ SmartAccessAdapter.refineRawTokenSet = function (rawTokenSet) {
489
+ var refreshToken = rawTokenSet.refresh_token, accessToken = rawTokenSet.access_token, expiresIn = rawTokenSet.expires_in, tokenType = rawTokenSet.token_type;
490
+ var tokenSet = {
491
+ refreshToken: refreshToken,
492
+ accessToken: accessToken,
493
+ expiresIn: expiresIn,
494
+ tokenType: tokenType,
495
+ };
496
+ return tokenSet;
497
+ };
498
+ return SmartAccessAdapter;
499
+ }());
500
+ exports.default = SmartAccessAdapter;