@@ -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,10 +1178,10 @@ 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
1182- const { apiKey, includeHighlights, includeParticipants, includeAiSummary } =
1184+ const { apiKey, triggerId , includeHighlights, includeParticipants, includeAiSummary } =
11831185 providerConfig || { }
11841186
11851187 if ( ! apiKey ) {
@@ -1191,12 +1193,53 @@ async function createGrainWebhookSubscription(
11911193 )
11921194 }
11931195
1196+ // Map trigger IDs to Grain API hook_type (only 2 options: recording_added, upload_status)
1197+ const hookTypeMap : Record < string , string > = {
1198+ grain_webhook : 'recording_added' ,
1199+ grain_recording_created : 'recording_added' ,
1200+ grain_recording_updated : 'recording_added' ,
1201+ grain_highlight_created : 'recording_added' ,
1202+ grain_highlight_updated : 'recording_added' ,
1203+ grain_story_created : 'recording_added' ,
1204+ grain_upload_status : 'upload_status' ,
1205+ }
1206+
1207+ const eventTypeMap : Record < string , string [ ] > = {
1208+ grain_webhook : [ ] ,
1209+ grain_recording_created : [ 'recording_added' ] ,
1210+ grain_recording_updated : [ 'recording_updated' ] ,
1211+ grain_highlight_created : [ 'highlight_created' ] ,
1212+ grain_highlight_updated : [ 'highlight_updated' ] ,
1213+ grain_story_created : [ 'story_created' ] ,
1214+ grain_upload_status : [ 'upload_status' ] ,
1215+ }
1216+
1217+ const hookType = hookTypeMap [ triggerId ] ?? 'recording_added'
1218+ const eventTypes = eventTypeMap [ triggerId ] ?? [ ]
1219+
1220+ if ( ! hookTypeMap [ triggerId ] ) {
1221+ logger . warn (
1222+ `[${ requestId } ] Unknown triggerId for Grain: ${ triggerId } , defaulting to recording_added` ,
1223+ {
1224+ webhookId : webhookData . id ,
1225+ }
1226+ )
1227+ }
1228+
1229+ logger . info ( `[${ requestId } ] Creating Grain webhook` , {
1230+ triggerId,
1231+ hookType,
1232+ eventTypes,
1233+ webhookId : webhookData . id ,
1234+ } )
1235+
11941236 const notificationUrl = `${ getBaseUrl ( ) } /api/webhooks/trigger/${ path } `
11951237
11961238 const grainApiUrl = 'https://api.grain.com/_/public-api/v2/hooks/create'
11971239
11981240 const requestBody : Record < string , any > = {
11991241 hook_url : notificationUrl ,
1242+ hook_type : hookType ,
12001243 }
12011244
12021245 // Build include object based on configuration
@@ -1226,8 +1269,10 @@ async function createGrainWebhookSubscription(
12261269
12271270 const responseBody = await grainResponse . json ( )
12281271
1229- if ( ! grainResponse . ok || responseBody . error ) {
1272+ if ( ! grainResponse . ok || responseBody . error || responseBody . errors ) {
1273+ logger . warn ( '[App] Grain response body:' , responseBody )
12301274 const errorMessage =
1275+ responseBody . errors ?. detail ||
12311276 responseBody . error ?. message ||
12321277 responseBody . error ||
12331278 responseBody . message ||
@@ -1255,10 +1300,11 @@ async function createGrainWebhookSubscription(
12551300 `[${ requestId } ] Successfully created webhook in Grain for webhook ${ webhookData . id } .` ,
12561301 {
12571302 grainWebhookId : responseBody . id ,
1303+ eventTypes,
12581304 }
12591305 )
12601306
1261- return responseBody . id
1307+ return { id : responseBody . id , eventTypes }
12621308 } catch ( error : any ) {
12631309 logger . error (
12641310 `[${ requestId } ] Exception during Grain webhook creation for webhook ${ webhookData . id } .` ,
0 commit comments