mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
automation service
This commit is contained in:
@ -18,6 +18,11 @@ import {
|
||||
} from '../dtos/automation.dto';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import {
|
||||
AutomationParamDto,
|
||||
DeleteAutomationParamDto,
|
||||
SpaceParamDto,
|
||||
} from '../dtos';
|
||||
|
||||
@ApiTags('Automation Module')
|
||||
@Controller({
|
||||
@ -42,28 +47,29 @@ export class AutomationController {
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':unitUuid')
|
||||
async getAutomationByUnit(@Param('unitUuid') unitUuid: string) {
|
||||
const automation =
|
||||
await this.automationService.getAutomationByUnit(unitUuid);
|
||||
@Get(':spaceUuid')
|
||||
async getAutomationByUnit(@Param() param: SpaceParamDto) {
|
||||
const automation = await this.automationService.getAutomationByUnit(
|
||||
param.spaceUuid,
|
||||
);
|
||||
return automation;
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('details/:automationId')
|
||||
async getAutomationDetails(@Param('automationId') automationId: string) {
|
||||
const automation =
|
||||
await this.automationService.getAutomationDetails(automationId);
|
||||
@Get('details/:automationUuid')
|
||||
async getAutomationDetails(@Param() param: AutomationParamDto) {
|
||||
const automation = await this.automationService.getAutomationDetails(
|
||||
param.automationUuid,
|
||||
);
|
||||
return automation;
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete(':unitUuid/:automationId')
|
||||
async deleteAutomation(
|
||||
@Param('unitUuid') unitUuid: string,
|
||||
@Param('automationId') automationId: string,
|
||||
) {
|
||||
await this.automationService.deleteAutomation(unitUuid, automationId);
|
||||
async deleteAutomation(@Param() param: DeleteAutomationParamDto) {
|
||||
await this.automationService.deleteAutomation(param);
|
||||
return {
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'Automation Deleted Successfully',
|
||||
@ -74,11 +80,11 @@ export class AutomationController {
|
||||
@Put(':automationId')
|
||||
async updateAutomation(
|
||||
@Body() updateAutomationDto: UpdateAutomationDto,
|
||||
@Param('automationId') automationId: string,
|
||||
@Param() param: AutomationParamDto,
|
||||
) {
|
||||
const automation = await this.automationService.updateAutomation(
|
||||
updateAutomationDto,
|
||||
automationId,
|
||||
param.automationUuid,
|
||||
);
|
||||
return {
|
||||
statusCode: HttpStatus.CREATED,
|
||||
@ -87,16 +93,17 @@ export class AutomationController {
|
||||
data: automation,
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('status/:automationId')
|
||||
async updateAutomationStatus(
|
||||
@Body() updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||
@Param('automationId') automationId: string,
|
||||
@Param() param: AutomationParamDto,
|
||||
) {
|
||||
await this.automationService.updateAutomationStatus(
|
||||
updateAutomationStatusDto,
|
||||
automationId,
|
||||
param.automationUuid,
|
||||
);
|
||||
return {
|
||||
statusCode: HttpStatus.CREATED,
|
||||
|
11
src/automation/dtos/automation.param.dto.ts
Normal file
11
src/automation/dtos/automation.param.dto.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class AutomationParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the automation',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
automationUuid: string;
|
||||
}
|
18
src/automation/dtos/delete.automation.param.dto.ts
Normal file
18
src/automation/dtos/delete.automation.param.dto.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class DeleteAutomationParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the Space',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
spaceUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'UUID of the Automation',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
automationUuid: string;
|
||||
}
|
@ -1 +1,4 @@
|
||||
export * from './automation.dto';
|
||||
export * from './space.param.dto';
|
||||
export * from './automation.param.dto';
|
||||
export * from './delete.automation.param.dto';
|
||||
|
11
src/automation/dtos/space.param.dto.ts
Normal file
11
src/automation/dtos/space.param.dto.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class SpaceParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the space',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
spaceUuid: string;
|
||||
}
|
@ -7,6 +7,7 @@ import {
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import {
|
||||
AddAutomationDto,
|
||||
DeleteAutomationParamDto,
|
||||
UpdateAutomationDto,
|
||||
UpdateAutomationStatusDto,
|
||||
} from '../dtos';
|
||||
@ -217,9 +218,9 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async getAutomationDetails(automationId: string, withSpaceId = false) {
|
||||
async getAutomationDetails(automationUuid: string, withSpaceId = false) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/scene/rule/${automationId}`;
|
||||
const path = `/v2.0/cloud/scene/rule/${automationUuid}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
@ -305,15 +306,12 @@ export class AutomationService {
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAutomation(
|
||||
unitUuid: string,
|
||||
automationId: string,
|
||||
spaceTuyaId = null,
|
||||
) {
|
||||
async deleteAutomation(param: DeleteAutomationParamDto, spaceTuyaId = null) {
|
||||
try {
|
||||
const { automationUuid, spaceUuid } = param;
|
||||
let unitSpaceTuyaId;
|
||||
if (!spaceTuyaId) {
|
||||
const unitDetails = await this.getUnitByUuid(unitUuid);
|
||||
const unitDetails = await this.getUnitByUuid(spaceUuid);
|
||||
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||
if (!unitSpaceTuyaId) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
@ -322,7 +320,7 @@ export class AutomationService {
|
||||
unitSpaceTuyaId = spaceTuyaId;
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${automationId}&space_id=${unitSpaceTuyaId}`;
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${automationUuid}&space_id=${unitSpaceTuyaId}`;
|
||||
const response: DeleteAutomationInterface = await this.tuya.request({
|
||||
method: 'DELETE',
|
||||
path,
|
||||
@ -347,10 +345,10 @@ export class AutomationService {
|
||||
|
||||
async updateAutomation(
|
||||
updateAutomationDto: UpdateAutomationDto,
|
||||
automationId: string,
|
||||
automationUuid: string,
|
||||
) {
|
||||
try {
|
||||
const spaceTuyaId = await this.getAutomationDetails(automationId, true);
|
||||
const spaceTuyaId = await this.getAutomationDetails(automationUuid, true);
|
||||
if (!spaceTuyaId.spaceId) {
|
||||
throw new HttpException(
|
||||
"Automation doesn't exist",
|
||||
@ -365,8 +363,12 @@ export class AutomationService {
|
||||
addAutomation,
|
||||
spaceTuyaId.spaceId,
|
||||
);
|
||||
const params: DeleteAutomationParamDto = {
|
||||
spaceUuid: spaceTuyaId.spaceId,
|
||||
automationUuid: automationUuid,
|
||||
};
|
||||
if (newAutomation.id) {
|
||||
await this.deleteAutomation(null, automationId, spaceTuyaId.spaceId);
|
||||
await this.deleteAutomation(null, params);
|
||||
return newAutomation;
|
||||
}
|
||||
} catch (err) {
|
||||
@ -382,7 +384,7 @@ export class AutomationService {
|
||||
}
|
||||
async updateAutomationStatus(
|
||||
updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||
automationId: string,
|
||||
automationUuid: string,
|
||||
) {
|
||||
try {
|
||||
const unitDetails = await this.getUnitByUuid(
|
||||
@ -397,7 +399,7 @@ export class AutomationService {
|
||||
method: 'PUT',
|
||||
path,
|
||||
body: {
|
||||
ids: automationId,
|
||||
ids: automationUuid,
|
||||
is_enable: updateAutomationStatusDto.isEnable,
|
||||
},
|
||||
});
|
||||
|
@ -14,6 +14,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos/scene.dto';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { DeleteSceneParamDto, SceneParamDto, SpaceParamDto } from '../dtos';
|
||||
|
||||
@ApiTags('Scene Module')
|
||||
@Controller({
|
||||
@ -36,22 +37,22 @@ export class SceneController {
|
||||
data: tapToRunScene,
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('tap-to-run/:unitUuid')
|
||||
async getTapToRunSceneByUnit(@Param('unitUuid') unitUuid: string) {
|
||||
const tapToRunScenes =
|
||||
await this.sceneService.getTapToRunSceneByUnit(unitUuid);
|
||||
@Get('tap-to-run/:spaceUuid')
|
||||
async getTapToRunSceneByUnit(@Param() param: SpaceParamDto) {
|
||||
const tapToRunScenes = await this.sceneService.getTapToRunSceneByUnit(
|
||||
param.spaceUuid,
|
||||
);
|
||||
return tapToRunScenes;
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete('tap-to-run/:unitUuid/:sceneId')
|
||||
async deleteTapToRunScene(
|
||||
@Param('unitUuid') unitUuid: string,
|
||||
@Param('sceneId') sceneId: string,
|
||||
) {
|
||||
await this.sceneService.deleteTapToRunScene(unitUuid, sceneId);
|
||||
async deleteTapToRunScene(@Param() param: DeleteSceneParamDto) {
|
||||
await this.sceneService.deleteTapToRunScene(param);
|
||||
return {
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'Scene Deleted Successfully',
|
||||
@ -60,8 +61,8 @@ export class SceneController {
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('tap-to-run/trigger/:sceneId')
|
||||
async triggerTapToRunScene(@Param('sceneId') sceneId: string) {
|
||||
await this.sceneService.triggerTapToRunScene(sceneId);
|
||||
async triggerTapToRunScene(@Param() param: SceneParamDto) {
|
||||
await this.sceneService.triggerTapToRunScene(param.sceneUuid);
|
||||
return {
|
||||
statusCode: HttpStatus.CREATED,
|
||||
success: true,
|
||||
@ -72,21 +73,23 @@ export class SceneController {
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('tap-to-run/details/:sceneId')
|
||||
async getTapToRunSceneDetails(@Param('sceneId') sceneId: string) {
|
||||
const tapToRunScenes =
|
||||
await this.sceneService.getTapToRunSceneDetails(sceneId);
|
||||
async getTapToRunSceneDetails(@Param() param: SceneParamDto) {
|
||||
const tapToRunScenes = await this.sceneService.getTapToRunSceneDetails(
|
||||
param.sceneUuid,
|
||||
);
|
||||
return tapToRunScenes;
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('tap-to-run/:sceneId')
|
||||
async updateTapToRunScene(
|
||||
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||
@Param('sceneId') sceneId: string,
|
||||
@Param() param: SceneParamDto,
|
||||
) {
|
||||
const tapToRunScene = await this.sceneService.updateTapToRunScene(
|
||||
updateSceneTapToRunDto,
|
||||
sceneId,
|
||||
param.sceneUuid,
|
||||
);
|
||||
return {
|
||||
statusCode: HttpStatus.CREATED,
|
||||
|
18
src/scene/dtos/delete.scene.param.dto.ts
Normal file
18
src/scene/dtos/delete.scene.param.dto.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class DeleteSceneParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the Space',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
spaceUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'UUID of the Scene',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
sceneUuid: string;
|
||||
}
|
@ -1 +1,4 @@
|
||||
export * from './scene.dto';
|
||||
export * from './space.param.dto';
|
||||
export * from './scene.param.dto';
|
||||
export * from './delete.scene.param.dto';
|
||||
|
11
src/scene/dtos/scene.param.dto.ts
Normal file
11
src/scene/dtos/scene.param.dto.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class SceneParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the Scene',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
sceneUuid: string;
|
||||
}
|
11
src/scene/dtos/space.param.dto.ts
Normal file
11
src/scene/dtos/space.param.dto.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsUUID } from 'class-validator';
|
||||
|
||||
export class SpaceParamDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the space',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
})
|
||||
@IsUUID()
|
||||
spaceUuid: string;
|
||||
}
|
@ -5,7 +5,11 @@ import {
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos';
|
||||
import {
|
||||
AddSceneTapToRunDto,
|
||||
DeleteSceneParamDto,
|
||||
UpdateSceneTapToRunDto,
|
||||
} from '../dtos';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
|
||||
@ -44,7 +48,7 @@ export class SceneService {
|
||||
try {
|
||||
let unitSpaceTuyaId;
|
||||
if (!spaceTuyaId) {
|
||||
const unitDetails = await this.getUnitByUuid(
|
||||
const unitDetails = await this.getSpaceByUuid(
|
||||
addSceneTapToRunDto.unitUuid,
|
||||
);
|
||||
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||
@ -102,34 +106,37 @@ export class SceneService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async getUnitByUuid(unitUuid: string) {
|
||||
async getSpaceByUuid(spaceUuid: string) {
|
||||
try {
|
||||
const unit = await this.spaceRepository.findOne({
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: unitUuid,
|
||||
uuid: spaceUuid,
|
||||
},
|
||||
});
|
||||
if (!unit) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
if (!space) {
|
||||
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
||||
}
|
||||
return {
|
||||
uuid: unit.uuid,
|
||||
createdAt: unit.createdAt,
|
||||
updatedAt: unit.updatedAt,
|
||||
name: unit.spaceName,
|
||||
spaceTuyaUuid: unit.spaceTuyaUuid,
|
||||
uuid: space.uuid,
|
||||
createdAt: space.createdAt,
|
||||
updatedAt: space.updatedAt,
|
||||
name: space.spaceName,
|
||||
spaceTuyaUuid: space.spaceTuyaUuid,
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
|
||||
throw new HttpException(
|
||||
`Space with id ${spaceUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async getTapToRunSceneByUnit(unitUuid: string) {
|
||||
async getTapToRunSceneByUnit(spaceUuid: string) {
|
||||
try {
|
||||
const unit = await this.getUnitByUuid(unitUuid);
|
||||
const unit = await this.getSpaceByUuid(spaceUuid);
|
||||
if (!unit.spaceTuyaUuid) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
}
|
||||
@ -165,15 +172,12 @@ export class SceneService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async deleteTapToRunScene(
|
||||
unitUuid: string,
|
||||
sceneId: string,
|
||||
spaceTuyaId = null,
|
||||
) {
|
||||
async deleteTapToRunScene(param: DeleteSceneParamDto, spaceTuyaId = null) {
|
||||
const { spaceUuid, sceneUuid } = param;
|
||||
try {
|
||||
let unitSpaceTuyaId;
|
||||
if (!spaceTuyaId) {
|
||||
const unitDetails = await this.getUnitByUuid(unitUuid);
|
||||
const unitDetails = await this.getSpaceByUuid(spaceUuid);
|
||||
unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
|
||||
if (!unitSpaceTuyaId) {
|
||||
throw new BadRequestException('Invalid unit UUID');
|
||||
@ -182,7 +186,7 @@ export class SceneService {
|
||||
unitSpaceTuyaId = spaceTuyaId;
|
||||
}
|
||||
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${sceneId}&space_id=${unitSpaceTuyaId}`;
|
||||
const path = `/v2.0/cloud/scene/rule?ids=${sceneUuid}&space_id=${unitSpaceTuyaId}`;
|
||||
const response: DeleteTapToRunSceneInterface = await this.tuya.request({
|
||||
method: 'DELETE',
|
||||
path,
|
||||
@ -204,6 +208,7 @@ export class SceneService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async triggerTapToRunScene(sceneId: string) {
|
||||
try {
|
||||
const path = `/v2.0/cloud/scene/rule/${sceneId}/actions/trigger`;
|
||||
@ -317,10 +322,10 @@ export class SceneService {
|
||||
}
|
||||
async updateTapToRunScene(
|
||||
updateSceneTapToRunDto: UpdateSceneTapToRunDto,
|
||||
sceneId: string,
|
||||
sceneUuid: string,
|
||||
) {
|
||||
try {
|
||||
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneId, true);
|
||||
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneUuid, true);
|
||||
if (!spaceTuyaId.spaceId) {
|
||||
throw new HttpException("Scene doesn't exist", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
@ -332,8 +337,14 @@ export class SceneService {
|
||||
addSceneTapToRunDto,
|
||||
spaceTuyaId.spaceId,
|
||||
);
|
||||
|
||||
const param: DeleteSceneParamDto = {
|
||||
spaceUuid: spaceTuyaId.spaceId,
|
||||
sceneUuid,
|
||||
};
|
||||
|
||||
if (newTapToRunScene.id) {
|
||||
await this.deleteTapToRunScene(null, sceneId, spaceTuyaId.spaceId);
|
||||
await this.deleteTapToRunScene(null, param);
|
||||
return newTapToRunScene;
|
||||
}
|
||||
} catch (err) {
|
||||
|
@ -5,6 +5,7 @@ import {
|
||||
SpaceController,
|
||||
SpaceUserController,
|
||||
SubSpaceController,
|
||||
SubSpaceDeviceController,
|
||||
} from './controllers';
|
||||
import {
|
||||
SpaceService,
|
||||
@ -29,7 +30,7 @@ import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
SpaceController,
|
||||
SpaceUserController,
|
||||
SubSpaceController,
|
||||
SubSpaceController,
|
||||
SubSpaceDeviceController,
|
||||
],
|
||||
providers: [
|
||||
SpaceService,
|
||||
|
Reference in New Issue
Block a user