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 | export const dynamic = 'force-dynamic'; /** * Admin Chat Conversations API * GET /api/admin/support/chat/conversations - List all chat conversations */ import { NextRequest, NextResponse } from 'next/server'; import { withAdmin, withErrorHandling, ApiSuccessResponse, ApiErrorResponse, } from '@/lib/api'; import { prisma } from '@/lib/prisma'; import { ChatStatus } from '@prisma/client'; // ============================================================================ // HANDLERS // ============================================================================ /** * GET /api/admin/support/chat/conversations * List all chat conversations for agents */ async function handleGet( request: NextRequest, _context: unknown, session: { user: { id: number } } ): Promise<NextResponse<ApiSuccessResponse<unknown> | ApiErrorResponse>> { const { searchParams } = request.nextUrl; const statusParam = searchParams.get('status'); const agentId = searchParams.get('agentId'); const assignedToMe = searchParams.get('assignedToMe') === 'true'; const page = parseInt(searchParams.get('page') || '1', 10); const limit = parseInt(searchParams.get('limit') || '20', 10); // Build where clause const where: Record<string, unknown> = {}; if (statusParam) { const statuses = statusParam.split(',') as ChatStatus[]; if (statuses.length === 1) { where.status = statuses[0]; } else { where.status = { in: statuses }; } } if (agentId) { where.assignedToId = parseInt(agentId, 10); } if (assignedToMe) { where.assignedToId = session.user.id; } // Get total count const total = await prisma.chatConversation.count({ where }); // Get conversations const conversations = await prisma.chatConversation.findMany({ where, orderBy: [{ priority: 'desc' }, { startedAt: 'desc' }], skip: (page - 1) * limit, take: limit, include: { user: { select: { id: true, name: true, email: true, image: true }, }, agent: { select: { id: true, name: true, email: true }, }, messages: { take: 1, orderBy: { createdAt: 'desc' }, select: { content: true, senderType: true, createdAt: true, isRead: true, }, }, _count: { select: { messages: true, }, }, }, }); return NextResponse.json({ success: true, data: conversations, pagination: { page, limit, total, totalPages: Math.ceil(total / limit), }, }); } // ============================================================================ // EXPORTS // ============================================================================ export const GET = withErrorHandling(withAdmin(handleGet)); |