Skip to content

Commit 7dd25d8

Browse files
committed
improvement(workflow): seed start block on server side
1 parent 076c835 commit 7dd25d8

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

apps/sim/app/api/workflows/route.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
88
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
99
import { generateRequestId } from '@/lib/core/utils/request'
1010
import { getNextWorkflowColor } from '@/lib/workflows/colors'
11+
import { buildDefaultWorkflowArtifacts } from '@/lib/workflows/defaults'
12+
import { saveWorkflowToNormalizedTables } from '@/lib/workflows/persistence/utils'
1113
import { deduplicateWorkflowName, listWorkflows, type WorkflowScope } from '@/lib/workflows/utils'
1214
import { getUserEntityPermissions, workspaceExists } from '@/lib/workspaces/permissions/utils'
1315
import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
@@ -247,6 +249,8 @@ export async function POST(req: NextRequest) {
247249
// Silently fail
248250
})
249251

252+
const { workflowState, subBlockValues, startBlockId } = buildDefaultWorkflowArtifacts()
253+
250254
await db.insert(workflow).values({
251255
id: workflowId,
252256
userId,
@@ -264,7 +268,14 @@ export async function POST(req: NextRequest) {
264268
variables: {},
265269
})
266270

267-
logger.info(`[${requestId}] Successfully created empty workflow ${workflowId}`)
271+
const saveResult = await saveWorkflowToNormalizedTables(workflowId, workflowState)
272+
if (!saveResult.success) {
273+
logger.error(
274+
`[${requestId}] Failed to persist default blocks for workflow ${workflowId}: ${saveResult.error}`
275+
)
276+
}
277+
278+
logger.info(`[${requestId}] Successfully created workflow ${workflowId} with default blocks`)
268279

269280
recordAudit({
270281
workspaceId,
@@ -290,6 +301,8 @@ export async function POST(req: NextRequest) {
290301
sortOrder,
291302
createdAt: now,
292303
updatedAt: now,
304+
startBlockId,
305+
subBlockValues,
293306
})
294307
} catch (error) {
295308
if (error instanceof z.ZodError) {

apps/sim/hooks/queries/workflows.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
useQueryClient,
1212
} from '@tanstack/react-query'
1313
import { getNextWorkflowColor } from '@/lib/workflows/colors'
14-
import { buildDefaultWorkflowArtifacts } from '@/lib/workflows/defaults'
1514
import { deploymentKeys } from '@/hooks/queries/deployments'
1615
import { fetchDeploymentVersionState } from '@/hooks/queries/utils/fetch-deployment-version-state'
1716
import { getFolderMap } from '@/hooks/queries/utils/folder-cache'
@@ -105,6 +104,8 @@ interface CreateWorkflowResult {
105104
workspaceId: string
106105
folderId?: string | null
107106
sortOrder: number
107+
startBlockId?: string
108+
subBlockValues?: Record<string, Record<string, unknown>>
108109
}
109110

110111
export function useCreateWorkflow() {
@@ -144,19 +145,6 @@ export function useCreateWorkflow() {
144145

145146
logger.info(`Successfully created workflow ${workflowId}`)
146147

147-
const { workflowState } = buildDefaultWorkflowArtifacts()
148-
149-
const stateResponse = await fetch(`/api/workflows/${workflowId}/state`, {
150-
method: 'PUT',
151-
headers: { 'Content-Type': 'application/json' },
152-
body: JSON.stringify(workflowState),
153-
})
154-
155-
if (!stateResponse.ok) {
156-
const text = await stateResponse.text()
157-
logger.error('Failed to persist default workflow state:', text)
158-
}
159-
160148
return {
161149
id: workflowId,
162150
name: createdWorkflow.name,
@@ -165,6 +153,8 @@ export function useCreateWorkflow() {
165153
workspaceId,
166154
folderId: createdWorkflow.folderId,
167155
sortOrder: createdWorkflow.sortOrder ?? 0,
156+
startBlockId: createdWorkflow.startBlockId,
157+
subBlockValues: createdWorkflow.subBlockValues,
168158
}
169159
},
170160
onMutate: async (variables) => {
@@ -247,13 +237,14 @@ export function useCreateWorkflow() {
247237
})
248238
}
249239

250-
const { subBlockValues } = buildDefaultWorkflowArtifacts()
251-
useSubBlockStore.setState((state) => ({
252-
workflowValues: {
253-
...state.workflowValues,
254-
[data.id]: subBlockValues,
255-
},
256-
}))
240+
if (data.subBlockValues) {
241+
useSubBlockStore.setState((state) => ({
242+
workflowValues: {
243+
...state.workflowValues,
244+
[data.id]: data.subBlockValues!,
245+
},
246+
}))
247+
}
257248

258249
logger.info(`[CreateWorkflow] Success, replaced temp entry ${tempId}`)
259250
},

0 commit comments

Comments
 (0)