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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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> = {},
|
||||
|
||||
Reference in New Issue
Block a user