updated api route and added dtos

This commit is contained in:
hannathkadher
2025-03-13 14:23:40 +04:00
parent 8b3232b2f4
commit 76fd4b640f
5 changed files with 58 additions and 25 deletions

View File

@ -8,6 +8,7 @@ import { SpaceRepository } from '@app/common/modules/space/repositories';
import {
AddAutomationDto,
AutomationParamDto,
SpaceParamDto,
UpdateAutomationDto,
UpdateAutomationStatusDto,
} 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 { AutomationEntity } from '@app/common/modules/automation/entities';
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()
export class AutomationService {
@ -51,6 +54,7 @@ export class AutomationService {
private readonly sceneDeviceRepository: SceneDeviceRepository,
private readonly sceneRepository: SceneRepository,
private readonly automationRepository: AutomationRepository,
private readonly projectRepository: ProjectRepository,
) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
@ -63,8 +67,11 @@ export class AutomationService {
}
async addAutomation(
param: ProjectParam,
addAutomationDto: AddAutomationDto,
): Promise<BaseResponseDto> {
await this.validateProject(param.projectUuid);
try {
const {
automationName,
@ -196,12 +203,14 @@ export class AutomationService {
}
}
async getAutomationBySpace(spaceUuid: string) {
async getAutomationBySpace(param: SpaceParamDto) {
try {
await this.validateProject(param.projectUuid);
// Fetch automation data from the repository
const automationData = await this.automationRepository.find({
where: {
space: { uuid: spaceUuid },
space: { uuid: param.spaceUuid },
disabled: false,
},
relations: ['space'],
@ -321,16 +330,18 @@ export class AutomationService {
}
}
}
async getAutomationDetails(automationUuid: string) {
async getAutomationDetails(param: AutomationParamDto) {
await this.validateProject(param.projectUuid);
try {
const automation = await this.findAutomation(automationUuid);
const automation = await this.findAutomation(param.automationUuid);
const automationDetails = await this.getAutomation(automation);
return automationDetails;
} catch (error) {
console.error(
`Error fetching automation details for automationUuid ${automationUuid}:`,
`Error fetching automation details for automationUuid ${param.automationUuid}:`,
error,
);
@ -466,6 +477,7 @@ export class AutomationService {
async deleteAutomation(param: AutomationParamDto) {
const { automationUuid } = param;
await this.validateProject(param.projectUuid);
try {
const automationData = await this.findAutomation(automationUuid);
@ -564,10 +576,12 @@ export class AutomationService {
}
async updateAutomation(
updateAutomationDto: UpdateAutomationDto,
automationUuid: string,
param: AutomationParamDto,
) {
await this.validateProject(param.projectUuid);
try {
const automation = await this.findAutomation(automationUuid);
const automation = await this.findAutomation(param.automationUuid);
const space = await this.getSpaceByUuid(automation.space.uuid);
const updateTuyaAutomationResponse =
@ -584,14 +598,14 @@ export class AutomationService {
);
}
const updatedScene = await this.automationRepository.update(
{ uuid: automationUuid },
{ uuid: param.automationUuid },
{
space: { uuid: automation.space.uuid },
},
);
return new SuccessResponseDto({
data: updatedScene,
message: `Automation with ID ${automationUuid} updated successfully`,
message: `Automation with ID ${param.automationUuid} updated successfully`,
});
} catch (err) {
if (err instanceof BadRequestException) {
@ -606,11 +620,13 @@ export class AutomationService {
}
async updateAutomationStatus(
updateAutomationStatusDto: UpdateAutomationStatusDto,
automationUuid: string,
param: AutomationParamDto,
) {
const { isEnable, spaceUuid } = updateAutomationStatusDto;
await this.validateProject(param.projectUuid);
try {
const automation = await this.findAutomation(automationUuid);
const automation = await this.findAutomation(param.automationUuid);
const space = await this.getSpaceByUuid(spaceUuid);
if (!space.spaceTuyaUuid) {
throw new HttpException(
@ -698,4 +714,17 @@ export class AutomationService {
);
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;
}
}