diff --git a/src/modules/connection/client.service.ts b/src/modules/connection/client.service.ts index 0f8a752..32f5303 100644 --- a/src/modules/connection/client.service.ts +++ b/src/modules/connection/client.service.ts @@ -64,4 +64,118 @@ export class ConnectionClientService implements OnModuleInit { ); }); } + + async updateConnection({ body, info }) { + const meta = new Metadata(); + meta.add('customer_id', info.customer_id); + meta.add('customer_name', info.customer_name); + meta.add('user_id', info.user_id); + + const { + id, + description, + image_url, + name, + network_config_id, + properties, + type, + } = body; + + return lastValueFrom( + this.connectionServiceWrite.UpdateConnection( + { + id, + description, + image_url, + name, + network_config_id, + properties: JSON.stringify(properties), + type, + }, + meta, + ), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async deleteConnection({ body, info }) { + const meta = new Metadata(); + meta.add('customer_id', info.customer_id); + meta.add('customer_name', info.customer_name); + meta.add('user_id', info.user_id); + + const { id } = body; + + return lastValueFrom( + this.connectionServiceWrite.DeleteConnection( + { + id, + }, + meta, + ), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async getAllConnection({ body, info }) { + const meta = new Metadata(); + meta.add('customer_id', info.customer_id); + meta.add('customer_name', info.customer_name); + meta.add('user_id', info.user_id); + + const { id } = body; + + return lastValueFrom( + this.connectionServiceRead.GetAllConnection({}, meta), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async getConnection({ body, info }) { + const meta = new Metadata(); + meta.add('customer_id', info.customer_id); + meta.add('customer_name', info.customer_name); + meta.add('user_id', info.user_id); + + const { id } = body; + + return lastValueFrom( + this.connectionServiceRead.GetConnection({ id }, meta), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } + + async getConnectionDetails({ body, info }) { + const meta = new Metadata(); + meta.add('customer_id', info.customer_id); + meta.add('customer_name', info.customer_name); + meta.add('user_id', info.user_id); + + const { id } = body; + + return lastValueFrom( + this.connectionServiceRead.GetConnectionDetails({ id }, meta), + ).catch((err) => { + throw new HttpException( + err.details, + err.code === 6 ? HttpStatus.CONFLICT : 400, + ); + }); + } } diff --git a/src/modules/connection/connection.controller.ts b/src/modules/connection/connection.controller.ts index f8b93d3..20b2705 100644 --- a/src/modules/connection/connection.controller.ts +++ b/src/modules/connection/connection.controller.ts @@ -1,4 +1,13 @@ -import { Body, Controller, Post, Inject } from '@nestjs/common'; +import { + Body, + Controller, + Post, + Inject, + Put, + Param, + Delete, + Get, +} from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { ConnectionClientService } from './client.service'; import { CreateConnectionDto } from './dtos/create-connection'; @@ -10,6 +19,8 @@ import { import { Permissions } from 'src/authentication/permissions.enum'; import { User } from 'src/authentication/user.decorator'; import { ValidationPipe } from '../../pipes/object-validation.pipe'; +import { UpdateConnectionDto } from './dtos/update-connection'; +import { identity } from 'rxjs'; @ApiTags('connections') @ApiBearerAuth() @@ -48,4 +59,100 @@ export class ConnectionController { return response; } + + @Put('/:id') + // @RequireAllPermissions(Permissions.CONNECTIONS.CREATE) + async updateConnection( + @User() user, + @Param('id') id, + @Body(new ValidationPipe()) body: UpdateConnectionDto, + ) { + this.logger.info('/connections - Update Connection'); + + const { user_id, customer_id, customer_name } = user; + + const response = await this.clientService.updateConnection({ + body: { ...body, id }, + info: { user_id, customer_id, customer_name }, + }); + + delete response.connection.properties; + delete response.connection.user_id; + delete response.connection.customer_id; + delete response.connection.customer_name; + delete response.connection.updated_at; + + return response; + } + + @Delete('/:id') + // @RequireAllPermissions(Permissions.CONNECTIONS.CREATE) + async deleteConnection(@User() user, @Param('id') id) { + this.logger.info('/connections - Delete Connection'); + + const { user_id, customer_id, customer_name } = user; + + const response = await this.clientService.deleteConnection({ + body: { id }, + info: { user_id, customer_id, customer_name }, + }); + + delete response.connection.properties; + delete response.connection.user_id; + delete response.connection.customer_id; + delete response.connection.customer_name; + delete response.connection.updated_at; + + return response; + } + + @Get() + // @RequireAllPermissions(Permissions.CONNECTIONS.CREATE) + async getAllConnection(@User() user) { + this.logger.info('/connections - Get All Connection'); + + const { user_id, customer_id, customer_name } = user; + + const response = await this.clientService.getAllConnection({ + body: {}, + info: { user_id, customer_id, customer_name }, + }); + + return response; + } + + @Get('/:id') + // @RequireAllPermissions(Permissions.CONNECTIONS.CREATE) + async getConnection(@User() user, @Param('id') id) { + this.logger.info('/connections - Get Connection'); + + const { user_id, customer_id, customer_name } = user; + + const response = await this.clientService.getConnection({ + body: { id }, + info: { user_id, customer_id, customer_name }, + }); + + delete response.connection.user_id; + delete response.connection.customer_id; + delete response.connection.customer_name; + delete response.connection.updated_at; + + return response; + } + + @Get('/:id/details') + // @RequireAllPermissions(Permissions.CONNECTIONS.CREATE) + async getConnectionDetails(@User() user, @Param('id') id) { + this.logger.info('/connections - Get Connection Details'); + + const { user_id, customer_id, customer_name } = user; + + const response = await this.clientService.getConnectionDetails({ + body: { id }, + info: { user_id, customer_id, customer_name }, + }); + + return response; + } } diff --git a/src/modules/connection/dtos/update-connection.ts b/src/modules/connection/dtos/update-connection.ts new file mode 100644 index 0000000..e4e84ca --- /dev/null +++ b/src/modules/connection/dtos/update-connection.ts @@ -0,0 +1,24 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString, IsObject, IsOptional } from 'class-validator'; + +export class UpdateConnectionDto { + @ApiProperty() + @IsString() + @IsOptional() + name: string; + + @ApiProperty() + @IsString() + @IsOptional() + description: string; + + @ApiProperty() + @IsString() + @IsOptional() + network_config_id: string; + + @ApiProperty() + @IsObject() + @IsOptional() + properties: any; +}