Add getTapToRunSceneDetailsTuya method to AutomationService

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

View File

@ -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<AutomationDetailsResult> {
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;
}
}
}