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 =
BILLING_MODE === 'subscription'
? api.getSubscriptionData(userId)
: api.getOrdersData(userId);
? api.getSubscription(userId)
: api.getOrder(userId);
const customerId = api.getBillingCustomerId(userId);

View File

@@ -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);

View File

@@ -10,6 +10,10 @@ import { Database } from '@kit/supabase/database';
class AccountsApi {
constructor(private readonly client: SupabaseClient<Database>) {}
/**
* @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;
}
}

View File

@@ -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.