mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-15 20:54:48 +00:00
219 lines
6.3 KiB
TypeScript
219 lines
6.3 KiB
TypeScript
import { Controller, Inject, OnModuleInit, Post } from '@nestjs/common';
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
import { DadosferaLogger } from '@dadosfera/dadosfera-logs';
|
|
import {
|
|
objectCamelToSnake,
|
|
objectSnakeToCamel,
|
|
} 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: Transformation.WriteService.TransformationService;
|
|
logger: DadosferaLogger;
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
@Inject(TransformationsClientConfiguration.name)
|
|
private readonly grpcClient: ClientGrpc,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
onModuleInit() {
|
|
this.transformationService =
|
|
this.grpcClient.getService<Transformation.WriteService.TransformationService>(
|
|
Transformation.ProtoServices.TransformationService,
|
|
);
|
|
}
|
|
|
|
@Post()
|
|
async create(createTransformationsDto: ICreateTransformationsRequest) {
|
|
this.logger.info('TransformationClientService - Create');
|
|
|
|
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'
|
|
].map((tr) => {
|
|
return objectCamelToSnake(tr);
|
|
});
|
|
|
|
return { transformations: convertedTransforms };
|
|
}
|
|
|
|
async findAll(data) {
|
|
this.logger.info('TransformationClientService - FindAll');
|
|
|
|
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) => {
|
|
return objectCamelToSnake(tr);
|
|
});
|
|
return { transformations: convertedTransforms };
|
|
}
|
|
|
|
async findOne(data: IIdRequest) {
|
|
this.logger.info('TransformationClientService - FindOne');
|
|
|
|
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);
|
|
}
|
|
|
|
async update(updateTransformationDTO) {
|
|
this.logger.info('TransformationClientService - Update');
|
|
|
|
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);
|
|
}
|
|
|
|
async remove(data: IIdRequest) {
|
|
this.logger.info('TransformationClientService - Remove');
|
|
|
|
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);
|
|
}
|
|
}
|