import { BadRequestException, Body, Controller, DefaultValuePipe, Get, Headers, Param, ParseIntPipe, ParseUUIDPipe, Post, Query, UseGuards, } from '@nestjs/common'; import { CurrentUser } from '../common/auth/current-user.decorator.js'; import { CurrentUserGuard } from '../common/auth/current-user.guard.js'; import type { User } from '../users/user.js'; import { CreateMessageDto } from './dto/create-message.dto.js'; import { MessagesService } from './messages.service.js'; @Controller('api/v1/chats/:chatId/messages') @UseGuards(CurrentUserGuard) export class MessagesController { constructor(private readonly messagesService: MessagesService) {} @Post() create( @Param('chatId', new ParseUUIDPipe()) chatId: string, @Body() body: CreateMessageDto, @CurrentUser() currentUser: User, @Headers('idempotency-key') idempotencyKey?: string | string[], ) { return this.messagesService.create( chatId, body, currentUser, idempotencyKey, ); } @Get() list( @Param('chatId', new ParseUUIDPipe()) chatId: string, @CurrentUser() currentUser: User, @Query('limit', new DefaultValuePipe(50), new ParseIntPipe()) limit: number, @Query('cursor') cursor?: string, ) { if (limit < 1 || limit > 100) throw new BadRequestException('limit must be between 1 and 100'); return this.messagesService.list(chatId, currentUser, limit, cursor); } }