feat(MED-122): update current user account loader
This commit is contained in:
@@ -1,14 +1,13 @@
|
|||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
|
|
||||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
import { PageBody, PageHeader } from '@kit/ui/page';
|
import { PageBody, PageHeader } from '@kit/ui/page';
|
||||||
import { Trans } from '@kit/ui/trans';
|
import { Trans } from '@kit/ui/trans';
|
||||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
|
||||||
import { toTitleCase } from '@/lib/utils';
|
import { toTitleCase } from '@/lib/utils';
|
||||||
|
|
||||||
import Dashboard from '../_components/dashboard';
|
import Dashboard from '../_components/dashboard';
|
||||||
|
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
|
||||||
|
|
||||||
export const generateMetadata = async () => {
|
export const generateMetadata = async () => {
|
||||||
const i18n = await createI18nServerInstance();
|
const i18n = await createI18nServerInstance();
|
||||||
@@ -20,7 +19,7 @@ export const generateMetadata = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function UserHomePage() {
|
async function UserHomePage() {
|
||||||
const account = await getAccount();
|
const account = await loadCurrentUserAccount();
|
||||||
if (!account) {
|
if (!account) {
|
||||||
redirect('/');
|
redirect('/');
|
||||||
}
|
}
|
||||||
@@ -44,21 +43,4 @@ async function UserHomePage() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAccount() {
|
|
||||||
const client = getSupabaseServerClient();
|
|
||||||
|
|
||||||
const {
|
|
||||||
data: { user },
|
|
||||||
} = await client.auth.getUser();
|
|
||||||
|
|
||||||
const accountResponse = await client
|
|
||||||
.schema('medreport')
|
|
||||||
.from('accounts')
|
|
||||||
.select('name')
|
|
||||||
.eq('primary_owner_user_id', user!.id)
|
|
||||||
.single();
|
|
||||||
|
|
||||||
return accountResponse.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default withI18n(UserHomePage);
|
export default withI18n(UserHomePage);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { cache } from 'react';
|
|||||||
|
|
||||||
import { createAccountsApi } from '@kit/accounts/api';
|
import { createAccountsApi } from '@kit/accounts/api';
|
||||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
import { requireUserInServerComponent } from '@/lib/server/require-user-in-server-component';
|
||||||
|
|
||||||
export type UserAccount = Awaited<ReturnType<typeof loadUserAccount>>;
|
export type UserAccount = Awaited<ReturnType<typeof loadUserAccount>>;
|
||||||
|
|
||||||
@@ -13,6 +14,13 @@ export type UserAccount = Awaited<ReturnType<typeof loadUserAccount>>;
|
|||||||
*/
|
*/
|
||||||
export const loadUserAccount = cache(accountLoader);
|
export const loadUserAccount = cache(accountLoader);
|
||||||
|
|
||||||
|
export async function loadCurrentUserAccount() {
|
||||||
|
const user = await requireUserInServerComponent();
|
||||||
|
return user?.identities?.[0]?.id
|
||||||
|
? await loadUserAccount(user?.identities?.[0]?.id)
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
async function accountLoader(accountId: string) {
|
async function accountLoader(accountId: string) {
|
||||||
const client = getSupabaseServerClient();
|
const client = getSupabaseServerClient();
|
||||||
const api = createAccountsApi(client);
|
const api = createAccountsApi(client);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { requireUserInServerComponent } from '../../lib/server/require-user-in-server-component';
|
import { requireUserInServerComponent } from '@/lib/server/require-user-in-server-component';
|
||||||
import ConsentDialog from './(user)/_components/consent-dialog';
|
import ConsentDialog from './(user)/_components/consent-dialog';
|
||||||
import { loadUserAccount } from './(user)/_lib/server/load-user-account';
|
import { loadCurrentUserAccount } from './(user)/_lib/server/load-user-account';
|
||||||
|
|
||||||
export default async function HomeLayout({
|
export default async function HomeLayout({
|
||||||
children,
|
children,
|
||||||
@@ -8,9 +8,7 @@ export default async function HomeLayout({
|
|||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}) {
|
}) {
|
||||||
const user = await requireUserInServerComponent();
|
const user = await requireUserInServerComponent();
|
||||||
const account = user?.identities?.[0]?.id
|
const account = await loadCurrentUserAccount()
|
||||||
? await loadUserAccount(user?.identities?.[0]?.id)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (account && account?.has_consent_anonymized_company_statistics === null) {
|
if (account && account?.has_consent_anonymized_company_statistics === null) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -117,6 +117,7 @@ class AccountsApi {
|
|||||||
*/
|
*/
|
||||||
async getSubscription(accountId: string) {
|
async getSubscription(accountId: string) {
|
||||||
const response = await this.client
|
const response = await this.client
|
||||||
|
.schema('medreport')
|
||||||
.from('subscriptions')
|
.from('subscriptions')
|
||||||
.select('*, items: subscription_items !inner (*)')
|
.select('*, items: subscription_items !inner (*)')
|
||||||
.eq('account_id', accountId)
|
.eq('account_id', accountId)
|
||||||
|
|||||||
Reference in New Issue
Block a user