import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { Body, Controller, Get, HttpCode, HttpStatus, Inject, Param, Post, Put, Query, UseFilters, } from '@nestjs/common'; import { ApiOkResponse, ApiProduces, ApiTags } from '@nestjs/swagger'; import { DADOSFERA_MODULES_KEYS, PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; import { Authenticated, RequireAllPermissions, RequireModule, RequireSomePermission, } from 'src/decorators/authentication.decorator'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { CustomersService } from './customers.service'; import { CustomerLinkRequest, CustomerLinksResponse } from './dtos/customers'; import { RequestUser, User } from 'src/decorators/user.decorator'; import type { StringValue } from 'ms'; import { PackTheMetadata } from 'src/utils/PackTheMetadata'; import { EnforceMfa } from './dtos/enforce-mfa'; @ApiTags('Customers') @Controller('customers') @UseFilters(GrpcToHttpExceptionFilter) export class CustomersController { logger: DadosferaLogger; constructor( @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, private customersService: CustomersService, ) { this.logger = dadosferaLogger.logger; } @Post(':id/mfa') @Authenticated() @RequireSomePermission(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) @RequireModule(DADOSFERA_MODULES_KEYS.DANGER_ZONE) async enableMfaEnforce(@Param('id') id: string, @Body() data: EnforceMfa) { this.logger.info('enableMfaEnforce', { id }); return await this.customersService.enableEnforceMfa(id, data.enabled); } @Get(':id/links') @Authenticated() @ApiOkResponse({ type: CustomerLinksResponse }) async getCustomerLinks(@Param('id') id: string) { this.logger.info('getCustomerLinks', { id }); const links = await this.customersService.getLinks(id); return { links }; } @Put(':id/links') @Authenticated() @RequireAllPermissions(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) @ApiOkResponse() @HttpCode(HttpStatus.OK) async setCustomerLinks( @Body() body: CustomerLinkRequest, @Param('id') id: string, ) { const { links } = body; this.logger.info('setCustomerLinks', { id, links }); await this.customersService.setLinks(id, links); } @Get('token') @Authenticated() @RequireAllPermissions(PERMISSIONS_GROUPS.AUTH.permissions.GENERATE_TOKEN) @ApiProduces('text/plain') async getCustomerToken( @Query('exp') exp: StringValue, @User() user: RequestUser, ): Promise { this.logger.info('getCustomerToken', { exp, user }); const data = { customerId: user.customer_id, userId: user.user_id, customerName: user.customer_name, }; return this.customersService.generateToken(exp, data); } @Get('monitoring-dashboard') @Authenticated() @RequireAllPermissions( PERMISSIONS_GROUPS.CUSTOMER.permissions.MONITORING_DASHBOARD, ) async getCustomerMonitoringDashboard( @User() user: RequestUser, ): Promise<{ url: string }> { this.logger.info('getCustomerMonitoringDashboard'); const metadata = PackTheMetadata(user); return this.customersService.getMonitoringDashboardUrl(metadata); } @Get('logs-dashboard') @Authenticated() @RequireAllPermissions( PERMISSIONS_GROUPS.USERS.permissions.ADMIN, ) @RequireModule(DADOSFERA_MODULES_KEYS.LOG_DASHBOARD) async getCustomerMixPanelLogsDashboard( @User() user: RequestUser, ): Promise<{ url: string }> { this.logger.info('getLogsDashboardUrl'); const metadata = PackTheMetadata(user); const result = await this.customersService.getLogsDashboardUrl(metadata); return result; } @Get('access-dashboard') @Authenticated() @RequireAllPermissions( PERMISSIONS_GROUPS.USERS.permissions.ADMIN, ) @RequireModule(DADOSFERA_MODULES_KEYS.ACCESS_DASHBOARD) async getAccessDashboard( @User() user: RequestUser, ): Promise<{ url: string }> { this.logger.info('getAccessDashboard'); const metadata = PackTheMetadata(user); const result = await this.customersService.getAccessDashboardUrl(user.customer_name, metadata); return result; } }