Refactor API code and simplify billing display

The code in the webhook API has been refactored to move the DatabaseWebhookHandlerService instance out of the POST function scope. Furthermore, the display of renewal plan details on the billing page has been simplified and certain parts deemed superfluous have been removed. Numerous types and interfaces in the database.types.ts file have also been corrected and formatted for consistency and improved readability.
This commit is contained in:
giancarlo
2024-03-31 15:13:44 +08:00
parent 2c0c616a2d
commit aa12ecd5a2
10 changed files with 1133 additions and 1026 deletions

View File

@@ -10,9 +10,14 @@ export class DatabaseWebhookHandlerService {
private readonly namespace = 'database-webhook-handler';
async handleWebhook(request: Request, webhooksSecret: string) {
const json = await request.clone().json();
const { table, type } = json as RecordChange<keyof Tables>;
Logger.info(
{
name: this.namespace,
table,
type,
},
'Received webhook from DB. Processing...',
);
@@ -21,11 +26,17 @@ export class DatabaseWebhookHandlerService {
this.assertSignatureIsAuthentic(request, webhooksSecret);
// all good, handle the webhook
const json = await request.json();
await this.handleWebhookBody(json);
// create a client with admin access since we are handling webhooks
// and no user is authenticated
const client = getSupabaseRouteHandlerClient({
admin: true,
});
const { table, type } = json as RecordChange<keyof Tables>;
// handle the webhook
const service = new DatabaseWebhookRouterService(client);
await service.handleWebhook(json);
Logger.info(
{
@@ -37,16 +48,6 @@ export class DatabaseWebhookHandlerService {
);
}
private handleWebhookBody(body: RecordChange<keyof Tables>) {
const client = getSupabaseRouteHandlerClient({
admin: true,
});
const service = new DatabaseWebhookRouterService(client);
return service.handleWebhook(body);
}
private assertSignatureIsAuthentic(request: Request, webhooksSecret: string) {
const header = request.headers.get('X-Supabase-Event-Signature');