Skip to content

Commit 07e2d89

Browse files
committed
cleanup
1 parent 23e1328 commit 07e2d89

1 file changed

Lines changed: 11 additions & 58 deletions

File tree

apps/sim/lib/workflows/utils.ts

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
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'
93
import type { InferSelectModel } from 'drizzle-orm'
104
import { and, eq } from 'drizzle-orm'
115
import { NextResponse } from 'next/server'
126
import { getSession } from '@/lib/auth'
13-
import { getEnv } from '@/lib/env'
147
import { createLogger } from '@/lib/logs/console/logger'
158
import type { PermissionType } from '@/lib/permissions/utils'
9+
import { getBaseUrl } from '@/lib/urls/utils'
1610
import type { ExecutionResult } from '@/executor/types'
1711
import type { WorkflowState } from '@/stores/workflows/workflow/types'
1812

@@ -178,60 +172,19 @@ export async function updateWorkflowRunCounts(workflowId: string, runs = 1) {
178172
throw new Error(`Workflow ${workflowId} not found`)
179173
}
180174

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+
})
194179

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')
230183
}
231184

232-
return { success: true, runsAdded: runs }
185+
return response.json()
233186
} catch (error) {
234-
logger.error('Error updating workflow run counts:', error)
187+
logger.error(`Error updating workflow stats for ${workflowId}`, error)
235188
throw error
236189
}
237190
}

0 commit comments

Comments
 (0)