|
| 1 | +import { type NextRequest, NextResponse } from 'next/server' |
| 2 | +import { z } from 'zod' |
| 3 | +import { checkHybridAuth } from '@/lib/auth/hybrid' |
| 4 | +import { getUserUsageLogs, type UsageLogSource } from '@/lib/billing/core/usage-log' |
| 5 | +import { createLogger } from '@/lib/logs/console/logger' |
| 6 | + |
| 7 | +const logger = createLogger('UsageLogsAPI') |
| 8 | + |
| 9 | +const QuerySchema = z.object({ |
| 10 | + source: z.enum(['workflow', 'wand', 'copilot']).optional(), |
| 11 | + workspaceId: z.string().optional(), |
| 12 | + period: z.enum(['1d', '7d', '30d', 'all']).optional().default('30d'), |
| 13 | + limit: z.coerce.number().min(1).max(100).optional().default(50), |
| 14 | + cursor: z.string().optional(), |
| 15 | +}) |
| 16 | + |
| 17 | +/** |
| 18 | + * GET /api/users/me/usage-logs |
| 19 | + * Get usage logs for the authenticated user |
| 20 | + */ |
| 21 | +export async function GET(req: NextRequest) { |
| 22 | + try { |
| 23 | + const auth = await checkHybridAuth(req, { requireWorkflowId: false }) |
| 24 | + |
| 25 | + if (!auth.success || !auth.userId) { |
| 26 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 27 | + } |
| 28 | + |
| 29 | + const userId = auth.userId |
| 30 | + |
| 31 | + const { searchParams } = new URL(req.url) |
| 32 | + const queryParams = { |
| 33 | + source: searchParams.get('source') || undefined, |
| 34 | + workspaceId: searchParams.get('workspaceId') || undefined, |
| 35 | + period: searchParams.get('period') || '30d', |
| 36 | + limit: searchParams.get('limit') || '50', |
| 37 | + cursor: searchParams.get('cursor') || undefined, |
| 38 | + } |
| 39 | + |
| 40 | + const validation = QuerySchema.safeParse(queryParams) |
| 41 | + |
| 42 | + if (!validation.success) { |
| 43 | + return NextResponse.json( |
| 44 | + { |
| 45 | + error: 'Invalid query parameters', |
| 46 | + details: validation.error.issues, |
| 47 | + }, |
| 48 | + { status: 400 } |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + const { source, workspaceId, period, limit, cursor } = validation.data |
| 53 | + |
| 54 | + let startDate: Date | undefined |
| 55 | + const endDate = new Date() |
| 56 | + |
| 57 | + if (period !== 'all') { |
| 58 | + startDate = new Date() |
| 59 | + switch (period) { |
| 60 | + case '1d': |
| 61 | + startDate.setDate(startDate.getDate() - 1) |
| 62 | + break |
| 63 | + case '7d': |
| 64 | + startDate.setDate(startDate.getDate() - 7) |
| 65 | + break |
| 66 | + case '30d': |
| 67 | + startDate.setDate(startDate.getDate() - 30) |
| 68 | + break |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const result = await getUserUsageLogs(userId, { |
| 73 | + source: source as UsageLogSource | undefined, |
| 74 | + workspaceId, |
| 75 | + startDate, |
| 76 | + endDate, |
| 77 | + limit, |
| 78 | + cursor, |
| 79 | + }) |
| 80 | + |
| 81 | + logger.debug('Retrieved usage logs', { |
| 82 | + userId, |
| 83 | + source, |
| 84 | + period, |
| 85 | + logCount: result.logs.length, |
| 86 | + hasMore: result.pagination.hasMore, |
| 87 | + }) |
| 88 | + |
| 89 | + return NextResponse.json({ |
| 90 | + success: true, |
| 91 | + ...result, |
| 92 | + }) |
| 93 | + } catch (error) { |
| 94 | + logger.error('Failed to get usage logs', { |
| 95 | + error: error instanceof Error ? error.message : String(error), |
| 96 | + }) |
| 97 | + |
| 98 | + return NextResponse.json( |
| 99 | + { |
| 100 | + error: 'Failed to retrieve usage logs', |
| 101 | + }, |
| 102 | + { status: 500 } |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
0 commit comments