import { Body, Controller, Post, Inject, Put, Param, Delete, Get, Headers, Query, } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { ConnectionClientService } from './client.service'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { Authenticated, RequireAllPermissions, } from 'src/authentication/authentication.decorator'; import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; import { RequestUser, User } from 'src/authentication/user.decorator'; import { ValidationPipe } from '../../pipes/object-validation.pipe'; import { ConnectionDetailsRes, ConnectionRes, ConnectionsRes, UpdateConnectionDto, } from './dtos/connection'; import { CreateConnectionDto } from './dtos/connection'; import { PackTheMetadata } from 'src/utils/ PackTheMetadata'; const connectionPermissions = PERMISSIONS_GROUPS.CONNECTION.permissions; @ApiTags('connections') @ApiBearerAuth() @Authenticated() @Controller('connections') export class ConnectionController { logger: any; constructor( @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, private clientService: ConnectionClientService, ) { this.logger = dadosferaLogger.logger; } @Get('connections-migration') @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, @Headers('Dadosfera-Lang') language: string, @Param('id') id: string, @Body(new ValidationPipe()) body: UpdateConnectionDto, ): Promise { this.logger.info('/connections - Update Connection'); if (!language) language = 'en-us'; 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, @Headers('Dadosfera-Lang') language, @Query() queries, ): Promise { this.logger.info('/connections - Get All Connection'); if (!language) language = 'en-us'; 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( @Headers('Dadosfera-Lang') language, @User() user: RequestUser, @Param('id') id, @Query('details') details, ): Promise { this.logger.info('/connections - Get Connection Details'); if (!language) language = 'en-us'; const { user_id, customer_id, customer_name, username } = user; const metadata = PackTheMetadata({ user_id, customer_id, customer_name, username, details, language, }); 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; } }