From 8a69bf00a07a5d7aa89f607d08cd5dc84d345f00 Mon Sep 17 00:00:00 2001
From: ernstmul
Date: Wed, 20 Nov 2024 15:09:44 +0100
Subject: [PATCH 1/3] Fix missing limits data for non-standard tiers
---
src/lib/stores/billing.ts | 6 ++-
.../usage/[[invoice]]/+page.svelte | 51 ++++---------------
.../usage/[[invoice]]/+page.ts | 6 ++-
3 files changed, 18 insertions(+), 45 deletions(-)
diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts
index e6688ea06..c7ab3752a 100644
--- a/src/lib/stores/billing.ts
+++ b/src/lib/stores/billing.ts
@@ -130,12 +130,14 @@ export type PlanServices =
| 'usersAddon'
| 'webhooks';
-export function getServiceLimit(serviceId: PlanServices, tier: Tier = null): number {
+export function getServiceLimit(serviceId: PlanServices, tier: Tier = null, plan?: Plan): number {
if (!isCloud) return 0;
if (!serviceId) return 0;
const info = get(plansInfo);
if (!info) return 0;
- const plan = info.get(tier ?? get(organization)?.billingPlan);
+ if (!plan) {
+ plan = info.get(tier ?? get(organization)?.billingPlan);
+ }
return plan?.[serviceId];
}
diff --git a/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.svelte b/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.svelte
index 8dddbb419..82c211c19 100644
--- a/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.svelte
+++ b/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.svelte
@@ -4,7 +4,7 @@
import {
getServiceLimit,
showUsageRatesModal,
- tierToPlan,
+ type Tier,
upgradeURL
} from '$lib/stores/billing';
import { organization } from '$lib/stores/organization';
@@ -21,23 +21,10 @@
export let data;
- const tier = data?.currentInvoice?.plan ?? $organization?.billingPlan;
- const plan = tierToPlan(tier).name;
-
- // let invoice = null;
- // async function handlePeriodChange() {
- // const target = invoice
- // ? `/console/organization-${$organization.$id}/usage/${invoice}`
- // : `/console/organization-${$organization.$id}/usage`;
- // if ($page.url.pathname !== target) {
- // await goto(target);
- // }
- // }
-
- // const cycles = data.invoices.invoices.map((invoice) => ({
- // label: toLocaleDate(invoice.from),
- // value: invoice.$id
- // }));
+ const tier = data?.plan
+ ? (data.plan.$id as Tier)
+ : (data?.currentInvoice?.plan ?? $organization?.billingPlan);
+ const plan = data?.plan ?? undefined;
$: project = (data.organizationUsage as OrganizationUsage).projects;
@@ -78,29 +65,11 @@
{:else if $organization.billingPlan === BillingPlan.FREE}
- If you exceed the limits of the {plan} plan, services for your organization's projects
+ If you exceed the limits of the Free plan, services for your organization's projects
may be disrupted.
Upgrade for greater capacity.
{/if}
-
-
@@ -115,7 +84,7 @@
{#if data.organizationUsage.bandwidth}
{@const current = total(data.organizationUsage.bandwidth)}
{@const currentHumanized = humanFileSize(current)}
- {@const max = getServiceLimit('bandwidth', tier)}
+ {@const max = getServiceLimit('bandwidth', tier, plan)}
{#if data.organizationUsage.users}
{@const current = data.organizationUsage.usersTotal}
- {@const max = getServiceLimit('users', tier)}
+ {@const max = getServiceLimit('users', tier, plan)}
{#if data.organizationUsage.executionsTotal}
{@const current = data.organizationUsage.executionsTotal}
- {@const max = getServiceLimit('executions', tier)}
+ {@const max = getServiceLimit('executions', tier, plan)}
{
@@ -41,6 +41,7 @@ export const load: PageLoad = async ({ params, parent }) => {
startDate = currentInvoice.from;
endDate = currentInvoice.to;
}
+ const plan = await sdk.forConsole.billing.getPlan(org.$id);
const [invoices, usage, organizationMembers] = await Promise.all([
sdk.forConsole.billing.listInvoices(org.$id, [Query.orderDesc('from')]),
@@ -77,6 +78,7 @@ export const load: PageLoad = async ({ params, parent }) => {
projectNames,
invoices,
currentInvoice,
- organizationMembers
+ organizationMembers,
+ plan
};
};
From 75f4b07d4db5fe57df689c8978fcc6fdf3e3b93d Mon Sep 17 00:00:00 2001
From: ernstmul
Date: Wed, 20 Nov 2024 15:18:24 +0100
Subject: [PATCH 2/3] Move to promise.all
---
.../organization-[organization]/usage/[[invoice]]/+page.ts | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.ts b/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.ts
index 1e6f2cfae..9ffafbac2 100644
--- a/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.ts
+++ b/src/routes/(console)/organization-[organization]/usage/[[invoice]]/+page.ts
@@ -41,12 +41,11 @@ export const load: PageLoad = async ({ params, parent }) => {
startDate = currentInvoice.from;
endDate = currentInvoice.to;
}
- const plan = await sdk.forConsole.billing.getPlan(org.$id);
-
- const [invoices, usage, organizationMembers] = await Promise.all([
+ const [invoices, usage, organizationMembers, plan] = await Promise.all([
sdk.forConsole.billing.listInvoices(org.$id, [Query.orderDesc('from')]),
sdk.forConsole.billing.listUsage(params.organization, startDate, endDate),
- sdk.forConsole.teams.listMemberships(params.organization)
+ sdk.forConsole.teams.listMemberships(params.organization),
+ sdk.forConsole.billing.getPlan(org.$id)
]);
const projectNames: { [key: string]: Models.Project } = {};
From e935fe066b3905a987936ef78abb83a1f8ef8aa4 Mon Sep 17 00:00:00 2001
From: Ernst Mulders
Date: Wed, 20 Nov 2024 15:30:19 +0100
Subject: [PATCH 3/3] Update src/lib/stores/billing.ts
code suggestion
Co-authored-by: Torsten Dittmann
---
src/lib/stores/billing.ts | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts
index c7ab3752a..c14b4b949 100644
--- a/src/lib/stores/billing.ts
+++ b/src/lib/stores/billing.ts
@@ -135,9 +135,7 @@ export function getServiceLimit(serviceId: PlanServices, tier: Tier = null, plan
if (!serviceId) return 0;
const info = get(plansInfo);
if (!info) return 0;
- if (!plan) {
- plan = info.get(tier ?? get(organization)?.billingPlan);
- }
+ plan ??= info.get(tier ?? get(organization)?.billingPlan);
return plan?.[serviceId];
}