Files
myeasycms-v2/packages/database-webhooks/src/server/services/database-webhook-router.service.ts
giancarlo 5adfb3edac Refactor account deletion process and improve invitation functionalities
The account deletion process has been refactored to send account deletion emails from the AccountWebhooksService instead of from the deletePersonalAccount service. This has resulted in the addition of the AccountWebhooksService and modification of the seeds.sql file to trigger a webhook after account deletion. Along with this, the account invitation functionalities within the accountInvitations service have been considerably enhanced, making it much clearer and easier to use.
2024-04-09 16:26:50 +08:00

77 lines
2.1 KiB
TypeScript

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 {
constructor(private readonly adminClient: SupabaseClient<Database>) {}
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);
}
default: {
const logger = await getLogger();
logger.warn(
{
table: body.table,
},
'No handler found for table',
);
}
}
}
private async handleInvitationsWebhook(body: RecordChange<'invitations'>) {
const { AccountInvitationsWebhookService } = await import(
'@kit/team-accounts/webhooks'
);
const service = new AccountInvitationsWebhookService(this.adminClient);
return service.handleInvitationWebhook(body.record);
}
private async handleSubscriptionsWebhook(
body: RecordChange<'subscriptions'>,
) {
const { BillingWebhooksService } = await import('@kit/billing-gateway');
const service = new BillingWebhooksService();
if (body.type === 'DELETE' && body.old_record) {
return service.handleSubscriptionDeletedWebhook(body.old_record);
}
}
private async handleAccountsWebhook(body: RecordChange<'accounts'>) {
const { AccountWebhooksService } = await import(
'@kit/team-accounts/webhooks'
);
const service = new AccountWebhooksService();
if (body.type === 'DELETE' && body.old_record) {
return service.handleAccountDeletedWebhook(body.old_record);
}
}
}