Files
myeasycms-v2/apps/web/app/(dashboard)/home/_lib/load-user-workspace.ts
giancarlo 8dd4b594d2 Update database types and relationships
The commit removes semicolons at the ends of lines and makes updates to the Database object in the `database.types.ts` file. This better aligns the syntax with TypeScript norms. It also affects many database relationships, including but not limited to `Accounts`, `Roles`, and `Subscriptions`.
2024-04-16 22:17:28 +08:00

76 lines
1.8 KiB
TypeScript

import { cache } from 'react';
import { SupabaseClient } from '@supabase/supabase-js';
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
import featureFlagsConfig from '~/config/feature-flags.config';
import { Database } from '~/lib/database.types';
/**
* @name loadUserWorkspace
* @description
* Load the user workspace data. It's a cached per-request function that fetches the user workspace data.
* It can be used across the server components to load the user workspace data.
*/
export const loadUserWorkspace = cache(async () => {
const client = getSupabaseServerComponentClient();
const loadAccounts = featureFlagsConfig.enableTeamAccounts;
const accountsPromise = loadAccounts
? loadUserAccounts(client)
: Promise.resolve([]);
const workspacePromise = loadUserAccountWorkspace(client);
const userPromise = client.auth.getUser();
const [accounts, workspace, userResult] = await Promise.all([
accountsPromise,
workspacePromise,
userPromise,
]);
const user = userResult.data.user;
if (!user) {
throw new Error('User is not logged in');
}
return {
accounts,
workspace,
user,
};
});
async function loadUserAccountWorkspace(client: SupabaseClient<Database>) {
const { data, error } = await client
.from('user_account_workspace')
.select(`*`)
.single();
if (error) {
throw error;
}
return data;
}
async function loadUserAccounts(client: SupabaseClient<Database>) {
const { data: accounts, error } = await client
.from('user_accounts')
.select(`name, slug, picture_url`);
if (error) {
throw error;
}
return accounts.map(({ name, slug, picture_url }) => {
return {
label: name,
value: slug,
image: picture_url,
};
});
}