import { DadosferaLogger } from '@dadosfera/dadosfera-logs'; import { Body, Controller, Get, HttpException, HttpStatus, Inject, Param, Post, Put, Query, UploadedFiles, UseFilters, UseInterceptors, HttpCode } from '@nestjs/common'; import { ApiOkResponse, 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 { CustomerThemeRequest, CustomerThemeResponse } from './dtos/customers'; import ErrorCodes from 'src/utils/errorCodes'; import { AnyFilesInterceptor } from '@nestjs/platform-express'; import { ThemeService } from './theme.service'; @ApiTags('Theme') @Controller('customers') @UseFilters(GrpcToHttpExceptionFilter) export class ThemeController { logger: DadosferaLogger; constructor( @Inject(DadosferaLogger) dadosferaLogger: DadosferaLogger, private themeService: ThemeService, ) { this.logger = dadosferaLogger.logger; } @Post('/:id/theme') @Authenticated() @RequireAllPermissions(PERMISSIONS_GROUPS.USERS.permissions.ADMIN) @ApiOkResponse({ type: CustomerThemeResponse }) @UseInterceptors(AnyFilesInterceptor()) @HttpCode(HttpStatus.OK) async saveCustomertheme( @Param('id') id: string, @Body() data: CustomerThemeRequest, @UploadedFiles() files: Array ) { this.logger.info('saveCustomertheme' + JSON.stringify({ id, })); const logo = files.find(file => file.fieldname === 'logo'); const logoLogin = files.find(file => file.fieldname === 'logoLogin'); this.validFileSize(logo); this.validFileSize(logoLogin); this.validMimeType(logo); this.validMimeType(logoLogin); try { const theme = await this.themeService.createThemeByCustomer(id, { ...data, logo, logoLogin }); this.logger.info('saveCustomertheme' + JSON.stringify(theme)); return theme; } 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 }; }; } } private validFileSize(file: Express.Multer.File) { const maxFileSize = 10 * 1024 * 1024; // 10MB if (file && file.size > maxFileSize) { throw new HttpException(`O Arquivo ${file.filename} possui mais de 10MB`, HttpStatus.BAD_REQUEST); } } private validMimeType(file: Express.Multer.File) { const mimeTypesValid = ['image/jpeg', 'image/jpg', 'image/png']; if (file && !mimeTypesValid.includes(file.mimetype)) { throw new HttpException(`O Arquivo ${file.fieldname} deve ser jpeg, jpg, ou png`, HttpStatus.BAD_REQUEST); } } @Get('/:id/theme') @ApiOkResponse({ type: CustomerThemeResponse }) async getCustomerTheme(@Param('id') id: string) { this.logger.info('getCustomerTheme with id' + id); try { const data = await this.themeService.getThemeByCustomer(id); this.logger.info('Success - getCustomerTheme'+ JSON.stringify(data)); if (data?.theme) return data; return { theme: null }; }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 }; }; } } @Post('/:id/theme/reset') @ApiOkResponse({ type: CustomerThemeResponse }) async resetTheme(@Param('id') id: string) { this.logger.info('getCustomerTheme with id' + id); try { await this.themeService.resetTheme(id); return { theme: null }; }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 }; }; } } }