mirror of
https://github.com/dadosfera/maestro.git
synced 2026-10-01 18:19:08 +00:00
144 lines
4.5 KiB
TypeScript
144 lines
4.5 KiB
TypeScript
import { DadosferaLogger } from '@dadosfera/dadosfera-logs';
|
|
import { IdResponse } from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/messages';
|
|
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
HttpException,
|
|
HttpStatus,
|
|
Inject,
|
|
Param,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
UseFilters,
|
|
} from '@nestjs/common';
|
|
import { ApiOkResponse, ApiProduces, ApiTags } from '@nestjs/swagger';
|
|
import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum';
|
|
import {
|
|
Authenticated,
|
|
RequireAllPermissions,
|
|
} from 'src/decorators/authentication.decorator';
|
|
import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter';
|
|
import { CustomersService } from './customers.service';
|
|
import { CustomerLinkRequest, CustomerLinksResponse, CustomerThemeRequest, CustomerThemeResponse } from './dtos/customers';
|
|
import { RequestUser, User } from 'src/decorators/user.decorator';
|
|
import type { StringValue } from 'ms';
|
|
import { PackTheMetadata } from 'src/utils/ PackTheMetadata';
|
|
import ErrorCodes from 'src/utils/errorCodes';
|
|
|
|
@ApiTags('Customers')
|
|
@Controller('customers')
|
|
@UseFilters(GrpcToHttpExceptionFilter)
|
|
export class CustomersController {
|
|
logger: DadosferaLogger;
|
|
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
private customersService: CustomersService,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
@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 };
|
|
}
|
|
|
|
@Post(':id/theme')
|
|
@Authenticated()
|
|
@RequireAllPermissions(PERMISSIONS_GROUPS.USERS.permissions.ADMIN)
|
|
@ApiOkResponse({ type: CustomerThemeResponse })
|
|
async saveCustomertheme(@Param('id') id: string, @Body() data: CustomerThemeRequest) {
|
|
this.logger.info('saveCustomertheme', JSON.stringify({
|
|
id, theme: data
|
|
}));
|
|
|
|
try {
|
|
// const theme = await this.customersService.createThemeByCustomer(id, data);
|
|
this.logger.info('saveCustomertheme', JSON.stringify(data));
|
|
return { theme: null };
|
|
} catch (err) {
|
|
if (err.details === ErrorCodes.CUSTOMER.NOT_FOUND) {
|
|
this.logger.error('Error - saveCustomertheme - Expect CUSTOMER.NOT_FOUND');
|
|
throw new HttpException(err.details, HttpStatus.NOT_FOUND);
|
|
} else {
|
|
this.logger.error('Error - saveCustomertheme Unknown Error:' + err?.message);
|
|
return { theme: null };
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
@Get(':id/theme')
|
|
@ApiOkResponse({ type: CustomerThemeResponse })
|
|
async getCustomerTheme(@Param('id') id: string) {
|
|
this.logger.info('getCustomerTheme with id' + id);
|
|
|
|
try {
|
|
const data = await this.customersService.getThemeByCustomer(id);
|
|
this.logger.info('Success - getCustomerTheme with id'+ id);
|
|
return data;
|
|
}catch (err) {
|
|
if (err.details === ErrorCodes.CUSTOMER.NOT_FOUND) {
|
|
this.logger.error('Error - getCustomerTheme - Expect CUSTOMER.NOT_FOUND');
|
|
throw new HttpException(err.details, HttpStatus.NOT_FOUND);
|
|
} else {
|
|
this.logger.error('Error - getCustomerTheme Unknown Error:' + err?.message);
|
|
return { theme: null };
|
|
};
|
|
}
|
|
}
|
|
|
|
@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<string> {
|
|
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);
|
|
}
|
|
}
|