import { Metadata } from '@grpc/grpc-js'; import { Inject, Injectable } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; import { NetworkConfig } from '@dadosfera/protospack-v2'; import { lastValueFrom } from 'rxjs'; import { CreateNetworkConfigReq, CreateNetworkConfigRes, DeleteNetworkConfigRes, FindAllNetworkConfigsRes, FindNetworkConfigByidDetailsRes, FindNetworkConfigByidRes, UpdateNetworkConfigReq, UpdateNetworkConfigRes, } from './dto/network-config'; import { NetworkConfigGrpcClient } from './network-config-client.config'; @Injectable() export class NetworkConfigService { private networkConfigReadClient: NetworkConfig.ReadService.NetworkConfigReadService; private networkConfigWriteClient: NetworkConfig.WriteService.NetworkConfigWriteService; constructor( @Inject(NetworkConfigGrpcClient.name) private readonly grpcClient: ClientGrpc, ) { this.networkConfigReadClient = grpcClient.getService( NetworkConfig.ProtoServices.NetworkConfigReadService, ); this.networkConfigWriteClient = grpcClient.getService( NetworkConfig.ProtoServices.NetworkConfigWriteService, ); } async findAllNetworkConfigs({ body, info, }): Promise { const { search, page, size, filters } = body; const { language, customer_id, customer_name, user_id } = info; const meta = new Metadata(); meta.add('language', language); meta.add('customer_id', customer_id); meta.add('customer_name', customer_name); meta.add('user_id', user_id); return lastValueFrom( this.networkConfigReadClient.NetworkConfigFindAll( { search, filters: JSON.stringify(filters), page, size }, meta, ), ); } async findNetworkConfigById(id: string): Promise { const { network_config } = await lastValueFrom( this.networkConfigReadClient.NetworkConfigFindOneById({ id, }), ); return { network_config }; } async findNetworkConfigByIdDetails( id: string, info?, ): Promise { const { customer_name, customer_id, language } = info; const meta = new Metadata(); meta.add('customer_name', customer_name); meta.add('customer_id', customer_id); meta.add('language', language); const { network_config } = await lastValueFrom( this.networkConfigReadClient.NetworkConfigFindOneDetailsById( { id }, meta, ), ); Object.assign(network_config, { properties: JSON.parse(network_config.properties), }); return { network_config }; } async createNetworkConfig( body: CreateNetworkConfigReq, file: Express.Multer.File, { customer_name, customer_id, user_id }, ): Promise { const { description, name, network_type_id, network_type_name } = body; const type = 'vpn'; const plugin = 'openvpn'; const OVPN_FILE_STRING = Buffer.from(file.buffer).toString(); const meta = new Metadata(); meta.add('customer_id', customer_id); meta.add('customer_name', customer_name); meta.add('user_id', user_id); const { network_config } = await lastValueFrom( this.networkConfigWriteClient.NetworkConfigCreate( { description, name, properties: JSON.stringify({ OVPN_FILE: OVPN_FILE_STRING, plugin, }), type, network_type_id, network_type_name, plugin, }, meta, ), ); return { network_config }; } async updateNetworkConfig( body: UpdateNetworkConfigReq, network_config_id: string, ): Promise { const { description, name } = body; const { network_config } = await lastValueFrom( this.networkConfigWriteClient.NetworkConfigUpdate({ id: network_config_id, description, name, }), ); return { network_config }; } async deleteNetworkConfig( network_config_id: string, { customer_name, customer_id }, ): Promise { const meta = new Metadata(); meta.add('customer_id', customer_id); meta.add('customer_name', customer_name); return lastValueFrom( this.networkConfigWriteClient.NetworkConfigDelete( { id: network_config_id }, meta, ), ); } }