|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | +import { eq, sql } from 'drizzle-orm' |
| 3 | +import { createLogger } from '@/lib/logs/console-logger' |
| 4 | +import { db } from '@/db' |
| 5 | +import { userStats, workflow } from '@/db/schema' |
| 6 | + |
| 7 | +const logger = createLogger('WorkflowStatsAPI') |
| 8 | + |
| 9 | +export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { |
| 10 | + const { id } = await params |
| 11 | + const searchParams = request.nextUrl.searchParams |
| 12 | + const runs = parseInt(searchParams.get('runs') || '1', 10) |
| 13 | + |
| 14 | + if (isNaN(runs) || runs < 1 || runs > 100) { |
| 15 | + logger.error(`Invalid number of runs: ${runs}`) |
| 16 | + return NextResponse.json( |
| 17 | + { error: 'Invalid number of runs. Must be between 1 and 100.' }, |
| 18 | + { status: 400 } |
| 19 | + ) |
| 20 | + } |
| 21 | + |
| 22 | + try { |
| 23 | + // Get workflow record |
| 24 | + const [workflowRecord] = await db.select().from(workflow).where(eq(workflow.id, id)).limit(1) |
| 25 | + |
| 26 | + if (!workflowRecord) { |
| 27 | + return NextResponse.json({ error: `Workflow ${id} not found` }, { status: 404 }) |
| 28 | + } |
| 29 | + |
| 30 | + // Update workflow runCount |
| 31 | + try { |
| 32 | + await db |
| 33 | + .update(workflow) |
| 34 | + .set({ |
| 35 | + runCount: workflowRecord.runCount + runs, |
| 36 | + lastRunAt: new Date(), |
| 37 | + }) |
| 38 | + .where(eq(workflow.id, id)) |
| 39 | + } catch (error) { |
| 40 | + logger.error('Error updating workflow runCount:', error) |
| 41 | + throw error |
| 42 | + } |
| 43 | + |
| 44 | + // Upsert user stats record |
| 45 | + try { |
| 46 | + // Check if record exists |
| 47 | + const userStatsRecords = await db |
| 48 | + .select() |
| 49 | + .from(userStats) |
| 50 | + .where(eq(userStats.userId, workflowRecord.userId)) |
| 51 | + |
| 52 | + if (userStatsRecords.length === 0) { |
| 53 | + // Create new record if none exists |
| 54 | + await db.insert(userStats).values({ |
| 55 | + id: crypto.randomUUID(), |
| 56 | + userId: workflowRecord.userId, |
| 57 | + totalManualExecutions: runs, |
| 58 | + totalApiCalls: 0, |
| 59 | + totalWebhookTriggers: 0, |
| 60 | + totalScheduledExecutions: 0, |
| 61 | + totalTokensUsed: 0, |
| 62 | + totalCost: '0.00', |
| 63 | + lastActive: new Date(), |
| 64 | + }) |
| 65 | + } else { |
| 66 | + // Update existing record |
| 67 | + await db |
| 68 | + .update(userStats) |
| 69 | + .set({ |
| 70 | + totalManualExecutions: sql`total_manual_executions + ${runs}`, |
| 71 | + lastActive: new Date(), |
| 72 | + }) |
| 73 | + .where(eq(userStats.userId, workflowRecord.userId)) |
| 74 | + } |
| 75 | + } catch (error) { |
| 76 | + logger.error(`Error upserting userStats for userId ${workflowRecord.userId}:`, error) |
| 77 | + // Don't rethrow - we want to continue even if this fails |
| 78 | + } |
| 79 | + |
| 80 | + return NextResponse.json({ |
| 81 | + success: true, |
| 82 | + runsAdded: runs, |
| 83 | + newTotal: workflowRecord.runCount + runs, |
| 84 | + }) |
| 85 | + } catch (error) { |
| 86 | + logger.error('Error updating workflow stats:', error) |
| 87 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 88 | + } |
| 89 | +} |
0 commit comments