import { Controller, Get, Post, Param, Body, UseGuards, UseInterceptors, UploadedFiles, BadRequestException, } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { FileFieldsInterceptor } from '@nestjs/platform-express'; import { CommentService } from './comment.service'; import { Roles } from '../common/decorators/roles.decorator'; import { RolesGuard } from '../common/guards/roles.guard'; import { TenantGuard } from '../common/guards/tenant.guard'; import { CurrentUser, CurrentUserPayload } from '../common/decorators/current-user.decorator'; import { multerOptions } from '../common/multer/multer.config'; @Controller('tickets/:ticketId/comments') @UseGuards(AuthGuard('jwt'), TenantGuard, RolesGuard) export class CommentController { constructor(private readonly commentService: CommentService) {} @Get() @Roles('technician', 'collector') async findAll( @CurrentUser() user: CurrentUserPayload, @Param('ticketId') ticketId: string, ) { return this.commentService.findAll(user.tenantId, ticketId); } @Post() @Roles('technician', 'collector') @UseInterceptors(FileFieldsInterceptor([{ name: 'files', maxCount: 3 }], multerOptions)) async create( @CurrentUser() user: CurrentUserPayload, @Param('ticketId') ticketId: string, @Body() body: any, @UploadedFiles() files?: { files?: Express.Multer.File[] }, ) { let content = body?.content; if (Array.isArray(content)) content = content[0]; if (typeof content !== 'string') content = String(content ?? ''); if (!content || content.length > 50000) { throw new BadRequestException('Content must be between 1 and 50000 characters'); } return this.commentService.create( user.tenantId, ticketId, user.sub, content, files?.files, ); } }