import { Controller, Get, Patch, Headers, Param, Body, HttpException, Logger, } from '@nestjs/common'; import { PlatformGraphqlService } from '../platform/platform-graphql.service'; import { WorklistService } from './worklist.service'; import { MissedQueueService } from './missed-queue.service'; @Controller('api/worklist') export class WorklistController { private readonly logger = new Logger(WorklistController.name); constructor( private readonly worklist: WorklistService, private readonly missedQueue: MissedQueueService, private readonly platform: PlatformGraphqlService, ) {} @Get() async getWorklist(@Headers('authorization') authHeader: string) { if (!authHeader) { throw new HttpException('Authorization required', 401); } const agentName = await this.resolveAgentName(authHeader); this.logger.log(`Fetching worklist for agent: ${agentName}`); return this.worklist.getWorklist(agentName, authHeader); } @Get('missed-queue') async getMissedQueue(@Headers('authorization') authHeader: string) { if (!authHeader) throw new HttpException('Authorization header required', 401); const agentName = await this.resolveAgentName(authHeader); return this.missedQueue.getMissedQueue(agentName, authHeader); } @Patch('missed-queue/:id/status') async updateMissedCallStatus( @Param('id') id: string, @Headers('authorization') authHeader: string, @Body() body: { status: string }, ) { if (!authHeader) throw new HttpException('Authorization header required', 401); if (!body.status) throw new HttpException('status is required', 400); return this.missedQueue.updateStatus(id, body.status, authHeader); } private async resolveAgentName(authHeader: string): Promise { try { const data = await this.platform.queryWithAuth( `{ currentUser { workspaceMember { name { firstName lastName } } } }`, undefined, authHeader, ); const name = data.currentUser?.workspaceMember?.name; const full = `${name?.firstName ?? ''} ${name?.lastName ?? ''}`.trim(); if (full) return full; } catch (err) { this.logger.warn(`Failed to resolve agent name: ${err}`); } throw new HttpException('Could not determine agent identity', 400); } }