Skip to content

Commit 8767660

Browse files
authored
Merge pull request #2667 from appwrite/fix-wrong-platform-hit
2 parents ebbfca1 + 10e28a2 commit 8767660

3 files changed

Lines changed: 91 additions & 7 deletions

File tree

src/lib/stores/feedback.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export type FeedbackOption = {
3030
export const feedbackOptions: FeedbackOption[] = [
3131
{
3232
type: 'general',
33-
desc: `Imagine evolves with your input. Share your thoughts and help us improve Imagine.`,
33+
desc: `${resolvedProfile.platform} evolves with your input. Share your thoughts and help us improve ${resolvedProfile.platform}.`,
3434
component: FeedbackGeneral
3535
},
3636
{
3737
type: 'nps',
38-
desc: `How likely are you to recommend Imagine to a friend or colleague?`,
38+
desc: `How likely are you to recommend ${resolvedProfile.platform} to a friend or colleague?`,
3939
component: FeedbackNps
4040
}
4141
];

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

Lines changed: 78 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';
@@ -15,14 +15,17 @@ import type { Plan } from '$lib/sdk/billing';
1515
import { loadAvailableRegions } from '$routes/(console)/regions';
1616
import type { Organization } from '$lib/stores/organization';
1717
import { resolvedProfile } from '$lib/profiles/index.svelte';
18+
import { resolve } from '$app/paths';
1819

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ export const load: LayoutLoad = async ({ params, route, depends, parent }) => {
6363

6464
if (!organization) organization = org;
6565

66+
// not the right organization project based on platform,
67+
// redirect to organization, and it should handle the rest!
68+
if (organization.platform !== resolvedProfile.organizationPlatform) {
69+
redirect(
70+
303,
71+
resolve('/(console)/organization-[organization]', {
72+
organization: organization.$id
73+
})
74+
);
75+
}
76+
6677
// fetch if not available in `plansInfo`.
6778
// out of promise.all because we filter orgs based on platform now!
6879
const organizationPlan = includedInBasePlans

0 commit comments

Comments
 (0)