automation service

This commit is contained in:
hannathkadher
2024-10-30 14:42:01 +04:00
parent 2422f40a2d
commit 295013fd15
13 changed files with 183 additions and 73 deletions

View File

@ -18,6 +18,11 @@ import {
} from '../dtos/automation.dto'; } from '../dtos/automation.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum'; import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
import {
AutomationParamDto,
DeleteAutomationParamDto,
SpaceParamDto,
} from '../dtos';
@ApiTags('Automation Module') @ApiTags('Automation Module')
@Controller({ @Controller({
@ -42,28 +47,29 @@ export class AutomationController {
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get(':unitUuid') @Get(':spaceUuid')
async getAutomationByUnit(@Param('unitUuid') unitUuid: string) { async getAutomationByUnit(@Param() param: SpaceParamDto) {
const automation = const automation = await this.automationService.getAutomationByUnit(
await this.automationService.getAutomationByUnit(unitUuid); param.spaceUuid,
);
return automation; return automation;
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('details/:automationId') @Get('details/:automationUuid')
async getAutomationDetails(@Param('automationId') automationId: string) { async getAutomationDetails(@Param() param: AutomationParamDto) {
const automation = const automation = await this.automationService.getAutomationDetails(
await this.automationService.getAutomationDetails(automationId); param.automationUuid,
);
return automation; return automation;
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Delete(':unitUuid/:automationId') @Delete(':unitUuid/:automationId')
async deleteAutomation( async deleteAutomation(@Param() param: DeleteAutomationParamDto) {
@Param('unitUuid') unitUuid: string, await this.automationService.deleteAutomation(param);
@Param('automationId') automationId: string,
) {
await this.automationService.deleteAutomation(unitUuid, automationId);
return { return {
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
message: 'Automation Deleted Successfully', message: 'Automation Deleted Successfully',
@ -74,11 +80,11 @@ export class AutomationController {
@Put(':automationId') @Put(':automationId')
async updateAutomation( async updateAutomation(
@Body() updateAutomationDto: UpdateAutomationDto, @Body() updateAutomationDto: UpdateAutomationDto,
@Param('automationId') automationId: string, @Param() param: AutomationParamDto,
) { ) {
const automation = await this.automationService.updateAutomation( const automation = await this.automationService.updateAutomation(
updateAutomationDto, updateAutomationDto,
automationId, param.automationUuid,
); );
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
@ -87,16 +93,17 @@ export class AutomationController {
data: automation, data: automation,
}; };
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Put('status/:automationId') @Put('status/:automationId')
async updateAutomationStatus( async updateAutomationStatus(
@Body() updateAutomationStatusDto: UpdateAutomationStatusDto, @Body() updateAutomationStatusDto: UpdateAutomationStatusDto,
@Param('automationId') automationId: string, @Param() param: AutomationParamDto,
) { ) {
await this.automationService.updateAutomationStatus( await this.automationService.updateAutomationStatus(
updateAutomationStatusDto, updateAutomationStatusDto,
automationId, param.automationUuid,
); );
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,

View 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;
}

View 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;
}

View File

@ -1 +1,4 @@
export * from './automation.dto'; export * from './automation.dto';
export * from './space.param.dto';
export * from './automation.param.dto';
export * from './delete.automation.param.dto';

View 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;
}

View File

@ -7,6 +7,7 @@ import {
import { SpaceRepository } from '@app/common/modules/space/repositories'; import { SpaceRepository } from '@app/common/modules/space/repositories';
import { import {
AddAutomationDto, AddAutomationDto,
DeleteAutomationParamDto,
UpdateAutomationDto, UpdateAutomationDto,
UpdateAutomationStatusDto, UpdateAutomationStatusDto,
} from '../dtos'; } from '../dtos';
@ -217,9 +218,9 @@ export class AutomationService {
} }
} }
} }
async getAutomationDetails(automationId: string, withSpaceId = false) { async getAutomationDetails(automationUuid: string, withSpaceId = false) {
try { try {
const path = `/v2.0/cloud/scene/rule/${automationId}`; const path = `/v2.0/cloud/scene/rule/${automationUuid}`;
const response = await this.tuya.request({ const response = await this.tuya.request({
method: 'GET', method: 'GET',
path, path,
@ -305,15 +306,12 @@ export class AutomationService {
} }
} }
async deleteAutomation( async deleteAutomation(param: DeleteAutomationParamDto, spaceTuyaId = null) {
unitUuid: string,
automationId: string,
spaceTuyaId = null,
) {
try { try {
const { automationUuid, spaceUuid } = param;
let unitSpaceTuyaId; let unitSpaceTuyaId;
if (!spaceTuyaId) { if (!spaceTuyaId) {
const unitDetails = await this.getUnitByUuid(unitUuid); const unitDetails = await this.getUnitByUuid(spaceUuid);
unitSpaceTuyaId = unitDetails.spaceTuyaUuid; unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
if (!unitSpaceTuyaId) { if (!unitSpaceTuyaId) {
throw new BadRequestException('Invalid unit UUID'); throw new BadRequestException('Invalid unit UUID');
@ -322,7 +320,7 @@ export class AutomationService {
unitSpaceTuyaId = spaceTuyaId; 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({ const response: DeleteAutomationInterface = await this.tuya.request({
method: 'DELETE', method: 'DELETE',
path, path,
@ -347,10 +345,10 @@ export class AutomationService {
async updateAutomation( async updateAutomation(
updateAutomationDto: UpdateAutomationDto, updateAutomationDto: UpdateAutomationDto,
automationId: string, automationUuid: string,
) { ) {
try { try {
const spaceTuyaId = await this.getAutomationDetails(automationId, true); const spaceTuyaId = await this.getAutomationDetails(automationUuid, true);
if (!spaceTuyaId.spaceId) { if (!spaceTuyaId.spaceId) {
throw new HttpException( throw new HttpException(
"Automation doesn't exist", "Automation doesn't exist",
@ -365,8 +363,12 @@ export class AutomationService {
addAutomation, addAutomation,
spaceTuyaId.spaceId, spaceTuyaId.spaceId,
); );
const params: DeleteAutomationParamDto = {
spaceUuid: spaceTuyaId.spaceId,
automationUuid: automationUuid,
};
if (newAutomation.id) { if (newAutomation.id) {
await this.deleteAutomation(null, automationId, spaceTuyaId.spaceId); await this.deleteAutomation(null, params);
return newAutomation; return newAutomation;
} }
} catch (err) { } catch (err) {
@ -382,7 +384,7 @@ export class AutomationService {
} }
async updateAutomationStatus( async updateAutomationStatus(
updateAutomationStatusDto: UpdateAutomationStatusDto, updateAutomationStatusDto: UpdateAutomationStatusDto,
automationId: string, automationUuid: string,
) { ) {
try { try {
const unitDetails = await this.getUnitByUuid( const unitDetails = await this.getUnitByUuid(
@ -397,7 +399,7 @@ export class AutomationService {
method: 'PUT', method: 'PUT',
path, path,
body: { body: {
ids: automationId, ids: automationUuid,
is_enable: updateAutomationStatusDto.isEnable, is_enable: updateAutomationStatusDto.isEnable,
}, },
}); });

View File

@ -14,6 +14,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos/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';
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum'; import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
import { DeleteSceneParamDto, SceneParamDto, SpaceParamDto } from '../dtos';
@ApiTags('Scene Module') @ApiTags('Scene Module')
@Controller({ @Controller({
@ -36,22 +37,22 @@ export class SceneController {
data: tapToRunScene, data: tapToRunScene,
}; };
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('tap-to-run/:unitUuid') @Get('tap-to-run/:spaceUuid')
async getTapToRunSceneByUnit(@Param('unitUuid') unitUuid: string) { async getTapToRunSceneByUnit(@Param() param: SpaceParamDto) {
const tapToRunScenes = const tapToRunScenes = await this.sceneService.getTapToRunSceneByUnit(
await this.sceneService.getTapToRunSceneByUnit(unitUuid); param.spaceUuid,
);
return tapToRunScenes; return tapToRunScenes;
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Delete('tap-to-run/:unitUuid/:sceneId') @Delete('tap-to-run/:unitUuid/:sceneId')
async deleteTapToRunScene( async deleteTapToRunScene(@Param() param: DeleteSceneParamDto) {
@Param('unitUuid') unitUuid: string, await this.sceneService.deleteTapToRunScene(param);
@Param('sceneId') sceneId: string,
) {
await this.sceneService.deleteTapToRunScene(unitUuid, sceneId);
return { return {
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
message: 'Scene Deleted Successfully', message: 'Scene Deleted Successfully',
@ -60,8 +61,8 @@ export class SceneController {
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('tap-to-run/trigger/:sceneId') @Post('tap-to-run/trigger/:sceneId')
async triggerTapToRunScene(@Param('sceneId') sceneId: string) { async triggerTapToRunScene(@Param() param: SceneParamDto) {
await this.sceneService.triggerTapToRunScene(sceneId); await this.sceneService.triggerTapToRunScene(param.sceneUuid);
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
success: true, success: true,
@ -72,21 +73,23 @@ export class SceneController {
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('tap-to-run/details/:sceneId') @Get('tap-to-run/details/:sceneId')
async getTapToRunSceneDetails(@Param('sceneId') sceneId: string) { async getTapToRunSceneDetails(@Param() param: SceneParamDto) {
const tapToRunScenes = const tapToRunScenes = await this.sceneService.getTapToRunSceneDetails(
await this.sceneService.getTapToRunSceneDetails(sceneId); param.sceneUuid,
);
return tapToRunScenes; return tapToRunScenes;
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Put('tap-to-run/:sceneId') @Put('tap-to-run/:sceneId')
async updateTapToRunScene( async updateTapToRunScene(
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto, @Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
@Param('sceneId') sceneId: string, @Param() param: SceneParamDto,
) { ) {
const tapToRunScene = await this.sceneService.updateTapToRunScene( const tapToRunScene = await this.sceneService.updateTapToRunScene(
updateSceneTapToRunDto, updateSceneTapToRunDto,
sceneId, param.sceneUuid,
); );
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,

View 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;
}

View File

@ -1 +1,4 @@
export * from './scene.dto'; export * from './scene.dto';
export * from './space.param.dto';
export * from './scene.param.dto';
export * from './delete.scene.param.dto';

View 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;
}

View 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;
}

View File

@ -5,7 +5,11 @@ 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, UpdateSceneTapToRunDto } from '../dtos'; import {
AddSceneTapToRunDto,
DeleteSceneParamDto,
UpdateSceneTapToRunDto,
} from '../dtos';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter'; import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
@ -44,7 +48,7 @@ export class SceneService {
try { try {
let unitSpaceTuyaId; let unitSpaceTuyaId;
if (!spaceTuyaId) { if (!spaceTuyaId) {
const unitDetails = await this.getUnitByUuid( const unitDetails = await this.getSpaceByUuid(
addSceneTapToRunDto.unitUuid, addSceneTapToRunDto.unitUuid,
); );
unitSpaceTuyaId = unitDetails.spaceTuyaUuid; unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
@ -102,34 +106,37 @@ export class SceneService {
} }
} }
} }
async getUnitByUuid(unitUuid: string) { async getSpaceByUuid(spaceUuid: string) {
try { try {
const unit = await this.spaceRepository.findOne({ const space = await this.spaceRepository.findOne({
where: { where: {
uuid: unitUuid, uuid: spaceUuid,
}, },
}); });
if (!unit) { if (!space) {
throw new BadRequestException('Invalid unit UUID'); throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
} }
return { return {
uuid: unit.uuid, uuid: space.uuid,
createdAt: unit.createdAt, createdAt: space.createdAt,
updatedAt: unit.updatedAt, updatedAt: space.updatedAt,
name: unit.spaceName, name: space.spaceName,
spaceTuyaUuid: unit.spaceTuyaUuid, spaceTuyaUuid: space.spaceTuyaUuid,
}; };
} catch (err) { } catch (err) {
if (err instanceof BadRequestException) { if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException throw err; // Re-throw BadRequestException
} else { } 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 { try {
const unit = await this.getUnitByUuid(unitUuid); const unit = await this.getSpaceByUuid(spaceUuid);
if (!unit.spaceTuyaUuid) { if (!unit.spaceTuyaUuid) {
throw new BadRequestException('Invalid unit UUID'); throw new BadRequestException('Invalid unit UUID');
} }
@ -165,15 +172,12 @@ export class SceneService {
} }
} }
} }
async deleteTapToRunScene( async deleteTapToRunScene(param: DeleteSceneParamDto, spaceTuyaId = null) {
unitUuid: string, const { spaceUuid, sceneUuid } = param;
sceneId: string,
spaceTuyaId = null,
) {
try { try {
let unitSpaceTuyaId; let unitSpaceTuyaId;
if (!spaceTuyaId) { if (!spaceTuyaId) {
const unitDetails = await this.getUnitByUuid(unitUuid); const unitDetails = await this.getSpaceByUuid(spaceUuid);
unitSpaceTuyaId = unitDetails.spaceTuyaUuid; unitSpaceTuyaId = unitDetails.spaceTuyaUuid;
if (!unitSpaceTuyaId) { if (!unitSpaceTuyaId) {
throw new BadRequestException('Invalid unit UUID'); throw new BadRequestException('Invalid unit UUID');
@ -182,7 +186,7 @@ export class SceneService {
unitSpaceTuyaId = spaceTuyaId; 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({ const response: DeleteTapToRunSceneInterface = await this.tuya.request({
method: 'DELETE', method: 'DELETE',
path, path,
@ -204,6 +208,7 @@ export class SceneService {
} }
} }
} }
async triggerTapToRunScene(sceneId: string) { async triggerTapToRunScene(sceneId: string) {
try { try {
const path = `/v2.0/cloud/scene/rule/${sceneId}/actions/trigger`; const path = `/v2.0/cloud/scene/rule/${sceneId}/actions/trigger`;
@ -317,10 +322,10 @@ export class SceneService {
} }
async updateTapToRunScene( async updateTapToRunScene(
updateSceneTapToRunDto: UpdateSceneTapToRunDto, updateSceneTapToRunDto: UpdateSceneTapToRunDto,
sceneId: string, sceneUuid: string,
) { ) {
try { try {
const spaceTuyaId = await this.getTapToRunSceneDetails(sceneId, 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);
} }
@ -332,8 +337,14 @@ export class SceneService {
addSceneTapToRunDto, addSceneTapToRunDto,
spaceTuyaId.spaceId, spaceTuyaId.spaceId,
); );
const param: DeleteSceneParamDto = {
spaceUuid: spaceTuyaId.spaceId,
sceneUuid,
};
if (newTapToRunScene.id) { if (newTapToRunScene.id) {
await this.deleteTapToRunScene(null, sceneId, spaceTuyaId.spaceId); await this.deleteTapToRunScene(null, param);
return newTapToRunScene; return newTapToRunScene;
} }
} catch (err) { } catch (err) {

View File

@ -5,6 +5,7 @@ import {
SpaceController, SpaceController,
SpaceUserController, SpaceUserController,
SubSpaceController, SubSpaceController,
SubSpaceDeviceController,
} from './controllers'; } from './controllers';
import { import {
SpaceService, SpaceService,
@ -29,7 +30,7 @@ import { DeviceRepository } from '@app/common/modules/device/repositories';
SpaceController, SpaceController,
SpaceUserController, SpaceUserController,
SubSpaceController, SubSpaceController,
SubSpaceController, SubSpaceDeviceController,
], ],
providers: [ providers: [
SpaceService, SpaceService,