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.
This commit is contained in:
giancarlo
2024-04-24 12:07:09 +07:00
parent abd0771c4a
commit dbdccc59bc
4 changed files with 74 additions and 32 deletions

View File

@@ -36,8 +36,8 @@ export const loadPersonalAccountBillingPageData = cache((userId: string) => {
const data = const data =
BILLING_MODE === 'subscription' BILLING_MODE === 'subscription'
? api.getSubscriptionData(userId) ? api.getSubscription(userId)
: api.getOrdersData(userId); : api.getOrder(userId);
const customerId = api.getBillingCustomerId(userId); const customerId = api.getBillingCustomerId(userId);

View File

@@ -28,8 +28,8 @@ export const loadTeamAccountBillingPage = cache((accountId: string) => {
const data = const data =
BILLING_MODE === 'subscription' BILLING_MODE === 'subscription'
? api.getSubscriptionData(accountId) ? api.getSubscription(accountId)
: api.getOrdersData(accountId); : api.getOrder(accountId);
const customerId = api.getBillingCustomerId(accountId); const customerId = api.getBillingCustomerId(accountId);

View File

@@ -10,6 +10,10 @@ import { Database } from '@kit/supabase/database';
class AccountsApi { class AccountsApi {
constructor(private readonly client: SupabaseClient<Database>) {} constructor(private readonly client: SupabaseClient<Database>) {}
/**
* @name getAccountWorkspace
* @description Get the account workspace data.
*/
async getAccountWorkspace() { async getAccountWorkspace() {
const { data, error } = await this.client const { data, error } = await this.client
.from('user_account_workspace') .from('user_account_workspace')
@@ -23,6 +27,10 @@ class AccountsApi {
return data; return data;
} }
/**
* @name loadUserAccounts
* Load the user accounts.
*/
async loadUserAccounts() { async loadUserAccounts() {
const { data: accounts, error } = await this.client const { data: accounts, error } = await this.client
.from('user_accounts') .from('user_accounts')
@@ -42,42 +50,40 @@ class AccountsApi {
} }
/** /**
* @name getSubscriptionData * @name getSubscription
* Get the subscription data for the given user. * Get the subscription data for the given user.
* @param accountId * @param accountId
*/ */
getSubscriptionData(accountId: string) { async getSubscription(accountId: string) {
return this.client const response = await this.client
.from('subscriptions') .from('subscriptions')
.select('*, items: subscription_items !inner (*)') .select('*, items: subscription_items !inner (*)')
.eq('account_id', accountId) .eq('account_id', accountId)
.maybeSingle() .maybeSingle();
.then((response) => {
if (response.error) {
throw response.error;
}
return response.data; if (response.error) {
}); throw response.error;
}
return response.data;
} }
/** /**
* Get the orders data for the given account. * Get the orders data for the given account.
* @param accountId * @param accountId
*/ */
getOrdersData(accountId: string) { async getOrder(accountId: string) {
return this.client const response = await this.client
.from('orders') .from('orders')
.select('*, items: order_items !inner (*)') .select('*, items: order_items !inner (*)')
.eq('account_id', accountId) .eq('account_id', accountId)
.maybeSingle() .maybeSingle();
.then((response) => {
if (response.error) {
throw response.error;
}
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. * If the user does not have a billing customer ID, it will return null.
* @param accountId * @param accountId
*/ */
getBillingCustomerId(accountId: string) { async getBillingCustomerId(accountId: string) {
return this.client const response = await this.client
.from('billing_customers') .from('billing_customers')
.select('customer_id') .select('customer_id')
.eq('account_id', accountId) .eq('account_id', accountId)
.maybeSingle() .maybeSingle();
.then((response) => {
if (response.error) {
throw response.error;
}
return response.data?.customer_id; if (response.error) {
}); throw response.error;
}
return response.data?.customer_id;
} }
} }

View File

@@ -29,6 +29,43 @@ export class TeamAccountsApi {
return data; 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 * @name getAccountWorkspace
* @description Get the account workspace data. * @description Get the account workspace data.