|
1 | 1 | import { db } from '@sim/db' |
2 | | -import { |
3 | | - apiKey, |
4 | | - permissions, |
5 | | - userStats, |
6 | | - workflow as workflowTable, |
7 | | - workspace, |
8 | | -} from '@sim/db/schema' |
| 2 | +import { apiKey, permissions, workflow as workflowTable, workspace } from '@sim/db/schema' |
9 | 3 | import type { InferSelectModel } from 'drizzle-orm' |
10 | 4 | import { and, eq } from 'drizzle-orm' |
11 | 5 | import { NextResponse } from 'next/server' |
12 | 6 | import { getSession } from '@/lib/auth' |
13 | | -import { getEnv } from '@/lib/env' |
14 | 7 | import { createLogger } from '@/lib/logs/console/logger' |
15 | 8 | import type { PermissionType } from '@/lib/permissions/utils' |
| 9 | +import { getBaseUrl } from '@/lib/urls/utils' |
16 | 10 | import type { ExecutionResult } from '@/executor/types' |
17 | 11 | import type { WorkflowState } from '@/stores/workflows/workflow/types' |
18 | 12 |
|
@@ -178,60 +172,19 @@ export async function updateWorkflowRunCounts(workflowId: string, runs = 1) { |
178 | 172 | throw new Error(`Workflow ${workflowId} not found`) |
179 | 173 | } |
180 | 174 |
|
181 | | - // Get the origin from the environment |
182 | | - const origin = getEnv('NEXT_PUBLIC_APP_URL') |
183 | | - |
184 | | - if (origin) { |
185 | | - // Use absolute URL with origin |
186 | | - const response = await fetch(`${origin}/api/workflows/${workflowId}/stats?runs=${runs}`, { |
187 | | - method: 'POST', |
188 | | - }) |
189 | | - |
190 | | - if (!response.ok) { |
191 | | - const error = await response.json() |
192 | | - throw new Error(error.error || 'Failed to update workflow stats') |
193 | | - } |
| 175 | + // Use the API to update stats |
| 176 | + const response = await fetch(`${getBaseUrl()}/api/workflows/${workflowId}/stats?runs=${runs}`, { |
| 177 | + method: 'POST', |
| 178 | + }) |
194 | 179 |
|
195 | | - return response.json() |
196 | | - } |
197 | | - logger.warn('No origin available, updating workflow stats directly via DB') |
198 | | - |
199 | | - // Update workflow directly through database |
200 | | - await db |
201 | | - .update(workflowTable) |
202 | | - .set({ |
203 | | - runCount: (workflow.runCount as number) + runs, |
204 | | - lastRunAt: new Date(), |
205 | | - }) |
206 | | - .where(eq(workflowTable.id, workflowId)) |
207 | | - |
208 | | - // Update user stats if needed |
209 | | - if (workflow.userId) { |
210 | | - const userStatsRecord = await db |
211 | | - .select() |
212 | | - .from(userStats) |
213 | | - .where(eq(userStats.userId, workflow.userId)) |
214 | | - .limit(1) |
215 | | - |
216 | | - if (userStatsRecord.length === 0) { |
217 | | - console.warn('User stats record not found - should be created during onboarding', { |
218 | | - userId: workflow.userId, |
219 | | - }) |
220 | | - return // Skip stats update if record doesn't exist |
221 | | - } |
222 | | - // Update existing record |
223 | | - await db |
224 | | - .update(userStats) |
225 | | - .set({ |
226 | | - totalManualExecutions: userStatsRecord[0].totalManualExecutions + runs, |
227 | | - lastActive: new Date(), |
228 | | - }) |
229 | | - .where(eq(userStats.userId, workflow.userId)) |
| 180 | + if (!response.ok) { |
| 181 | + const error = await response.json() |
| 182 | + throw new Error(error.error || 'Failed to update workflow stats') |
230 | 183 | } |
231 | 184 |
|
232 | | - return { success: true, runsAdded: runs } |
| 185 | + return response.json() |
233 | 186 | } catch (error) { |
234 | | - logger.error('Error updating workflow run counts:', error) |
| 187 | + logger.error(`Error updating workflow stats for ${workflowId}`, error) |
235 | 188 | throw error |
236 | 189 | } |
237 | 190 | } |
|
0 commit comments