diff --git a/apps/dashboard/src/lib/utils/services/org/index.ts b/apps/dashboard/src/lib/utils/services/org/index.ts index 3af9044c..f51d4c25 100644 --- a/apps/dashboard/src/lib/utils/services/org/index.ts +++ b/apps/dashboard/src/lib/utils/services/org/index.ts @@ -1,5 +1,5 @@ import { get } from 'svelte/store'; - +import type { PostgrestError } from '@supabase/supabase-js'; import { goto } from '$app/navigation'; import { supabase } from '$lib/utils/functions/supabase'; import { orgs, currentOrg, orgAudience, orgTeam } from '$lib/utils/store/org'; @@ -204,34 +204,42 @@ export async function getCourseBySiteName(siteName: string) { return data; } +const CURRENT_ORG_QUERY = ` + id, + name, + siteName, + avatar_url, + landingpage, + is_restricted, + customization, + theme, + favicon, + customDomain, + isCustomDomainVerified, + customCode, + organization_plan( + plan_name, + is_active + ) +`; export async function getCurrentOrg(siteName: string, justGet = false, isCustomDomain = false) { - const { data, error } = await supabase - .from('organization') - .select( - ` - id, - name, - siteName, - avatar_url, - landingpage, - is_restricted, - customization, - theme, - favicon, - customDomain, - isCustomDomainVerified, - customCode, - organization_plan( - plan_name, - is_active - ) - ` - ) - .eq(isCustomDomain ? 'customDomain' : 'siteName', siteName) - .filter('isCustomDomainVerified', 'eq', isCustomDomain ? true : false) - .returns(); - console.log('data =', data); - console.log('error =', error); + let response: { data: CurrentOrg[] | null; error: PostgrestError | null } | null = null; + + if (isCustomDomain) { + response = await supabase + .from('organization') + .select(CURRENT_ORG_QUERY) + .eq('customDomain', siteName) + .filter('isCustomDomainVerified', 'eq', true) + .returns(); + } else { + response = await supabase + .from('organization') + .select(CURRENT_ORG_QUERY) + .eq('siteName', siteName) + .returns(); + } + const { data, error } = response; const isDataEmpty = !data?.[0]; diff --git a/apps/dashboard/src/routes/+layout.svelte b/apps/dashboard/src/routes/+layout.svelte index f131d16d..de9cc2da 100644 --- a/apps/dashboard/src/routes/+layout.svelte +++ b/apps/dashboard/src/routes/+layout.svelte @@ -272,15 +272,11 @@ // } }); - if (data.isOrgSite) { - if (!data.org) { - goto('/404'); - } else { - $globalStore.orgSiteName = data.orgSiteName; - $globalStore.isOrgSite = data.isOrgSite; + if (data.isOrgSite && data.org) { + $globalStore.orgSiteName = data.orgSiteName; + $globalStore.isOrgSite = data.isOrgSite; - currentOrg.set(data.org); - } + currentOrg.set(data.org); } return () => { diff --git a/apps/dashboard/src/routes/invite/t/[hash]/+layout.server.ts b/apps/dashboard/src/routes/invite/t/[hash]/+layout.server.ts index c9c57ab5..93a00ac6 100644 --- a/apps/dashboard/src/routes/invite/t/[hash]/+layout.server.ts +++ b/apps/dashboard/src/routes/invite/t/[hash]/+layout.server.ts @@ -16,16 +16,18 @@ export const load = async ({ params = { hash: '' } }) => { const { orgId, email, orgSiteName } = JSON.parse(hashData); - const _currentOrg = await getCurrentOrg(orgSiteName, true); + const currentOrg = await getCurrentOrg(orgSiteName, true); const profile = await getProfile(email); console.log('profile data', profile); return { - orgId, - email, - currentOrg: _currentOrg, - profile + invite: { + orgId, + email, + currentOrg, + profile + } }; } catch (error) { console.error('Error decoding invite params.hash', error); diff --git a/apps/dashboard/src/routes/invite/t/[hash]/+page.svelte b/apps/dashboard/src/routes/invite/t/[hash]/+page.svelte index a06b9d54..b03dc57e 100644 --- a/apps/dashboard/src/routes/invite/t/[hash]/+page.svelte +++ b/apps/dashboard/src/routes/invite/t/[hash]/+page.svelte @@ -48,12 +48,12 @@ try { loading = true; - let profile: Profile | null = data.profile; + let profile: Profile | null = data.invite.profile; - if (!data.profile) { + if (!data.invite.profile) { // Signup const { data: signupData, error } = await supabase.auth.signUp({ - email: data.email, + email: data.invite.email, password: fields.password }); @@ -72,7 +72,7 @@ id: userId, username: fields.name.toLowerCase().replace(' ', '-') + new Date().getTime(), fullname: fields.name, - email: data.email + email: data.invite.email }) .select(); @@ -96,12 +96,12 @@ verified: true, profile_id: profile.id }) - .match({ email: data.email, organization_id: data.currentOrg?.id }); + .match({ email: data.invite.email, organization_id: data.invite.currentOrg?.id }); console.log('Update member response', updateMemberRes); const res = await supabase.auth.signInWithPassword({ - email: data.email, + email: data.invite.email, password: fields.password }); @@ -110,6 +110,8 @@ } formRef?.reset(); + + console.log({ path: $currentOrgPath }); return goto($currentOrgPath); } catch (error) { if (error instanceof Error) { @@ -124,20 +126,26 @@ function setCurOrg(cOrg: CurrentOrg) { if (!cOrg) return; + + console.log(cOrg); currentOrg.set(cOrg); } - onMount(() => { - setTheme(data.currentOrg?.theme || ''); + onMount(async () => { + await logout(false); - logout(false); + setTheme(data.invite.currentOrg?.theme || ''); + + setCurOrg(data.invite.currentOrg as CurrentOrg); }); $: errors.confirmPassword = getConfirmPasswordError(fields); $: disableSubmit = getDisableSubmit(fields); - $: setCurOrg(data.currentOrg as CurrentOrg); - $: console.log('data.profile', data.profile); + $: console.log('data', data.invite); + + $: console.log('org', $currentOrg); + $: console.log('path', $currentOrgPath); @@ -147,7 +155,7 @@

- {#if data.profile} + {#if data.invite.profile} Log in to join {:else} Create a free account to join @@ -155,14 +163,14 @@

- {#if !data.profile} + {#if !data.invite.profile} - {#if !data.profile} + {#if !data.invite.profile}