Skip to content

Commit 0d881ec

Browse files
fix(deployed-version-check): check deployed version existence pre-queuing (#1508)
* fix(deployed-version-check): check deployed version existence pre-queuing * fix tests * fix edge case
1 parent 7e6a5dc commit 0d881ec

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

apps/sim/app/api/webhooks/trigger/[path]/route.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ describe('Webhook Trigger API Route', () => {
133133
parallels: {},
134134
isFromNormalizedTables: true,
135135
}),
136+
blockExistsInDeployment: vi.fn().mockResolvedValue(true),
136137
}))
137138

138139
hasProcessedMessageMock.mockResolvedValue(false)

apps/sim/app/api/webhooks/trigger/[path]/route.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
queueWebhookExecution,
1111
verifyProviderAuth,
1212
} from '@/lib/webhooks/processor'
13+
import { blockExistsInDeployment } from '@/lib/workflows/db-helpers'
1314

1415
const logger = createLogger('WebhookTriggerAPI')
1516

@@ -62,6 +63,16 @@ export async function POST(
6263
return usageLimitError
6364
}
6465

66+
if (foundWebhook.blockId) {
67+
const blockExists = await blockExistsInDeployment(foundWorkflow.id, foundWebhook.blockId)
68+
if (!blockExists) {
69+
logger.warn(
70+
`[${requestId}] Trigger block ${foundWebhook.blockId} not found in deployment for workflow ${foundWorkflow.id}`
71+
)
72+
return new NextResponse('Trigger block not deployed', { status: 404 })
73+
}
74+
}
75+
6576
return queueWebhookExecution(foundWebhook, foundWorkflow, body, request, {
6677
requestId,
6778
path,

apps/sim/lib/workflows/db-helpers.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,34 @@ export interface NormalizedWorkflowData {
3636
isFromNormalizedTables: boolean // Flag to indicate source (true = normalized tables, false = deployed state)
3737
}
3838

39-
/**
40-
* Load deployed workflow state for execution
41-
* Returns deployed state if available, otherwise throws error
42-
*/
39+
export async function blockExistsInDeployment(
40+
workflowId: string,
41+
blockId: string
42+
): Promise<boolean> {
43+
try {
44+
const [result] = await db
45+
.select({ state: workflowDeploymentVersion.state })
46+
.from(workflowDeploymentVersion)
47+
.where(
48+
and(
49+
eq(workflowDeploymentVersion.workflowId, workflowId),
50+
eq(workflowDeploymentVersion.isActive, true)
51+
)
52+
)
53+
.limit(1)
54+
55+
if (!result?.state) {
56+
return false
57+
}
58+
59+
const state = result.state as WorkflowState
60+
return !!state.blocks?.[blockId]
61+
} catch (error) {
62+
logger.error(`Error checking block ${blockId} in deployment for workflow ${workflowId}:`, error)
63+
return false
64+
}
65+
}
66+
4367
export async function loadDeployedWorkflowState(
4468
workflowId: string
4569
): Promise<NormalizedWorkflowData> {

0 commit comments

Comments
 (0)