From 6807b3e1a9502432b4c1c3e576e2a854111d241d Mon Sep 17 00:00:00 2001 From: Gabriel Rosa Date: Tue, 16 Aug 2022 10:18:54 -0300 Subject: [PATCH] FEAT: inputs and transformation using protospack-v2 --- package-lock.json | 14 +- package.json | 2 +- src/modules/inputs/dtos/old_interfaces.ts | 20 +- src/modules/inputs/inputs-client.config.ts | 18 +- src/modules/inputs/inputs.controller.ts | 18 +- src/modules/inputs/inputs.service.ts | 255 +++++++++-------- src/modules/transformations/client.service.ts | 268 ++++++++++-------- .../transformations/transformations-client.ts | 15 +- .../transformations.controller.ts | 64 ++--- 9 files changed, 343 insertions(+), 331 deletions(-) diff --git a/package-lock.json b/package-lock.json index e9836ce..a3657d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@aws-sdk/client-secrets-manager": "^3.112.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.3", - "@dadosfera/protospack-v2": "3.11.4", + "@dadosfera/protospack-v2": "3.12.0", "@grpc/grpc-js": "^1.6.7", "@grpc/proto-loader": "^0.6.13", "@nestjs/common": "^8.4.7", @@ -1725,9 +1725,9 @@ } }, "node_modules/@dadosfera/protospack-v2": { - "version": "3.11.4", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.11.4.tgz", - "integrity": "sha512-Rp+v/plT4m/xDHGa8lTkDANY/vlz21ditCH5mrzNY1yONOpoVSQtcMRlnaOrfjZMLw844+Lv+Br2VHc4p+lPMw==", + "version": "3.12.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.12.0.tgz", + "integrity": "sha512-BJNmVC+bjO+ICc13VWllmzAMqqI4/1x6H2LxXaVyi65rmn6FNHMNj6aC/WMlko17II/X2Ih40QdrrQ5io019mg==", "dependencies": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", @@ -12283,9 +12283,9 @@ } }, "@dadosfera/protospack-v2": { - "version": "3.11.4", - "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.11.4.tgz", - "integrity": "sha512-Rp+v/plT4m/xDHGa8lTkDANY/vlz21ditCH5mrzNY1yONOpoVSQtcMRlnaOrfjZMLw844+Lv+Br2VHc4p+lPMw==", + "version": "3.12.0", + "resolved": "https://dadosfera-611330257153.d.codeartifact.us-east-1.amazonaws.com:443/npm/dadosfera-npm/@dadosfera/protospack-v2/-/protospack-v2-3.12.0.tgz", + "integrity": "sha512-BJNmVC+bjO+ICc13VWllmzAMqqI4/1x6H2LxXaVyi65rmn6FNHMNj6aC/WMlko17II/X2Ih40QdrrQ5io019mg==", "requires": { "@grpc/grpc-js": "^1.6.7", "rxjs": "^7.5.5", diff --git a/package.json b/package.json index adc6277..7421ef6 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "dependencies": { "@aws-sdk/client-secrets-manager": "^3.112.0", "@dadosfera/dadosfera-logs": "^1.0.0-beta.3", + "@dadosfera/protospack-v2": "3.12.0", "@grpc/grpc-js": "^1.6.7", "@grpc/proto-loader": "^0.6.13", "@nestjs/common": "^8.4.7", @@ -53,7 +54,6 @@ "passport-hubspot-oauth2": "^1.0.3", "passport-mailchimp": "^1.1.0", "protospack": "2.5.2", - "@dadosfera/protospack-v2": "3.11.4", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.5.5", diff --git a/src/modules/inputs/dtos/old_interfaces.ts b/src/modules/inputs/dtos/old_interfaces.ts index aaa516f..9fe22ad 100644 --- a/src/modules/inputs/dtos/old_interfaces.ts +++ b/src/modules/inputs/dtos/old_interfaces.ts @@ -11,25 +11,6 @@ interface Values { schema: string; } -interface Cron { - hours: string[]; - hour_interval: boolean; - hour_resourse: boolean; - hour_resourse_value: number; - month_day: string[]; - month_day_interval: boolean; - month_day_resourse: boolean; - month_day_resourse_value: number; - month: string[]; - month_interval: boolean; - month_resourse: boolean; - month_resourse_value: number; - week_days: string[]; - week_days_interval: boolean; - week_days_resourse: boolean; - week_days_resourse_value: number; -} - export interface ICreateInputRequest { cron: string; name: string; @@ -51,6 +32,7 @@ export interface UpdateInputRequest { values: Values; operation: string; info: Info; + tables: any[]; } export interface ITestConnectionRequest { diff --git a/src/modules/inputs/inputs-client.config.ts b/src/modules/inputs/inputs-client.config.ts index fc4f4b0..d5efede 100644 --- a/src/modules/inputs/inputs-client.config.ts +++ b/src/modules/inputs/inputs-client.config.ts @@ -1,24 +1,30 @@ +import { Input } from '@dadosfera/protospack-v2'; import { credentials } from '@grpc/grpc-js'; import { ClientProviderOptions, GrpcOptions, Transport, } from '@nestjs/microservices'; -import { InputPackages, InputProtoFilePath } from 'protospack'; export class InputsGrpcClient { - public name = 'InputsGrpcClient'; + public readonly name = 'InputsGrpcClient'; private config: GrpcOptions = { transport: Transport.GRPC, options: { url: process.env.INFACTORY_URL, - package: InputPackages, + package: [ + Input.ProtoPackages.WritePackage, + Input.ProtoPackages.ReadPackage, + ], credentials: process.env.LOCAL_ENV ? undefined : credentials.createSsl(), - protoPath: InputProtoFilePath, + protoPath: [ + Input.ProtoPaths.WriteFilePath, + Input.ProtoPaths.ReadFilePath, + ], loader: { + keepCase: true, enums: String, - objects: true, - arrays: true, + defaults: false, }, }, }; diff --git a/src/modules/inputs/inputs.controller.ts b/src/modules/inputs/inputs.controller.ts index 138e15b..2d64e47 100644 --- a/src/modules/inputs/inputs.controller.ts +++ b/src/modules/inputs/inputs.controller.ts @@ -24,7 +24,8 @@ import { TestConnectionRes, } from './dtos/input.model'; import { UpdateInputRequest } from './dtos/old_interfaces'; -import { User } from 'src/authentication/user.decorator'; +import { RequestUser, User } from 'src/authentication/user.decorator'; +import { Info } from '@dadosfera/protospack-v2/dist/lib/Input/interfaces/entities'; @ApiTags('inputs') @Controller('inputs') @@ -132,15 +133,18 @@ export class InputsController { } @Get() - async findAll(@User() user, @Body() body) { + async findAll(@User() user: RequestUser) { + const { user_id, customer_id, customer_name: customer } = user; this.logger.info(`/input - ON FIND ALL ROUTE`, { user: user.user_id, - customer: user.customer_id, - }); - - const response = await this.inputService.findAll({ - info: { user: user.user_id, customer: user.customer_id }, + customer, }); + const info: Info = { + customer, + customer_id, + user_id, + }; + const response = await this.inputService.findAll(info); return response; } diff --git a/src/modules/inputs/inputs.service.ts b/src/modules/inputs/inputs.service.ts index f611808..78127e3 100644 --- a/src/modules/inputs/inputs.service.ts +++ b/src/modules/inputs/inputs.service.ts @@ -5,44 +5,56 @@ import { HttpStatus, Inject, Injectable, - InternalServerErrorException, - NotFoundException, } from '@nestjs/common'; import { Timeout } from '@nestjs/schedule'; -import { - DecodeGrpcStruct, - EncodeJsonToGrpcStruct, - InputCreateS3Request, - InputNewCreateRequest, - InputService, - InputServicesNames, - TestConnectionGetColumnsRequest, - TestConnectionRequest, -} from 'protospack'; import CronParser, { CronExpression } from 'cron-parser'; -import { Info } from 'protospack/dist/lib/interfaces'; import { ClientGrpc } from '@nestjs/microservices'; import DadosferaLogger from '@dadosfera/dadosfera-logs/dist'; import { lastValueFrom } from 'rxjs'; -import { - objectSnakeToCamel, - objectCamelToSnake, -} from 'src/utils/CaseConverter'; +import { objectCamelToSnake } from 'src/utils/CaseConverter'; import { IIdRequest, UpdateInputRequest } from './dtos/old_interfaces'; -import { InputsGrpcClient } from './inputs-client.config'; +import { Input } from '@dadosfera/protospack-v2'; +import { + InputCreateS3Request, + InputNewCreateRequest, + TestConnectionGetColumnsRequest, + TestConnectionRequest, +} from '@dadosfera/protospack-v2/dist/lib/Input/interfaces/messages'; +import { Info } from '@dadosfera/protospack-v2/dist/lib/Input/interfaces/entities'; @Injectable() //TODO refactor this class to remove OLD_inputClient export class InputsService { logger: DadosferaLogger; - inputService: InputService; + inputWriteService: Input.WriteService.InputWriteService; + inputReadService: Input.ReadService.InputReadService; + + constructor( + @Inject(DadosferaLogger) + dadosferaLogger: DadosferaLogger, + @Inject('InputsGrpcClient') private readonly grpcClient: ClientGrpc, + ) { + this.logger = dadosferaLogger.logger; + } + + async onModuleInit() { + this.inputWriteService = + this.grpcClient.getService( + Input.ProtoServices.InputWriteService, + ); + this.inputReadService = + this.grpcClient.getService( + Input.ProtoServices.InputReadService, + ); + } + OLD_inputClient = { newCreate: async (createInputDto: InputNewCreateRequest) => { this.logger.info('InputClientService - Create'); const input = await lastValueFrom( - this.inputService.NewCreate(objectSnakeToCamel(createInputDto)), + this.inputWriteService.InputNewCreate(createInputDto), ); objectCamelToSnake(input); return input; @@ -50,31 +62,30 @@ export class InputsService { createGeneric: async (createInputGeneric) => { this.logger.info('InputClientService - Create'); const { info, ...data } = createInputGeneric; + if (data.options) delete data.options; if (data.credentials.oauth_code) delete data.credentials.oauth_code; - const grpcPayload = { - input: EncodeJsonToGrpcStruct(data), - info, - }; - objectSnakeToCamel(grpcPayload); - const structReturn = await lastValueFrom( - this.inputService.Create(grpcPayload), - ).catch((err: { details: string }) => { - if (err.details === 'Item Not found!') { - throw new NotFoundException('Input not found'); - } - throw new InternalServerErrorException(err.details); - }); - objectCamelToSnake(structReturn); - const inputCreated = DecodeGrpcStruct(structReturn.input); - return { input: inputCreated }; + if (data.credentials) data.credentials = JSON.stringify(data.credentials); + + const input = await lastValueFrom( + this.inputWriteService.InputCreate(data), + ); + // .catch((err: { details: string }) => { + // if (err.details === 'Item Not found!') { + // throw new NotFoundException('Input not found'); + // } + // throw new InternalServerErrorException(err.details); + // }); + // objectCamelToSnake(structReturn); + // const inputCreated = DecodeGrpcStruct(structReturn.input); + return { input }; }, createS3Inputs: async (createInputDto: InputCreateS3Request) => { this.logger.info('InputClientService - Create'); - objectSnakeToCamel(createInputDto); + createInputDto; const createInputResponse = await lastValueFrom( - this.inputService.CreateS3(createInputDto), + this.inputWriteService.InputCreateS3(createInputDto), ).catch((e) => { throw new HttpException(e.details, 500); }); @@ -85,7 +96,7 @@ export class InputsService { findOne: async (data: IIdRequest) => { this.logger.info('InputClientService - FindOne'); const findOneInputResponse = await lastValueFrom( - this.inputService.FindOne(objectSnakeToCamel(data)), + this.inputReadService.InputFindOne(data), ).catch((e) => { this.logger.error(e.details); throw new HttpException(e.details, HttpStatus.INTERNAL_SERVER_ERROR); @@ -93,41 +104,32 @@ export class InputsService { objectCamelToSnake(findOneInputResponse); return findOneInputResponse; }, - findAll: async (data) => { - this.logger.info('InputClientService - FindAll'); - const findAllInputResponse = await lastValueFrom( - this.inputService.FindAll(objectSnakeToCamel(data)), - ).catch((e) => { - this.logger.error(e.details); - throw new HttpException(e.details, HttpStatus.INTERNAL_SERVER_ERROR); - }); - findAllInputResponse.inputs.forEach((input) => objectCamelToSnake(input)); - - return findAllInputResponse; - }, update: async (updateInputDTO: UpdateInputRequest) => { this.logger.info('InputClientService - Update'); - const updateInputResponse = await new Promise((resolve, reject) => { - this.inputService.Update(objectSnakeToCamel(updateInputDTO)).subscribe({ - next(x) { - resolve(objectCamelToSnake(x)); - }, - error(err) { - reject(err); - }, - complete() { - // console.log('done'); - }, - }); - }) - .then((res) => { - this.logger.info('Done'); - return res; - }) - .catch((err) => { - this.logger.error(err.message); - throw new Error(err); - }); + const updateInputResponse = await lastValueFrom( + this.inputWriteService.InputUpdate(updateInputDTO), + ); + // const updateInputResponse = await new Promise((resolve, reject) => { + // this.inputWriteService.InputUpdate(updateInputDTO).subscribe({ + // next(x) { + // resolve(objectCamelToSnake(x)); + // }, + // error(err) { + // reject(err); + // }, + // complete() { + // // console.log('done'); + // }, + // }); + // }) + // .then((res) => { + // this.logger.info('Done'); + // return res; + // }) + // .catch((err) => { + // this.logger.error(err.message); + // throw new Error(err); + // }); return updateInputResponse; }, @@ -135,7 +137,7 @@ export class InputsService { this.logger.info('InputClientService - Remove'); const removeInputResponse = await new Promise((resolve, reject) => { - this.inputService.Remove(objectSnakeToCamel(idRequest)).subscribe({ + this.inputWriteService.InputRemove(idRequest).subscribe({ next(x) { resolve(objectCamelToSnake(x)); }, @@ -161,19 +163,17 @@ export class InputsService { testConnection: async (data: TestConnectionRequest) => { this.logger.info('InputClientService - TestConnection'); const testConnectionResponse = await new Promise((resolve, reject) => { - this.inputService - .NewTestConnection(objectSnakeToCamel(data)) - .subscribe({ - next(x) { - resolve(objectCamelToSnake(x)); - }, - error(err) { - reject(err); - }, - complete() { - // console.log('done'); - }, - }); + this.inputReadService.NewTestConnection(data).subscribe({ + next(x) { + resolve(objectCamelToSnake(x)); + }, + error(err) { + reject(err); + }, + complete() { + // console.log('done'); + }, + }); }) .then((res) => { this.logger.info('Done'); @@ -189,7 +189,7 @@ export class InputsService { getColumns: async (data: TestConnectionGetColumnsRequest) => { this.logger.info('InputClientService - TestConnection/Get-Columns'); const getColumnsResponse = await new Promise((resolve, reject) => { - this.inputService.GetColumns(objectSnakeToCamel(data)).subscribe({ + this.inputReadService.GetColumns(data).subscribe({ next(x) { resolve(objectCamelToSnake(x)); }, @@ -214,9 +214,9 @@ export class InputsService { }, getAvailableEntities: async (data) => { this.logger.info('InputClientService - GetAvailableEntities'); - objectSnakeToCamel(data); + data; const response = await lastValueFrom( - this.inputService.GetAvailableEntities(data), + this.inputReadService.GetAvailableEntities(data), ).catch((err) => { this.logger.info(err.details); if (err.details && err.details.includes('400')) @@ -228,28 +228,22 @@ export class InputsService { return response; }, }; - - constructor( - @Inject(DadosferaLogger) - dadosferaLogger: DadosferaLogger, - @Inject(InputsGrpcClient.name) private readonly grpcClient: ClientGrpc, - ) { - this.logger = dadosferaLogger.logger; - } - - async onModuleInit() { - this.inputService = this.grpcClient.getService( - InputServicesNames.InputService, - ); - } - secondsInADay = 60 * 60 * 24; secondsInAnHour = 60 * 60; adjustInputPayload(payload) { - if (payload?.input_generic) return DecodeGrpcStruct(payload.input_generic); - if (!payload.input_s3 && !payload.input_jdbc) return payload; - return payload?.input_s3 || payload?.input_jdbc; + if (payload) { + if (payload.input_generic) { + const { credentials } = payload.input_generic; + return { + ...payload.input_generic, + credentials: credentials && JSON.parse(credentials), + }; + } + if (payload.input_s3) return payload.input_s3; + if (payload.input_jdbc) return payload.input_jdbc; + } + return payload; } getDifferenceInSeconds(date1: Date, date2: Date) { const diffInMs = Math.abs(date2.getTime() - date1.getTime()); @@ -319,20 +313,39 @@ export class InputsService { async getAvailableEntities(data): Promise<{ entities: string[] }> { return await this.OLD_inputClient.getAvailableEntities(data); } - async findAll(body) { - try { - const findAllInputResponse: any = await this.OLD_inputClient.findAll( - body, - ); - if (findAllInputResponse?.inputs?.length) { - findAllInputResponse.inputs = findAllInputResponse.inputs.map((input) => - this.adjustInputPayload(input), - ); - } - return findAllInputResponse; - } catch (err) { - throw new HttpException(err.message, HttpStatus.NOT_FOUND); - } + async findAll(info: Info) { + const { inputs } = await lastValueFrom( + this.inputReadService.InputFindAll({ info }), + ); + if (!inputs) return { inputs: [] }; + + const inputsReturn = inputs.map((i) => this.adjustInputPayload(i)); + return { inputs: inputsReturn }; + // findAll: async (data) => { + // this.logger.info('InputClientService - FindAll'); + // const findAllInputResponse = await lastValueFrom( + // this.inputReadService.InputFindAll(data), + // ).catch((e) => { + // this.logger.error(e.details); + // throw new HttpException(e.details, HttpStatus.INTERNAL_SERVER_ERROR); + // }); + // findAllInputResponse.inputs.forEach((input) => objectCamelToSnake(input)); + + // return findAllInputResponse; + // }, + // try { + // const findAllInputResponse: any = await this.OLD_inputClient.findAll( + // body, + // ); + // if (findAllInputResponse?.inputs?.length) { + // findAllInputResponse.inputs = findAllInputResponse.inputs.map((input) => + // this.adjustInputPayload(input), + // ); + // } + // return findAllInputResponse; + // } catch (err) { + // throw new HttpException(err.message, HttpStatus.NOT_FOUND); + // } } async findOne(idRequest: IIdRequest) { diff --git a/src/modules/transformations/client.service.ts b/src/modules/transformations/client.service.ts index 5cc86f9..c5de27d 100644 --- a/src/modules/transformations/client.service.ts +++ b/src/modules/transformations/client.service.ts @@ -1,6 +1,5 @@ import { Controller, Inject, OnModuleInit, Post } from '@nestjs/common'; import { ClientGrpc } from '@nestjs/microservices'; -import { TransformationsServiceInterface } from 'protospack'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { objectCamelToSnake, @@ -8,10 +7,12 @@ import { } from 'src/utils/CaseConverter'; import { ICreateTransformationsRequest, IIdRequest } from './interfaces'; import { TransformationsClientConfiguration } from './transformations-client'; +import { Transformation } from '@dadosfera/protospack-v2'; +import { lastValueFrom } from 'rxjs'; @Controller('transformation') export class TransformationsClientService implements OnModuleInit { - private transformationService: TransformationsServiceInterface; + private transformationService: Transformation.WriteService.TransformationService; logger: DadosferaLogger; constructor( @Inject(DadosferaLogger) @@ -24,8 +25,8 @@ export class TransformationsClientService implements OnModuleInit { onModuleInit() { this.transformationService = - this.grpcClient.getService( - 'TransformationService', + this.grpcClient.getService( + Transformation.ProtoServices.TransformationService, ); } @@ -33,31 +34,37 @@ export class TransformationsClientService implements OnModuleInit { async create(createTransformationsDto: ICreateTransformationsRequest) { this.logger.info('TransformationClientService - Create'); - const createTransformationResponse = await new Promise( - (resolve, reject) => { - this.transformationService - .Create(objectSnakeToCamel(createTransformationsDto)) - .subscribe({ - next(x) { - resolve(x); - }, - error(err) { - reject(err); - }, - complete() { - // console.log('done'); - }, - }); - }, - ) - .then((res) => { - this.logger.info('Done'); - return res; - }) - .catch((err) => { - this.logger.error(err.message); - throw new Error(err); - }); + const createTransformationResponse = await lastValueFrom( + this.transformationService.WriteCreate( + objectSnakeToCamel(createTransformationsDto), + ), + ); + + // const createTransformationResponse = await new Promise( + // (resolve, reject) => { + // this.transformationService + // .WriteCreate(objectSnakeToCamel(createTransformationsDto)) + // .subscribe({ + // next(x) { + // resolve(x); + // }, + // error(err) { + // reject(err); + // }, + // complete() { + // // console.log('done'); + // }, + // }); + // }, + // ) + // .then((res) => { + // this.logger.info('Done'); + // return res; + // }) + // .catch((err) => { + // this.logger.error(err.message); + // throw new Error(err); + // }); const convertedTransforms = createTransformationResponse[ 'transformations' @@ -71,29 +78,33 @@ export class TransformationsClientService implements OnModuleInit { async findAll(data) { this.logger.info('TransformationClientService - FindAll'); - const findAllTransformationResponse = await new Promise( - (resolve, reject) => { - this.transformationService.FindAll(objectSnakeToCamel(data)).subscribe({ - next(x) { - resolve(x); - }, - error(err) { - reject(err); - }, - complete() { - // console.log('done'); - }, - }); - }, - ) - .then((res) => { - this.logger.info('Done'); - return res; - }) - .catch((err) => { - this.logger.error(err.message); - throw new Error(err); - }); + const findAllTransformationResponse = await lastValueFrom( + this.transformationService.WriteFindAll(objectSnakeToCamel(data)), + ); + + // const findAllTransformationResponse = await new Promise( + // (resolve, reject) => { + // this.transformationService.FindAll(objectSnakeToCamel(data)).subscribe({ + // next(x) { + // resolve(x); + // }, + // error(err) { + // reject(err); + // }, + // complete() { + // // console.log('done'); + // }, + // }); + // }, + // ) + // .then((res) => { + // this.logger.info('Done'); + // return res; + // }) + // .catch((err) => { + // this.logger.error(err.message); + // throw new Error(err); + // }); const convertedTransforms = findAllTransformationResponse[ 'transformations' ].map((tr) => { @@ -105,29 +116,32 @@ export class TransformationsClientService implements OnModuleInit { async findOne(data: IIdRequest) { this.logger.info('TransformationClientService - FindOne'); - const findOneTransformationResponse = await new Promise( - (resolve, reject) => { - this.transformationService.FindOne(objectSnakeToCamel(data)).subscribe({ - next(x) { - resolve(x); - }, - error(err) { - reject(err); - }, - complete() { - // console.log('done'); - }, - }); - }, - ) - .then((res) => { - this.logger.info('Done'); - return res; - }) - .catch((err) => { - this.logger.error(err.message); - throw new Error(err); - }); + const findOneTransformationResponse = await lastValueFrom( + this.transformationService.WriteFindOne(objectSnakeToCamel(data)), + ); + // const findOneTransformationResponse = await new Promise( + // (resolve, reject) => { + // this.transformationService.FindOne(objectSnakeToCamel(data)).subscribe({ + // next(x) { + // resolve(x); + // }, + // error(err) { + // reject(err); + // }, + // complete() { + // // console.log('done'); + // }, + // }); + // }, + // ) + // .then((res) => { + // this.logger.info('Done'); + // return res; + // }) + // .catch((err) => { + // this.logger.error(err.message); + // throw new Error(err); + // }); return objectCamelToSnake(findOneTransformationResponse); } @@ -135,31 +149,36 @@ export class TransformationsClientService implements OnModuleInit { async update(updateTransformationDTO) { this.logger.info('TransformationClientService - Update'); - const updateTransformationResponse = await new Promise( - (resolve, reject) => { - this.transformationService - .Update(objectSnakeToCamel(updateTransformationDTO)) - .subscribe({ - next(x) { - resolve(x); - }, - error(err) { - reject(err); - }, - complete() { - // console.log('done'); - }, - }); - }, - ) - .then((res) => { - this.logger.info('Done'); - return res; - }) - .catch((err) => { - this.logger.error(err.message); - throw new Error(err); - }); + const updateTransformationResponse = await lastValueFrom( + this.transformationService.WriteUpdate( + objectSnakeToCamel(updateTransformationDTO), + ), + ); + // const updateTransformationResponse = await new Promise( + // (resolve, reject) => { + // this.transformationService + // .Update(objectSnakeToCamel(updateTransformationDTO)) + // .subscribe({ + // next(x) { + // resolve(x); + // }, + // error(err) { + // reject(err); + // }, + // complete() { + // // console.log('done'); + // }, + // }); + // }, + // ) + // .then((res) => { + // this.logger.info('Done'); + // return res; + // }) + // .catch((err) => { + // this.logger.error(err.message); + // throw new Error(err); + // }); return objectCamelToSnake(updateTransformationResponse); } @@ -167,29 +186,32 @@ export class TransformationsClientService implements OnModuleInit { async remove(data: IIdRequest) { this.logger.info('TransformationClientService - Remove'); - const removeTransformationResponse = await new Promise( - (resolve, reject) => { - this.transformationService.Remove(objectSnakeToCamel(data)).subscribe({ - next(x) { - resolve(x); - }, - error(err) { - reject(err); - }, - complete() { - // console.log('done'); - }, - }); - }, - ) - .then((res) => { - this.logger.info('Done'); - return res; - }) - .catch((err) => { - this.logger.error(err.message); - throw new Error(err); - }); + const removeTransformationResponse = await lastValueFrom( + this.transformationService.WriteRemove(objectSnakeToCamel(data)), + ); + // const removeTransformationResponse = await new Promise( + // (resolve, reject) => { + // this.transformationService.Remove(objectSnakeToCamel(data)).subscribe({ + // next(x) { + // resolve(x); + // }, + // error(err) { + // reject(err); + // }, + // complete() { + // // console.log('done'); + // }, + // }); + // }, + // ) + // .then((res) => { + // this.logger.info('Done'); + // return res; + // }) + // .catch((err) => { + // this.logger.error(err.message); + // throw new Error(err); + // }); return objectCamelToSnake(removeTransformationResponse); } diff --git a/src/modules/transformations/transformations-client.ts b/src/modules/transformations/transformations-client.ts index bb15b3e..fe030b7 100644 --- a/src/modules/transformations/transformations-client.ts +++ b/src/modules/transformations/transformations-client.ts @@ -1,13 +1,10 @@ import { - ClientsProviderAsyncOptions, + ClientProviderOptions, GrpcOptions, Transport, } from '@nestjs/microservices'; -import { - TransformationPackages, - TransformationProtoFilePath, -} from 'protospack'; import { credentials } from '@grpc/grpc-js'; +import { Transformation } from '@dadosfera/protospack-v2'; export class TransformationsClientConfiguration { public name = 'TransformationsClientConfiguration'; @@ -15,9 +12,9 @@ export class TransformationsClientConfiguration { transport: Transport.GRPC, options: { url: process.env.INFACTORY_URL, - package: TransformationPackages, - credentials: credentials.createSsl(), - protoPath: TransformationProtoFilePath, + package: [Transformation.ProtoPackages.WritePackage], + credentials: process.env.LOCAL_ENV ? undefined : credentials.createSsl(), + protoPath: [Transformation.ProtoPaths.WriteFilePath], loader: { enums: String, objects: true, @@ -26,7 +23,7 @@ export class TransformationsClientConfiguration { }, }; - providerOptions: ClientsProviderAsyncOptions = { + providerOptions: ClientProviderOptions = { name: this.name, ...this.config, }; diff --git a/src/modules/transformations/transformations.controller.ts b/src/modules/transformations/transformations.controller.ts index fb84079..d6ebef9 100644 --- a/src/modules/transformations/transformations.controller.ts +++ b/src/modules/transformations/transformations.controller.ts @@ -8,7 +8,6 @@ import { Post, Put, } from '@nestjs/common'; -import { Payload } from '@nestjs/microservices'; import { ApiTags } from '@nestjs/swagger'; import { TransformationsClientService } from './client.service'; import { IIdRequest } from './interfaces'; @@ -16,6 +15,7 @@ import { AuthenticateCondition } from 'src/authentication/authentication.decorat import { PERMISSIONS } from '../../authentication/permissions.enum'; import { TransformationsService } from './transformations.service'; import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; +import { RequestUser, User } from 'src/authentication/user.decorator'; @ApiTags('Transformations') @Controller('transformations') @@ -51,13 +51,10 @@ export class TransformationsController { @Post() async create(@Body() createTransformationDto) { - this.logger.info( - process.env.DEV_URL + `/transformation - ON CREATE ROUTE`, - { - user: createTransformationDto.info.user_id, - customer: createTransformationDto.info.customer, - }, - ); + this.logger.info(`/transformation - ON CREATE ROUTE`, { + user: createTransformationDto.info.user_id, + customer: createTransformationDto.info.customer, + }); const transformationService = new TransformationsService( this.transformationsClientService, @@ -71,13 +68,10 @@ export class TransformationsController { @Get() async findAll(@Body() data: IIdRequest) { - this.logger.info( - process.env.DEV_URL + `/transformation - ON Find All ROUTE`, - { - user: data.info.user_id, - customer: data.info.customer_id, - }, - ); + this.logger.info(`/transformation - ON Find All ROUTE`, { + user: data.info.user_id, + customer: data.info.customer_id, + }); const transformationService = new TransformationsService( this.transformationsClientService, @@ -90,13 +84,10 @@ export class TransformationsController { @Get('/:id') async findOne(@Body() data, @Param() params) { const { id } = params; - this.logger.info( - process.env.DEV_URL + `/transformation/${id} - ON Find One ROUTE`, - { - user: data.info.user_id, - customer: data.info.customer, - }, - ); + this.logger.info(`/transformation/${id} - ON Find One ROUTE`, { + user: data.info.user_id, + customer: data.info.customer, + }); const transformationService = new TransformationsService( this.transformationsClientService, @@ -107,15 +98,15 @@ export class TransformationsController { } @Put(':id') - async update(@Payload() updateTransformationDto, @Param() params) { - const { id } = params; - this.logger.info( - process.env.DEV_URL + `/transformation/${id} - ON UPDATE ROUTE`, - { - user: updateTransformationDto.info.user_id, - customer: updateTransformationDto.info.customer, - }, - ); + async update( + @User() user: RequestUser, + @Body() updateTransformationDto, + @Param('id') id: string, + ) { + this.logger.info(`/transformation/${id} - ON UPDATE ROUTE`, { + user: user.user_id, + customer: user.customer_name, + }); const transformationService = new TransformationsService( this.transformationsClientService, @@ -131,13 +122,10 @@ export class TransformationsController { @Delete(':id') async delete(@Body() data, @Param() params) { const { id } = params; - this.logger.info( - process.env.DEV_URL + `/transformation/${id} - ON DELETE ROUTE`, - { - user: data.info.user_id, - customer: data.info.customer, - }, - ); + this.logger.info(`/transformation/${id} - ON DELETE ROUTE`, { + user: data.info.user_id, + customer: data.info.customer, + }); const transformationService = new TransformationsService( this.transformationsClientService,