Files
maestro/src/modules/network-config/network-config.service.ts
T

143 lines
4.1 KiB
TypeScript

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.ReadService.NetworkConfigReadService>(
NetworkConfig.ProtoServices.NetworkConfigReadService,
);
this.networkConfigWriteClient =
grpcClient.getService<NetworkConfig.WriteService.NetworkConfigWriteService>(
NetworkConfig.ProtoServices.NetworkConfigWriteService,
);
}
async findAllNetworkConfigs({
body,
info,
}): Promise<FindAllNetworkConfigsRes> {
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<FindNetworkConfigByidRes> {
const { network_config } = await lastValueFrom(
this.networkConfigReadClient.NetworkConfigFindOneById({
id,
}),
);
return { network_config };
}
async findNetworkConfigByIdDetails(
id: string,
info?,
): Promise<FindNetworkConfigByidDetailsRes> {
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<CreateNetworkConfigRes> {
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<UpdateNetworkConfigRes> {
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<DeleteNetworkConfigRes> {
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,
),
);
}
}