Skip to content

Commit 76f4ec1

Browse files
authored
Merge pull request #2670 from appwrite/fix-direct-org-hits
2 parents 5eac7d7 + d3bd71e commit 76f4ec1

File tree

3 files changed

+98
-9
lines changed

3 files changed

+98
-9
lines changed

src/lib/stores/organization.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { page } from '$app/stores';
2-
import { derived, writable } from 'svelte/store';
3-
import type { Models } from '@appwrite.io/console';
42
import type { Tier } from './billing';
53
import type { Plan } from '$lib/sdk/billing';
4+
import { derived, writable } from 'svelte/store';
5+
import { type Models, Platform } from '@appwrite.io/console';
66

77
export type OrganizationError = {
88
status: number;
@@ -35,6 +35,7 @@ export type Organization = Models.Team<Record<string, unknown>> & {
3535
status: string;
3636
remarks: string;
3737
projects: string[];
38+
platform: Platform;
3839
};
3940

4041
export type OrganizationList = {

src/routes/(console)/organization-[organization]/+layout.ts

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Dependencies } from '$lib/constants';
22
import { failedInvoice } from '$lib/stores/billing';
33
import { isCloud } from '$lib/system';
44
import { sdk } from '$lib/stores/sdk';
5-
import { error } from '@sveltejs/kit';
5+
import { error, redirect } from '@sveltejs/kit';
66
import type { LayoutLoad } from './$types';
77
import Breadcrumbs from './breadcrumbs.svelte';
88
import Header from './header.svelte';
@@ -14,14 +14,18 @@ import { defaultRoles, defaultScopes } from '$lib/constants';
1414
import type { Plan } from '$lib/sdk/billing';
1515
import { loadAvailableRegions } from '$routes/(console)/regions';
1616
import type { Organization } from '$lib/stores/organization';
17+
import { Platform } from '@appwrite.io/console';
18+
import { resolve } from '$app/paths';
1719

1820
export 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+
}

src/routes/(console)/project-[region]-[project]/+layout.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { Dependencies } from '$lib/constants';
1+
import { defaultRoles, defaultScopes, Dependencies } from '$lib/constants';
22
import { sdk } from '$lib/stores/sdk';
33
import type { LayoutLoad } from './$types';
44
import { preferences } from '$lib/stores/preferences';
55
import { failedInvoice } from '$lib/stores/billing';
66
import { isCloud } from '$lib/system';
7-
import { defaultRoles, defaultScopes } from '$lib/constants';
87
import { get } from 'svelte/store';
98
import { headerAlert } from '$lib/stores/headerAlert';
109
import PaymentFailed from '$lib/components/billing/alerts/paymentFailed.svelte';
1110
import { loadAvailableRegions } from '$routes/(console)/regions';
1211
import type { Organization, OrganizationList } from '$lib/stores/organization';
12+
import { Platform } from '@appwrite.io/console';
13+
import { redirect } from '@sveltejs/kit';
14+
import { resolve } from '$app/paths';
1315

1416
export const load: LayoutLoad = async ({ params, depends, parent }) => {
1517
const { plansInfo, organizations, preferences: prefs } = await parent();
@@ -38,6 +40,17 @@ export const load: LayoutLoad = async ({ params, depends, parent }) => {
3840

3941
if (!organization) organization = org;
4042

43+
// not the right organization project based on platform,
44+
// redirect to organization, and it should handle the rest!
45+
if (organization.platform !== Platform.Appwrite) {
46+
redirect(
47+
303,
48+
resolve('/(console)/organization-[organization]', {
49+
organization: organization.$id
50+
})
51+
);
52+
}
53+
4154
// fetch if not available in `plansInfo`.
4255
// out of promise.all because we filter orgs based on platform now!
4356
const organizationPlan = includedInBasePlans

0 commit comments

Comments
 (0)