mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
updated api route and added dtos
This commit is contained in:
@ -624,7 +624,7 @@ export class ControllerRoute {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static AUTOMATION = class {
|
static AUTOMATION = class {
|
||||||
public static readonly ROUTE = 'automation';
|
public static readonly ROUTE = '/projects/:projectUuid/automation';
|
||||||
|
|
||||||
static ACTIONS = class {
|
static ACTIONS = class {
|
||||||
public static readonly ADD_AUTOMATION_SUMMARY = 'Add automation';
|
public static readonly ADD_AUTOMATION_SUMMARY = 'Add automation';
|
||||||
|
@ -21,6 +21,7 @@ import { AutomationParamDto, SpaceParamDto } from '../dtos';
|
|||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
||||||
|
|
||||||
@ApiTags('Automation Module')
|
@ApiTags('Automation Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -38,9 +39,14 @@ export class AutomationController {
|
|||||||
summary: ControllerRoute.AUTOMATION.ACTIONS.ADD_AUTOMATION_SUMMARY,
|
summary: ControllerRoute.AUTOMATION.ACTIONS.ADD_AUTOMATION_SUMMARY,
|
||||||
description: ControllerRoute.AUTOMATION.ACTIONS.ADD_AUTOMATION_DESCRIPTION,
|
description: ControllerRoute.AUTOMATION.ACTIONS.ADD_AUTOMATION_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async addAutomation(@Body() addAutomationDto: AddAutomationDto) {
|
async addAutomation(
|
||||||
const automation =
|
@Param() param: ProjectParam,
|
||||||
await this.automationService.addAutomation(addAutomationDto);
|
@Body() addAutomationDto: AddAutomationDto,
|
||||||
|
) {
|
||||||
|
const automation = await this.automationService.addAutomation(
|
||||||
|
param,
|
||||||
|
addAutomationDto,
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
success: true,
|
success: true,
|
||||||
@ -59,9 +65,7 @@ export class AutomationController {
|
|||||||
ControllerRoute.AUTOMATION.ACTIONS.GET_AUTOMATION_BY_SPACE_DESCRIPTION,
|
ControllerRoute.AUTOMATION.ACTIONS.GET_AUTOMATION_BY_SPACE_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async getAutomationBySpace(@Param() param: SpaceParamDto) {
|
async getAutomationBySpace(@Param() param: SpaceParamDto) {
|
||||||
const automation = await this.automationService.getAutomationBySpace(
|
const automation = await this.automationService.getAutomationBySpace(param);
|
||||||
param.spaceUuid,
|
|
||||||
);
|
|
||||||
return automation;
|
return automation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,9 +79,7 @@ export class AutomationController {
|
|||||||
ControllerRoute.AUTOMATION.ACTIONS.GET_AUTOMATION_DETAILS_DESCRIPTION,
|
ControllerRoute.AUTOMATION.ACTIONS.GET_AUTOMATION_DETAILS_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async getAutomationDetails(@Param() param: AutomationParamDto) {
|
async getAutomationDetails(@Param() param: AutomationParamDto) {
|
||||||
const automation = await this.automationService.getAutomationDetails(
|
const automation = await this.automationService.getAutomationDetails(param);
|
||||||
param.automationUuid,
|
|
||||||
);
|
|
||||||
return automation;
|
return automation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +115,7 @@ export class AutomationController {
|
|||||||
) {
|
) {
|
||||||
const automation = await this.automationService.updateAutomation(
|
const automation = await this.automationService.updateAutomation(
|
||||||
updateAutomationDto,
|
updateAutomationDto,
|
||||||
param.automationUuid,
|
param,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
@ -139,7 +141,7 @@ export class AutomationController {
|
|||||||
) {
|
) {
|
||||||
await this.automationService.updateAutomationStatus(
|
await this.automationService.updateAutomationStatus(
|
||||||
updateAutomationStatusDto,
|
updateAutomationStatusDto,
|
||||||
param.automationUuid,
|
param,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class AutomationParamDto {
|
export class AutomationParamDto extends ProjectParam {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'TuyaId of the automation',
|
description: 'TuyaId of the automation',
|
||||||
example: 'SfFi2Tbn09btes84',
|
example: 'SfFi2Tbn09btes84',
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsUUID } from 'class-validator';
|
import { IsUUID } from 'class-validator';
|
||||||
|
|
||||||
export class SpaceParamDto {
|
export class SpaceParamDto extends ProjectParam {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'UUID of the space',
|
description: 'UUID of the space',
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||||
|
@ -8,6 +8,7 @@ import { SpaceRepository } from '@app/common/modules/space/repositories';
|
|||||||
import {
|
import {
|
||||||
AddAutomationDto,
|
AddAutomationDto,
|
||||||
AutomationParamDto,
|
AutomationParamDto,
|
||||||
|
SpaceParamDto,
|
||||||
UpdateAutomationDto,
|
UpdateAutomationDto,
|
||||||
UpdateAutomationStatusDto,
|
UpdateAutomationStatusDto,
|
||||||
} from '../dtos';
|
} from '../dtos';
|
||||||
@ -39,6 +40,8 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
import { AutomationEntity } from '@app/common/modules/automation/entities';
|
import { AutomationEntity } from '@app/common/modules/automation/entities';
|
||||||
import { DeleteTapToRunSceneInterface } from 'src/scene/interface/scene.interface';
|
import { DeleteTapToRunSceneInterface } from 'src/scene/interface/scene.interface';
|
||||||
|
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
||||||
|
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AutomationService {
|
export class AutomationService {
|
||||||
@ -51,6 +54,7 @@ export class AutomationService {
|
|||||||
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
||||||
private readonly sceneRepository: SceneRepository,
|
private readonly sceneRepository: SceneRepository,
|
||||||
private readonly automationRepository: AutomationRepository,
|
private readonly automationRepository: AutomationRepository,
|
||||||
|
private readonly projectRepository: ProjectRepository,
|
||||||
) {
|
) {
|
||||||
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');
|
||||||
@ -63,8 +67,11 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async addAutomation(
|
async addAutomation(
|
||||||
|
param: ProjectParam,
|
||||||
addAutomationDto: AddAutomationDto,
|
addAutomationDto: AddAutomationDto,
|
||||||
): Promise<BaseResponseDto> {
|
): Promise<BaseResponseDto> {
|
||||||
|
await this.validateProject(param.projectUuid);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
automationName,
|
automationName,
|
||||||
@ -196,12 +203,14 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAutomationBySpace(spaceUuid: string) {
|
async getAutomationBySpace(param: SpaceParamDto) {
|
||||||
try {
|
try {
|
||||||
|
await this.validateProject(param.projectUuid);
|
||||||
|
|
||||||
// Fetch automation data from the repository
|
// Fetch automation data from the repository
|
||||||
const automationData = await this.automationRepository.find({
|
const automationData = await this.automationRepository.find({
|
||||||
where: {
|
where: {
|
||||||
space: { uuid: spaceUuid },
|
space: { uuid: param.spaceUuid },
|
||||||
disabled: false,
|
disabled: false,
|
||||||
},
|
},
|
||||||
relations: ['space'],
|
relations: ['space'],
|
||||||
@ -321,16 +330,18 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getAutomationDetails(automationUuid: string) {
|
async getAutomationDetails(param: AutomationParamDto) {
|
||||||
|
await this.validateProject(param.projectUuid);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const automation = await this.findAutomation(automationUuid);
|
const automation = await this.findAutomation(param.automationUuid);
|
||||||
|
|
||||||
const automationDetails = await this.getAutomation(automation);
|
const automationDetails = await this.getAutomation(automation);
|
||||||
|
|
||||||
return automationDetails;
|
return automationDetails;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
`Error fetching automation details for automationUuid ${automationUuid}:`,
|
`Error fetching automation details for automationUuid ${param.automationUuid}:`,
|
||||||
error,
|
error,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -466,6 +477,7 @@ export class AutomationService {
|
|||||||
|
|
||||||
async deleteAutomation(param: AutomationParamDto) {
|
async deleteAutomation(param: AutomationParamDto) {
|
||||||
const { automationUuid } = param;
|
const { automationUuid } = param;
|
||||||
|
await this.validateProject(param.projectUuid);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const automationData = await this.findAutomation(automationUuid);
|
const automationData = await this.findAutomation(automationUuid);
|
||||||
@ -564,10 +576,12 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
async updateAutomation(
|
async updateAutomation(
|
||||||
updateAutomationDto: UpdateAutomationDto,
|
updateAutomationDto: UpdateAutomationDto,
|
||||||
automationUuid: string,
|
param: AutomationParamDto,
|
||||||
) {
|
) {
|
||||||
|
await this.validateProject(param.projectUuid);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const automation = await this.findAutomation(automationUuid);
|
const automation = await this.findAutomation(param.automationUuid);
|
||||||
const space = await this.getSpaceByUuid(automation.space.uuid);
|
const space = await this.getSpaceByUuid(automation.space.uuid);
|
||||||
|
|
||||||
const updateTuyaAutomationResponse =
|
const updateTuyaAutomationResponse =
|
||||||
@ -584,14 +598,14 @@ export class AutomationService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
const updatedScene = await this.automationRepository.update(
|
const updatedScene = await this.automationRepository.update(
|
||||||
{ uuid: automationUuid },
|
{ uuid: param.automationUuid },
|
||||||
{
|
{
|
||||||
space: { uuid: automation.space.uuid },
|
space: { uuid: automation.space.uuid },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return new SuccessResponseDto({
|
return new SuccessResponseDto({
|
||||||
data: updatedScene,
|
data: updatedScene,
|
||||||
message: `Automation with ID ${automationUuid} updated successfully`,
|
message: `Automation with ID ${param.automationUuid} updated successfully`,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
@ -606,11 +620,13 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
async updateAutomationStatus(
|
async updateAutomationStatus(
|
||||||
updateAutomationStatusDto: UpdateAutomationStatusDto,
|
updateAutomationStatusDto: UpdateAutomationStatusDto,
|
||||||
automationUuid: string,
|
param: AutomationParamDto,
|
||||||
) {
|
) {
|
||||||
const { isEnable, spaceUuid } = updateAutomationStatusDto;
|
const { isEnable, spaceUuid } = updateAutomationStatusDto;
|
||||||
|
await this.validateProject(param.projectUuid);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const automation = await this.findAutomation(automationUuid);
|
const automation = await this.findAutomation(param.automationUuid);
|
||||||
const space = await this.getSpaceByUuid(spaceUuid);
|
const space = await this.getSpaceByUuid(spaceUuid);
|
||||||
if (!space.spaceTuyaUuid) {
|
if (!space.spaceTuyaUuid) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
@ -698,4 +714,17 @@ export class AutomationService {
|
|||||||
);
|
);
|
||||||
return convertedData;
|
return convertedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async validateProject(uuid: string) {
|
||||||
|
const project = await this.projectRepository.findOne({
|
||||||
|
where: { uuid },
|
||||||
|
});
|
||||||
|
if (!project) {
|
||||||
|
throw new HttpException(
|
||||||
|
`A project with the uuid '${uuid}' doesn't exists.`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return project;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user