Files
medreport_mrb2b/packages/features/accounts/src/server/api.ts

170 lines
3.5 KiB
TypeScript

import { SupabaseClient } from '@supabase/supabase-js';
import { Database } from '@kit/supabase/database';
/**
* Class representing an API for interacting with user accounts.
* @constructor
* @param {SupabaseClient<Database>} client - The Supabase client instance.
*/
class AccountsApi {
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
* @description Get the account workspace data.
*/
async getAccountWorkspace() {
const { data, error } = await this.client
.from('user_account_workspace')
.select(`*`)
.single();
if (error) {
throw error;
}
return data;
}
/**
* @name loadUserAccounts
* Load only user-owned accounts (not just memberships).
*/
async loadUserAccounts() {
const authUser = await this.client.auth.getUser();
const {
data,
error: userError,
} = authUser
if (userError) {
throw userError;
}
const { user } = data;
const { data: accounts, error } = await this.client
.from('accounts_memberships')
.select(`
account_id,
user_accounts (
name,
slug,
picture_url
)
`)
.eq('user_id', user.id)
.eq('account_role', 'owner');
if (error) {
throw error;
}
return accounts.map(({ user_accounts }) => ({
label: user_accounts.name,
value: user_accounts.slug,
image: user_accounts.picture_url,
}));
}
async loadTempUserAccounts() {
const { data: accounts, error } = await this.client
.from('user_accounts')
.select(`name, slug`);
if (error) {
throw error;
}
return accounts.map(({ name, slug }) => {
return {
label: name,
value: slug,
};
});
}
/**
* @name getSubscription
* Get the subscription data for the given user.
* @param accountId
*/
async getSubscription(accountId: string) {
const response = await this.client
.from('subscriptions')
.select('*, items: subscription_items !inner (*)')
.eq('account_id', accountId)
.maybeSingle();
if (response.error) {
throw response.error;
}
return response.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 getCustomerId
* Get the billing customer ID for the given user.
* If the user does not have a billing customer ID, it will return null.
* @param accountId
*/
async getCustomerId(accountId: string) {
const response = await this.client
.from('billing_customers')
.select('customer_id')
.eq('account_id', accountId)
.maybeSingle();
if (response.error) {
throw response.error;
}
return response.data?.customer_id;
}
}
export function createAccountsApi(client: SupabaseClient<Database>) {
return new AccountsApi(client);
}