fix(chat-subs): always use getBaseUrl helper to fetch base url (#1643)

* fix(chat-subs): always use next public app url env

* use getBaseUrl everywhere

* move remaining uses

* fix test

* change auth.ts and make getBaseUrl() call not top level for emails

* change remaining uses

* revert csp

* cleanup

* fix
This commit is contained in:
Vikhyath Mondreti
2025-10-15 14:13:23 -07:00
committed by GitHub
parent 4cceb22f21
commit eb4821ff30
46 changed files with 180 additions and 306 deletions

View File

@@ -1,18 +1,12 @@
import { db } from '@sim/db'
import {
apiKey,
permissions,
userStats,
workflow as workflowTable,
workspace,
} from '@sim/db/schema'
import { apiKey, permissions, workflow as workflowTable, workspace } from '@sim/db/schema'
import type { InferSelectModel } from 'drizzle-orm'
import { and, eq } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { getSession } from '@/lib/auth'
import { getEnv } from '@/lib/env'
import { createLogger } from '@/lib/logs/console/logger'
import type { PermissionType } from '@/lib/permissions/utils'
import { getBaseUrl } from '@/lib/urls/utils'
import type { ExecutionResult } from '@/executor/types'
import type { WorkflowState } from '@/stores/workflows/workflow/types'
@@ -178,61 +172,19 @@ export async function updateWorkflowRunCounts(workflowId: string, runs = 1) {
throw new Error(`Workflow ${workflowId} not found`)
}
// Get the origin from the environment or use direct DB update as fallback
const origin =
getEnv('NEXT_PUBLIC_APP_URL') || (typeof window !== 'undefined' ? window.location.origin : '')
// Use the API to update stats
const response = await fetch(`${getBaseUrl()}/api/workflows/${workflowId}/stats?runs=${runs}`, {
method: 'POST',
})
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()
}
logger.warn('No origin available, updating workflow stats directly via DB')
// Update workflow directly through database
await db
.update(workflowTable)
.set({
runCount: (workflow.runCount as number) + 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) {
console.warn('User stats record not found - should be created during onboarding', {
userId: workflow.userId,
})
return // Skip stats update if record doesn't exist
}
// Update existing record
await db
.update(userStats)
.set({
totalManualExecutions: userStatsRecord[0].totalManualExecutions + runs,
lastActive: new Date(),
})
.where(eq(userStats.userId, workflow.userId))
if (!response.ok) {
const error = await response.json()
throw new Error(error.error || 'Failed to update workflow stats')
}
return { success: true, runsAdded: runs }
return response.json()
} catch (error) {
logger.error('Error updating workflow run counts:', error)
logger.error(`Error updating workflow stats for ${workflowId}`, error)
throw error
}
}