Add functionality to retrieve Tap-to-Run scene details from Tuya API

This commit is contained in:
faris Aljohari
2024-07-30 15:14:06 +03:00
parent 714cd937b4
commit 0ad6cfb6ef
2 changed files with 50 additions and 0 deletions

View File

@ -21,3 +21,8 @@ export interface DeleteTapToRunSceneInterface {
msg?: string;
result: boolean;
}
export interface SceneDetailsResult {
id: string;
name: string;
type: string;
}

View File

@ -15,6 +15,7 @@ import {
AddTapToRunSceneInterface,
DeleteTapToRunSceneInterface,
GetTapToRunSceneByUnitInterface,
SceneDetailsResult,
} from '../interface/scene.interface';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
@ -256,6 +257,18 @@ export class SceneService {
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;
}
}
}
@ -275,6 +288,38 @@ export class SceneService {
}
}
}
async getTapToRunSceneDetailsTuya(
sceneId: string,
): Promise<SceneDetailsResult> {
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 SceneDetailsResult;
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException(
'Scene not found for Tuya',
HttpStatus.NOT_FOUND,
);
}
}
}
async updateTapToRunScene(
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
sceneId: string,