Files
myeasycms-v2/packages/database-webhooks/src/server/services/database-webhook-handler.service.ts
giancarlo 9fca45c2de Replace Logger with getLogger and update next version
This commit replaces the use of Logger with getLogger in various parts of the code to handle logging. The Logger has been replaced with getLogger, which assists in getting logs in an asynchronous manner. In addition to this, it updates the next version in pnpm-lock.yaml from next@14.2.0-canary.61 to next@14.2.0-canary.62 and various other dependencies. Also made minor annotations and comments to the function 'isBrowser' and 'formatCurrency' in the 'utils.ts' file.
2024-04-08 12:23:15 +08:00

76 lines
1.9 KiB
TypeScript

import 'server-only';
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';
export class DatabaseWebhookHandlerService {
private readonly namespace = 'database-webhook-handler';
async handleWebhook(request: Request, webhooksSecret: string) {
const logger = await getLogger();
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...',
);
// check if the signature is valid
this.assertSignatureIsAuthentic(request, webhooksSecret);
// all good, handle the webhook
// create a client with admin access since we are handling webhooks
// and no user is authenticated
const client = getSupabaseRouteHandlerClient({
admin: true,
});
// handle the webhook
const service = new DatabaseWebhookRouterService(client);
try {
// handle the webhook event based on the table
await service.handleWebhook(json);
logger.info(
{
name: this.namespace,
table,
type,
},
'Webhook processed successfully',
);
} catch (error) {
logger.error(
{
name: this.namespace,
table,
type,
error,
},
'Failed to process webhook',
);
throw error;
}
}
private assertSignatureIsAuthentic(request: Request, webhooksSecret: string) {
const header = request.headers.get('X-Supabase-Event-Signature');
if (header !== webhooksSecret) {
throw new Error('Invalid signature');
}
}
}