File

src/messages/messages.controller.ts

Prefix

messages

Index

Methods

Methods

Async create
create(orgId: string, senderId: string, senderRole: string, dto: CreateMessageDto)
Decorators :
@Post()
@Roles('DISPATCHER', 'OPERATIONS_MANAGER', 'PLANNER', 'ADMIN', 'SUPER_ADMIN', 'CUSTOMER_SERVICE', 'DRIVER')
@ApiOperation({summary: 'Send a message', description: 'Dispatchers/admins can start new threads with drivers (provide driverId). Drivers can ONLY reply to existing threads (provide replyToId).'})
Parameters :
Name Type Optional
orgId string No
senderId string No
senderRole string No
dto CreateMessageDto No
Returns : unknown
Async findMine
findMine(orgId: string, userId: string, page?: string, limit?: string)
Decorators :
@Get()
@ApiOperation({summary: 'List my received messages'})
Parameters :
Name Type Optional
orgId string No
userId string No
page string Yes
limit string Yes
Returns : unknown
Async findSent
findSent(orgId: string, userId: string, page?: string, limit?: string)
Decorators :
@Get('sent')
@Roles('DISPATCHER', 'OPERATIONS_MANAGER', 'PLANNER', 'ADMIN', 'SUPER_ADMIN', 'CUSTOMER_SERVICE', 'DRIVER')
@ApiOperation({summary: 'List messages I sent'})
Parameters :
Name Type Optional
orgId string No
userId string No
page string Yes
limit string Yes
Returns : unknown
Async markRead
markRead(orgId: string, userId: string, id: string)
Decorators :
@Post(':id/read')
@ApiOperation({summary: 'Mark a message as read'})
Parameters :
Name Type Optional
orgId string No
userId string No
id string No
Returns : unknown
Async remove
remove(orgId: string, userId: string, id: string)
Decorators :
@Delete(':id')
@ApiOperation({summary: 'Delete a message'})
Parameters :
Name Type Optional
orgId string No
userId string No
id string No
Returns : unknown
Async unreadCount
unreadCount(orgId: string, userId: string)
Decorators :
@Get('unread-count')
@ApiOperation({summary: 'Unread messages count for badge'})
Parameters :
Name Type Optional
orgId string No
userId string No
Returns : unknown
import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { MessagesService } from './messages.service';
import { CreateMessageDto } from './dto/create-message.dto';
import { CombinedAuthGuard } from '../auth/guards/combined-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { CurrentUser } from '../auth/decorators/current-user.decorator';

@ApiTags('Messages')
@ApiBearerAuth()
@UseGuards(CombinedAuthGuard, RolesGuard)
@Controller('messages')
export class MessagesController {
  constructor(private readonly messagesService: MessagesService) {}

  @Post()
  @Roles(
    'DISPATCHER',
    'OPERATIONS_MANAGER',
    'PLANNER',
    'ADMIN',
    'SUPER_ADMIN',
    'CUSTOMER_SERVICE',
    'DRIVER',
  )
  @ApiOperation({
    summary: 'Send a message',
    description:
      'Dispatchers/admins can start new threads with drivers (provide driverId). Drivers can ONLY reply to existing threads (provide replyToId).',
  })
  async create(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser('id') senderId: string,
    @CurrentUser('role') senderRole: string,
    @Body() dto: CreateMessageDto,
  ) {
    return this.messagesService.create(orgId, senderId, senderRole, dto);
  }

  @Get()
  @ApiOperation({ summary: 'List my received messages' })
  async findMine(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser('id') userId: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.messagesService.findMyMessages(
      orgId,
      userId,
      page ? parseInt(page, 10) : 1,
      limit ? parseInt(limit, 10) : 20,
    );
  }

  @Get('sent')
  // Drivers also need access to their own sent thread (replies they
  // wrote to dispatchers). The previous list locked them out and they
  // would have seen 403.
  @Roles(
    'DISPATCHER',
    'OPERATIONS_MANAGER',
    'PLANNER',
    'ADMIN',
    'SUPER_ADMIN',
    'CUSTOMER_SERVICE',
    'DRIVER',
  )
  @ApiOperation({ summary: 'List messages I sent' })
  async findSent(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser('id') userId: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.messagesService.findSentMessages(
      orgId,
      userId,
      page ? parseInt(page, 10) : 1,
      limit ? parseInt(limit, 10) : 20,
    );
  }

  @Get('unread-count')
  @ApiOperation({ summary: 'Unread messages count for badge' })
  async unreadCount(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.messagesService.getUnreadCount(orgId, userId);
  }

  @Post(':id/read')
  @ApiOperation({ summary: 'Mark a message as read' })
  async markRead(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser('id') userId: string,
    @Param('id') id: string,
  ) {
    return this.messagesService.markRead(orgId, userId, id);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete a message' })
  async remove(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser('id') userId: string,
    @Param('id') id: string,
  ) {
    return this.messagesService.remove(orgId, userId, id);
  }
}

results matching ""

    No results matching ""