mirror of
https://github.com/dadosfera/maestro.git
synced 2026-10-03 14:29:08 +00:00
603 lines
15 KiB
TypeScript
603 lines
15 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
ForbiddenException,
|
|
Get,
|
|
HttpException,
|
|
Inject,
|
|
Param,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
} from '@nestjs/common';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
import {
|
|
AuthenticateCondition,
|
|
Authenticated,
|
|
} from '../../authentication/authentication.decorator';
|
|
import { PERMISSIONS_GROUPS } from '../../authentication/permissions.enum';
|
|
import { CatalogService } from './catalog.service';
|
|
import { DadosferaLogger } from '@dadosfera/dadosfera-logs';
|
|
import { PackTheMetadata } from 'src/utils/ PackTheMetadata';
|
|
import { RequestUser, User } from 'src/authentication/user.decorator';
|
|
|
|
@ApiTags('Catalog')
|
|
@Controller('catalog')
|
|
@Authenticated()
|
|
@AuthenticateCondition((req, user) => {
|
|
if (req.path.includes('/manage_permissions/')) return true;
|
|
let action;
|
|
|
|
switch (req.method) {
|
|
case 'POST':
|
|
action = 'CREATE';
|
|
break;
|
|
|
|
case 'PUT':
|
|
action = 'UPDATE';
|
|
break;
|
|
|
|
default:
|
|
action = req.method;
|
|
}
|
|
|
|
return user.permissions.includes(
|
|
PERMISSIONS_GROUPS.CATALOG.permissions[action].seqid,
|
|
);
|
|
})
|
|
export class CatalogController {
|
|
logger: DadosferaLogger;
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
private catalogService: CatalogService,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
@Put('manage_permissions/:id')
|
|
async manageDataAssetPermissions(
|
|
@Param('id') id: string,
|
|
@User() user: RequestUser,
|
|
@Body() body,
|
|
) {
|
|
const { customer_id, customer_name, user_id, username } = user;
|
|
const metadata = PackTheMetadata({
|
|
customer_id,
|
|
customer_name,
|
|
user_id,
|
|
username,
|
|
});
|
|
const response = await this.catalogService.managePermissions(
|
|
{ ...body, id },
|
|
metadata,
|
|
);
|
|
|
|
return { data_asset: JSON.parse(response.data_asset) };
|
|
}
|
|
|
|
@Put('revoke_permissions/:id')
|
|
async revokeDataAssetPermissions(
|
|
@Param('id') id: string,
|
|
@User() user: RequestUser,
|
|
@Body() body,
|
|
) {
|
|
const { customer_id, customer_name, user_id, username } = user;
|
|
const metadata = PackTheMetadata({
|
|
customer_id,
|
|
customer_name,
|
|
user_id,
|
|
username,
|
|
});
|
|
const response = await this.catalogService.revokePermissions(
|
|
{ ...body, id },
|
|
metadata,
|
|
);
|
|
|
|
return { message: response.message };
|
|
}
|
|
|
|
@Get('all')
|
|
async catalogAll(@User() user: RequestUser, @Body() body, @Query() params) {
|
|
const { user_id, customer_name, customer_id, username, permissions } = user;
|
|
this.logger.info(`/catalog - ON CATALOG ALL ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const is_data_manager = permissions.includes(
|
|
PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid,
|
|
);
|
|
|
|
const roles = await this.catalogService.getUserRolesIds(user_id);
|
|
|
|
const metadata = PackTheMetadata({
|
|
user_id,
|
|
customer_id,
|
|
customer_name,
|
|
username,
|
|
roles,
|
|
is_data_manager,
|
|
});
|
|
|
|
const res = await this.catalogService.getAllOpenSearchDataAssets(
|
|
params,
|
|
metadata,
|
|
customer_id,
|
|
);
|
|
|
|
return res.data_assets;
|
|
}
|
|
|
|
@Get('data_apps')
|
|
async dataAppsAll(@Body() body) {
|
|
this.logger.info(`/catalog - ON FIND ALL DATA APPS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.dataAppsAll(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Delete('data_apps/:id')
|
|
async dataAppsOne(@Param() params, @Body() body) {
|
|
this.logger.info(`/catalog - ON FIND ONE DATA APP ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
const { id } = params;
|
|
|
|
const res = await this.catalogService.dataAppsOne(id, body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('dashboard-metabase')
|
|
async getAllDashboardMetabase(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET ALL DASHBOARDS METABASE ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getAllDashboardMetabase(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('dashboard-metabase/:id')
|
|
async getOneDashboardMetabase(
|
|
@Body() body,
|
|
@Param() params,
|
|
@User() user: RequestUser,
|
|
) {
|
|
const { id } = params;
|
|
this.logger.info(`/catalog - ON GET ONE DASHBOARD METABASE ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const { username, user_id, customer_id, customer_name, permissions } = user;
|
|
const is_data_manager = permissions.includes(
|
|
PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid,
|
|
);
|
|
let has_permission = false;
|
|
const metadata = PackTheMetadata({
|
|
username,
|
|
user_id,
|
|
customer_id,
|
|
customer_name,
|
|
});
|
|
|
|
const user_roles = await this.catalogService.getUserRolesIds(user_id);
|
|
|
|
const res = await this.catalogService.getOneDashboardMetabase(
|
|
id,
|
|
body,
|
|
metadata,
|
|
customer_id,
|
|
);
|
|
|
|
const dashboard = res.dashboard;
|
|
|
|
if (dashboard.owner === username) has_permission = true;
|
|
|
|
for (const role of user_roles) {
|
|
if (dashboard.p_roles.includes(role)) has_permission = true;
|
|
}
|
|
|
|
if (dashboard.p_users.includes(user_id)) has_permission = true;
|
|
|
|
if (is_data_manager || has_permission) {
|
|
delete dashboard.p_roles;
|
|
delete dashboard.p_users;
|
|
return res;
|
|
}
|
|
|
|
throw new HttpException(
|
|
'You do not have permission to access this data asset.',
|
|
403,
|
|
);
|
|
}
|
|
|
|
@Get('table-metadata')
|
|
async getAllTableMetadata(
|
|
@Body() body,
|
|
@Query() query,
|
|
@User() user: RequestUser,
|
|
) {
|
|
this.logger.info(`/catalog - ON GET ALL TABLES METADATA ROUTE`, {
|
|
user: user.user_id,
|
|
customer: user.customer_name,
|
|
});
|
|
const { user_id, customer_name, customer_id, permissions, username } = user;
|
|
const is_data_manager = permissions.includes(
|
|
PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER.seqid,
|
|
);
|
|
let has_permission = false;
|
|
const metadata = PackTheMetadata({
|
|
user_id,
|
|
customer_id,
|
|
customer_name,
|
|
});
|
|
|
|
if (!query) {
|
|
const res = await this.catalogService.getAllTableMetadata(body);
|
|
return res;
|
|
} else {
|
|
const user_roles = await this.catalogService.getUserRolesIds(user_id);
|
|
const res = await this.catalogService.getOneTableMetadata(
|
|
body,
|
|
query,
|
|
metadata,
|
|
customer_id,
|
|
);
|
|
const table = res[0];
|
|
|
|
if (table.owner === username) has_permission = true;
|
|
|
|
for (const role of user_roles) {
|
|
if (table.p_roles.includes(role)) has_permission = true;
|
|
}
|
|
|
|
if (table.p_users.includes(user_id)) has_permission = true;
|
|
|
|
if (is_data_manager || has_permission) {
|
|
delete table.p_roles;
|
|
delete table.p_users;
|
|
return res;
|
|
}
|
|
|
|
throw new ForbiddenException(
|
|
'You do not have permission to access this data asset.',
|
|
);
|
|
}
|
|
}
|
|
|
|
@Delete('table-metadata/:id')
|
|
async deleteOneTableMetadata(@Param() params, @Body() body) {
|
|
const { id } = params;
|
|
this.logger.info(`/catalog - ON DELETE ONE TABLE METADATA ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.deleteOneTableMetadata(id, body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('column-metadata')
|
|
async getOneColumnMetadata(@Body() body, @Query() params) {
|
|
this.logger.info(`/catalog - ON GET ONE COLUMN METADATA ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getOneColumnMetadata(body, params);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Delete('column-metadata/:id')
|
|
async deleteOneColumnMetadata(@Param() params, @Body() body) {
|
|
this.logger.info(`/catalog - ON DELETE ONE COLUMN METADATA ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
const { id } = params;
|
|
|
|
const res = await this.catalogService.deleteOneColumnMetadata(id, body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('data-preview')
|
|
async getOneDataPreview(@Body() body, @Query() params) {
|
|
this.logger.info(`/catalog - ON GET ONE DATAPREVIEW ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getOneDataPreview(body, params);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('data-status')
|
|
async getDataStatus(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET DATA STATUS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getDataStatus(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('data-status')
|
|
async createDataStatus(@Body() body) {
|
|
this.logger.info(`/catalog - ON CREATE DATA STATUS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.createDataStatus(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('data-description')
|
|
async getDataDescription(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET DATA DESCRIPTION ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getDataDescription(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Post('data-description')
|
|
async createDataDescription(@Body() body, @User() user: RequestUser) {
|
|
this.logger.info(`/catalog - ON CREATE DATA DESCRIPTION ROUTE`, {
|
|
user: user.user_id,
|
|
customer: user.customer_name,
|
|
});
|
|
|
|
const { user_id, customer_name, customer_id } = user;
|
|
const metadata = PackTheMetadata({
|
|
user_id,
|
|
customer_id,
|
|
customer_name,
|
|
});
|
|
return await this.catalogService.createDataDescription(body, metadata);
|
|
}
|
|
|
|
@Get('data-docs')
|
|
async getDataDocs(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET DATA DOCS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getDataDocs(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Post('data-docs')
|
|
async createDataDocs(@Body() body) {
|
|
this.logger.info(`/catalog - ON CREATE DATA DOCS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.createDataDocs(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('data-rating')
|
|
async getDataRating(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET DATA RATING ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getDataRating(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Post('data-rating')
|
|
async createDataRating(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET DATA RATING ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.createDataRating(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('summary-rating/:id')
|
|
async getSummaryRating() {
|
|
this.logger.info(`/catalog - ON GET SUMMARY RATING ROUTE`);
|
|
//const { id } = params;
|
|
|
|
//
|
|
|
|
//const res = await this.catalogService.getSummaryRating(id, body);
|
|
|
|
return {
|
|
avg_rating: 0,
|
|
rating_count: 0,
|
|
};
|
|
}
|
|
|
|
@Get('data-comment')
|
|
async getDataComment(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET DATA COMMENT ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getDataComment(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Post('data-comment')
|
|
async createDataComment(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET DATA COMMENT ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.createDataComment(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('data-review')
|
|
async getDataReview(@Body() body, @Query() params) {
|
|
this.logger.info(`/catalog - ON GET DATA REVIEW ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getDataReview(body, params);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Post('tags')
|
|
async createTags(@Body() body, @User() user: RequestUser) {
|
|
this.logger.info(`/catalog - ON CREATE TAG ROUTE`, {
|
|
user: user.user_id,
|
|
customer: user.customer_name,
|
|
});
|
|
const { user_id, customer_name, customer_id } = user;
|
|
const metadata = PackTheMetadata({
|
|
user_id,
|
|
customer_id,
|
|
customer_name,
|
|
});
|
|
|
|
const res = await this.catalogService.createTags(body, metadata);
|
|
|
|
return res.data_assets;
|
|
}
|
|
|
|
@Get('tags')
|
|
async findAllTags(@Body() body) {
|
|
this.logger.info(`/catalog - ON FIND ALL TAGS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const { user_id, customer, customer_id } = body.info;
|
|
const metadata = PackTheMetadata({
|
|
user_id,
|
|
customer_id,
|
|
customer_name: customer,
|
|
});
|
|
|
|
const res = await this.catalogService.findAllTags(body, metadata);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Delete('tags/:id')
|
|
async deleteTags(@Param() params, @Body() body) {
|
|
this.logger.info(`/catalog - ON DELETE TAG ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
const { id } = params;
|
|
body.data_asset_id = id;
|
|
const { user_id, customer, customer_id } = body.info;
|
|
const metadata = PackTheMetadata({
|
|
user_id,
|
|
customer_id,
|
|
customer_name: customer,
|
|
});
|
|
const res = await this.catalogService.deleteTags(body, metadata);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Post('table-tags')
|
|
async createTableTags(@Body() body) {
|
|
this.logger.info(`/catalog - ON CREATE TABLE TAG ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.createTableTags(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('table-tags/:data_asset_id')
|
|
async getAllTableTags(@Body() body, @Param() params) {
|
|
this.logger.info(`/catalog - ON GET ALL TABLE TAGS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
const { user_id, customer, customer_id } = body.info;
|
|
const metadata = PackTheMetadata({
|
|
user_id,
|
|
customer_id,
|
|
customer_name: customer,
|
|
});
|
|
const res = await this.catalogService.findAllTableTags(params, metadata);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Delete('table-tags/:id')
|
|
async deleteTableTags(@Param() params, @Body() body) {
|
|
this.logger.info(`/catalog - ON CREATE TABLE TAGS ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
const { id } = params;
|
|
|
|
const res = await this.catalogService.deleteTableTags(id, body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Get('table-rules')
|
|
async getTableRules(@Body() body) {
|
|
this.logger.info(`/catalog - ON GET TABLE RULES ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
|
|
const res = await this.catalogService.getTableRules(body);
|
|
|
|
return res;
|
|
}
|
|
|
|
@Delete('table-rules/:id')
|
|
async deleteTableRules(@Param() params, @Body() body) {
|
|
this.logger.info(`/catalog - ON DELETE TABLE RULES ROUTE`, {
|
|
user: body.info.user_id,
|
|
customer: body.info.customer,
|
|
});
|
|
const { id } = params;
|
|
|
|
const res = await this.catalogService.deleteTableRules(id, body);
|
|
|
|
return res;
|
|
}
|
|
}
|