Refactor BillingEventHandlerService and update SQL permissions

Changed BillingEventHandlerService from a class to a method. Also, the SQL permissions for service_role on public.order_items table have been updated to include insert, update and delete operations. Additionally, made adjustment to configuration values in the billing.sample.config.ts file.
This commit is contained in:
giancarlo
2024-06-04 13:32:02 +07:00
parent cc4453ae4c
commit 2696c08d7d
3 changed files with 36 additions and 6 deletions

View File

@@ -1889,7 +1889,7 @@ select
on table public.order_items to authenticated,
service_role;
grant insert on table public.order_items to service_role;
grant insert, update, delete on table public.order_items to service_role;
-- Indexes on the order_items table
create index ix_order_items_order_id on public.order_items (order_id);

View File

@@ -5,16 +5,23 @@ import { Database } from '@kit/supabase/database';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { BillingEventHandlerFactoryService } from './billing-event-handler-factory.service';
import { BillingEventHandlerService } from './billing-event-handler.service';
import { createBillingEventHandlerService } from './billing-event-handler.service';
// a function that returns a Supabase client
type ClientProvider = () => ReturnType<typeof getSupabaseServerActionClient>;
// the billing provider from the database
type BillingProvider = Database['public']['Enums']['billing_provider'];
/**
* @name getBillingEventHandlerService
* @description This function retrieves the billing provider from the database and returns a
* new instance of the `BillingGatewayService` class. This class is used to interact with the server actions
* defined in the host application.
*/
export async function getBillingEventHandlerService(
clientProvider: () => ReturnType<typeof getSupabaseServerActionClient>,
provider: Database['public']['Enums']['billing_provider'],
clientProvider: ClientProvider,
provider: BillingProvider,
config: BillingConfig,
) {
const strategy = await BillingEventHandlerFactoryService.GetProviderStrategy(
@@ -22,5 +29,5 @@ export async function getBillingEventHandlerService(
config,
);
return new BillingEventHandlerService(clientProvider, strategy);
return createBillingEventHandlerService(clientProvider, strategy);
}

View File

@@ -30,7 +30,24 @@ interface CustomHandlersParams {
onEvent?: (event: string, data: unknown) => Promise<unknown>;
}
export class BillingEventHandlerService {
/**
* @name createBillingEventHandlerService
* @description Create a new instance of the `BillingEventHandlerService` class
* @param clientProvider
* @param strategy
*/
export function createBillingEventHandlerService(
clientProvider: () => SupabaseClient<Database>,
strategy: BillingWebhookHandlerService,
) {
return new BillingEventHandlerService(clientProvider, strategy);
}
/**
* @name BillingEventHandlerService
* @description This class is used to handle the webhook events from the billing provider
*/
class BillingEventHandlerService {
private readonly namespace = 'billing';
constructor(
@@ -38,6 +55,12 @@ export class BillingEventHandlerService {
private readonly strategy: BillingWebhookHandlerService,
) {}
/**
* @name handleWebhookEvent
* @description Handle the webhook event from the billing provider
* @param request
* @param params
*/
async handleWebhookEvent(
request: Request,
params: Partial<CustomHandlersParams> = {},