diff --git a/docsfera.json b/docsfera.json index 96612ac..4797f1d 100644 --- a/docsfera.json +++ b/docsfera.json @@ -969,6 +969,41 @@ ] } }, + "/users/download": { + "get": { + "operationId": "UsersController_downloadUsersInCsv", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Users" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, "/users/hierarchies": { "get": { "operationId": "UsersController_getAllHierarchies", @@ -3874,6 +3909,85 @@ ] } }, + "/catalog/download": { + "get": { + "operationId": "CatalogController_dowloadAsserts", + "parameters": [ + { + "name": "dadosfera-lang", + "in": "header", + "required": false, + "schema": { + "enum": [ + "pt-br", + "en-us" + ], + "type": "string" + } + }, + { + "name": "size", + "required": false, + "in": "query", + "description": "Número de registros a ser retornado", + "schema": { + "minimum": 1, + "maximum": 10000, + "type": "number" + } + }, + { + "name": "page", + "required": false, + "in": "query", + "description": "Usado para paginação, em conjunto com `size`", + "schema": { + "minimum": 1, + "type": "number" + } + }, + { + "name": "sort_by", + "required": false, + "in": "query", + "description": "Campo do data asset para usar na ordenação. Padrão: `created_at` ", + "schema": { + "type": "string" + } + }, + { + "name": "order", + "required": false, + "in": "query", + "description": "Tipo de ordenação - `asc`: crescente; `desc`: decrescente ", + "schema": { + "default": "asc", + "enum": [ + "asc", + "desc" + ], + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "" + } + }, + "tags": [ + "Catalog" + ], + "security": [ + { + "access-token": [] + }, + { + "access-token": [] + } + ] + } + }, "/catalog/data-asset": { "get": { "operationId": "CatalogController_findByPipelineAndObject", diff --git a/helmfiles/prd.yaml b/helmfiles/prd.yaml index c426fa9..b44aaf3 100644 --- a/helmfiles/prd.yaml +++ b/helmfiles/prd.yaml @@ -23,7 +23,7 @@ charts: - name: replicaCount value: 2 - - name: unimed + - name: unimed-maestro chart: ../maestro values: - ../maestro/values.yaml @@ -33,7 +33,7 @@ charts: - name: maestro.duc_url value: duc.dadosfera.ai - name: hostname - value: maestro.dadosfera.ai + value: maestro-unimed.dadosfera.ai - name: maestro.pi_factory_url value: pi-factory.dadosfera.ai - name: maestro.in_factory_url @@ -49,5 +49,5 @@ charts: value: dea2c27f-0973-4588-a2e0-9e31b64c7ffd - name: replicaCount value: 1 - - name: restricted-ip - value: "177.52.172.0/24" + - name: maestro.restricted_ip + value: "177.52.172.0/24,57.151.113.140/30" diff --git a/maestro/templates/ingress.yaml b/maestro/templates/ingress.yaml index ccd8360..1594baa 100644 --- a/maestro/templates/ingress.yaml +++ b/maestro/templates/ingress.yaml @@ -3,6 +3,9 @@ kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/proxy-body-size: "0" + nginx.ingress.kubernetes.io/proxy-read-timeout: "300" + nginx.ingress.kubernetes.io/proxy-connect-timeout: "300" + nginx.ingress.kubernetes.io/proxy-send-timeout: "300" nginx.ingress.kubernetes.io/server-snippet: | underscores_in_headers on; ignore_invalid_headers on; diff --git a/src/modules/catalog/catalog.controller.ts b/src/modules/catalog/catalog.controller.ts index f0727f0..b87829b 100644 --- a/src/modules/catalog/catalog.controller.ts +++ b/src/modules/catalog/catalog.controller.ts @@ -117,6 +117,52 @@ export class CatalogController { return res; } + @Get('/download') + @RequireSomePermission( + PERMISSIONS_GROUPS.CATALOG.permissions.GET, + PERMISSIONS_GROUPS.CATALOG.permissions.DATA_MANAGER, + ) + async dowloadAsserts( + @User() user: RequestUser, + @Query() query: ICatalogAllRequest, + @Res() res: Response + ) { + const { user_id, customer_name, customer_id, username, permissions } = user; + this.logger.info(`/catalog/download - searchCatalog`, { + user_id, + customer_name, + }); + + 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 { + file, + filename + } = await this.catalogService.downloadAssets( + query, + metadata, + customer_id, + ); + + res.setHeader('Content-Disposition', `attachment; filename="${filename}"`); + res.setHeader('Content-Type', 'text/csv'); + + res.end(file); + } + @ApiInternalOnlyEndpoint() @Get('data-asset') async findByPipelineAndObject(@User() user: RequestUser, @Query() query) { diff --git a/src/modules/catalog/catalog.service.ts b/src/modules/catalog/catalog.service.ts index 9bf0047..fafc14a 100644 --- a/src/modules/catalog/catalog.service.ts +++ b/src/modules/catalog/catalog.service.ts @@ -25,6 +25,7 @@ import { UsersService } from '../users/users.service'; import { RolesService } from '../roles/roles.service'; import { Metadata } from '@grpc/grpc-js'; import { + AssetReporter, BatchRemoveRlsRulesRequest, IUpdateDataRequest, TriggerCatalogReq, @@ -234,6 +235,34 @@ class CatalogService implements OnModuleInit { return { data_assets: response, total }; } + async downloadAssets( + query: Record, + metadata: Metadata, + customer_id: string, + ) { + const data = await this.searchDataAssets(query, metadata, customer_id); + + const formatData = data.data_assets.map(asset => ({ + id: asset.id, + display_name: asset.display_name, + data_asset_type: asset.data_asset_type, + created_at: asset.created_at, + tags: '[' + asset.tags.join(', ') + ']' + })) + + const parser = ParserBuilder.build('csv'); + + const file = await parser.parse(formatData); + + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const filename = `dadosfera_assets_${timestamp}.csv`; + + return { + file, + filename + } + } + async getOneDataAsset(data: { id: string; customer_id: string; diff --git a/src/modules/catalog/dtos/index.ts b/src/modules/catalog/dtos/index.ts index b869e5e..ef12e78 100644 --- a/src/modules/catalog/dtos/index.ts +++ b/src/modules/catalog/dtos/index.ts @@ -320,3 +320,11 @@ export class BatchRemoveRlsRulesRequest { @ApiPropertyOptional() id_rls?: string; } + +export type AssetReporter = { + id: string; + display_name: string; + data_asset_type: string; + created_at: string; + tags: string; +} diff --git a/src/modules/users/dtos/entities.ts b/src/modules/users/dtos/entities.ts index 9272e52..8e8b852 100644 --- a/src/modules/users/dtos/entities.ts +++ b/src/modules/users/dtos/entities.ts @@ -174,3 +174,14 @@ export class GetAllDepartmentsRes { @ApiProperty() departments: string[]; } + + +export interface UserReporter { + name: string; + email: string; + mfaStatus: string; + status: string; + lastLogin: string; + createdAt: string; + updatedAt: string; +} diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 5fdc603..e565f18 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -12,6 +12,7 @@ import { Post, Put, Query, + Res, UseFilters, } from '@nestjs/common'; import { @@ -24,6 +25,7 @@ import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; import { Authenticated, RequireAllPermissions, + RequireSomePermission, } from 'src/decorators/authentication.decorator'; import { Language } from 'src/decorators/language.decorator'; import { ApiInternalOnlyController } from 'src/decorators/swagger.decorator'; @@ -52,6 +54,7 @@ import { UpdateUserRes, } from './dtos/entities'; import { UsersService } from './users.service'; +import { Response } from 'express'; @ApiInternalOnlyController() @ApiTags('Users') @@ -80,6 +83,26 @@ export class UsersController { return await this.userService.findAllUsersByCustomerId(user.customer_id); } + @Get('/download') + @RequireSomePermission(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) + async downloadUsersInCsv( + @User() user: RequestUser, + @Language() language: LanguageEnum, + @Res() res: Response + ) { + this.logger.info('downloadUsersInCsv'); + this.userService.setLanguage(language); + const { + file, + filename + } = await this.userService.downloadUsersInCsv(user.customer_id); + + res.setHeader('Content-Disposition', `attachment; filename="${filename}"`); + res.setHeader('Content-Type', 'text/csv'); + + res.end(file); + } + @Get('hierarchies') @RequireAllPermissions(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) @ApiOkResponse({ type: GetAllHierarchiesRes }) diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index e6717fa..3057010 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -21,6 +21,7 @@ import { IUserByCustomer, SetUserRolesReq, UpdateUserReq, + UserReporter, } from './dtos/entities'; import { RolesService } from '../roles/roles.service'; import { HIERARCHIES } from './hierarchies'; @@ -29,6 +30,7 @@ import { UserByCustomer } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces import { EnrichErrorCode } from 'src/utils/ErrorBuilder'; import { DucClient } from '../duc/client.config'; import { Metadata } from '@grpc/grpc-js'; +import { ParserBuilder } from 'src/utils/FileParser/parser.builder'; @Injectable() export class UsersService implements OnModuleInit { @@ -78,6 +80,34 @@ export class UsersService implements OnModuleInit { }; } + async downloadUsersInCsv(customerId: string) { + const { users } = await lastValueFrom( + this.usersClientService.UserFindAllByCustomerId({ customerId }), + ); + + const formatUsers: UserReporter[] = users.map(user => ({ + createdAt: user.createdAt, + email: user.email, + lastLogin: user.lastLogin, + mfaStatus: user.mfaStatus, + name: user.name, + status: user.status, + updatedAt: user.updatedAt + })) + + const parser = ParserBuilder.build('csv'); + + const file = await parser.parse(formatUsers); + + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); + const filename = `dadosfera_users_${timestamp}.csv`; + + return { + file, + filename + } + } + async findOneById(id: string): Promise<{ user: IUserByCustomer }> { const { user } = await lastValueFrom( this.usersClientService.UserFindOneById({ id }),