diff --git a/package-lock.json b/package-lock.json index 7af3cdb..96b91b6 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.9.0-beta.8", + "protospack-v2": "^3.9.0-beta.11", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", @@ -9018,9 +9018,9 @@ } }, "node_modules/protospack-v2": { - "version": "3.9.0-beta.8", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.8.tgz", - "integrity": "sha512-C3h62JYRm8Xh5X6mPnKF7ZZA0Ds0qMdu/gFYBu+FEfME5segFgbuiivepkqoVQ3MUoN1gIyV0J5ezmV9kXPPTw==", + "version": "3.9.0-beta.11", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.11.tgz", + "integrity": "sha512-l2tU5vIlwnr7zgqsX8szMJoOzE9CdBYRJEj48yZc1AK9xVcXZa7+/Ik4n/N/JTAjKsCmQspbxuBezSbKcCpsvA==", "dependencies": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", @@ -17247,9 +17247,9 @@ } }, "protospack-v2": { - "version": "3.9.0-beta.8", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.8.tgz", - "integrity": "sha512-C3h62JYRm8Xh5X6mPnKF7ZZA0Ds0qMdu/gFYBu+FEfME5segFgbuiivepkqoVQ3MUoN1gIyV0J5ezmV9kXPPTw==", + "version": "3.9.0-beta.11", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/protospack-v2/-/protospack-v2-3.9.0-beta.11.tgz", + "integrity": "sha512-l2tU5vIlwnr7zgqsX8szMJoOzE9CdBYRJEj48yZc1AK9xVcXZa7+/Ik4n/N/JTAjKsCmQspbxuBezSbKcCpsvA==", "requires": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", diff --git a/package.json b/package.json index 7057652..20f092f 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.9.0-beta.8", + "protospack-v2": "^3.9.0-beta.11", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", diff --git a/src/modules/network-config/dto/network-config.ts b/src/modules/network-config/dto/network-config.ts index 0a779bc..80b4f68 100644 --- a/src/modules/network-config/dto/network-config.ts +++ b/src/modules/network-config/dto/network-config.ts @@ -13,7 +13,7 @@ export enum NetworkConfigPluginsEnum { } export class NetworkConfigDto { @ApiProperty() - config_id: string; + id: string; @ApiProperty() secret_id: string; @ApiProperty() @@ -23,9 +23,9 @@ export class NetworkConfigDto { @ApiProperty() description: string; @ApiProperty({ enum: NetworkConfigTypesEnum }) - type: NetworkConfigTypesEnum; + type: string; @ApiProperty({ enum: NetworkConfigPluginsEnum }) - plugin: NetworkConfigPluginsEnum; + plugin: string; @ApiProperty() credentials: any; @ApiProperty() @@ -68,11 +68,7 @@ export class CreateNetworkConfigReq extends PickType(NetworkConfigDto, [ } export class CreateNetworkConfigRes { @ApiProperty() - id: string; - @ApiProperty() - name: string; - @ApiProperty() - description: string; + network_config: NetworkConfigNoSecrets; } export class UpdateNetworkConfigReq { @@ -83,9 +79,10 @@ export class UpdateNetworkConfigReq { } export class UpdateNetworkConfigRes { @ApiProperty() - id: string; + network_config: NetworkConfigNoSecrets; +} + +export class DeleteNetworkConfigRes { @ApiProperty() - name: string; - @ApiProperty() - description: string; + message: string; } diff --git a/src/modules/network-config/network-config-client.config.ts b/src/modules/network-config/network-config-client.config.ts new file mode 100644 index 0000000..a0a6783 --- /dev/null +++ b/src/modules/network-config/network-config-client.config.ts @@ -0,0 +1,36 @@ +import { credentials } from '@grpc/grpc-js'; +import { + ClientProviderOptions, + GrpcOptions, + Transport, +} from '@nestjs/microservices'; +import { NetworkConfig } from 'protospack-v2'; + +export class NetworkConfigClientConfiguration { + private config: GrpcOptions = { + transport: Transport.GRPC, + options: { + url: process.env.INFACTORY_URL, + package: [ + NetworkConfig.ProtoPackages.ReadPackage, + NetworkConfig.ProtoPackages.WritePackage, + ], + credentials: + process.env.ENV === 'local' ? undefined : credentials.createSsl(), + protoPath: [ + NetworkConfig.ProtoPaths.ReadFilePath, + NetworkConfig.ProtoPaths.WriteFilePath, + ], + loader: { + keepCase: true, + enums: String, + objects: true, + arrays: true, + }, + }, + }; + providerOptions: ClientProviderOptions = { + name: 'NetworkConfigGrpcClient', + ...this.config, + }; +} diff --git a/src/modules/network-config/network-config.controller.ts b/src/modules/network-config/network-config.controller.ts index 63a4794..231eebf 100644 --- a/src/modules/network-config/network-config.controller.ts +++ b/src/modules/network-config/network-config.controller.ts @@ -1,6 +1,7 @@ import { Body, Controller, + Delete, Get, Inject, Param, @@ -14,6 +15,7 @@ import { RequestUser, User } from 'src/authentication/user.decorator'; import { CreateNetworkConfigReq, CreateNetworkConfigRes, + DeleteNetworkConfigRes, FindAllNetworkConfigsRes, UpdateNetworkConfigReq, UpdateNetworkConfigRes, @@ -39,6 +41,19 @@ export class NetworkConfigController { return this.networkConfigService.findAllNetworkConfigs(user.customer_name); } + @Get(':id') + @ApiOkResponse({ type: FindAllNetworkConfigsRes }) + async findNetworkConfigById( + @User() user: RequestUser, + @Param('id') id: string, + ) { + this.logger.info('GET /network-config/:id', { + user: user.user_id, + customer: user.customer_name, + }); + return this.networkConfigService.findNetworkConfigById(id); + } + @Post() @ApiOkResponse({ type: CreateNetworkConfigRes }) async createNetworkConfigs( @@ -62,7 +77,7 @@ export class NetworkConfigController { @Param('id') network_config_id: string, @Body() body: UpdateNetworkConfigReq, ) { - this.logger.info('POST /network-config', { + this.logger.info('PATCH /network-config/:id', { user: user.user_id, customer: user.customer_name, }); @@ -72,16 +87,16 @@ export class NetworkConfigController { ); } - @Get(':id') - @ApiOkResponse({ type: FindAllNetworkConfigsRes }) - async findNetworkConfigById( + @Delete(':id') + @ApiOkResponse({ type: DeleteNetworkConfigRes }) + async deleteNetworkConfig( @User() user: RequestUser, - @Param('id') id: string, + @Param('id') network_config_id: string, ) { - this.logger.info('GET /network-config', { + this.logger.info('DELETE /network-config', { user: user.user_id, customer: user.customer_name, }); - return this.networkConfigService.findNetworkConfigById(id); + return this.networkConfigService.deleteNetworkConfig(network_config_id); } } diff --git a/src/modules/network-config/network-config.module.ts b/src/modules/network-config/network-config.module.ts index e1dd524..acd8a89 100644 --- a/src/modules/network-config/network-config.module.ts +++ b/src/modules/network-config/network-config.module.ts @@ -2,9 +2,13 @@ import { Module } from '@nestjs/common'; import { NetworkConfigController } from './network-config.controller'; import { NetworkConfigService } from './network-config.service'; import { DadosferaLogger } from 'dadosfera-logs'; +import { ClientsModule } from '@nestjs/microservices'; +import { NetworkConfigClientConfiguration } from './network-config-client.config'; +const client = new NetworkConfigClientConfiguration(); @Module({ controllers: [NetworkConfigController], providers: [NetworkConfigService, DadosferaLogger], + imports: [ClientsModule.register([client.providerOptions])], }) export class NetworkConfigModule {} diff --git a/src/modules/network-config/network-config.service.ts b/src/modules/network-config/network-config.service.ts index ca9081a..296ecad 100644 --- a/src/modules/network-config/network-config.service.ts +++ b/src/modules/network-config/network-config.service.ts @@ -1,7 +1,11 @@ -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; +import { ClientGrpc } from '@nestjs/microservices'; +import { NetworkConfig } from 'protospack-v2'; +import { lastValueFrom } from 'rxjs'; import { CreateNetworkConfigReq, CreateNetworkConfigRes, + DeleteNetworkConfigRes, FindAllNetworkConfigsRes, FindNetworkConfigByidRes, UpdateNetworkConfigReq, @@ -10,22 +14,54 @@ import { @Injectable() export class NetworkConfigService { + private networkConfigReadClient: NetworkConfig.ReadService.NetworkConfigReadService; + private networkConfigWriteClient: NetworkConfig.WriteService.NetworkConfigWriteService; + constructor( + @Inject('NetworkConfigGrpcClient') private readonly grpcClient: ClientGrpc, + ) { + this.networkConfigReadClient = + grpcClient.getService( + NetworkConfig.ProtoServices.NetworkConfigReadService, + ); + this.networkConfigWriteClient = + grpcClient.getService( + NetworkConfig.ProtoServices.NetworkConfigWriteService, + ); + } async findAllNetworkConfigs( customer_name: string, ): Promise { - return { network_configs: [] }; + return lastValueFrom( + this.networkConfigReadClient.NetworkConfigFindByCustomer({ + customer_name, + }), + ); } async findNetworkConfigById(id: string): Promise { - return { network_config: undefined }; + const { network_config } = await lastValueFrom( + this.networkConfigReadClient.NetworkConfigFindOneById({ + id, + }), + ); + return { network_config }; } async createNetworkConfig( body: CreateNetworkConfigReq, customer_name: string, ): Promise { - const { description, name } = body; - return { description, id: '', name }; + const { description, name, properties, type } = body; + const { network_config } = await lastValueFrom( + this.networkConfigWriteClient.NetworkConfigCreate({ + customer_name, + description, + name, + properties: JSON.stringify(properties), + type, + }), + ); + return { network_config }; } async updateNetworkConfig( @@ -33,6 +69,23 @@ export class NetworkConfigService { network_config_id: string, ): Promise { const { description, name } = body; - return { description, id: network_config_id, name }; + const { network_config } = await lastValueFrom( + this.networkConfigWriteClient.NetworkConfigUpdate({ + id: network_config_id, + description, + name, + }), + ); + return { network_config }; + } + + async deleteNetworkConfig( + network_config_id: string, + ): Promise { + return lastValueFrom( + this.networkConfigWriteClient.NetworkConfigDelete({ + id: network_config_id, + }), + ); } }