From 714cd937b4c48342da96dfeed8ef588c2c325428 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 30 Jul 2024 15:13:59 +0300 Subject: [PATCH] Add getTapToRunSceneDetailsTuya method to AutomationService --- .../interface/automation.interface.ts | 5 +++ src/automation/services/automation.service.ts | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/automation/interface/automation.interface.ts b/src/automation/interface/automation.interface.ts index 6ddec4d..298d58c 100644 --- a/src/automation/interface/automation.interface.ts +++ b/src/automation/interface/automation.interface.ts @@ -43,3 +43,8 @@ export interface AutomationResponseData { conditions: Condition[]; [key: string]: any; // Allow additional properties } +export interface AutomationDetailsResult { + id: string; + name: string; + type: string; +} diff --git a/src/automation/services/automation.service.ts b/src/automation/services/automation.service.ts index ce9bfa5..bbb7651 100644 --- a/src/automation/services/automation.service.ts +++ b/src/automation/services/automation.service.ts @@ -17,6 +17,7 @@ import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter'; import { DeviceService } from 'src/device/services'; import { AddAutomationInterface, + AutomationDetailsResult, AutomationResponseData, DeleteAutomationInterface, GetAutomationByUnitInterface, @@ -186,6 +187,38 @@ export class AutomationService { } } } + async getTapToRunSceneDetailsTuya( + sceneId: string, + ): Promise { + try { + const path = `/v2.0/cloud/scene/rule/${sceneId}`; + const response = await this.tuya.request({ + method: 'GET', + path, + }); + + if (!response.success) { + throw new HttpException(response.msg, HttpStatus.BAD_REQUEST); + } + const camelCaseResponse = convertKeysToCamelCase(response); + const { id, name, type } = camelCaseResponse.result; + + return { + id, + name, + type, + } as AutomationDetailsResult; + } catch (err) { + if (err instanceof BadRequestException) { + throw err; // Re-throw BadRequestException + } else { + throw new HttpException( + 'Scene not found for Tuya', + HttpStatus.NOT_FOUND, + ); + } + } + } async getAutomationDetails(automationId: string, withSpaceId = false) { try { const path = `/v2.0/cloud/scene/rule/${automationId}`; @@ -215,6 +248,18 @@ export class AutomationService { if (device) { action.entityId = device.uuid; } + } else if ( + action.actionExecutor !== 'device_issue' && + action.actionExecutor !== 'delay' + ) { + const sceneDetails = await this.getTapToRunSceneDetailsTuya( + action.entityId, + ); + + if (sceneDetails.id) { + action.name = sceneDetails.name; + action.type = sceneDetails.type; + } } }