feat(account): refactor team account retrieval and update SQL function for workspace
This commit is contained in:
@@ -33,7 +33,7 @@ function TeamAccountHomePage({ params }: TeamAccountHomePageProps) {
|
||||
const client = getSupabaseServerClient();
|
||||
const api = createTeamAccountsApi(client);
|
||||
const teamAccount = use(api.getTeamAccount(account));
|
||||
console.log('teamAccount', teamAccount);
|
||||
|
||||
return (
|
||||
<>
|
||||
<TeamAccountLayoutPageHeader
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { getSupabaseServerClient } from "@kit/supabase/server-client";
|
||||
import { getSupabaseServerAdminClient } from "@kit/supabase/server-admin-client";
|
||||
import type { Tables } from "@/packages/supabase/src/database.types";
|
||||
import type { Tables } from '@/packages/supabase/src/database.types';
|
||||
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
type Account = Tables<{ schema: 'medreport' }, 'accounts'>;
|
||||
type Membership = Tables<{ schema: 'medreport' }, 'accounts_memberships'>;
|
||||
|
||||
export type AccountWithMemberships = Account & { memberships: Membership[] }
|
||||
export type AccountWithMemberships = Account & { memberships: Membership[] };
|
||||
|
||||
export async function getAccount(id: string): Promise<AccountWithMemberships> {
|
||||
const { data } = await getSupabaseServerClient()
|
||||
@@ -28,6 +29,7 @@ export async function getAccountAdmin({
|
||||
.schema('medreport')
|
||||
.from('accounts')
|
||||
.select('*, memberships: accounts_memberships (*)')
|
||||
.eq('is_personal_account', true);
|
||||
|
||||
if (primaryOwnerUserId) {
|
||||
query.eq('primary_owner_user_id', primaryOwnerUserId);
|
||||
|
||||
@@ -1640,9 +1640,7 @@ export type Database = {
|
||||
Returns: Json
|
||||
}
|
||||
create_team_account: {
|
||||
Args:
|
||||
| { account_name: string }
|
||||
| { account_name: string; new_personal_code: string }
|
||||
Args: { account_name: string; new_personal_code: string }
|
||||
Returns: {
|
||||
application_role: Database["medreport"]["Enums"]["application_role"]
|
||||
city: string | null
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
DROP FUNCTION IF EXISTS medreport.team_account_workspace(text);
|
||||
|
||||
CREATE FUNCTION medreport.team_account_workspace(account_slug text)
|
||||
RETURNS TABLE(
|
||||
id uuid,
|
||||
name character varying,
|
||||
picture_url character varying,
|
||||
slug text,
|
||||
role character varying,
|
||||
role_hierarchy_level integer,
|
||||
primary_owner_user_id uuid,
|
||||
subscription_status medreport.subscription_status,
|
||||
permissions medreport.app_permissions[],
|
||||
account_role varchar,
|
||||
application_role medreport.application_role
|
||||
)
|
||||
LANGUAGE plpgsql
|
||||
SET search_path TO ''
|
||||
AS $function$begin
|
||||
return QUERY
|
||||
select
|
||||
accounts.id,
|
||||
accounts.name,
|
||||
accounts.picture_url,
|
||||
accounts.slug,
|
||||
roles.name,
|
||||
roles.hierarchy_level,
|
||||
accounts.primary_owner_user_id,
|
||||
subscriptions.status,
|
||||
array_agg(role_permissions.permission),
|
||||
accounts_memberships.account_role,
|
||||
accounts.application_role
|
||||
from
|
||||
medreport.accounts
|
||||
join medreport.accounts_memberships on accounts.id = accounts_memberships.account_id
|
||||
left join medreport.subscriptions on accounts.id = subscriptions.account_id
|
||||
join medreport.roles on accounts_memberships.account_role = roles.name
|
||||
left join medreport.role_permissions on accounts_memberships.account_role = role_permissions.role
|
||||
where
|
||||
accounts.slug = account_slug
|
||||
and medreport.accounts_memberships.user_id = (select auth.uid())
|
||||
group by
|
||||
accounts.id,
|
||||
accounts_memberships.account_role,
|
||||
subscriptions.status,
|
||||
roles.hierarchy_level,
|
||||
roles.name;
|
||||
end;$function$;
|
||||
|
||||
GRANT EXECUTE ON FUNCTION medreport.team_account_workspace(text) TO authenticated, service_role;
|
||||
Reference in New Issue
Block a user