From 22631478b932dbe9490acb816423175a8dce2264 Mon Sep 17 00:00:00 2001 From: Victor Radael Date: Mon, 25 Jul 2022 17:08:01 -0300 Subject: [PATCH] CRUD Connection --- package-lock.json | 14 +-- package.json | 2 +- src/modules/connection/client.service.ts | 114 ++++++++++++++++++ .../connection/connection.controller.ts | 109 ++++++++++++++++- .../connection/dtos/update-connection.ts | 24 ++++ 5 files changed, 254 insertions(+), 9 deletions(-) create mode 100644 src/modules/connection/dtos/update-connection.ts diff --git a/package-lock.json b/package-lock.json index 3b533d9..4f59365 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,7 +38,7 @@ "passport-hubspot-oauth2": "^1.0.3", "passport-mailchimp": "^1.1.0", "protospack": "2.5.2", - "protospack-v2": "3.8.11", + "protospack-v2": "3.8.13", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", @@ -9018,9 +9018,9 @@ } }, "node_modules/protospack-v2": { - "version": "3.8.11", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.8.11.tgz", - "integrity": "sha512-xInbwBwbRrir7Pmgdt4Qv7cA5gjfzHRLl/LOR24vfeXiGO8kome+iAUvk68pwsg529XL1fIl1rOMTsSakCMY9A==", + "version": "3.8.13", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.8.13.tgz", + "integrity": "sha512-arUr08PAMB5Qkz/5W1VR/czKGOYgHZA3nJmoWzhJTjuCn1kSMVdIHMPLfpUC49IJPkgYXCjUy0ehEwWUj4BAZA==", "dependencies": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", @@ -17247,9 +17247,9 @@ } }, "protospack-v2": { - "version": "3.8.11", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.8.11.tgz", - "integrity": "sha512-xInbwBwbRrir7Pmgdt4Qv7cA5gjfzHRLl/LOR24vfeXiGO8kome+iAUvk68pwsg529XL1fIl1rOMTsSakCMY9A==", + "version": "3.8.13", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.8.13.tgz", + "integrity": "sha512-arUr08PAMB5Qkz/5W1VR/czKGOYgHZA3nJmoWzhJTjuCn1kSMVdIHMPLfpUC49IJPkgYXCjUy0ehEwWUj4BAZA==", "requires": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", diff --git a/package.json b/package.json index 2e69580..0c63618 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "passport-hubspot-oauth2": "^1.0.3", "passport-mailchimp": "^1.1.0", "protospack": "2.5.2", - "protospack-v2": "3.8.11", + "protospack-v2": "3.8.13", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", 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; +}