mirror of
https://github.com/appwrite/console.git
synced 2026-06-06 19:27:48 +00:00
update: address comments.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import type { Models } from '@appwrite.io/console';
|
||||
|
||||
export function makePlansMap(
|
||||
plansArray: Models.BillingPlanList | null
|
||||
): Map<string, Models.BillingPlan> {
|
||||
const plansMap = new Map<string, Models.BillingPlan>();
|
||||
if (!plansArray?.plans.length) return plansMap;
|
||||
|
||||
const plans = plansArray.plans;
|
||||
for (let index = 0; index < plans.length; index++) {
|
||||
const plan = plans[index];
|
||||
plansMap.set(plan.$id, plan);
|
||||
}
|
||||
|
||||
return plansMap;
|
||||
}
|
||||
@@ -88,12 +88,8 @@ export function planHasGroup(billingPlanId: string, group: BillingPlanGroup) {
|
||||
export function getBasePlanFromGroup(billingPlanGroup: BillingPlanGroup): Models.BillingPlan {
|
||||
const plansInfoStore = get(plansInfo);
|
||||
|
||||
// hot fix for now, starter doesn't have a group atm.
|
||||
const correctBillingPlanGroup =
|
||||
billingPlanGroup === BillingPlanGroup.Starter ? null : billingPlanGroup;
|
||||
|
||||
const proPlans = Array.from(plansInfoStore.values()).filter(
|
||||
(plan) => plan.group === correctBillingPlanGroup
|
||||
(plan) => plan.group === billingPlanGroup
|
||||
);
|
||||
|
||||
return proPlans.sort((a, b) => a.order - b.order)[0];
|
||||
@@ -530,12 +526,14 @@ export function checkForMarkedForDeletion(org: Models.Organization) {
|
||||
}
|
||||
|
||||
export async function checkForMissingPaymentMethod() {
|
||||
const starterPlan = getBasePlanFromGroup(BillingPlanGroup.Starter);
|
||||
|
||||
const orgs = await sdk.forConsole.organizations.list({
|
||||
queries: [
|
||||
Query.notEqual('billingPlan', getBasePlanFromGroup(BillingPlanGroup.Starter).$id),
|
||||
Query.isNull('paymentMethodId'),
|
||||
Query.isNull('backupPaymentMethodId'),
|
||||
Query.equal('platform', Platform.Appwrite)
|
||||
Query.equal('platform', Platform.Appwrite),
|
||||
Query.notEqual('billingPlan', starterPlan.$id)
|
||||
]
|
||||
});
|
||||
|
||||
|
||||
@@ -2,30 +2,38 @@ import { sdk } from '$lib/stores/sdk';
|
||||
import { isCloud } from '$lib/system';
|
||||
import type { LayoutLoad } from './$types';
|
||||
import { Dependencies } from '$lib/constants';
|
||||
import { type Models, Platform, Query } from '@appwrite.io/console';
|
||||
import { Platform, Query } from '@appwrite.io/console';
|
||||
import { makePlansMap } from '$lib/helpers/billing';
|
||||
|
||||
export const load: LayoutLoad = async ({ depends, parent }) => {
|
||||
const { organizations } = await parent();
|
||||
const { organizations, plansInfo } = await parent();
|
||||
|
||||
depends(Dependencies.RUNTIMES);
|
||||
depends(Dependencies.CONSOLE_VARIABLES);
|
||||
depends(Dependencies.ORGANIZATION);
|
||||
|
||||
const { endpoint, project } = sdk.forConsole.client.config;
|
||||
|
||||
const plansArrayPromise =
|
||||
plansInfo || !isCloud
|
||||
? null
|
||||
: sdk.forConsole.console.getPlans({
|
||||
platform: Platform.Appwrite
|
||||
});
|
||||
|
||||
const [preferences, plansArray, versionData, consoleVariables] = await Promise.all([
|
||||
sdk.forConsole.account.getPrefs(),
|
||||
isCloud
|
||||
? sdk.forConsole.console.getPlans({
|
||||
platform: Platform.Appwrite
|
||||
})
|
||||
: null,
|
||||
plansArrayPromise,
|
||||
fetch(`${endpoint}/health/version`, {
|
||||
headers: { 'X-Appwrite-Project': project as string }
|
||||
}).then((response) => response.json() as { version?: string }),
|
||||
sdk.forConsole.console.variables()
|
||||
]);
|
||||
|
||||
const plansInfo = toPlanMap(plansArray);
|
||||
let fallbackPlansInfoArray = plansInfo;
|
||||
if (!fallbackPlansInfoArray) {
|
||||
fallbackPlansInfoArray = makePlansMap(plansArray);
|
||||
}
|
||||
|
||||
const currentOrgId =
|
||||
preferences.organization ??
|
||||
@@ -50,27 +58,14 @@ export const load: LayoutLoad = async ({ depends, parent }) => {
|
||||
}
|
||||
|
||||
return {
|
||||
plansInfo,
|
||||
roles: [],
|
||||
scopes: [],
|
||||
preferences,
|
||||
currentOrgId,
|
||||
organizations,
|
||||
consoleVariables,
|
||||
version: versionData?.version ?? null,
|
||||
allProjectsCount: projectsCount
|
||||
allProjectsCount: projectsCount,
|
||||
plansInfo: fallbackPlansInfoArray,
|
||||
version: versionData?.version ?? null
|
||||
};
|
||||
};
|
||||
|
||||
function toPlanMap(plansArray: Models.BillingPlanList | null): Map<string, Models.BillingPlan> {
|
||||
const map = new Map<string, Models.BillingPlan>();
|
||||
if (!plansArray?.plans.length) return map;
|
||||
|
||||
const plans = plansArray.plans;
|
||||
for (let i = 0; i < plans.length; i++) {
|
||||
const plan = plans[i];
|
||||
map.set(plan.$id, plan);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
+22
-5
@@ -6,12 +6,13 @@ import { redirect } from '@sveltejs/kit';
|
||||
import { Dependencies } from '$lib/constants';
|
||||
import type { LayoutLoad } from './$types';
|
||||
import { redirectTo } from './store';
|
||||
import { base, resolve } from '$app/paths';
|
||||
import { resolve } from '$app/paths';
|
||||
import type { Account } from '$lib/stores/user';
|
||||
import { type AppwriteException } from '@appwrite.io/console';
|
||||
import { type AppwriteException, Platform } from '@appwrite.io/console';
|
||||
import { isCloud, VARS } from '$lib/system';
|
||||
import { checkPricingRefAndRedirect } from '$lib/helpers/pricingRedirect';
|
||||
import { getTeamOrOrganizationList } from '$lib/stores/organization';
|
||||
import { makePlansMap } from '$lib/helpers/billing';
|
||||
|
||||
export const ssr = false;
|
||||
|
||||
@@ -39,7 +40,10 @@ export const load: LayoutLoad = async ({ depends, url, route }) => {
|
||||
}
|
||||
}
|
||||
|
||||
const plansInfo = await getPlatformPlans();
|
||||
|
||||
return {
|
||||
plansInfo,
|
||||
account: account,
|
||||
organizations: await getTeamOrOrganizationList()
|
||||
};
|
||||
@@ -51,11 +55,13 @@ export const load: LayoutLoad = async ({ depends, url, route }) => {
|
||||
}
|
||||
|
||||
if (error.type === 'user_more_factors_required') {
|
||||
if (url.pathname === `${base}/mfa`)
|
||||
const mfaUrl = resolve('/(authenticated)/mfa');
|
||||
|
||||
if (url.pathname === mfaUrl)
|
||||
return {
|
||||
mfaRequired: true
|
||||
};
|
||||
redirect(303, withParams(`${base}/mfa`, url.searchParams));
|
||||
redirect(303, withParams(mfaUrl, url.searchParams));
|
||||
}
|
||||
|
||||
if (!isPublicRoute) {
|
||||
@@ -63,7 +69,8 @@ export const load: LayoutLoad = async ({ depends, url, route }) => {
|
||||
checkPricingRefAndRedirect(url.searchParams, true);
|
||||
}
|
||||
|
||||
redirect(303, withParams(`${base}/login`, url.searchParams));
|
||||
const loginUrl = resolve('/(public)/(guest)/login');
|
||||
redirect(303, withParams(loginUrl, url.searchParams));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -71,3 +78,13 @@ function withParams(pathname: string, searchParams: URLSearchParams) {
|
||||
if (searchParams.size > 0) return `${pathname}?${searchParams.toString()}`;
|
||||
return pathname;
|
||||
}
|
||||
|
||||
async function getPlatformPlans() {
|
||||
if (!isCloud) return null;
|
||||
|
||||
const plansArray = await sdk.forConsole.console.getPlans({
|
||||
platform: Platform.Appwrite
|
||||
});
|
||||
|
||||
return makePlansMap(plansArray);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user