import { Controller, Get, Post, Patch, Delete, Param, Body, Query, Req, UseInterceptors, UploadedFiles, Res, } from '@nestjs/common'; import { Request, Response } from 'express'; import { FilesInterceptor } from '@nestjs/platform-express'; import { SupportService } from './support.service'; import { UpdateTicketDto } from './dto/update-ticket.dto'; import { CommentDto } from './dto/comment.dto'; import { TicketListQueryDto } from './dto/list-query.dto'; import { supportUploadOptions } from './multer-options'; import { createReadStream } from 'fs'; @Controller('support/tickets') export class SupportController { constructor(private readonly service: SupportService) {} @Get() findAll(@Query() query: TicketListQueryDto) { return this.service.findAll(query); } @Get('uploads/:fileName') async downloadFile( @Param('fileName') fileName: string, @Res() res: Response, ) { const { filePath, attachment } = await this.service.getAttachment(fileName); res.setHeader('Content-Type', attachment.mimeType); res.setHeader( 'Content-Disposition', `inline; filename="${attachment.originalName}"`, ); createReadStream(filePath).pipe(res); } @Get(':id') findOne(@Param('id') id: string) { return this.service.findOne(id); } @Patch(':id') update(@Param('id') id: string, @Body() dto: UpdateTicketDto) { return this.service.update(id, dto); } @Patch(':id/assign') assign(@Param('id') id: string, @Body() body: { adminId: string }) { return this.service.assign(id, body.adminId); } @Patch(':id/resolve') resolve(@Param('id') id: string) { return this.service.resolve(id); } @Patch(':id/close') close(@Param('id') id: string) { return this.service.close(id); } @Post(':id/comments') addComment( @Param('id') id: string, @Body() dto: CommentDto, @Req() req: Request & { admin: any }, ) { const admin = req.admin; return this.service.addComment( id, admin.sub, `${admin.firstName || ''} ${admin.lastName || ''}`.trim(), 'super_admin', dto, ); } // ─── Attachments ──────────────────────────────── @Post(':id/attachments') @UseInterceptors(FilesInterceptor('files', 5, supportUploadOptions)) async uploadAttachments( @Param('id') id: string, @UploadedFiles() files: Express.Multer.File[], @Body() body: { commentId?: string }, @Req() req: Request & { admin: any }, ) { const admin = req.admin; const results = await Promise.all( files.map((f) => this.service.addAttachment(id, f, admin.sub, body.commentId), ), ); return results; } @Get(':id/attachments') getAttachments(@Param('id') id: string) { return this.service.getAttachments(id); } @Delete(':id/attachments/:attachmentId') deleteAttachment( @Param('id') id: string, @Param('attachmentId') attachmentId: string, ) { return this.service.deleteAttachment(id, attachmentId); } }