import { Body, Controller, Post, Inject, Put, Param, Delete, Get, Query, UseFilters, } from '@nestjs/common'; import { ApiExcludeEndpoint, ApiTags } from '@nestjs/swagger'; import { ConnectionClientService } from './client.service'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { Authenticated, RequireAllPermissions, } from 'src/decorators/authentication.decorator'; import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; import { RequestUser, User } from 'src/decorators/user.decorator'; import { ValidationPipe } from '../../pipes/object-validation.pipe'; import { ConnectionDetailsRes, ConnectionRes, ConnectionsRes, GetAllConnectionsReq, UpdateConnectionDto, } from './dtos/connection'; import { CreateConnectionDto } from './dtos/connection'; import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { Language } from 'src/decorators/language.decorator'; import { LanguageEnum } from 'src/utils/languages.enum'; import { ApiInternalOnlyEndpoint } from 'src/decorators/swagger.decorator'; const connectionPermissions = PERMISSIONS_GROUPS.CONNECTION.permissions; @UseFilters(new GrpcToHttpExceptionFilter()) @ApiTags('connections') @Authenticated() @Controller('connections') export class ConnectionController { logger: any; constructor( @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, private clientService: ConnectionClientService, ) { this.logger = dadosferaLogger.logger; } @ApiInternalOnlyEndpoint() @Get('connections-migration') @ApiExcludeEndpoint() @RequireAllPermissions( PERMISSIONS_GROUPS.DADOSFERA.permissions.CONNECTIONS_MIGRATION, ) async synchronizeConnections(@Query('apply') apply: string) { return this.clientService.synchronizeConnections(apply === 'true'); } @Post() @RequireAllPermissions(connectionPermissions.CREATE) async createConnection( @User() user: RequestUser, @Body(new ValidationPipe()) body: CreateConnectionDto, ): Promise { this.logger.info('/connections - Create Connection'); const { user_id, customer_id, customer_name, username } = user; const metadata = PackTheMetadata({ user_id, customer_id, customer_name, username, }); const response: any = await this.clientService.createConnection( body, metadata, ); return response; } @Put('/:id') @RequireAllPermissions(connectionPermissions.CREATE) async updateConnection( @User() user: RequestUser, @Language() language: LanguageEnum, @Param('id') id: string, @Body(new ValidationPipe()) body: UpdateConnectionDto, ): Promise { this.logger.info('/connections - Update Connection'); const { user_id, customer_id, customer_name, username } = user; const metadata = PackTheMetadata({ user_id, customer_id, customer_name, language, username, }); const response: any = await this.clientService.updateConnection( id, body, metadata, ); return response; } @Delete('/:id') @RequireAllPermissions(connectionPermissions.DELETE) async deleteConnection( @User() user: RequestUser, @Param('id') id: string, ): Promise { this.logger.info('/connections - Delete Connection'); const { user_id, customer_id, customer_name, username } = user; const metadata = PackTheMetadata({ user_id, customer_id, customer_name, username, }); const response: any = await this.clientService.deleteConnection({ body: { id }, metadata, }); return response; } @Get() async getAllConnections( @User() user: RequestUser, @Language() language: LanguageEnum, @Query() queries: GetAllConnectionsReq, ): Promise { this.logger.info('/connections - Get All Connection'); const { user_id, customer_id, customer_name, username } = user; const { search, size, page, ...filters } = queries; const metadata = PackTheMetadata({ user_id, customer_id, customer_name, language, username, }); const response = await this.clientService.getAllConnections({ body: { search, size, page, filters }, metadata, }); return response; } @Get('/:id') async getConnectionDetails( @Language() language: LanguageEnum, @User() user: RequestUser, @Param('id') id: string, @Query() queries, ): Promise { this.logger.info('/connections - Get Connection Details'); console.log(queries); const { details, sensitive } = queries; const { user_id, customer_id, customer_name, username } = user; const metadata = PackTheMetadata({ user_id, customer_id, customer_name, username, details, language, sensitive, }); const response: any = await this.clientService.getConnectionDetails({ body: { id }, metadata, }); if (details === 'true') { const properties = JSON.parse(response.connection.properties); Object.assign(response.connection, { pipelines: JSON.parse(response.connection.pipelines), connection_controls: properties.connection_controls, config_controls: properties.config_controls, connection_steps: properties.connection_steps, }); delete response.connection.properties; } else { Object.assign(response.connection, { properties: JSON.parse(response.connection.properties), pipelines: JSON.parse(response.connection.pipelines), }); } return response; } }