import { Body, Controller, Delete, Get, Inject, Param, Patch, Post, } from '@nestjs/common'; import { InputsService } from './inputs.service'; import { DadosferaLogger } from 'dadosfera-logs'; import { InputsClientService } from 'src/clients/inputs/client.service'; import { UpdateInputRequest } from 'src/clients/inputs/interfaces'; import { PERMISSIONS } from '../../authentication/permissions.enum'; import { AuthenticateCondition } from 'src/authentication/authentication.decorator'; import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; import { CreateInputReq, GetAvailableEntitiesReq, GetAvailableEntitiesRes, Input, TestConnectionGetColumnsReq, TestConnectionGetColumnsRes, TestConnectionReq, TestConnectionRes, } from './dtos/input.model'; @ApiTags('inputs') @Controller('inputs') @AuthenticateCondition((req, user) => { let action: any; switch (req.method) { case 'POST': action = 'CREATE'; break; case 'PUT': action = 'UPDATE'; break; case 'PATCH': action = 'UPDATE'; break; default: action = req.method; } if ( `${req.method} ${req.route.path}` === 'GET /inputs/available-entities/:plugin' ) { action = 'GET_ENTITIES'; } return user.permissions.includes(PERMISSIONS.INPUT.permissions[action].seqid); }) export class InputsController { inputService: InputsService; logger: any; constructor( @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, private inputsClientService: InputsClientService, ) { this.logger = dadosferaLogger.logger; this.inputService = new InputsService(this.inputsClientService); } @Get('available-entities/:plugin') @ApiOkResponse({ type: GetAvailableEntitiesRes }) async getAvailableEntities( @Body() body, @Param() params: GetAvailableEntitiesReq, ) { const { info } = body; const { plugin } = params; return await this.inputService.getAvailableEntities({ info, plugin }); } @Post('/test-connection') @ApiOkResponse({ type: TestConnectionRes }) async testConnection(@Body() data: TestConnectionReq) { this.logger.info(`/test-connection - ON TEST CONNECTION ROUTE`, { user: data.info.user_id, customer: data.info.customer, }); const response = await this.inputService.testConnection(data); return response; } @Post('/test-connection/get-columns') @ApiOkResponse({ type: TestConnectionGetColumnsRes }) async getColumns(@Body() data: TestConnectionGetColumnsReq) { this.logger.info( `/test-connection/get-columns - ON TEST CONNECTION GET COLUMNS ROUTE`, { user: data.info.user_id, customer: data.info.customer, }, ); const response = await this.inputService.getColumns(data); return response; } @Post() @ApiOkResponse({ type: Input }) async create(@Body() createInputDto: CreateInputReq) { this.logger.info(`/input - ON CREATE ROUTE`, { user: createInputDto.info.user_id, customer: createInputDto.info.customer, }); const response = await this.inputService.create(createInputDto); return response; } @Post(':id') @ApiOkResponse({ type: Input }) async reCreate(@Param('id') id: string, @Body() input: CreateInputReq) { if (!input.id) input.id = id; this.logger.info(`POST /${id}`, 'ON RECREATE ROUTE'); const response = await this.inputService.reCreate(input); return response; } @Get() async findAll(@Body() body) { this.logger.info(`/input - ON FIND ALL ROUTE`, { user: body.info.user_id, customer: body.info.customer, }); const response = await this.inputService.findAll(body); return response; } @Get('/:id') async findOne(@Body() body, @Param() params) { const { id } = params; this.logger.info(`/input/${id} - ON FIND ONE ROUTE`, { user: body.info.user_id, customer: body.info.customer, }); const response = await this.inputService.findOne({ id, ...body }); return response; } @Patch(':id') async update(@Body() updateInputDto: UpdateInputRequest, @Param() params) { const { id } = params; const { info } = updateInputDto; this.logger.info(`/input/${id} - ON UPDATE ROUTE`, { user: info.user_id, customer: info.customer_id, }); delete updateInputDto.info; const response = await this.inputService.update(id, updateInputDto, info); return response; } @Delete(':id') async delete(@Body() data, @Param() params) { const { id } = params; this.logger.info(`/input/${id} - ON DELETE ROUTE`, { user: data.info.user_id, customer: data.info.customer, }); const response = await this.inputService.remove({ id, ...data }); return response; } }