Refactor billing service and enhance account APIs

This commit includes renaming and refactoring of some billing services for clarification and coherence. It also improves account APIs in team-accounts and accounts packages by having added `getAccount` and `getTeamAccount` methods, allowing retrieval of account details via slug and id. Changes also include type modifications in `getSupabaseServerActionClient` function for better flexibility and precision.
This commit is contained in:
giancarlo
2024-05-12 14:05:08 +07:00
parent f22c9817bd
commit 7ada6b53c6
7 changed files with 50 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
export * from './server/services/billing-gateway/billing-gateway.service'; export * from './server/services/billing-gateway/billing-gateway.service';
export * from './server/services/billing-gateway/billing-gateway-provider-factory'; export * from './server/services/billing-gateway/billing-gateway-provider-factory';
export * from './server/services/billing-event-handler/billing-gateway-provider-factory'; export * from './server/services/billing-event-handler/billing-event-handler-provider';
export * from './server/services/billing-webhooks/billing-webhooks.service'; export * from './server/services/billing-webhooks/billing-webhooks.service';

View File

@@ -4,8 +4,8 @@ import { BillingConfig } from '@kit/billing';
import { Database } from '@kit/supabase/database'; import { Database } from '@kit/supabase/database';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client'; import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { BillingEventHandlerFactoryService } from './billing-event-handler-factory.service';
import { BillingEventHandlerService } from './billing-event-handler.service'; import { BillingEventHandlerService } from './billing-event-handler.service';
import { BillingEventHandlerFactoryService } from './billing-gateway-factory.service';
/** /**
* @description This function retrieves the billing provider from the database and returns a * @description This function retrieves the billing provider from the database and returns a

View File

@@ -1,7 +1,8 @@
import 'server-only'; import 'server-only';
import { SupabaseClient } from '@supabase/supabase-js';
import { Database } from '@kit/supabase/database'; import { Database } from '@kit/supabase/database';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { createBillingGatewayService } from './billing-gateway.service'; import { createBillingGatewayService } from './billing-gateway.service';
@@ -11,16 +12,14 @@ import { createBillingGatewayService } from './billing-gateway.service';
* defined in the host application. * defined in the host application.
*/ */
export async function getBillingGatewayProvider( export async function getBillingGatewayProvider(
client: ReturnType<typeof getSupabaseServerActionClient>, client: SupabaseClient<Database>,
) { ) {
const provider = await getBillingProvider(client); const provider = await getBillingProvider(client);
return createBillingGatewayService(provider); return createBillingGatewayService(provider);
} }
async function getBillingProvider( async function getBillingProvider(client: SupabaseClient<Database>) {
client: ReturnType<typeof getSupabaseServerActionClient<Database>>,
) {
const { data, error } = await client const { data, error } = await client
.from('config') .from('config')
.select('billing_provider') .select('billing_provider')

View File

@@ -10,6 +10,25 @@ import { Database } from '@kit/supabase/database';
class AccountsApi { class AccountsApi {
constructor(private readonly client: SupabaseClient<Database>) {} constructor(private readonly client: SupabaseClient<Database>) {}
/**
* @name getAccount
* @description Get the account data for the given ID.
* @param id
*/
async getAccount(id: string) {
const { data, error } = await this.client
.from('accounts')
.select('*')
.eq('id', id)
.single();
if (error) {
throw error;
}
return data;
}
/** /**
* @name getAccountWorkspace * @name getAccountWorkspace
* @description Get the account workspace data. * @description Get the account workspace data.

View File

@@ -10,6 +10,25 @@ import { Database } from '@kit/supabase/database';
export class TeamAccountsApi { export class TeamAccountsApi {
constructor(private readonly client: SupabaseClient<Database>) {} constructor(private readonly client: SupabaseClient<Database>) {}
/**
* @name getTeamAccount
* @description Get the account data for the given slug.
* @param slug
*/
async getTeamAccount(slug: string) {
const { data, error } = await this.client
.from('accounts')
.select('*')
.eq('slug', slug)
.single();
if (error) {
throw error;
}
return data;
}
/** /**
* @name getTeamAccountById * @name getTeamAccountById
* @description Check if the user is already in the account. * @description Check if the user is already in the account.
@@ -20,7 +39,7 @@ export class TeamAccountsApi {
.from('accounts') .from('accounts')
.select('*') .select('*')
.eq('id', accountId) .eq('id', accountId)
.maybeSingle(); .single();
if (error) { if (error) {
throw error; throw error;

View File

@@ -14,14 +14,16 @@ import { getSupabaseClientKeys } from '../get-supabase-client-keys';
const keys = getSupabaseClientKeys(); const keys = getSupabaseClientKeys();
const serviceRoleKey = getServiceRoleKey(); const serviceRoleKey = getServiceRoleKey();
function createServerSupabaseClient() { function createServerSupabaseClient<
return createServerClient<Database>(keys.url, keys.anonKey, { GenericSchema extends Database = Database,
>() {
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
cookies: getCookiesStrategy(), cookies: getCookiesStrategy(),
}); });
} }
export function getSupabaseServerActionClient< export function getSupabaseServerActionClient<
GenericSchema = Database, GenericSchema extends Database = Database,
>(params?: { admin: boolean }) { >(params?: { admin: boolean }) {
const keys = getSupabaseClientKeys(); const keys = getSupabaseClientKeys();
const admin = params?.admin ?? false; const admin = params?.admin ?? false;