mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Refactor Scene Module: Add Update Scene Tap To Run Endpoint
This commit is contained in:
@ -8,10 +8,11 @@ import {
|
|||||||
HttpStatus,
|
HttpStatus,
|
||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
|
Put,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { AddSceneTapToRunDto } from '../dtos/add.scene.dto';
|
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos/scene.dto';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
|
||||||
@ApiTags('Scene Module')
|
@ApiTags('Scene Module')
|
||||||
@ -99,11 +100,37 @@ export class SceneController {
|
|||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get('tap-to-run/details/:sceneId')
|
@Get('tap-to-run/details/:sceneId')
|
||||||
async triggerTapToRunSceneDetails(@Param('sceneId') sceneId: string) {
|
async getTapToRunSceneDetails(@Param('sceneId') sceneId: string) {
|
||||||
try {
|
try {
|
||||||
const tapToRunScenes =
|
const tapToRunScenes =
|
||||||
await this.sceneService.triggerTapToRunSceneDetails(sceneId);
|
await this.sceneService.getTapToRunSceneDetails(sceneId);
|
||||||
return tapToRunScenes;
|
return tapToRunScenes;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
``;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Put('tap-to-run/:sceneId')
|
||||||
|
async updateTapToRunScene(
|
||||||
|
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||||
|
@Param('sceneId') sceneId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const tapToRunScene = await this.sceneService.updateTapToRunScene(
|
||||||
|
updateSceneTapToRunDto,
|
||||||
|
sceneId,
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Scene updated successfully',
|
||||||
|
data: tapToRunScene,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
|
@ -1 +1 @@
|
|||||||
export * from './add.scene.dto';
|
export * from './scene.dto';
|
||||||
|
@ -101,3 +101,34 @@ export class AddSceneTapToRunDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class UpdateSceneTapToRunDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Scene name',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public sceneName: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Decision expression',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public decisionExpr: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Actions',
|
||||||
|
required: true,
|
||||||
|
type: [Action],
|
||||||
|
})
|
||||||
|
@IsArray()
|
||||||
|
@ValidateNested({ each: true })
|
||||||
|
@Type(() => Action)
|
||||||
|
public actions: Action[];
|
||||||
|
|
||||||
|
constructor(dto: Partial<AddSceneTapToRunDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddSceneTapToRunDto } from '../dtos';
|
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos';
|
||||||
import { GetUnitByUuidInterface } from 'src/unit/interface/unit.interface';
|
import { GetUnitByUuidInterface } from 'src/unit/interface/unit.interface';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
@ -36,12 +36,24 @@ export class SceneService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async addTapToRunScene(addSceneTapToRunDto: AddSceneTapToRunDto) {
|
async addTapToRunScene(
|
||||||
|
addSceneTapToRunDto: AddSceneTapToRunDto,
|
||||||
|
spaceTuyaId = null,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const unit = await this.getUnitByUuid(addSceneTapToRunDto.unitUuid);
|
let unitSpaceTuyaId;
|
||||||
if (!unit.spaceTuyaUuid) {
|
if (!spaceTuyaId) {
|
||||||
throw new BadRequestException('Invalid unit UUID');
|
const unitDetails = await this.getUnitByUuid(
|
||||||
|
addSceneTapToRunDto.unitUuid,
|
||||||
|
);
|
||||||
|
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||||
|
if (!unitDetails) {
|
||||||
|
throw new BadRequestException('Invalid unit UUID');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unitSpaceTuyaId = spaceTuyaId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const actions = addSceneTapToRunDto.actions.map((action) => {
|
const actions = addSceneTapToRunDto.actions.map((action) => {
|
||||||
return {
|
return {
|
||||||
...action,
|
...action,
|
||||||
@ -65,7 +77,7 @@ export class SceneService {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
path,
|
path,
|
||||||
body: {
|
body: {
|
||||||
space_id: unit.spaceTuyaUuid,
|
space_id: unitSpaceTuyaId,
|
||||||
name: addSceneTapToRunDto.sceneName,
|
name: addSceneTapToRunDto.sceneName,
|
||||||
type: 'scene',
|
type: 'scene',
|
||||||
decision_expr: addSceneTapToRunDto.decisionExpr,
|
decision_expr: addSceneTapToRunDto.decisionExpr,
|
||||||
@ -157,14 +169,24 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async deleteTapToRunScene(unitUuid: string, sceneId: string) {
|
async deleteTapToRunScene(
|
||||||
|
unitUuid: string,
|
||||||
|
sceneId: string,
|
||||||
|
spaceTuyaId = null,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const unit = await this.getUnitByUuid(unitUuid);
|
let unitSpaceTuyaId;
|
||||||
if (!unit.spaceTuyaUuid) {
|
if (!spaceTuyaId) {
|
||||||
throw new BadRequestException('Invalid unit UUID');
|
const unitDetails = await this.getUnitByUuid(unitUuid);
|
||||||
|
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||||
|
if (!unitSpaceTuyaId) {
|
||||||
|
throw new BadRequestException('Invalid unit UUID');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unitSpaceTuyaId = spaceTuyaId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = `/v2.0/cloud/scene/rule?ids=${sceneId}&space_id=${unit.spaceTuyaUuid}`;
|
const path = `/v2.0/cloud/scene/rule?ids=${sceneId}&space_id=${unitSpaceTuyaId}`;
|
||||||
const response: DeleteTapToRunSceneInterface = await this.tuya.request({
|
const response: DeleteTapToRunSceneInterface = await this.tuya.request({
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
path,
|
path,
|
||||||
@ -208,7 +230,7 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async triggerTapToRunSceneDetails(sceneId: string) {
|
async getTapToRunSceneDetails(sceneId: string, withSpaceId = false) {
|
||||||
try {
|
try {
|
||||||
const path = `/v2.0/cloud/scene/rule/${sceneId}`;
|
const path = `/v2.0/cloud/scene/rule/${sceneId}`;
|
||||||
const response = await this.tuya.request({
|
const response = await this.tuya.request({
|
||||||
@ -225,7 +247,37 @@ export class SceneService {
|
|||||||
status: responseData.status,
|
status: responseData.status,
|
||||||
type: 'tap_to_run',
|
type: 'tap_to_run',
|
||||||
actions: responseData.actions,
|
actions: responseData.actions,
|
||||||
|
...(withSpaceId && { spaceId: responseData.spaceId }),
|
||||||
};
|
};
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async updateTapToRunScene(
|
||||||
|
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||||
|
sceneId: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneId, true);
|
||||||
|
if (!spaceTuyaId.spaceId) {
|
||||||
|
throw new HttpException("Scene doesn't exist", HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
const addSceneTapToRunDto: AddSceneTapToRunDto = {
|
||||||
|
...updateSceneTapToRunDto,
|
||||||
|
unitUuid: null,
|
||||||
|
};
|
||||||
|
const newTapToRunScene = await this.addTapToRunScene(
|
||||||
|
addSceneTapToRunDto,
|
||||||
|
spaceTuyaId.spaceId,
|
||||||
|
);
|
||||||
|
if (newTapToRunScene.id) {
|
||||||
|
await this.deleteTapToRunScene(null, sceneId, spaceTuyaId.spaceId);
|
||||||
|
return newTapToRunScene;
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
|
Reference in New Issue
Block a user