From 4d7ea8c9bd402ae1c6dce340e9fe5078b7b960f3 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 12 Aug 2024 15:37:13 +0300 Subject: [PATCH] added password return data --- src/door-lock/services/door.lock.service.ts | 114 ++++++++++++++---- .../visitor-password.controller.ts | 4 +- .../services/visitor-password.service.ts | 29 +---- 3 files changed, 101 insertions(+), 46 deletions(-) diff --git a/src/door-lock/services/door.lock.service.ts b/src/door-lock/services/door.lock.service.ts index 0d90822..7a3cd72 100644 --- a/src/door-lock/services/door.lock.service.ts +++ b/src/door-lock/services/door.lock.service.ts @@ -1,4 +1,9 @@ -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; +import { + Injectable, + HttpException, + HttpStatus, + BadRequestException, +} from '@nestjs/common'; import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { ConfigService } from '@nestjs/config'; import { @@ -83,7 +88,10 @@ export class DoorLockService { ); } } - async getOfflineMultipleTimeTemporaryPasswords(doorLockUuid: string) { + async getOfflineMultipleTimeTemporaryPasswords( + doorLockUuid: string, + fromVisitor?: boolean, + ) { try { const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid); @@ -99,11 +107,25 @@ export class DoorLockService { deviceDetails.deviceTuyaUuid, 'multiple', ); - - if (passwords.result.records.length > 0) { - return convertKeysToCamelCase(passwords.result.records); + if (!passwords.result.records.length && fromVisitor) { + throw new BadRequestException(); + } + if (passwords.result.records.length > 0) { + return fromVisitor + ? convertKeysToCamelCase(passwords.result.records).map((password) => { + return { + passwodId: `${password.pwdId}`, + passwodExpired: `${password.gmtExpired}`, + passwordStart: `${password.gmtStart}`, + passwordCreated: `${password.gmtCreate}`, + passwodName: password.pwdName, + passwordStatus: password.status, + passwordType: 'OFFLINE_MULTIPLE', + deviceUuid: doorLockUuid, + }; + }) + : convertKeysToCamelCase(passwords.result.records); } - return passwords; } catch (error) { throw new HttpException( @@ -113,10 +135,12 @@ export class DoorLockService { ); } } - async getOfflineOneTimeTemporaryPasswords(doorLockUuid: string) { + async getOfflineOneTimeTemporaryPasswords( + doorLockUuid: string, + fromVisitor?: boolean, + ) { try { const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid); - if (!deviceDetails || !deviceDetails.deviceTuyaUuid) { throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND); } else if (deviceDetails.productDevice.prodType !== ProductType.DL) { @@ -129,9 +153,24 @@ export class DoorLockService { deviceDetails.deviceTuyaUuid, 'once', ); - + if (!passwords.result.records.length && fromVisitor) { + throw new BadRequestException(); + } if (passwords.result.records.length > 0) { - return convertKeysToCamelCase(passwords.result.records); + return fromVisitor + ? convertKeysToCamelCase(passwords.result.records).map((password) => { + return { + passwodId: `${password.pwdId}`, + passwodExpired: `${password.gmtExpired}`, + passwordStart: `${password.gmtStart}`, + passwordCreated: `${password.gmtCreate}`, + passwodName: password.pwdName, + passwordStatus: password.status, + passwordType: 'OFFLINE_ONETIME', + deviceUuid: doorLockUuid, + }; + }) + : convertKeysToCamelCase(passwords.result.records); } return passwords; @@ -142,7 +181,10 @@ export class DoorLockService { ); } } - async getOnlineTemporaryPasswords(doorLockUuid: string) { + async getOnlineTemporaryPasswords( + doorLockUuid: string, + fromVisitor?: boolean, + ) { try { const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid); @@ -157,8 +199,7 @@ export class DoorLockService { const passwords = await this.getOnlineTemporaryPasswordsTuya( deviceDetails.deviceTuyaUuid, ); - - if (passwords.result.length > 0) { + if (passwords.result?.length > 0) { const passwordFiltered = passwords.result .filter((item) => item.type === 0) .map((password: any) => { @@ -181,9 +222,24 @@ export class DoorLockService { return password; }); - return convertKeysToCamelCase(passwordFiltered); + return fromVisitor + ? convertKeysToCamelCase(passwordFiltered).map((password) => { + return { + passwodId: `${password.id}`, + passwodExpired: `${password.invalidTime}`, + passwordStart: `${password.effectiveTime}`, + passwordCreated: '', + passwodName: password.name, + passwordStatus: '', + passwordType: 'ONLINE_MULTIPLE', + deviceUuid: doorLockUuid, + }; + }) + : convertKeysToCamelCase(passwordFiltered); + } + if (fromVisitor) { + throw new BadRequestException(); } - return passwords; } catch (error) { throw new HttpException( @@ -192,8 +248,10 @@ export class DoorLockService { ); } } - - async getOnlineTemporaryPasswordsOneTime(doorLockUuid: string) { + async getOnlineTemporaryPasswordsOneTime( + doorLockUuid: string, + fromVisitor?: boolean, + ) { try { const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid); @@ -208,8 +266,7 @@ export class DoorLockService { const passwords = await this.getOnlineTemporaryPasswordsTuya( deviceDetails.deviceTuyaUuid, ); - - if (passwords.result.length > 0) { + if (passwords.result?.length > 0) { const passwordFiltered = passwords.result .filter((item) => item.type === 1) .map((password: any) => { @@ -232,9 +289,24 @@ export class DoorLockService { return password; }); - return convertKeysToCamelCase(passwordFiltered); + return fromVisitor + ? convertKeysToCamelCase(passwordFiltered).map((password) => { + return { + passwodId: `${password.id}`, + passwodExpired: `${password.invalidTime}`, + passwordStart: `${password.effectiveTime}`, + passwordCreated: '', + passwodName: password.name, + passwordStatus: '', + passwordType: 'ONLINE_ONETIME', + deviceUuid: doorLockUuid, + }; + }) + : convertKeysToCamelCase(passwordFiltered); + } + if (fromVisitor) { + throw new BadRequestException(); } - return passwords; } catch (error) { throw new HttpException( diff --git a/src/vistor-password/controllers/visitor-password.controller.ts b/src/vistor-password/controllers/visitor-password.controller.ts index c0f09b6..c3fdfc8 100644 --- a/src/vistor-password/controllers/visitor-password.controller.ts +++ b/src/vistor-password/controllers/visitor-password.controller.ts @@ -49,8 +49,8 @@ export class VisitorPasswordController { ); } } - @ApiBearerAuth() - @UseGuards(JwtAuthGuard) + // @ApiBearerAuth() + // @UseGuards(JwtAuthGuard) @Post('temporary-password/online/one-time') async addOnlineTemporaryPassword( @Body() addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto, diff --git a/src/vistor-password/services/visitor-password.service.ts b/src/vistor-password/services/visitor-password.service.ts index 2e64faa..98b3a1e 100644 --- a/src/vistor-password/services/visitor-password.service.ts +++ b/src/vistor-password/services/visitor-password.service.ts @@ -417,38 +417,21 @@ export class VisitorPasswordService { deviceIds.forEach((deviceId) => { data.push( this.doorLockService - .getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid) + .getOfflineMultipleTimeTemporaryPasswords(deviceId.uuid, true) .catch(() => {}), this.doorLockService - .getOnlineTemporaryPasswordsOneTime(deviceId.uuid) + .getOnlineTemporaryPasswordsOneTime(deviceId.uuid, true) .catch(() => {}), this.doorLockService - .getOnlineTemporaryPasswords(deviceId.uuid) + .getOnlineTemporaryPasswords(deviceId.uuid, true) .catch(() => {}), this.doorLockService - .getOfflineOneTimeTemporaryPasswords(deviceId.uuid) + .getOfflineOneTimeTemporaryPasswords(deviceId.uuid, true) .catch(() => {}), ); }); - return (await Promise.all(data)).flat().map((item) => { - return { - id: item.pwdId || item.id || null, - name: item.pwdName || item.name || '', - type: item.pwdTypeCode || item.type || '', - startTime: item.gmtStart || item.effectiveTime || null, - endTime: item.gmtExpired || item.invalidTime || null, - status: item.status || item.success || null, - additionalInfo: { - optUid: item.optUid || null, - hasClearPwd: item.hasClearPwd || false, - phase: item.phase || null, - phone: item.phone || '', - timeZone: item.timeZone || '', - result: item.result || null, - tid: item.tid || null, - t: item.t || null, - }, - }; + return (await Promise.all(data)).flat().filter((datum) => { + return datum != null; }); }