All files / src/modules/messaging messaging.controller.ts

100% Statements 32/32
77.77% Branches 14/18
100% Functions 10/10
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 9312x                           12x 12x 12x 12x 12x       12x   12x 12x       12x 6x       12x 2x       12x 3x       12x           3x       12x         13x     11x   11x       12x 1x       12x 1x       12x 1x           12x 2x   2x        
import {
  Controller,
  Get,
  Post,
  Patch,
  Param,
  Body,
  Query,
  Req,
  UseGuards,
  ParseIntPipe,
  DefaultValuePipe,
  HttpCode,
} from '@nestjs/common';
import { MessagingService } from './messaging.service';
import { JwtAuthGuard } from '@app/common/guards/jwt-auth.guard';
import { CreateConversationDto } from './dto/create-conversation.dto';
import { SendMessageDto } from './dto/send-message.dto';
import { MessagingGateway } from './messaging.gateway';
 
@Controller('messaging/conversations')
@UseGuards(JwtAuthGuard)
export class MessagingController {
  constructor(
    private readonly service: MessagingService,
    private readonly gateway: MessagingGateway,
  ) {}
 
  @Post()
  async createConversation(@Req() req: any, @Body() dto: CreateConversationDto) {
    return this.service.createConversation(req.user.id, dto);
  }
 
  @Get()
  async getMyConversations(@Req() req: any) {
    return this.service.getMyConversations(req.user.id);
  }
 
  @Get(':id')
  async getConversation(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
    return this.service.getConversation(id, req.user.id);
  }
 
  @Get(':id/messages')
  async getMessages(
    @Param('id', ParseIntPipe) id: number,
    @Query('before', new DefaultValuePipe(0), ParseIntPipe) before: number,
    @Query('limit', new DefaultValuePipe(50), ParseIntPipe) limit: number,
    @Req() req: any,
  ) {
    return this.service.getMessages(id, req.user.id, before || undefined, limit);
  }
 
  @Post(':id/messages')
  async sendMessage(
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: SendMessageDto,
    @Req() req: any,
  ) {
    const message = await this.service.sendMessage(id, req.user.id, dto);
    
    // Broadcast message via WebSocket
    this.gateway.broadcastMessage(id, message);
    
    return message;
  }
 
  @Patch('messages/:messageId/read')
  async markAsRead(@Param('messageId', ParseIntPipe) id: number, @Req() req: any) {
    return this.service.markAsRead(id, req.user.id);
  }
 
  @Patch(':id/read')
  async markConversationAsRead(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
    return this.service.markConversationAsRead(id, req.user.id);
  }
 
  @Get('unread-count')
  async getUnreadCount(@Req() req: any) {
    return this.service.getUnreadCount(req.user.id);
  }
 
  // Alias to match tests expecting POST /messaging/conversations/:id/read
  @Post(':id/read')
  @HttpCode(200)
  async markConversationAsReadPost(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
    await this.service.markConversationAsRead(id, req.user.id);
    // Explicitly return OK semantics
    return { success: true };
  }
}