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'; @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({ 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, { customer_name, customer_id }, ): Promise { const { description, name, properties, type, network_type_id } = body; const meta = new Metadata(); meta.add('customer_id', customer_id); meta.add('customer_name', customer_name); const { network_config } = await lastValueFrom( this.networkConfigWriteClient.NetworkConfigCreate( { customer_name, description, name, properties: JSON.stringify(properties), type, network_type_id, }, 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, ), ); } }