Skip to content

Commit 71bee4c

Browse files
committed
fix restoration edge case
1 parent fef652e commit 71bee4c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

apps/sim/lib/webhooks/deploy.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,43 @@ export async function saveTriggerWebhooksForDeploy({
371371
}
372372
}
373373

374+
const restorePreviousSubscriptions = async () => {
375+
if (!previousVersionId) return
376+
377+
const previousWebhooks = await db
378+
.select()
379+
.from(webhook)
380+
.where(eq(webhook.deploymentVersionId, previousVersionId))
381+
382+
if (previousWebhooks.length === 0) return
383+
384+
logger.info(
385+
`[${requestId}] Attempting to restore ${previousWebhooks.length} external subscription(s) for previous version`
386+
)
387+
for (const wh of previousWebhooks) {
388+
try {
389+
await createExternalWebhookSubscription(
390+
request,
391+
{
392+
id: wh.id,
393+
path: wh.path,
394+
provider: wh.provider,
395+
providerConfig: (wh.providerConfig as Record<string, unknown>) || {},
396+
},
397+
workflow,
398+
userId,
399+
requestId
400+
)
401+
logger.info(`[${requestId}] Restored external subscription for webhook ${wh.id}`)
402+
} catch (restoreError) {
403+
logger.error(
404+
`[${requestId}] Failed to restore external subscription for webhook ${wh.id}`,
405+
restoreError
406+
)
407+
}
408+
}
409+
}
410+
374411
const webhooksByBlockId = new Map<string, typeof existingWebhooks>()
375412
for (const wh of existingWebhooks) {
376413
if (!wh.blockId) continue
@@ -403,6 +440,7 @@ export async function saveTriggerWebhooksForDeploy({
403440
)
404441

405442
if (missingFields.length > 0) {
443+
await restorePreviousSubscriptions()
406444
return {
407445
success: false,
408446
error: {
@@ -504,10 +542,12 @@ export async function saveTriggerWebhooksForDeploy({
504542
})
505543

506544
if (credentialSetError) {
545+
await restorePreviousSubscriptions()
507546
return { success: false, error: credentialSetError }
508547
}
509548
} catch (error: any) {
510549
logger.error(`[${requestId}] Failed to create webhook for ${block.id}`, error)
550+
await restorePreviousSubscriptions()
511551
return {
512552
success: false,
513553
error: {
@@ -585,6 +625,7 @@ export async function saveTriggerWebhooksForDeploy({
585625
for (const block of triggerBlocks) {
586626
;(block as any)._webhookConfig = undefined
587627
}
628+
await restorePreviousSubscriptions()
588629
return {
589630
success: false,
590631
error: {
@@ -630,6 +671,7 @@ export async function saveTriggerWebhooksForDeploy({
630671
for (const block of triggerBlocks) {
631672
;(block as any)._webhookConfig = undefined
632673
}
674+
await restorePreviousSubscriptions()
633675
return { success: false, error: pollingError }
634676
}
635677
}
@@ -659,6 +701,7 @@ export async function saveTriggerWebhooksForDeploy({
659701
for (const block of triggerBlocks) {
660702
;(block as any)._webhookConfig = undefined
661703
}
704+
await restorePreviousSubscriptions()
662705
return {
663706
success: false,
664707
error: {

0 commit comments

Comments
 (0)