From 7ada6b53c64807476f706ab51b59d6f2e6067e61 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Sun, 12 May 2024 14:05:08 +0700 Subject: [PATCH] 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. --- packages/billing/gateway/src/index.ts | 2 +- ... billing-event-handler-factory.service.ts} | 0 ...y.ts => billing-event-handler-provider.ts} | 2 +- .../billing-gateway-provider-factory.ts | 9 ++++---- packages/features/accounts/src/server/api.ts | 19 +++++++++++++++++ .../features/team-accounts/src/server/api.ts | 21 ++++++++++++++++++- .../src/clients/server-actions.client.ts | 8 ++++--- 7 files changed, 50 insertions(+), 11 deletions(-) rename packages/billing/gateway/src/server/services/billing-event-handler/{billing-gateway-factory.service.ts => billing-event-handler-factory.service.ts} (100%) rename packages/billing/gateway/src/server/services/billing-event-handler/{billing-gateway-provider-factory.ts => billing-event-handler-provider.ts} (90%) diff --git a/packages/billing/gateway/src/index.ts b/packages/billing/gateway/src/index.ts index 38ecdabfd..2fc5c9211 100644 --- a/packages/billing/gateway/src/index.ts +++ b/packages/billing/gateway/src/index.ts @@ -1,4 +1,4 @@ export * from './server/services/billing-gateway/billing-gateway.service'; 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'; diff --git a/packages/billing/gateway/src/server/services/billing-event-handler/billing-gateway-factory.service.ts b/packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler-factory.service.ts similarity index 100% rename from packages/billing/gateway/src/server/services/billing-event-handler/billing-gateway-factory.service.ts rename to packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler-factory.service.ts diff --git a/packages/billing/gateway/src/server/services/billing-event-handler/billing-gateway-provider-factory.ts b/packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler-provider.ts similarity index 90% rename from packages/billing/gateway/src/server/services/billing-event-handler/billing-gateway-provider-factory.ts rename to packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler-provider.ts index 193dc74f0..6dcadf28a 100644 --- a/packages/billing/gateway/src/server/services/billing-event-handler/billing-gateway-provider-factory.ts +++ b/packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler-provider.ts @@ -4,8 +4,8 @@ import { BillingConfig } from '@kit/billing'; 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 { BillingEventHandlerFactoryService } from './billing-gateway-factory.service'; /** * @description This function retrieves the billing provider from the database and returns a diff --git a/packages/billing/gateway/src/server/services/billing-gateway/billing-gateway-provider-factory.ts b/packages/billing/gateway/src/server/services/billing-gateway/billing-gateway-provider-factory.ts index b30bc8243..3f8dd4793 100644 --- a/packages/billing/gateway/src/server/services/billing-gateway/billing-gateway-provider-factory.ts +++ b/packages/billing/gateway/src/server/services/billing-gateway/billing-gateway-provider-factory.ts @@ -1,7 +1,8 @@ import 'server-only'; +import { SupabaseClient } from '@supabase/supabase-js'; + import { Database } from '@kit/supabase/database'; -import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client'; import { createBillingGatewayService } from './billing-gateway.service'; @@ -11,16 +12,14 @@ import { createBillingGatewayService } from './billing-gateway.service'; * defined in the host application. */ export async function getBillingGatewayProvider( - client: ReturnType, + client: SupabaseClient, ) { const provider = await getBillingProvider(client); return createBillingGatewayService(provider); } -async function getBillingProvider( - client: ReturnType>, -) { +async function getBillingProvider(client: SupabaseClient) { const { data, error } = await client .from('config') .select('billing_provider') diff --git a/packages/features/accounts/src/server/api.ts b/packages/features/accounts/src/server/api.ts index 480e2a4f1..50b9ad4fb 100644 --- a/packages/features/accounts/src/server/api.ts +++ b/packages/features/accounts/src/server/api.ts @@ -10,6 +10,25 @@ import { Database } from '@kit/supabase/database'; class AccountsApi { constructor(private readonly client: SupabaseClient) {} + /** + * @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 * @description Get the account workspace data. diff --git a/packages/features/team-accounts/src/server/api.ts b/packages/features/team-accounts/src/server/api.ts index 1fdaa0bee..0037ab767 100644 --- a/packages/features/team-accounts/src/server/api.ts +++ b/packages/features/team-accounts/src/server/api.ts @@ -10,6 +10,25 @@ import { Database } from '@kit/supabase/database'; export class TeamAccountsApi { constructor(private readonly client: SupabaseClient) {} + /** + * @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 * @description Check if the user is already in the account. @@ -20,7 +39,7 @@ export class TeamAccountsApi { .from('accounts') .select('*') .eq('id', accountId) - .maybeSingle(); + .single(); if (error) { throw error; diff --git a/packages/supabase/src/clients/server-actions.client.ts b/packages/supabase/src/clients/server-actions.client.ts index b8f142b5e..14be8244d 100644 --- a/packages/supabase/src/clients/server-actions.client.ts +++ b/packages/supabase/src/clients/server-actions.client.ts @@ -14,14 +14,16 @@ import { getSupabaseClientKeys } from '../get-supabase-client-keys'; const keys = getSupabaseClientKeys(); const serviceRoleKey = getServiceRoleKey(); -function createServerSupabaseClient() { - return createServerClient(keys.url, keys.anonKey, { +function createServerSupabaseClient< + GenericSchema extends Database = Database, +>() { + return createServerClient(keys.url, keys.anonKey, { cookies: getCookiesStrategy(), }); } export function getSupabaseServerActionClient< - GenericSchema = Database, + GenericSchema extends Database = Database, >(params?: { admin: boolean }) { const keys = getSupabaseClientKeys(); const admin = params?.admin ?? false;