Skip to content

Commit 46740c9

Browse files
author
aadamgough
committed
fire only for specific trigger type
1 parent 12617f6 commit 46740c9

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ export async function POST(request: NextRequest) {
744744
if (savedWebhook && provider === 'grain') {
745745
logger.info(`[${requestId}] Grain provider detected. Creating Grain webhook subscription.`)
746746
try {
747-
const grainHookId = await createGrainWebhookSubscription(
747+
const grainResult = await createGrainWebhookSubscription(
748748
request,
749749
{
750750
id: savedWebhook.id,
@@ -754,11 +754,12 @@ export async function POST(request: NextRequest) {
754754
requestId
755755
)
756756

757-
if (grainHookId) {
758-
// Update the webhook record with the external Grain hook ID
757+
if (grainResult) {
758+
// Update the webhook record with the external Grain hook ID and event types for filtering
759759
const updatedConfig = {
760760
...(savedWebhook.providerConfig as Record<string, any>),
761-
externalId: grainHookId,
761+
externalId: grainResult.id,
762+
eventTypes: grainResult.eventTypes,
762763
}
763764
await db
764765
.update(webhook)
@@ -770,7 +771,8 @@ export async function POST(request: NextRequest) {
770771

771772
savedWebhook.providerConfig = updatedConfig
772773
logger.info(`[${requestId}] Successfully created Grain webhook`, {
773-
grainHookId,
774+
grainHookId: grainResult.id,
775+
eventTypes: grainResult.eventTypes,
774776
webhookId: savedWebhook.id,
775777
})
776778
}
@@ -1176,7 +1178,7 @@ async function createGrainWebhookSubscription(
11761178
request: NextRequest,
11771179
webhookData: any,
11781180
requestId: string
1179-
): Promise<string | undefined> {
1181+
): Promise<{ id: string; eventTypes: string[] } | undefined> {
11801182
try {
11811183
const { path, providerConfig } = webhookData
11821184
const { apiKey, triggerId, includeHighlights, includeParticipants, includeAiSummary } =
@@ -1191,6 +1193,7 @@ async function createGrainWebhookSubscription(
11911193
)
11921194
}
11931195

1196+
// Map trigger IDs to Grain API hook_type (only 2 options: recording_added, upload_status)
11941197
const hookTypeMap: Record<string, string> = {
11951198
grain_webhook: 'recording_added',
11961199
grain_recording_created: 'recording_added',
@@ -1201,7 +1204,20 @@ async function createGrainWebhookSubscription(
12011204
grain_upload_status: 'upload_status',
12021205
}
12031206

1207+
// Map trigger IDs to expected payload event types (for filtering incoming webhooks)
1208+
const eventTypeMap: Record<string, string[]> = {
1209+
grain_webhook: [],
1210+
grain_recording_created: ['recording_added'],
1211+
grain_recording_updated: ['recording_updated'],
1212+
grain_highlight_created: ['highlight_created'],
1213+
grain_highlight_updated: ['highlight_updated'],
1214+
grain_story_created: ['story_created'],
1215+
grain_upload_status: ['upload_status'],
1216+
}
1217+
12041218
const hookType = hookTypeMap[triggerId] ?? 'recording_added'
1219+
const eventTypes = eventTypeMap[triggerId] ?? []
1220+
12051221
if (!hookTypeMap[triggerId]) {
12061222
logger.warn(
12071223
`[${requestId}] Unknown triggerId for Grain: ${triggerId}, defaulting to recording_added`,
@@ -1211,8 +1227,10 @@ async function createGrainWebhookSubscription(
12111227
)
12121228
}
12131229

1214-
logger.info(`[${requestId}] Creating Grain webhook with hook_type: ${hookType}`, {
1230+
logger.info(`[${requestId}] Creating Grain webhook`, {
12151231
triggerId,
1232+
hookType,
1233+
eventTypes,
12161234
webhookId: webhookData.id,
12171235
})
12181236

@@ -1283,10 +1301,11 @@ async function createGrainWebhookSubscription(
12831301
`[${requestId}] Successfully created webhook in Grain for webhook ${webhookData.id}.`,
12841302
{
12851303
grainWebhookId: responseBody.id,
1304+
eventTypes,
12861305
}
12871306
)
12881307

1289-
return responseBody.id
1308+
return { id: responseBody.id, eventTypes }
12901309
} catch (error: any) {
12911310
logger.error(
12921311
`[${requestId}] Exception during Grain webhook creation for webhook ${webhookData.id}.`,

apps/sim/lib/webhooks/processor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,20 @@ export function shouldSkipWebhookEvent(webhook: any, body: any, requestId: strin
239239
}
240240
}
241241

242+
// Grain event filtering - filter by payload type field (e.g., recording_added, highlight_created)
243+
if (webhook.provider === 'grain') {
244+
const eventTypes = providerConfig.eventTypes
245+
if (eventTypes && Array.isArray(eventTypes) && eventTypes.length > 0) {
246+
const eventType = body?.type
247+
if (eventType && !eventTypes.includes(eventType)) {
248+
logger.info(
249+
`[${requestId}] Grain event type '${eventType}' not in allowed list for webhook ${webhook.id}, skipping`
250+
)
251+
return true
252+
}
253+
}
254+
}
255+
242256
return false
243257
}
244258

0 commit comments

Comments
 (0)