Transformations Refactor

This commit is contained in:
Victor Radael
2022-08-12 12:49:40 -03:00
parent 61752a547f
commit 3981751ac0
9 changed files with 67 additions and 75 deletions
@@ -0,0 +1,196 @@
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,
objectSnakeToCamel,
} from 'src/utils/CaseConverter';
import { ICreateTransformationsRequest, IIdRequest } from './interfaces';
import { TransformationsClientConfiguration } from './transformations-client';
@Controller('transformation')
export class TransformationsClientService implements OnModuleInit {
private transformationService: TransformationsServiceInterface;
logger: DadosferaLogger;
constructor(
@Inject(DadosferaLogger)
dadosferaLogger: DadosferaLogger,
@Inject(TransformationsClientConfiguration.name)
private readonly grpcClient: ClientGrpc,
) {
this.logger = dadosferaLogger.logger;
}
onModuleInit() {
this.transformationService =
this.grpcClient.getService<TransformationsServiceInterface>(
'TransformationService',
);
}
@Post()
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 convertedTransforms = createTransformationResponse[
'transformations'
].map((tr) => {
return objectCamelToSnake(tr);
});
return { transformations: convertedTransforms };
}
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 convertedTransforms = findAllTransformationResponse[
'transformations'
].map((tr) => {
return objectCamelToSnake(tr);
});
return { transformations: convertedTransforms };
}
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);
});
return objectCamelToSnake(findOneTransformationResponse);
}
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);
});
return objectCamelToSnake(updateTransformationResponse);
}
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);
});
return objectCamelToSnake(removeTransformationResponse);
}
}
+34
View File
@@ -0,0 +1,34 @@
import { Info } from 'protospack/dist/lib/interfaces';
export interface ICreateTransformationsRequest {
transformations: Transformation[];
info: Info;
}
interface Param {
base_column: string;
column_name: string;
n_digits: number;
start_index: number;
randbelow: number;
const: string;
}
interface Transformation {
created_at: string;
id: string;
client_id: string;
type: string;
params: Param[];
input_source: string;
table: string;
}
export interface IIdRequest {
id: string;
info: Info;
}
export interface IUpdateTransformationRequest {
transformation: Transformation;
info: Info;
}
@@ -0,0 +1,33 @@
import {
ClientsProviderAsyncOptions,
GrpcOptions,
Transport,
} from '@nestjs/microservices';
import {
TransformationPackages,
TransformationProtoFilePath,
} from 'protospack';
import { credentials } from '@grpc/grpc-js';
export class TransformationsClientConfiguration {
public name = 'TransformationsClientConfiguration';
private config: GrpcOptions = {
transport: Transport.GRPC,
options: {
url: process.env.INFACTORY_URL,
package: TransformationPackages,
credentials: credentials.createSsl(),
protoPath: TransformationProtoFilePath,
loader: {
enums: String,
objects: true,
arrays: true,
},
},
};
providerOptions: ClientsProviderAsyncOptions = {
name: this.name,
...this.config,
};
}
@@ -10,8 +10,8 @@ import {
} from '@nestjs/common';
import { Payload } from '@nestjs/microservices';
import { ApiTags } from '@nestjs/swagger';
import { TransformationsClientService } from 'src/clients/transformations/client.service';
import { IIdRequest } from 'src/clients/transformations/interfaces';
import { TransformationsClientService } from './client.service';
import { IIdRequest } from './interfaces';
import { AuthenticateCondition } from 'src/authentication/authentication.decorator';
import { PERMISSIONS } from '../../authentication/permissions.enum';
import { TransformationsService } from './transformations.service';
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { ClientsModule } from '@nestjs/microservices';
import { DadosferaLogger } from '@dadosfera/dadosfera-logs';
import { TransformationsController } from './transformations.controller';
import { TransformationsService } from './transformations.service';
import { TransformationsClientConfiguration } from './transformations-client';
import { TransformationsClientService } from './client.service';
const client = new TransformationsClientConfiguration();
@Module({
imports: [ClientsModule.register([client.providerOptions])],
controllers: [TransformationsController],
providers: [
TransformationsService,
TransformationsClientService,
DadosferaLogger,
],
exports: [TransformationsService],
})
export class TransformationsModule {}
@@ -1,9 +1,6 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { TransformationsClientService } from 'src/clients/transformations/client.service';
import {
ICreateTransformationsRequest,
IIdRequest,
} from 'src/clients/transformations/interfaces';
import { TransformationsClientService } from './client.service';
import { ICreateTransformationsRequest, IIdRequest } from './interfaces';
import { trSnakeToCamel } from 'src/utils/CaseConverter';
@Injectable()