From dbdccc59bc12c8d5cb50646cdd34fd57c220ac04 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Wed, 24 Apr 2024 12:07:09 +0700 Subject: [PATCH] Refactor subscription and order retrieval methods The changes refactor how subscription and order data is retrieved throughout the codebase. This includes renaming methods from 'getSubscriptionData' and 'getOrdersData' to 'getSubscription' and 'getOrder' respectively. The code for obtaining these subscriptions and orders has been rewritten for improved clarity and error handling. --- .../personal-account-billing-page.loader.ts | 4 +- .../team-account-billing-page.loader.ts | 4 +- packages/features/accounts/src/server/api.ts | 61 ++++++++++--------- .../features/team-accounts/src/server/api.ts | 37 +++++++++++ 4 files changed, 74 insertions(+), 32 deletions(-) diff --git a/apps/web/app/(dashboard)/home/(user)/billing/_lib/server/personal-account-billing-page.loader.ts b/apps/web/app/(dashboard)/home/(user)/billing/_lib/server/personal-account-billing-page.loader.ts index 15bb17c23..2750ef78b 100644 --- a/apps/web/app/(dashboard)/home/(user)/billing/_lib/server/personal-account-billing-page.loader.ts +++ b/apps/web/app/(dashboard)/home/(user)/billing/_lib/server/personal-account-billing-page.loader.ts @@ -36,8 +36,8 @@ export const loadPersonalAccountBillingPageData = cache((userId: string) => { const data = BILLING_MODE === 'subscription' - ? api.getSubscriptionData(userId) - : api.getOrdersData(userId); + ? api.getSubscription(userId) + : api.getOrder(userId); const customerId = api.getBillingCustomerId(userId); diff --git a/apps/web/app/(dashboard)/home/[account]/_lib/server/team-account-billing-page.loader.ts b/apps/web/app/(dashboard)/home/[account]/_lib/server/team-account-billing-page.loader.ts index 8f691d25c..6d4462d00 100644 --- a/apps/web/app/(dashboard)/home/[account]/_lib/server/team-account-billing-page.loader.ts +++ b/apps/web/app/(dashboard)/home/[account]/_lib/server/team-account-billing-page.loader.ts @@ -28,8 +28,8 @@ export const loadTeamAccountBillingPage = cache((accountId: string) => { const data = BILLING_MODE === 'subscription' - ? api.getSubscriptionData(accountId) - : api.getOrdersData(accountId); + ? api.getSubscription(accountId) + : api.getOrder(accountId); const customerId = api.getBillingCustomerId(accountId); diff --git a/packages/features/accounts/src/server/api.ts b/packages/features/accounts/src/server/api.ts index 4d32f6d73..c5e9a2044 100644 --- a/packages/features/accounts/src/server/api.ts +++ b/packages/features/accounts/src/server/api.ts @@ -10,6 +10,10 @@ import { Database } from '@kit/supabase/database'; class AccountsApi { constructor(private readonly client: SupabaseClient) {} + /** + * @name getAccountWorkspace + * @description Get the account workspace data. + */ async getAccountWorkspace() { const { data, error } = await this.client .from('user_account_workspace') @@ -23,6 +27,10 @@ class AccountsApi { return data; } + /** + * @name loadUserAccounts + * Load the user accounts. + */ async loadUserAccounts() { const { data: accounts, error } = await this.client .from('user_accounts') @@ -42,42 +50,40 @@ class AccountsApi { } /** - * @name getSubscriptionData + * @name getSubscription * Get the subscription data for the given user. * @param accountId */ - getSubscriptionData(accountId: string) { - return this.client + async getSubscription(accountId: string) { + const response = await this.client .from('subscriptions') .select('*, items: subscription_items !inner (*)') .eq('account_id', accountId) - .maybeSingle() - .then((response) => { - if (response.error) { - throw response.error; - } + .maybeSingle(); - return response.data; - }); + if (response.error) { + throw response.error; + } + + return response.data; } /** * Get the orders data for the given account. * @param accountId */ - getOrdersData(accountId: string) { - return this.client + async getOrder(accountId: string) { + const response = await this.client .from('orders') .select('*, items: order_items !inner (*)') .eq('account_id', accountId) - .maybeSingle() - .then((response) => { - if (response.error) { - throw response.error; - } + .maybeSingle(); - return response.data; - }); + if (response.error) { + throw response.error; + } + + return response.data; } /** @@ -86,19 +92,18 @@ class AccountsApi { * If the user does not have a billing customer ID, it will return null. * @param accountId */ - getBillingCustomerId(accountId: string) { - return this.client + async getBillingCustomerId(accountId: string) { + const response = await this.client .from('billing_customers') .select('customer_id') .eq('account_id', accountId) - .maybeSingle() - .then((response) => { - if (response.error) { - throw response.error; - } + .maybeSingle(); - return response.data?.customer_id; - }); + if (response.error) { + throw response.error; + } + + return response.data?.customer_id; } } diff --git a/packages/features/team-accounts/src/server/api.ts b/packages/features/team-accounts/src/server/api.ts index b9abc2e2d..2fb4e16c2 100644 --- a/packages/features/team-accounts/src/server/api.ts +++ b/packages/features/team-accounts/src/server/api.ts @@ -29,6 +29,43 @@ export class TeamAccountsApi { return data; } + /** + * @name getSubscription + * @description Get the subscription data for the account. + * @param accountId + */ + async getSubscription(accountId: string) { + const { data, error } = await this.client + .from('subscriptions') + .select('*') + .eq('account_id', accountId) + .maybeSingle(); + + if (error) { + throw error; + } + + return data; + } + + /** + * Get the orders data for the given account. + * @param accountId + */ + async getOrder(accountId: string) { + const response = await this.client + .from('orders') + .select('*, items: order_items !inner (*)') + .eq('account_id', accountId) + .maybeSingle(); + + if (response.error) { + throw response.error; + } + + return response.data; + } + /** * @name getAccountWorkspace * @description Get the account workspace data.