mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-17 03:05:13 +00:00
updated delete scene
This commit is contained in:
@ -52,4 +52,38 @@ export class TuyaService {
|
|||||||
|
|
||||||
return response.result;
|
return response.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteSceneRule(sceneId: string, spaceId: string) {
|
||||||
|
const path = `/v2.0/cloud/scene/rule?ids=${sceneId}&space_id=${spaceId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'DELETE',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
return response;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
`Error deleting scene rule: ${response.msg}`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSceneRule(sceneId: string) {
|
||||||
|
const path = `/v2.0/cloud/scene/rule/${sceneId}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.success) {
|
||||||
|
return response;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
`Error fetching scene rule: ${response.msg}`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,14 +60,15 @@ export class SceneController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Delete('tap-to-run/:unitUuid/:sceneUuid')
|
@Delete('tap-to-run/:spaceUuid/:sceneUuid')
|
||||||
async deleteTapToRunScene(@Param() param: DeleteSceneParamDto) {
|
async deleteTapToRunScene(@Param() param: DeleteSceneParamDto) {
|
||||||
await this.sceneService.deleteTapToRunScene(param);
|
await this.sceneService.deleteScene(param);
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.OK,
|
statusCode: HttpStatus.OK,
|
||||||
message: 'Scene Deleted Successfully',
|
message: 'Scene Deleted Successfully',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post('tap-to-run/trigger/:sceneUuid')
|
@Post('tap-to-run/trigger/:sceneUuid')
|
||||||
|
@ -12,6 +12,7 @@ import {
|
|||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
SceneRepository,
|
SceneRepository,
|
||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
|
import { TuyaService } from '@app/common/integrations/tuya/tuya.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||||
@ -20,6 +21,7 @@ import {
|
|||||||
SceneService,
|
SceneService,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
DeviceService,
|
DeviceService,
|
||||||
|
TuyaService,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
ProductRepository,
|
ProductRepository,
|
||||||
SceneIconRepository,
|
SceneIconRepository,
|
||||||
|
@ -3,7 +3,6 @@ import {
|
|||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
HttpCode,
|
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import {
|
import {
|
||||||
@ -29,6 +28,8 @@ import {
|
|||||||
SceneRepository,
|
SceneRepository,
|
||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
||||||
|
import { SceneEntity } from '@app/common/modules/scene/entities';
|
||||||
|
import { TuyaService } from '@app/common/integrations/tuya/tuya.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SceneService {
|
export class SceneService {
|
||||||
@ -39,6 +40,7 @@ export class SceneService {
|
|||||||
private readonly sceneIconRepository: SceneIconRepository,
|
private readonly sceneIconRepository: SceneIconRepository,
|
||||||
private readonly sceneRepository: SceneRepository,
|
private readonly sceneRepository: SceneRepository,
|
||||||
private readonly deviceService: DeviceService,
|
private readonly deviceService: DeviceService,
|
||||||
|
private readonly tuyaService: TuyaService,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
@ -121,6 +123,7 @@ export class SceneService {
|
|||||||
id: response.result.id,
|
id: response.result.id,
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
} else {
|
} else {
|
||||||
@ -131,6 +134,7 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSpaceByUuid(spaceUuid: string) {
|
async getSpaceByUuid(spaceUuid: string) {
|
||||||
try {
|
try {
|
||||||
const space = await this.spaceRepository.findOne({
|
const space = await this.spaceRepository.findOne({
|
||||||
@ -221,6 +225,8 @@ export class SceneService {
|
|||||||
path,
|
path,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(path);
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
||||||
} else {
|
} else {
|
||||||
@ -229,6 +235,7 @@ export class SceneService {
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
} else {
|
} else {
|
||||||
@ -262,6 +269,7 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTapToRunSceneDetails(sceneId: string, withSpaceId = false) {
|
async getTapToRunSceneDetails(sceneId: string, withSpaceId = false) {
|
||||||
const scene = await this.sceneRepository.findOne({
|
const scene = await this.sceneRepository.findOne({
|
||||||
where: { uuid: sceneId },
|
where: { uuid: sceneId },
|
||||||
@ -372,24 +380,35 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateTapToRunScene(
|
async updateTapToRunScene(
|
||||||
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||||
sceneUuid: string,
|
sceneUuid: string,
|
||||||
) {
|
) {
|
||||||
|
const scene = await this.sceneRepository.findOne({
|
||||||
|
where: { uuid: sceneUuid },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!scene) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Scene with ${sceneUuid} is not found`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneUuid, true);
|
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneUuid, true);
|
||||||
|
|
||||||
if (!spaceTuyaId.spaceId) {
|
if (!spaceTuyaId.spaceId) {
|
||||||
throw new HttpException("Scene doesn't exist", HttpStatus.NOT_FOUND);
|
throw new HttpException("Scene doesn't exist", HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
const addSceneTapToRunDto: AddSceneTapToRunDto = {
|
const addSceneTapToRunDto: AddSceneTapToRunDto = {
|
||||||
...updateSceneTapToRunDto,
|
...updateSceneTapToRunDto,
|
||||||
spaceUuid: null,
|
spaceUuid: scene.spaceUuid,
|
||||||
iconUuid: updateSceneTapToRunDto.iconUuid,
|
iconUuid: updateSceneTapToRunDto.iconUuid,
|
||||||
showInHomePage: updateSceneTapToRunDto.showInHomePage,
|
showInHomePage: updateSceneTapToRunDto.showInHomePage,
|
||||||
};
|
};
|
||||||
const scene = await this.sceneRepository.findOne({
|
|
||||||
where: { sceneTuyaUuid: sceneUuid },
|
|
||||||
});
|
|
||||||
|
|
||||||
const newTapToRunScene = await this.addTapToRunScene(
|
const newTapToRunScene = await this.addTapToRunScene(
|
||||||
addSceneTapToRunDto,
|
addSceneTapToRunDto,
|
||||||
@ -398,12 +417,14 @@ export class SceneService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const param: DeleteSceneParamDto = {
|
const param: DeleteSceneParamDto = {
|
||||||
spaceUuid: spaceTuyaId.spaceId,
|
spaceUuid: scene.spaceUuid,
|
||||||
sceneUuid,
|
sceneUuid: scene.sceneTuyaUuid,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(param);
|
||||||
|
|
||||||
if (newTapToRunScene.id) {
|
if (newTapToRunScene.id) {
|
||||||
await this.deleteTapToRunScene(null, param);
|
await this.deleteTapToRunScene(param, spaceTuyaId.spaceId);
|
||||||
|
|
||||||
await this.sceneRepository.update(
|
await this.sceneRepository.update(
|
||||||
{ sceneTuyaUuid: sceneUuid },
|
{ sceneTuyaUuid: sceneUuid },
|
||||||
@ -419,6 +440,7 @@ export class SceneService {
|
|||||||
return newTapToRunScene;
|
return newTapToRunScene;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
} else {
|
} else {
|
||||||
@ -429,6 +451,7 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async addSceneIcon(addSceneIconDto: AddSceneIconDto) {
|
async addSceneIcon(addSceneIconDto: AddSceneIconDto) {
|
||||||
try {
|
try {
|
||||||
const icon = await this.sceneIconRepository.save({
|
const icon = await this.sceneIconRepository.save({
|
||||||
@ -463,4 +486,67 @@ export class SceneService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteScene(params: DeleteSceneParamDto) {
|
||||||
|
try {
|
||||||
|
const { sceneUuid, spaceUuid } = params;
|
||||||
|
const scene = await this.findScene(sceneUuid);
|
||||||
|
const space = await this.getSpaceByUuid(spaceUuid);
|
||||||
|
if (!space.spaceTuyaUuid) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Invalid space UUID: ${spaceUuid}`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const response = await this.delete(
|
||||||
|
scene.sceneTuyaUuid,
|
||||||
|
space.spaceTuyaUuid,
|
||||||
|
);
|
||||||
|
return response;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
throw err; // Re-throw existing HttpException
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
err.message || `Scene not found for id ${params.sceneUuid}`,
|
||||||
|
err.status || HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async findScene(sceneUuid: string): Promise<SceneEntity> {
|
||||||
|
const scene = await this.sceneRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: sceneUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!scene) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Invalid scene with id ${sceneUuid}`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(tuyaSceneId: string, tuyaSpaceId: string) {
|
||||||
|
try {
|
||||||
|
const response = (await this.tuyaService.deleteSceneRule(
|
||||||
|
tuyaSceneId,
|
||||||
|
tuyaSpaceId,
|
||||||
|
)) as DeleteTapToRunSceneInterface;
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
'Failed to delete scene rule in Tuya',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user