import { CurrentUser } from "@edr/api-common"; import { SupportAuthorRole } from "@edr/types"; import { Body, Controller, Get, Post } from "@nestjs/common"; import { ApiOperation, ApiTags } from "@nestjs/swagger"; import { AuthUserPayload, resolveAuthUserId, } from "../../common/resolve-auth-user-id"; import { SendMessageDto } from "./dto/send-message.dto"; import { SupportChatService } from "./support-chat.service"; /** * Portal (customer) support-chat endpoints. The caller's company has exactly one * thread, so these are addressed as a singleton — no conversation id on the wire, * and nothing for a portal user to pick between. */ @ApiTags("support-chat") @Controller("support") export class SupportChatController { constructor(private readonly service: SupportChatService) {} @Get("conversation") @ApiOperation({ summary: "My company's support thread (null until someone speaks)", }) conversation(@CurrentUser() user: AuthUserPayload) { return this.service.getCustomerConversation(resolveAuthUserId(user)); } @Get("conversation/messages") @ApiOperation({ summary: "Messages in my company's support thread" }) messages(@CurrentUser() user: AuthUserPayload) { return this.service.getCustomerMessages(resolveAuthUserId(user)); } @Post("conversation/messages") @ApiOperation({ summary: "Send a message as the customer, opening the thread if needed", }) send(@CurrentUser() user: AuthUserPayload, @Body() body: SendMessageDto) { return this.service.sendAsCustomer(resolveAuthUserId(user), body.body); } @Post("conversation/read") @ApiOperation({ summary: "Mark my company's thread read (customer side)" }) read(@CurrentUser() user: AuthUserPayload) { return this.service.markCustomerRead(resolveAuthUserId(user)); } @Get("unread-count") @ApiOperation({ summary: "Count my unread support messages" }) unread(@CurrentUser() user: AuthUserPayload) { return this.service.unreadCount( SupportAuthorRole.CUSTOMER, resolveAuthUserId(user), ); } }