import { SupabaseClient } from '@supabase/supabase-js'; import { Database } from '@kit/supabase/database'; import { RecordChange, Tables } from '../record-change.type'; 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': { const payload = body as RecordChange; return this.handleInvitationsWebhook(payload); } case 'subscriptions': { const payload = body as RecordChange; return this.handleSubscriptionsWebhook(payload); } case 'accounts': { const payload = body as RecordChange; return this.handleAccountsWebhook(payload); } case 'analysis_orders': { const payload = body as RecordChange; return this.handleAnalysisOrdersWebhook(payload); } case 'connected_online_reservation': { const payload = body as RecordChange; return this.handleTtoOrdersWebhook(payload); } default: { return; } } } private async handleInvitationsWebhook(body: RecordChange<'invitations'>) { const { createAccountInvitationsWebhookService } = await import( '@kit/team-accounts/webhooks' ); const service = createAccountInvitationsWebhookService(this.adminClient); return service.handleInvitationWebhook(body.record); } private async handleSubscriptionsWebhook( body: RecordChange<'subscriptions'>, ) { if (body.type === 'DELETE' && body.old_record) { const { createBillingWebhooksService } = await import( '@kit/billing-gateway' ); const service = createBillingWebhooksService(); return service.handleSubscriptionDeletedWebhook(body.old_record); } } private async handleAccountsWebhook(body: RecordChange<'accounts'>) { if (body.type === 'DELETE' && body.old_record) { const { createAccountWebhooksService } = await import( '@kit/team-accounts/webhooks' ); const service = createAccountWebhooksService(); return service.handleAccountDeletedWebhook(body.old_record); } } private async handleAnalysisOrdersWebhook( body: RecordChange<'analysis_orders'>, ) { if (body.type === 'UPDATE' && body.record && body.old_record) { const { record, old_record } = body; if (record.status === old_record.status) { return; } const { createOrderWebhooksService } = await import( '@kit/notifications/webhooks/order-notifications.service' ); const service = createOrderWebhooksService(); return service.handleStatusChangeWebhook(record); } } private async handleTtoOrdersWebhook( body: RecordChange<'connected_online_reservation'>, ) { if (body.type === 'UPDATE' && body.record) { const { createOrderWebhooksService } = await import( '@kit/notifications/webhooks/order-notifications.service' ); const service = createOrderWebhooksService(); return service.handleTtoReservationConfirmationWebhook(body.record); } } }