diff --git a/.gitignore b/.gitignore index 21aa94870a..4be9817837 100644 --- a/.gitignore +++ b/.gitignore @@ -11,8 +11,6 @@ docs/node_modules !.yarn/plugins !.yarn/releases !.yarn/versions -package.json -package-lock.json # testing /coverage diff --git a/sim/lib/workflows/utils.ts b/sim/lib/workflows/utils.ts index c4b845d7ff..f849c126f3 100644 --- a/sim/lib/workflows/utils.ts +++ b/sim/lib/workflows/utils.ts @@ -18,16 +18,70 @@ export async function updateWorkflowRunCounts(workflowId: string, runs: number = throw new Error(`Workflow ${workflowId} not found`) } - const response = await fetch(`/api/workflows/${workflowId}/stats?runs=${runs}`, { - method: 'POST', - }) + // Get the origin from the environment or use direct DB update as fallback + const origin = + process.env.NEXT_PUBLIC_APP_URL || + (typeof window !== 'undefined' ? window.location.origin : '') - if (!response.ok) { - const error = await response.json() - throw new Error(error.error || 'Failed to update workflow stats') + if (origin) { + // Use absolute URL with origin + const response = await fetch(`${origin}/api/workflows/${workflowId}/stats?runs=${runs}`, { + method: 'POST', + }) + + if (!response.ok) { + const error = await response.json() + throw new Error(error.error || 'Failed to update workflow stats') + } + + return response.json() + } else { + logger.warn(`No origin available, updating workflow stats directly via DB`) + + // Update workflow directly through database + await db + .update(workflowTable) + .set({ + runCount: workflow.runCount + runs, + lastRunAt: new Date(), + }) + .where(eq(workflowTable.id, workflowId)) + + // Update user stats if needed + if (workflow.userId) { + const userStatsRecord = await db + .select() + .from(userStats) + .where(eq(userStats.userId, workflow.userId)) + .limit(1) + + if (userStatsRecord.length === 0) { + // Create new record + await db.insert(userStats).values({ + id: crypto.randomUUID(), + userId: workflow.userId, + totalManualExecutions: runs, + totalApiCalls: 0, + totalWebhookTriggers: 0, + totalScheduledExecutions: 0, + totalTokensUsed: 0, + totalCost: '0.00', + lastActive: new Date(), + }) + } else { + // Update existing record + await db + .update(userStats) + .set({ + totalManualExecutions: userStatsRecord[0].totalManualExecutions + runs, + lastActive: new Date(), + }) + .where(eq(userStats.userId, workflow.userId)) + } + } + + return { success: true, runsAdded: runs } } - - return response.json() } catch (error) { logger.error(`Error updating workflow run counts:`, error) throw error