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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | export const dynamic = "force-dynamic"; /** * Admin Support Agents API * GET /api/admin/support/agents - Get agent performance stats */ import { NextResponse } from 'next/server'; import { requireAdminRole, handleAuthError } from '@/lib/auth'; import { prisma } from '@/lib/prisma'; import { TicketStatus, AgentStats } from '@/types/support'; import { handleError } from '@/lib/error-handler'; import { logger } from '@/lib/logging'; export async function GET() { try { await requireAdminRole(); // Get all admin users (potential agents) const agents = await prisma.user.findMany({ where: { role: 'ADMIN' }, select: { id: true, name: true, email: true, supportAgentProfile: true}}); // Get stats for each agent const agentStats: AgentStats[] = await Promise.all( agents.map(async (agent) => { // Get total assigned tickets const totalTickets = await prisma.supportTicket.count({ where: { assignedToId: agent.id }}); // Get resolved tickets const resolvedTickets = await prisma.supportTicket.count({ where: { assignedToId: agent.id, status: { in: [TicketStatus.RESOLVED, TicketStatus.CLOSED]}}}); // Get open tickets const openTickets = await prisma.supportTicket.count({ where: { assignedToId: agent.id, status: { in: [ TicketStatus.OPEN, TicketStatus.IN_PROGRESS, TicketStatus.AWAITING_CUSTOMER, TicketStatus.AWAITING_AGENT, ]}}}); // Calculate average response time const ticketsWithResponse = await prisma.supportTicket.findMany({ where: { assignedToId: agent.id, firstResponseAt: { not: null }}, select: { createdAt: true, firstResponseAt: true}}); let avgResponseTime: number | null = null; if (ticketsWithResponse.length > 0) { const totalResponseTime = ticketsWithResponse.reduce((sum, ticket) => { if (ticket.firstResponseAt) { return sum + (ticket.firstResponseAt.getTime() - ticket.createdAt.getTime()); } return sum; }, 0); avgResponseTime = Math.round(totalResponseTime / ticketsWithResponse.length / 1000); } // Calculate average resolution time const resolvedTicketsWithTime = await prisma.supportTicket.findMany({ where: { assignedToId: agent.id, resolvedAt: { not: null }}, select: { createdAt: true, resolvedAt: true}}); let avgResolutionTime: number | null = null; if (resolvedTicketsWithTime.length > 0) { const totalResolutionTime = resolvedTicketsWithTime.reduce((sum, ticket) => { if (ticket.resolvedAt) { return sum + (ticket.resolvedAt.getTime() - ticket.createdAt.getTime()); } return sum; }, 0); avgResolutionTime = Math.round( totalResolutionTime / resolvedTicketsWithTime.length / 1000 ); } // Get customer rating from surveys const surveys = await prisma.ticketSurvey.findMany({ where: { ticket: { assignedToId: agent.id}}, select: { rating: true }}); let customerRating: number | null = null; if (surveys.length > 0) { const totalRating = surveys.reduce((sum, survey) => sum + survey.rating, 0); customerRating = Math.round((totalRating / surveys.length) * 10) / 10; } return { agentId: agent.id, agentName: agent.name || agent.email, totalTickets, resolvedTickets, openTickets, avgResponseTime, avgResolutionTime, customerRating, isAvailable: agent.supportAgentProfile?.isAvailable ?? true, maxActiveTickets: agent.supportAgentProfile?.maxActiveTickets ?? 10, specialties: agent.supportAgentProfile?.specialties ?? null}; }) ); // Sort by resolved tickets (most active agents first) agentStats.sort((a, b) => b.resolvedTickets - a.resolvedTickets); return NextResponse.json({ success: true, data: agentStats}); } catch (error) { const authResponse = handleAuthError(error as Error); if (authResponse) return authResponse; logger.error('Error fetching agent stats', error instanceof Error ? error : new Error(String(error)), { category: 'ADMIN_SUPPORT' }); return handleError(error); } } |