From d40af71e789f5fe4506e21b17519ba6ceff0e462 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Mon, 12 Aug 2024 11:46:24 +0300 Subject: [PATCH] Add Default Door Lock Password and Open Door Lock Endpoint --- .../src/constants/default.door-lock-pass.ts | 1 + .../controllers/door.lock.controller.ts | 19 +++++++ src/door-lock/services/door.lock.service.ts | 53 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 libs/common/src/constants/default.door-lock-pass.ts diff --git a/libs/common/src/constants/default.door-lock-pass.ts b/libs/common/src/constants/default.door-lock-pass.ts new file mode 100644 index 0000000..3267ab8 --- /dev/null +++ b/libs/common/src/constants/default.door-lock-pass.ts @@ -0,0 +1 @@ +export const defaultDoorLockPass = '1233214'; diff --git a/src/door-lock/controllers/door.lock.controller.ts b/src/door-lock/controllers/door.lock.controller.ts index 44b9276..e57035c 100644 --- a/src/door-lock/controllers/door.lock.controller.ts +++ b/src/door-lock/controllers/door.lock.controller.ts @@ -211,4 +211,23 @@ export class DoorLockController { ); } } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Post('open/:doorLockUuid') + async openDoorLock(@Param('doorLockUuid') doorLockUuid: string) { + try { + await this.doorLockService.openDoorLock(doorLockUuid); + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'door lock opened successfully', + }; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } diff --git a/src/door-lock/services/door.lock.service.ts b/src/door-lock/services/door.lock.service.ts index a5892a6..19b738c 100644 --- a/src/door-lock/services/door.lock.service.ts +++ b/src/door-lock/services/door.lock.service.ts @@ -16,6 +16,7 @@ import { PasswordEncryptionService } from './encryption.services'; import { AddDoorLockOfflineTempMultipleTimeDto } from '../dtos/add.offline-temp.dto'; import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter'; import { UpdateDoorLockOfflineTempDto } from '../dtos/update.offline-temp.dto'; +import { defaultDoorLockPass } from '@app/common/constants/default.door-lock-pass'; @Injectable() export class DoorLockService { @@ -639,4 +640,56 @@ export class DoorLockService { ); } } + async openDoorLock( + doorLockUuid: string, + ): Promise { + try { + // Fetch ticket and encrypted password + const { ticketKey, encryptedPassword, deviceTuyaUuid, ticketId } = + await this.getTicketAndEncryptedPassword( + doorLockUuid, + defaultDoorLockPass, + ); + + // Validate ticket and device Tuya UUID + if (!ticketKey || !encryptedPassword || !deviceTuyaUuid) { + throw new HttpException( + 'Invalid Ticket or Device UUID', + HttpStatus.NOT_FOUND, + ); + } + + // Retrieve device details + const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid); + + // Validate device details + if (!deviceDetails?.deviceTuyaUuid) { + throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND); + } + + if (deviceDetails.productDevice.prodType !== ProductType.DL) { + throw new HttpException( + 'This is not a door lock device', + HttpStatus.BAD_REQUEST, + ); + } + + // Construct the path for the Tuya API request + const path = `/v1.0/devices/${deviceDetails.deviceTuyaUuid}/door-lock/password-free/open-door`; + + // Make the Tuya API request to open the door + const response = await this.tuya.request({ + method: 'POST', + path, + body: { ticket_id: ticketId }, + }); + + return response as deleteTemporaryPasswordInterface; + } catch (error) { + const status = error.status || HttpStatus.INTERNAL_SERVER_ERROR; + const message = error.message || 'Error opening door lock from Tuya'; + + throw new HttpException(message, status); + } + } }