@@ -2,7 +2,7 @@ import { Dependencies } from '$lib/constants';
22import { failedInvoice } from '$lib/stores/billing' ;
33import { isCloud } from '$lib/system' ;
44import { sdk } from '$lib/stores/sdk' ;
5- import { error } from '@sveltejs/kit' ;
5+ import { error , redirect } from '@sveltejs/kit' ;
66import type { LayoutLoad } from './$types' ;
77import Breadcrumbs from './breadcrumbs.svelte' ;
88import Header from './header.svelte' ;
@@ -14,14 +14,18 @@ import { defaultRoles, defaultScopes } from '$lib/constants';
1414import type { Plan } from '$lib/sdk/billing' ;
1515import { loadAvailableRegions } from '$routes/(console)/regions' ;
1616import type { Organization } from '$lib/stores/organization' ;
17+ import { Platform } from '@appwrite.io/console' ;
18+ import { resolve } from '$app/paths' ;
1719
1820export const load : LayoutLoad = async ( { params, depends, parent } ) => {
19- const { preferences : prefs } = await parent ( ) ;
21+ const { organizations , preferences : prefs } = await parent ( ) ;
2022
2123 depends ( Dependencies . ORGANIZATION ) ;
2224 depends ( Dependencies . MEMBERS ) ;
2325 depends ( Dependencies . PAYMENT_METHODS ) ;
2426
27+ const requestedOrg = await checkPlatformAndRedirect ( params , organizations , prefs ) ;
28+
2529 let roles = isCloud ? [ ] : defaultRoles ;
2630 let scopes = isCloud ? [ ] : defaultScopes ;
2731 let currentPlan : Plan = null ;
@@ -42,8 +46,13 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
4246 sdk . forConsole . account . updatePrefs ( { prefs : newPrefs } ) ;
4347 }
4448
45- const [ organization , members , countryList , locale ] = await Promise . all ( [
46- sdk . forConsole . teams . get ( { teamId : params . organization } ) as Promise < Organization > ,
49+ // fetch org only if we haven't already fetched it for platform check
50+ const orgPromise : Promise < Organization > = requestedOrg
51+ ? Promise . resolve ( requestedOrg )
52+ : ( sdk . forConsole . teams . get ( { teamId : params . organization } ) as Promise < Organization > ) ;
53+
54+ const [ org , members , countryList , locale ] = await Promise . all ( [
55+ orgPromise ,
4756 sdk . forConsole . teams . listMemberships ( { teamId : params . organization } ) ,
4857 sdk . forConsole . locale . listCountries ( ) ,
4958 sdk . forConsole . locale . get ( ) ,
@@ -54,7 +63,7 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
5463 return {
5564 header : Header ,
5665 breadcrumbs : Breadcrumbs ,
57- organization,
66+ organization : org ,
5867 currentPlan,
5968 members,
6069 roles,
@@ -82,3 +91,69 @@ function loadFailedInvoices(teamId: string) {
8291 }
8392 } ) ;
8493}
94+
95+ async function checkPlatformAndRedirect (
96+ params : { organization : string } ,
97+ organizations : { teams : Array < { $id : string ; platform ?: string } > } ,
98+ prefs : Record < string , string >
99+ ) : Promise < Organization | null > {
100+ // check if preloaded
101+ let requestedOrg = organizations . teams . find ( ( team ) => team . $id === params . organization ) as
102+ | Organization
103+ | undefined ;
104+
105+ // not found, load!
106+ if ( ! requestedOrg ) {
107+ try {
108+ requestedOrg = ( await sdk . forConsole . teams . get ( {
109+ teamId : params . organization
110+ } ) ) as Organization ;
111+ } catch ( e ) {
112+ return null ;
113+ }
114+ }
115+
116+ if ( requestedOrg && requestedOrg . platform !== Platform . Appwrite ) {
117+ const orgIdInPrefs = prefs . organization ;
118+
119+ // grab the first org with matching platform
120+ const samePlatformOrganization = organizations . teams . find (
121+ ( team ) => team . platform === Platform . Appwrite
122+ ) ;
123+
124+ // exists, lets redirect!
125+ if ( samePlatformOrganization ) {
126+ redirect (
127+ 303 ,
128+ resolve ( '/(console)/organization-[organization]' , {
129+ organization : samePlatformOrganization . $id
130+ } )
131+ ) ;
132+ }
133+ // not in list, check prefs
134+ else if ( orgIdInPrefs ) {
135+ try {
136+ // check if exists and is valid
137+ const orgFromPrefs = ( await sdk . forConsole . teams . get ( {
138+ teamId : orgIdInPrefs
139+ } ) ) as Organization ;
140+
141+ // exists and is valid, redirect
142+ redirect (
143+ 303 ,
144+ resolve ( '/(console)/organization-[organization]' , {
145+ organization : orgFromPrefs . $id
146+ } )
147+ ) ;
148+ } catch ( e ) {
149+ redirect ( 303 , resolve ( '/(console)' ) ) ;
150+ }
151+ } else {
152+ redirect ( 303 , resolve ( '/(console)' ) ) ;
153+ }
154+ }
155+
156+ // send the org,
157+ // if already in the full list so we don't have to make another API request.
158+ return requestedOrg ;
159+ }
0 commit comments