mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-01 20:28:17 +00:00
168 lines
4.4 KiB
TypeScript
168 lines
4.4 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Inject,
|
|
Param,
|
|
Patch,
|
|
Post,
|
|
UseFilters,
|
|
} from '@nestjs/common';
|
|
import { InputsService } from './inputs.service';
|
|
import { DadosferaLogger } from '@dadosfera/dadosfera-logs';
|
|
import { PERMISSIONS_GROUPS } from '../../authentication/permissions.enum';
|
|
import { AuthenticateCondition } from 'src/decorators/authentication.decorator';
|
|
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
|
|
import {
|
|
CreateInputReq,
|
|
GetAvailableEntitiesReq,
|
|
GetAvailableEntitiesRes,
|
|
Input,
|
|
} from './dtos/input.model';
|
|
import { UpdateInputRequest } from './dtos/old_interfaces';
|
|
import { RequestUser, User } from 'src/decorators/user.decorator';
|
|
import { Info } from '@dadosfera/protospack-v2/dist/lib/Input/interfaces/entities';
|
|
import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter';
|
|
import { ApiInternalOnlyEndpoint } from 'src/decorators/swagger.decorator';
|
|
|
|
@ApiTags('Inputs')
|
|
@Controller('inputs')
|
|
@AuthenticateCondition((req, user) => {
|
|
switch (req.method) {
|
|
case 'POST':
|
|
case 'PUT':
|
|
case 'PATCH':
|
|
return (
|
|
user.permissions.includes(
|
|
PERMISSIONS_GROUPS.PIPELINE.permissions.CREATE.seqid,
|
|
) ||
|
|
user.permissions.includes(
|
|
PERMISSIONS_GROUPS.PIPELINE.permissions.UPDATE.seqid,
|
|
)
|
|
);
|
|
|
|
default:
|
|
return (
|
|
user.permissions.includes(
|
|
PERMISSIONS_GROUPS.PIPELINE.permissions.CREATE.seqid,
|
|
) ||
|
|
user.permissions.includes(
|
|
PERMISSIONS_GROUPS.PIPELINE.permissions.UPDATE.seqid,
|
|
) ||
|
|
user.permissions.includes(
|
|
PERMISSIONS_GROUPS.PIPELINE.permissions.GET.seqid,
|
|
)
|
|
);
|
|
}
|
|
})
|
|
@UseFilters(new GrpcToHttpExceptionFilter())
|
|
export class InputsController {
|
|
logger: DadosferaLogger;
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
private inputService: InputsService,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
@ApiInternalOnlyEndpoint()
|
|
@Get('available-entities/:plugin')
|
|
@ApiOkResponse({ type: GetAvailableEntitiesRes })
|
|
async getAvailableEntities(
|
|
@Param() params: GetAvailableEntitiesReq,
|
|
@User() user: RequestUser,
|
|
) {
|
|
const { plugin } = params;
|
|
const availableEntities = await this.inputService.getAvailableEntities({
|
|
info: {
|
|
customer: user.customer_name,
|
|
customer_id: user.customer_id,
|
|
user_id: user.user_id,
|
|
},
|
|
plugin,
|
|
});
|
|
return availableEntities;
|
|
}
|
|
|
|
@Post()
|
|
@ApiOkResponse({ type: Input })
|
|
async create(@Body() body: CreateInputReq, @User() user: RequestUser) {
|
|
const info: Info = {
|
|
user_id: user.user_id,
|
|
customer: user.customer_name,
|
|
customer_id: user.customer_id,
|
|
};
|
|
this.logger.info(`/input - ON CREATE ROUTE`, {
|
|
user: info.user_id,
|
|
customer: info.customer,
|
|
});
|
|
|
|
const response = await this.inputService.create({ body, info });
|
|
|
|
return response;
|
|
}
|
|
|
|
@ApiInternalOnlyEndpoint()
|
|
@Get()
|
|
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,
|
|
});
|
|
const info: Info = {
|
|
customer,
|
|
customer_id,
|
|
user_id,
|
|
};
|
|
const response = await this.inputService.findAll(info);
|
|
|
|
return response;
|
|
}
|
|
|
|
@Get('/:id')
|
|
async findOne(@Body() body, @Param('id') id: string) {
|
|
this.logger.info(`/input/${id} - ON FIND ONE ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const response = await this.inputService.findOne({ id, ...body });
|
|
|
|
return response;
|
|
}
|
|
|
|
@ApiInternalOnlyEndpoint()
|
|
@Patch(':id')
|
|
async update(@Body() updateInputDto: UpdateInputRequest, @Param() params) {
|
|
const { id } = params;
|
|
const { info } = updateInputDto;
|
|
this.logger.info(`/input/${id} - ON UPDATE ROUTE`, {
|
|
user: info.user_id,
|
|
customer: info.customer_id,
|
|
});
|
|
delete updateInputDto.info;
|
|
|
|
const response = await this.inputService.update(id, updateInputDto, info);
|
|
|
|
return response;
|
|
}
|
|
|
|
@ApiInternalOnlyEndpoint()
|
|
@Delete(':id')
|
|
async delete(@Body() data, @Param() params) {
|
|
const { id } = params;
|
|
|
|
this.logger.info(`/input/${id} - ON DELETE ROUTE`, {
|
|
user: data.info.user_id,
|
|
customer: data.info.customer,
|
|
});
|
|
|
|
const response = await this.inputService.remove({ id, ...data });
|
|
|
|
return response;
|
|
}
|
|
}
|