mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 06:44:55 +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,
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user