mirror of
https://github.com/dadosfera/maestro.git
synced 2026-08-31 19:58:21 +00:00
211 lines
5.2 KiB
TypeScript
211 lines
5.2 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
HttpException,
|
|
Param,
|
|
Put,
|
|
Post,
|
|
Query,
|
|
UploadedFile,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { ApiBearerAuth, ApiConsumes, ApiTags } from '@nestjs/swagger';
|
|
import { ConnectorClientService } from 'src/clients/connector/client.service';
|
|
import { AddTagDto } from './dtos/add-tag';
|
|
import { CreateConnectorDto } from './dtos/create-connector';
|
|
import { DeleteConnectorDto } from './dtos/delete-connector';
|
|
import { GetAllDto } from './dtos/get-all';
|
|
import { RemoveTagDto } from './dtos/remove-tag';
|
|
import { UpdateDto } from './dtos/update';
|
|
import { UploadFileDto } from './dtos/upload-file';
|
|
|
|
@ApiTags('connectors')
|
|
@ApiBearerAuth()
|
|
@Controller('connectors')
|
|
export class ConnectorController {
|
|
constructor(private connectorClientService: ConnectorClientService) {}
|
|
|
|
@Post()
|
|
@ApiConsumes('multipart/form-data')
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
async uploadConnector(
|
|
@UploadedFile() file,
|
|
@Body() body: CreateConnectorDto,
|
|
) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const response = await this.connectorClientService.uploadConnector({
|
|
file,
|
|
connector: JSON.parse(body.connector),
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Post('/upload')
|
|
@ApiConsumes('multipart/form-data')
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
async uploadFile(@UploadedFile() file, @Body() { name }: UploadFileDto) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const response = await this.connectorClientService.uploadFile({
|
|
file,
|
|
name,
|
|
});
|
|
|
|
return response;
|
|
}
|
|
|
|
@Get()
|
|
async getAllConnectors(@Query() queries: GetAllDto) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const { filters, search } = queries;
|
|
|
|
const response: any = await this.connectorClientService.getAllConnectors({
|
|
filters: filters || {},
|
|
search: search || '',
|
|
});
|
|
|
|
const connectors = JSON.parse(response.connectors).connectors;
|
|
|
|
return {
|
|
message: response.message,
|
|
connectors,
|
|
};
|
|
}
|
|
|
|
@Get('/:plugin')
|
|
async getConnector(
|
|
@Param('plugin') plugin: string,
|
|
@Query('version') version: string,
|
|
) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const pluginId = `${plugin}-${version}`;
|
|
|
|
const response: any = await this.connectorClientService.getConnector(
|
|
pluginId,
|
|
);
|
|
|
|
return {
|
|
message: response.message || 'ok',
|
|
connector: { ...JSON.parse(response.connector) },
|
|
};
|
|
}
|
|
|
|
@Get('/:plugin/details')
|
|
async getConnectorDetails(
|
|
@Param('plugin') plugin: string,
|
|
@Query('version') version: string,
|
|
) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const pluginId = `${plugin}-${version}`;
|
|
|
|
const response: any = await this.connectorClientService.getConnectorDetails(
|
|
pluginId,
|
|
);
|
|
|
|
return {
|
|
message: response.message || 'ok',
|
|
connector: { ...JSON.parse(response.connector) },
|
|
};
|
|
}
|
|
|
|
@Put('/:plugin')
|
|
@ApiConsumes('multipart/form-data')
|
|
async updateConnector(
|
|
@Param('plugin') plugin: string,
|
|
@Body() body: UpdateDto,
|
|
) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const changes = body;
|
|
|
|
const response: any = await this.connectorClientService.updateConnector({
|
|
plugin,
|
|
changes: JSON.stringify(changes),
|
|
});
|
|
|
|
return {
|
|
message: response.message || 'ok',
|
|
connector: { ...JSON.parse(response.connector) },
|
|
};
|
|
}
|
|
|
|
@Put('/:plugin/add-tag')
|
|
async addTagOnConnector(
|
|
@Param('plugin') plugin: string,
|
|
@Body() body: AddTagDto,
|
|
) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const { tags } = body;
|
|
|
|
if (!tags || !(typeof tags === 'object') || !tags.length) {
|
|
throw new HttpException('Not found tags attributes', 400);
|
|
}
|
|
|
|
const changes = { plugin, tags };
|
|
|
|
const response: any = await this.connectorClientService.updateConnector({
|
|
plugin,
|
|
changes: JSON.stringify(changes),
|
|
});
|
|
|
|
return {
|
|
message: response.message || 'ok',
|
|
connector: { ...JSON.parse(response.connector) },
|
|
};
|
|
}
|
|
|
|
@Put('/:plugin/remove-tag')
|
|
async removeTagOnConnector(
|
|
@Param('plugin') plugin: string,
|
|
@Body() body: RemoveTagDto,
|
|
) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const { tags } = body;
|
|
const remove = tags;
|
|
|
|
if (!remove || !(typeof remove === 'object') || !remove.length) {
|
|
throw new HttpException('Not found remove attribute', 400);
|
|
}
|
|
|
|
const changes = { plugin, remove };
|
|
|
|
const response: any = await this.connectorClientService.updateConnector({
|
|
plugin,
|
|
changes: JSON.stringify(changes),
|
|
});
|
|
|
|
return {
|
|
message: response.message || 'ok',
|
|
connector: { ...JSON.parse(response.connector) },
|
|
};
|
|
}
|
|
|
|
@Delete('/:plugin')
|
|
async deleteConnector(
|
|
@Param('plugin') plugin: string,
|
|
@Body() { version }: DeleteConnectorDto,
|
|
) {
|
|
console.log(`/upload`, 'Upload Connector Route');
|
|
|
|
const response: any = await this.connectorClientService.deleteConnector(
|
|
plugin,
|
|
version,
|
|
);
|
|
|
|
return {
|
|
message: response.message || 'ok',
|
|
connector: { ...JSON.parse(response.connector) },
|
|
};
|
|
}
|
|
}
|