feat: implement order notifications service with TTO reservation confirmation handling feat: create migration for TTO booking email webhook trigger
133 lines
3.6 KiB
TypeScript
133 lines
3.6 KiB
TypeScript
import { SupabaseClient } from '@supabase/supabase-js';
|
|
|
|
import { Database } from '@kit/supabase/database';
|
|
|
|
import { RecordChange, Tables } from '../record-change.type';
|
|
|
|
export function createDatabaseWebhookRouterService(
|
|
adminClient: SupabaseClient<Database>,
|
|
) {
|
|
return new DatabaseWebhookRouterService(adminClient);
|
|
}
|
|
|
|
/**
|
|
* @name DatabaseWebhookRouterService
|
|
* @description Service that routes the webhook event to the appropriate service
|
|
*/
|
|
class DatabaseWebhookRouterService {
|
|
constructor(private readonly adminClient: SupabaseClient<Database>) {}
|
|
|
|
/**
|
|
* @name handleWebhook
|
|
* @description Handle the webhook event
|
|
* @param body
|
|
*/
|
|
async handleWebhook(body: RecordChange<keyof Tables>) {
|
|
switch (body.table) {
|
|
case 'invitations': {
|
|
const payload = body as RecordChange<typeof body.table>;
|
|
|
|
return this.handleInvitationsWebhook(payload);
|
|
}
|
|
|
|
case 'subscriptions': {
|
|
const payload = body as RecordChange<typeof body.table>;
|
|
|
|
return this.handleSubscriptionsWebhook(payload);
|
|
}
|
|
|
|
case 'accounts': {
|
|
const payload = body as RecordChange<typeof body.table>;
|
|
|
|
return this.handleAccountsWebhook(payload);
|
|
}
|
|
|
|
case 'analysis_orders': {
|
|
const payload = body as RecordChange<typeof body.table>;
|
|
|
|
return this.handleAnalysisOrdersWebhook(payload);
|
|
}
|
|
|
|
case 'connected_online_reservation': {
|
|
const payload = body as RecordChange<typeof body.table>;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|