(req: NextRequest, { params }: RouteParams)
| 34 | } |
| 35 | |
| 36 | export async function GET(req: NextRequest, { params }: RouteParams) { |
| 37 | // Check admin authentication |
| 38 | const authResult = await checkAdminAuth() |
| 39 | if (authResult instanceof NextResponse) { |
| 40 | return authResult |
| 41 | } |
| 42 | |
| 43 | const { clientRequestId } = await params |
| 44 | |
| 45 | if (!clientRequestId) { |
| 46 | return NextResponse.json( |
| 47 | { error: 'Missing required parameter: clientRequestId' }, |
| 48 | { status: 400 }, |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | try { |
| 53 | // First, get the main request message to find the client_id |
| 54 | const mainMessage = await db |
| 55 | .select() |
| 56 | .from(schema.message) |
| 57 | .where(eq(schema.message.client_request_id, clientRequestId)) |
| 58 | .limit(1) |
| 59 | |
| 60 | if (mainMessage.length === 0) { |
| 61 | return NextResponse.json( |
| 62 | { error: 'No messages found for this client request ID' }, |
| 63 | { status: 404 }, |
| 64 | ) |
| 65 | } |
| 66 | |
| 67 | const clientId = mainMessage[0].client_id |
| 68 | |
| 69 | // Query all messages with the same client_id to include spawned agents |
| 70 | const allMessages = await db |
| 71 | .select() |
| 72 | .from(schema.message) |
| 73 | .where(eq(schema.message.client_id, clientId ?? 'NULL')) |
| 74 | .orderBy(schema.message.finished_at) |
| 75 | |
| 76 | // Build timeline events from messages using utility function |
| 77 | const timelineEvents = buildTimelineFromMessages( |
| 78 | allMessages, |
| 79 | clientRequestId, |
| 80 | ) |
| 81 | |
| 82 | logger.info( |
| 83 | { |
| 84 | adminId: authResult.id, |
| 85 | clientRequestId, |
| 86 | eventCount: timelineEvents.length, |
| 87 | }, |
| 88 | 'Admin fetched timeline events', |
| 89 | ) |
| 90 | |
| 91 | return NextResponse.json({ events: timelineEvents }) |
| 92 | } catch (error) { |
| 93 | logger.error({ error, clientRequestId }, 'Error building timeline') |
nothing calls this directly
no test coverage detected