diff --git a/packages/database-webhooks/src/server/services/database-webhook-handler.service.ts b/packages/database-webhooks/src/server/services/database-webhook-handler.service.ts index e585baf83..11c09186b 100644 --- a/packages/database-webhooks/src/server/services/database-webhook-handler.service.ts +++ b/packages/database-webhooks/src/server/services/database-webhook-handler.service.ts @@ -4,13 +4,23 @@ import { getLogger } from '@kit/shared/logger'; import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client'; import { RecordChange, Tables } from '../record-change.type'; -import { DatabaseWebhookRouterService } from './database-webhook-router.service'; +import { createDatabaseWebhookRouterService } from './database-webhook-router.service'; import { getDatabaseWebhookVerifier } from './verifier'; +/** + * @name DatabaseChangePayload + * @description Payload for the database change event. Useful for handling custom webhooks. + */ +export type DatabaseChangePayload = RecordChange; + export function getDatabaseWebhookHandlerService() { return new DatabaseWebhookHandlerService(); } +/** + * @name getDatabaseWebhookHandlerService + * @description Get the database webhook handler service + */ class DatabaseWebhookHandlerService { private readonly namespace = 'database-webhook-handler'; @@ -18,8 +28,14 @@ class DatabaseWebhookHandlerService { * @name handleWebhook * @description Handle the webhook event * @param request + * @param params */ - async handleWebhook(request: Request) { + async handleWebhook( + request: Request, + params?: { + handleEvent(payload: DatabaseChangePayload): unknown; + }, + ) { const logger = await getLogger(); const json = await request.clone().json(); @@ -47,12 +63,17 @@ class DatabaseWebhookHandlerService { }); // handle the webhook - const service = new DatabaseWebhookRouterService(client); + const service = createDatabaseWebhookRouterService(client); try { // handle the webhook event based on the table await service.handleWebhook(json); + // if a custom handler is provided, call it + if (params?.handleEvent) { + await params.handleEvent(json); + } + logger.info(ctx, 'Webhook processed successfully'); } catch (error) { logger.error( diff --git a/packages/database-webhooks/src/server/services/database-webhook-router.service.ts b/packages/database-webhooks/src/server/services/database-webhook-router.service.ts index 76b838ae7..a97d4924f 100644 --- a/packages/database-webhooks/src/server/services/database-webhook-router.service.ts +++ b/packages/database-webhooks/src/server/services/database-webhook-router.service.ts @@ -1,13 +1,27 @@ import { SupabaseClient } from '@supabase/supabase-js'; -import { getLogger } from '@kit/shared/logger'; import { Database } from '@kit/supabase/database'; import { RecordChange, Tables } from '../record-change.type'; -export class DatabaseWebhookRouterService { +export function createDatabaseWebhookRouterService( + adminClient: SupabaseClient, +) { + return new DatabaseWebhookRouterService(adminClient); +} + +/** + * @name DatabaseWebhookRouterService + * @description Service that routes the webhook event to the appropriate service + */ +class DatabaseWebhookRouterService { constructor(private readonly adminClient: SupabaseClient) {} + /** + * @name handleWebhook + * @description Handle the webhook event + * @param body + */ async handleWebhook(body: RecordChange) { switch (body.table) { case 'invitations': { @@ -29,14 +43,7 @@ export class DatabaseWebhookRouterService { } default: { - const logger = await getLogger(); - - logger.warn( - { - table: body.table, - }, - 'No handler found for table', - ); + return; } } }