|
| 1 | +import { and, eq } from 'drizzle-orm' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { getSession } from '@/lib/auth' |
| 4 | +import { createLogger } from '@/lib/logs/console/logger' |
| 5 | +import { db } from '@/db' |
| 6 | +import { permissions, workflow, workflowExecutionLogs } from '@/db/schema' |
| 7 | + |
| 8 | +const logger = createLogger('LogDetailsByIdAPI') |
| 9 | + |
| 10 | +export const revalidate = 0 |
| 11 | + |
| 12 | +export async function GET(_request: NextRequest, { params }: { params: Promise<{ id: string }> }) { |
| 13 | + const requestId = crypto.randomUUID().slice(0, 8) |
| 14 | + |
| 15 | + try { |
| 16 | + const session = await getSession() |
| 17 | + if (!session?.user?.id) { |
| 18 | + logger.warn(`[${requestId}] Unauthorized log details access attempt`) |
| 19 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 20 | + } |
| 21 | + |
| 22 | + const userId = session.user.id |
| 23 | + const { id } = await params |
| 24 | + |
| 25 | + const rows = await db |
| 26 | + .select({ |
| 27 | + id: workflowExecutionLogs.id, |
| 28 | + workflowId: workflowExecutionLogs.workflowId, |
| 29 | + executionId: workflowExecutionLogs.executionId, |
| 30 | + stateSnapshotId: workflowExecutionLogs.stateSnapshotId, |
| 31 | + level: workflowExecutionLogs.level, |
| 32 | + trigger: workflowExecutionLogs.trigger, |
| 33 | + startedAt: workflowExecutionLogs.startedAt, |
| 34 | + endedAt: workflowExecutionLogs.endedAt, |
| 35 | + totalDurationMs: workflowExecutionLogs.totalDurationMs, |
| 36 | + executionData: workflowExecutionLogs.executionData, |
| 37 | + cost: workflowExecutionLogs.cost, |
| 38 | + files: workflowExecutionLogs.files, |
| 39 | + createdAt: workflowExecutionLogs.createdAt, |
| 40 | + workflowName: workflow.name, |
| 41 | + workflowDescription: workflow.description, |
| 42 | + workflowColor: workflow.color, |
| 43 | + workflowFolderId: workflow.folderId, |
| 44 | + workflowUserId: workflow.userId, |
| 45 | + workflowWorkspaceId: workflow.workspaceId, |
| 46 | + workflowCreatedAt: workflow.createdAt, |
| 47 | + workflowUpdatedAt: workflow.updatedAt, |
| 48 | + }) |
| 49 | + .from(workflowExecutionLogs) |
| 50 | + .innerJoin(workflow, eq(workflowExecutionLogs.workflowId, workflow.id)) |
| 51 | + .innerJoin( |
| 52 | + permissions, |
| 53 | + and( |
| 54 | + eq(permissions.entityType, 'workspace'), |
| 55 | + eq(permissions.entityId, workflow.workspaceId), |
| 56 | + eq(permissions.userId, userId) |
| 57 | + ) |
| 58 | + ) |
| 59 | + .where(eq(workflowExecutionLogs.id, id)) |
| 60 | + .limit(1) |
| 61 | + |
| 62 | + const log = rows[0] |
| 63 | + if (!log) { |
| 64 | + return NextResponse.json({ error: 'Not found' }, { status: 404 }) |
| 65 | + } |
| 66 | + |
| 67 | + const workflowSummary = { |
| 68 | + id: log.workflowId, |
| 69 | + name: log.workflowName, |
| 70 | + description: log.workflowDescription, |
| 71 | + color: log.workflowColor, |
| 72 | + folderId: log.workflowFolderId, |
| 73 | + userId: log.workflowUserId, |
| 74 | + workspaceId: log.workflowWorkspaceId, |
| 75 | + createdAt: log.workflowCreatedAt, |
| 76 | + updatedAt: log.workflowUpdatedAt, |
| 77 | + } |
| 78 | + |
| 79 | + const response = { |
| 80 | + id: log.id, |
| 81 | + workflowId: log.workflowId, |
| 82 | + executionId: log.executionId, |
| 83 | + level: log.level, |
| 84 | + duration: log.totalDurationMs ? `${log.totalDurationMs}ms` : null, |
| 85 | + trigger: log.trigger, |
| 86 | + createdAt: log.startedAt.toISOString(), |
| 87 | + files: log.files || undefined, |
| 88 | + workflow: workflowSummary, |
| 89 | + executionData: { |
| 90 | + totalDuration: log.totalDurationMs, |
| 91 | + ...(log.executionData as any), |
| 92 | + enhanced: true, |
| 93 | + }, |
| 94 | + cost: log.cost as any, |
| 95 | + } |
| 96 | + |
| 97 | + return NextResponse.json({ data: response }) |
| 98 | + } catch (error: any) { |
| 99 | + logger.error(`[${requestId}] log details fetch error`, error) |
| 100 | + return NextResponse.json({ error: error.message }, { status: 500 }) |
| 101 | + } |
| 102 | +} |
0 commit comments