Skip to content

Commit f88451b

Browse files
committed
fix credential sets with new lifecycle
1 parent f40a68a commit f88451b

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

apps/sim/lib/webhooks/deploy.ts

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,25 @@ export async function saveTriggerWebhooksForDeploy({
403403
const currentBlockIds = new Set(triggerBlocks.map((b) => b.id))
404404

405405
// 1. Get all existing webhooks for this workflow
406-
const existingWebhooks = await db.select().from(webhook).where(eq(webhook.workflowId, workflowId))
406+
const existingWebhooks = await db
407+
.select()
408+
.from(webhook)
409+
.where(
410+
deploymentVersionId
411+
? and(
412+
eq(webhook.workflowId, workflowId),
413+
eq(webhook.deploymentVersionId, deploymentVersionId)
414+
)
415+
: eq(webhook.workflowId, workflowId)
416+
)
407417

408-
const webhooksByBlockId = new Map(
409-
existingWebhooks.filter((wh) => wh.blockId).map((wh) => [wh.blockId!, wh])
410-
)
418+
const webhooksByBlockId = new Map<string, typeof existingWebhooks>()
419+
for (const wh of existingWebhooks) {
420+
if (!wh.blockId) continue
421+
const existingForBlock = webhooksByBlockId.get(wh.blockId) ?? []
422+
existingForBlock.push(wh)
423+
webhooksByBlockId.set(wh.blockId, existingForBlock)
424+
}
411425

412426
logger.info(`[${requestId}] Starting webhook sync`, {
413427
workflowId,
@@ -418,6 +432,7 @@ export async function saveTriggerWebhooksForDeploy({
418432
// 2. Determine which webhooks to delete (orphaned or config changed)
419433
const webhooksToDelete: typeof existingWebhooks = []
420434
const blocksNeedingWebhook: BlockState[] = []
435+
const blocksNeedingCredentialSetSync: BlockState[] = []
421436

422437
for (const block of triggerBlocks) {
423438
const triggerId = resolveTriggerId(block)
@@ -444,11 +459,24 @@ export async function saveTriggerWebhooksForDeploy({
444459

445460
;(block as any)._webhookConfig = { provider, providerConfig, triggerPath, triggerDef }
446461

447-
const existingWh = webhooksByBlockId.get(block.id)
448-
if (!existingWh) {
462+
if (providerConfig.credentialSetId) {
463+
blocksNeedingCredentialSetSync.push(block)
464+
continue
465+
}
466+
467+
const existingForBlock = webhooksByBlockId.get(block.id) ?? []
468+
if (existingForBlock.length === 0) {
449469
// No existing webhook - needs creation
450470
blocksNeedingWebhook.push(block)
451471
} else {
472+
const [existingWh, ...extraWebhooks] = existingForBlock
473+
if (extraWebhooks.length > 0) {
474+
webhooksToDelete.push(...extraWebhooks)
475+
logger.info(
476+
`[${requestId}] Found ${extraWebhooks.length} extra webhook(s) for block ${block.id}`
477+
)
478+
}
479+
452480
// Check if config changed
453481
const existingConfig = (existingWh.providerConfig as Record<string, unknown>) || {}
454482
if (
@@ -494,15 +522,14 @@ export async function saveTriggerWebhooksForDeploy({
494522
await db.delete(webhook).where(inArray(webhook.id, idsToDelete))
495523
}
496524

497-
// 4. Create webhooks for blocks that need them
498-
for (const block of blocksNeedingWebhook) {
525+
// 4. Sync credential set webhooks
526+
for (const block of blocksNeedingCredentialSetSync) {
499527
const config = (block as any)._webhookConfig
500528
if (!config) continue
501529

502530
const { provider, providerConfig, triggerPath } = config
503531

504532
try {
505-
// Handle credential sets
506533
const credentialSetError = await syncCredentialSetWebhooks({
507534
workflowId,
508535
blockId: block.id,
@@ -516,11 +543,26 @@ export async function saveTriggerWebhooksForDeploy({
516543
if (credentialSetError) {
517544
return { success: false, error: credentialSetError }
518545
}
519-
520-
if (providerConfig.credentialSetId) {
521-
continue
546+
} catch (error: any) {
547+
logger.error(`[${requestId}] Failed to create webhook for ${block.id}`, error)
548+
return {
549+
success: false,
550+
error: {
551+
message: error?.message || 'Failed to save trigger configuration',
552+
status: 500,
553+
},
522554
}
555+
}
556+
}
523557

558+
// 5. Create webhooks for blocks that need them
559+
for (const block of blocksNeedingWebhook) {
560+
const config = (block as any)._webhookConfig
561+
if (!config) continue
562+
563+
const { provider, providerConfig, triggerPath } = config
564+
565+
try {
524566
const createError = await createWebhookForBlock({
525567
request,
526568
workflowId,
@@ -554,13 +596,6 @@ export async function saveTriggerWebhooksForDeploy({
554596
;(block as any)._webhookConfig = undefined
555597
}
556598

557-
if (deploymentVersionId) {
558-
await db
559-
.update(webhook)
560-
.set({ deploymentVersionId, updatedAt: new Date() })
561-
.where(eq(webhook.workflowId, workflowId))
562-
}
563-
564599
return { success: true }
565600
}
566601

apps/sim/lib/webhooks/utils.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,7 @@ export async function syncAllWebhooksForCredentialSet(
22092209
providerConfig: baseConfig,
22102210
requestId,
22112211
tx: dbCtx,
2212+
deploymentVersionId: representativeWebhook.deploymentVersionId || undefined,
22122213
})
22132214

22142215
workflowsUpdated++

0 commit comments

Comments
 (0)