@@ -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' ;
@@ -15,14 +15,17 @@ import type { Plan } from '$lib/sdk/billing';
1515import { loadAvailableRegions } from '$routes/(console)/regions' ;
1616import type { Organization } from '$lib/stores/organization' ;
1717import { resolvedProfile } from '$lib/profiles/index.svelte' ;
18+ import { resolve } from '$app/paths' ;
1819
1920export const load : LayoutLoad = async ( { params, depends, parent } ) => {
20- const { preferences : prefs } = await parent ( ) ;
21+ const { organizations , preferences : prefs } = await parent ( ) ;
2122
2223 depends ( Dependencies . ORGANIZATION ) ;
2324 depends ( Dependencies . MEMBERS ) ;
2425 depends ( Dependencies . PAYMENT_METHODS ) ;
2526
27+ const requestedOrg = await checkPlatformAndRedirect ( params , organizations , prefs ) ;
28+
2629 let roles = isCloud ? [ ] : defaultRoles ;
2730 let scopes = isCloud ? [ ] : defaultScopes ;
2831 let currentPlan : Plan = null ;
@@ -38,6 +41,7 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
3841 loadFailedInvoices ( params . organization ) ;
3942 }
4043 }
44+
4145 if ( prefs [ resolvedProfile . organizationPrefKey ] !== params . organization ) {
4246 const newPrefs = {
4347 ...prefs ,
@@ -46,8 +50,13 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
4650 sdk . forConsole . account . updatePrefs ( { prefs : newPrefs } ) ;
4751 }
4852
49- const [ organization , members , countryList , locale ] = await Promise . all ( [
50- sdk . forConsole . teams . get ( { teamId : params . organization } ) as Promise < Organization > ,
53+ // fetch org only if we haven't already fetched it for platform check
54+ const orgPromise : Promise < Organization > = requestedOrg
55+ ? Promise . resolve ( requestedOrg )
56+ : ( sdk . forConsole . teams . get ( { teamId : params . organization } ) as Promise < Organization > ) ;
57+
58+ const [ org , members , countryList , locale ] = await Promise . all ( [
59+ orgPromise ,
5160 sdk . forConsole . teams . listMemberships ( { teamId : params . organization } ) ,
5261 sdk . forConsole . locale . listCountries ( ) ,
5362 sdk . forConsole . locale . get ( ) ,
@@ -58,7 +67,7 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
5867 return {
5968 header : Header ,
6069 breadcrumbs : Breadcrumbs ,
61- organization,
70+ organization : org ,
6271 currentPlan,
6372 members,
6473 roles,
@@ -91,3 +100,67 @@ function loadFailedInvoices(teamId: string) {
91100 }
92101 } ) ;
93102}
103+
104+ async function checkPlatformAndRedirect (
105+ params : { organization : string } ,
106+ organizations : { teams : Organization [ ] } ,
107+ prefs : Record < string , string >
108+ ) : Promise < Organization | null > {
109+ // check if preloaded
110+ let requestedOrg = organizations . teams . find ( ( team ) => team . $id === params . organization ) ;
111+
112+ // not found, load!
113+ if ( ! requestedOrg ) {
114+ try {
115+ requestedOrg = ( await sdk . forConsole . teams . get ( {
116+ teamId : params . organization
117+ } ) ) as Organization ;
118+ } catch ( e ) {
119+ return null ;
120+ }
121+ }
122+
123+ if ( requestedOrg && requestedOrg . platform !== resolvedProfile . organizationPlatform ) {
124+ const orgIdInPrefs = prefs [ resolvedProfile . organizationPrefKey ] ;
125+
126+ // grab the first org with matching platform
127+ const samePlatformOrganization = organizations . teams . find (
128+ ( team ) => team . platform === resolvedProfile . organizationPlatform
129+ ) ;
130+
131+ // exists, lets redirect!
132+ if ( samePlatformOrganization ) {
133+ redirect (
134+ 303 ,
135+ resolve ( '/(console)/organization-[organization]' , {
136+ organization : samePlatformOrganization . $id
137+ } )
138+ ) ;
139+ }
140+ // not in list, check prefs
141+ else if ( orgIdInPrefs ) {
142+ try {
143+ // check if exists and is valid
144+ const orgFromPrefs = ( await sdk . forConsole . teams . get ( {
145+ teamId : orgIdInPrefs
146+ } ) ) as Organization ;
147+
148+ // exists and is valid, redirect
149+ redirect (
150+ 303 ,
151+ resolve ( '/(console)/organization-[organization]' , {
152+ organization : orgFromPrefs . $id
153+ } )
154+ ) ;
155+ } catch ( e ) {
156+ redirect ( 303 , resolve ( '/(console)' ) ) ;
157+ }
158+ } else {
159+ redirect ( 303 , resolve ( '/(console)' ) ) ;
160+ }
161+ }
162+
163+ // send the org,
164+ // if already in the full list so we don't have to make another API request.
165+ return requestedOrg ;
166+ }
0 commit comments