Files
backend/src/automation/controllers/automation.controller.ts
2024-07-20 18:18:32 +03:00

151 lines
4.0 KiB
TypeScript

import { AutomationService } from '../services/automation.service';
import {
Body,
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
Post,
Put,
UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import {
AddAutomationDto,
UpdateAutomationDto,
UpdateAutomationStatusDto,
} from '../dtos/automation.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
@ApiTags('Automation Module')
@Controller({
version: '1',
path: 'automation',
})
export class AutomationController {
constructor(private readonly automationService: AutomationService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post()
async addAutomation(@Body() addAutomationDto: AddAutomationDto) {
try {
const automation =
await this.automationService.addAutomation(addAutomationDto);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Automation added successfully',
data: automation,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':unitUuid')
async getAutomationByUnit(@Param('unitUuid') unitUuid: string) {
try {
const automation =
await this.automationService.getAutomationByUnit(unitUuid);
return automation;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('details/:automationId')
async getAutomationDetails(@Param('automationId') automationId: string) {
try {
const automation =
await this.automationService.getAutomationDetails(automationId);
return automation;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
``;
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Delete(':unitUuid/:automationId')
async deleteAutomation(
@Param('unitUuid') unitUuid: string,
@Param('automationId') automationId: string,
) {
try {
await this.automationService.deleteAutomation(unitUuid, automationId);
return {
statusCode: HttpStatus.OK,
message: 'Automation Deleted Successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put(':automationId')
async updateAutomation(
@Body() updateAutomationDto: UpdateAutomationDto,
@Param('automationId') automationId: string,
) {
try {
const automation = await this.automationService.updateAutomation(
updateAutomationDto,
automationId,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Automation updated successfully',
data: automation,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('status/:automationId')
async updateAutomationStatus(
@Body() updateAutomationStatusDto: UpdateAutomationStatusDto,
@Param('automationId') automationId: string,
) {
try {
await this.automationService.updateAutomationStatus(
updateAutomationStatusDto,
automationId,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Automation status updated successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}