finished refactor devices module

This commit is contained in:
faris Aljohari
2025-03-24 02:32:44 +03:00
parent 2712ce673a
commit 8efd8cfba2
11 changed files with 557 additions and 446 deletions
+10 -1
View File
@@ -8,6 +8,7 @@ import {
Param,
Post,
Put,
Req,
UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
@@ -42,8 +43,13 @@ export class SceneController {
})
async addTapToRunScene(
@Body() addSceneTapToRunDto: AddSceneTapToRunDto,
@Req() req: any,
): Promise<BaseResponseDto> {
return await this.sceneService.createScene(addSceneTapToRunDto);
const projectUuid = req.user.project.uuid;
return await this.sceneService.createScene(
addSceneTapToRunDto,
projectUuid,
);
}
@ApiBearerAuth()
@@ -100,10 +106,13 @@ export class SceneController {
async updateTapToRunScene(
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
@Param() param: SceneParamDto,
@Req() req: any,
) {
const projectUuid = req.user.project.uuid;
return await this.sceneService.updateTapToRunScene(
updateSceneTapToRunDto,
param.sceneUuid,
projectUuid,
);
}
+19 -4
View File
@@ -60,13 +60,18 @@ export class SceneService {
async createScene(
addSceneTapToRunDto: AddSceneTapToRunDto,
projectUuid: string,
): Promise<BaseResponseDto> {
try {
const { spaceUuid } = addSceneTapToRunDto;
const space = await this.getSpaceByUuid(spaceUuid);
const scene = await this.create(space.spaceTuyaUuid, addSceneTapToRunDto);
const scene = await this.create(
space.spaceTuyaUuid,
addSceneTapToRunDto,
projectUuid,
);
return new SuccessResponseDto({
message: `Successfully created new scene with uuid ${scene.uuid}`,
@@ -90,6 +95,7 @@ export class SceneService {
async create(
spaceTuyaUuid: string,
addSceneTapToRunDto: AddSceneTapToRunDto,
projectUuid: string,
): Promise<SceneEntity> {
const { iconUuid, showInHomePage, spaceUuid } = addSceneTapToRunDto;
@@ -107,6 +113,7 @@ export class SceneService {
const response = await this.createSceneExternalService(
spaceTuyaUuid,
addSceneTapToRunDto,
projectUuid,
);
const scene = await this.sceneRepository.save({
@@ -137,10 +144,11 @@ export class SceneService {
spaceTuyaUuid: string,
sceneTuyaUuid: string,
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
projectUuid: string,
) {
const { sceneName, decisionExpr, actions } = updateSceneTapToRunDto;
try {
const formattedActions = await this.prepareActions(actions);
const formattedActions = await this.prepareActions(actions, projectUuid);
const response = (await this.tuyaService.updateTapToRunScene(
sceneTuyaUuid,
@@ -178,10 +186,11 @@ export class SceneService {
async createSceneExternalService(
spaceTuyaUuid: string,
addSceneTapToRunDto: AddSceneTapToRunDto,
projectUuid: string,
) {
const { sceneName, decisionExpr, actions } = addSceneTapToRunDto;
try {
const formattedActions = await this.prepareActions(actions);
const formattedActions = await this.prepareActions(actions, projectUuid);
const response = (await this.tuyaService.addTapToRunScene(
spaceTuyaUuid,
@@ -324,6 +333,7 @@ export class SceneService {
async updateTapToRunScene(
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
sceneUuid: string,
projectUuid: string,
) {
try {
const scene = await this.findScene(sceneUuid);
@@ -340,6 +350,7 @@ export class SceneService {
space.spaceTuyaUuid,
scene.sceneTuyaUuid,
addSceneTapToRunDto,
projectUuid,
);
if (!updateTuyaSceneResponse.success) {
@@ -573,7 +584,10 @@ export class SceneService {
}
}
private async prepareActions(actions: Action[]): Promise<ConvertedAction[]> {
private async prepareActions(
actions: Action[],
projectUuid: string,
): Promise<ConvertedAction[]> {
const convertedData = convertKeysToSnakeCase(actions) as ConvertedAction[];
await Promise.all(
@@ -582,6 +596,7 @@ export class SceneService {
const device = await this.deviceService.getDeviceByDeviceUuid(
action.entity_id,
false,
projectUuid,
);
if (device) {
action.entity_id = device.deviceTuyaUuid;