From 1e21b57f34e4d527c5aa78f13b4f5a525d58daca Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 12 Mar 2025 06:34:05 +0000 Subject: [PATCH 01/13] Feat: show budget limit alert --- src/lib/stores/billing.ts | 22 ++++++++++++-- src/lib/stores/organization.ts | 1 + .../budgetLimitAlert.svelte | 30 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index dd698c912..65bed248c 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -32,6 +32,7 @@ import { organization, type Organization, type OrganizationError } from './organ import { canSeeBilling } from './roles'; import { sdk } from './sdk'; import { user } from './user'; +import BudgetLimitAlert from '$routes/(console)/organization-[organization]/budgetLimitAlert.svelte'; export type Tier = 'tier-0' | 'tier-1' | 'tier-2' | 'auto-1' | 'cont-1' | 'ent-1'; @@ -264,12 +265,27 @@ export function calculateTrialDay(org: Organization) { } export async function checkForUsageLimit(org: Organization) { - if (org?.billingPlan !== BillingPlan.FREE) { + if (!org?.billingLimits) { readOnly.set(false); return; } - if (!org?.billingLimits) { - readOnly.set(false); + if (org?.billingPlan !== BillingPlan.FREE) { + const { budgetLimit } = org?.billingLimits ?? {}; + + if (!budgetLimit || budgetLimit < 100) { + readOnly.set(false); + return; + + } + + headerAlert.add({ + id: 'budgetLimit', + component: BudgetLimitAlert, + show: true, + importance: 10 + }); + + readOnly.set(true); return; } const { bandwidth, documents, executions, storage, users } = org?.billingLimits ?? {}; diff --git a/src/lib/stores/organization.ts b/src/lib/stores/organization.ts index dcfd0a562..3f02fe50a 100644 --- a/src/lib/stores/organization.ts +++ b/src/lib/stores/organization.ts @@ -45,6 +45,7 @@ export type BillingLimits = { executions: number; storage: number; users: number; + budgetLimit: number; }; export const newOrgModal = writable(false); diff --git a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte new file mode 100644 index 000000000..b49bb49e1 --- /dev/null +++ b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte @@ -0,0 +1,30 @@ + + +{#if $organization?.$id && $organization?.billingPlan !== BillingPlan.FREE && $readOnly && !hideBillingHeaderRoutes.includes($page.url.pathname)} + + + This organization has reached its budget limit and is now blocked. To continue using Appwrite services, update the budget limit. + + + + + + +{/if} From 29046951943f65f3e744d2321e0113d73e21e1c7 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 12 Mar 2025 06:39:49 +0000 Subject: [PATCH 02/13] update --- .../organization-[organization]/budgetLimitAlert.svelte | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte index b49bb49e1..2ca0970db 100644 --- a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte +++ b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte @@ -9,18 +9,17 @@ {#if $organization?.$id && $organization?.billingPlan !== BillingPlan.FREE && $readOnly && !hideBillingHeaderRoutes.includes($page.url.pathname)} - + - This organization has reached its budget limit and is now blocked. To continue using Appwrite services, update the budget limit. + This organization has reached its budget limit and is now blocked. To continue using + Appwrite services, update the budget limit. - - - - - {#if alerts.length} - - - Alert at budget cap % - - - - {#each alerts.sort() as alert} - - - {alert}% - - - - - - {/each} - -
+

+ {#if !$currentPlan.budgeting} + Get notified by email when your organization meets a percentage of your budget + cap. {tierToPlan($organization.billingPlan).name} organizations will receive one + notification at 75% resource usage. + {:else} + Get notified by email when your organization meets or exceeds a percentage of + your specified billing alert(s). {/if} - {/if} - +

+ + {#if !$currentPlan.budgeting} + + Billing alerts are a Pro plan feature + + Upgrade to a Pro plan to manage when you receive billing alerts for your organization. + + {:else} + + + You can set a maximum of 4 billing alerts per organization. + - - {#if $organization?.billingPlan === BillingPlan.FREE || $organization?.billingPlan === BillingPlan.GITHUB_EDUCATION} - - {:else} - - {/if} - - - +
+ (search = selectedAlert.toString())} /> +
+ +
+
+
+ + {#if alerts.length} + + + Alert at budget cap % + + + + {#each alerts.sort() as alert} + + + {alert}% + + + + + + {/each} + +
+ {/if} + {/if} +
+ + + {#if $organization?.billingPlan === BillingPlan.FREE || $organization?.billingPlan === BillingPlan.GITHUB_EDUCATION} + + {:else} + + {/if} + + + + diff --git a/src/routes/(console)/organization-[organization]/billing/store.ts b/src/routes/(console)/organization-[organization]/billing/store.ts index ca081c291..9359f72e8 100644 --- a/src/routes/(console)/organization-[organization]/billing/store.ts +++ b/src/routes/(console)/organization-[organization]/billing/store.ts @@ -16,3 +16,5 @@ export const addCreditWizardStore = writable<{ coupon: string; paymentMethodId: export const selectedInvoice = writable(null); export const showRetryModal = writable(false); + +export const scrollToUpdateBudget = derived(page, ($page) => $page.url.hash.includes('#update-budget')); \ No newline at end of file diff --git a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte index fbe101f9b..bffa7e44a 100644 --- a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte +++ b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte @@ -19,7 +19,7 @@ View usage -
From 21a4d714a4642c366cd549813611a376db2bdbe1 Mon Sep 17 00:00:00 2001 From: Darshan Date: Tue, 18 Mar 2025 10:54:01 +0530 Subject: [PATCH 11/13] update: component to scroll to, better scroll logic. --- src/lib/elements/forms/button.svelte | 6 +- .../billing/budgetAlert.svelte | 205 ++++++++---------- .../billing/budgetCap.svelte | 147 +++++++------ .../budgetLimitAlert.svelte | 17 +- 4 files changed, 187 insertions(+), 188 deletions(-) diff --git a/src/lib/elements/forms/button.svelte b/src/lib/elements/forms/button.svelte index fc643e0af..b27dd441d 100644 --- a/src/lib/elements/forms/button.svelte +++ b/src/lib/elements/forms/button.svelte @@ -70,7 +70,8 @@ class={resolvedClasses} style="pointer-events: {internalDisabled ? 'none' : 'auto'};" aria-label={ariaLabel} - use:multiAction={actions}> + use:multiAction={actions} + {...$$restProps}> {:else} @@ -81,7 +82,8 @@ class={resolvedClasses} aria-label={ariaLabel} type={submit ? 'submit' : 'button'} - use:multiAction={actions}> + use:multiAction={actions} + {...$$restProps}> {#if $isSubmitting && submissionLoader} { - formHolderElement?.scrollIntoView({ - block: 'nearest' - }); - }); - } -
- - - Billing alerts + + + Billing alerts -

- {#if !$currentPlan.budgeting} - Get notified by email when your organization meets a percentage of your budget - cap. {tierToPlan($organization.billingPlan).name} organizations will receive one - notification at 75% resource usage. - {:else} - Get notified by email when your organization meets or exceeds a percentage of - your specified billing alert(s). - {/if} -

- - {#if !$currentPlan.budgeting} +

+ {#if !$currentPlan.budgeting} + Get notified by email when your organization meets a percentage of your budget cap. {tierToPlan($organization.billingPlan).name} organizations will receive one notification + at 75% resource usage. + {:else} + Get notified by email when your organization meets or exceeds a percentage of your + specified billing alert(s). + {/if} +

+ + {#if !$currentPlan.budgeting} + + Billing alerts are a Pro plan feature + + Upgrade to a Pro plan to manage when you receive billing alerts for your organization. + + {:else} + - Billing alerts are a Pro plan feature - - Upgrade to a Pro plan to manage when you receive billing alerts for your organization. + You can set a maximum of 4 billing alerts per organization. - {:else} - - - You can set a maximum of 4 billing alerts per organization. - -
- (search = selectedAlert.toString())} /> -
- -
+
+ (search = selectedAlert.toString())} /> +
+
- +
+ - {#if alerts.length} - - - Alert at budget cap % - - - - {#each alerts.sort() as alert} - - - {alert}% - - - - - - {/each} - -
- {/if} + {#if alerts.length} + + + Alert at budget cap % + + + + {#each alerts.sort() as alert} + + + {alert}% + + + + + + {/each} + +
{/if} - + {/if} + - - {#if $organization?.billingPlan === BillingPlan.FREE || $organization?.billingPlan === BillingPlan.GITHUB_EDUCATION} - - {:else} - - {/if} - - - -
+ + {#if $organization?.billingPlan === BillingPlan.FREE || $organization?.billingPlan === BillingPlan.GITHUB_EDUCATION} + + {:else} + + {/if} + +
+ diff --git a/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte b/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte index 3f3a5517f..007de8d7a 100644 --- a/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte +++ b/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte @@ -8,8 +8,10 @@ import { addNotification } from '$lib/stores/notifications'; import { organization, currentPlan } from '$lib/stores/organization'; import { sdk } from '$lib/stores/sdk'; - import { onMount } from 'svelte'; + import { onMount, tick } from 'svelte'; import BudgetAlert from './budgetAlert.svelte'; + import { page } from '$app/stores'; + import { scrollToUpdateBudget } from './store'; let capActive = false; let budget: number; @@ -47,72 +49,91 @@ $: if (!capActive) { budget = null; } + + let formHolderElement: HTMLDivElement; + + $: if ($scrollToUpdateBudget) { + // clean the url in the search bar. + history.replaceState(null, '', $page.url.pathname); + + tick().then(() => { + const elementTop = formHolderElement.getBoundingClientRect().top + window.scrollY; + // Offset 60 to prevent misalignment. + window.scrollTo({ top: elementTop - 60 }); + }); + } -
- - Budget cap +
+ + + Budget cap -

- Restrict your resource usage by setting a budget cap. Cap usage is reset at the - beginning of each billing cycle. -

- - {#if !$currentPlan.budgeting} - - - Budget caps are a Pro plan feature - - Upgrade to a Pro plan to set a budget cap for your organization. For more information - on what you can do with a Pro plan, - view our pricing guide. - - {:else} - - - - Budget cap limits do not include the base amount of your plan. +

+ Restrict your resource usage by setting a budget cap. Cap usage is reset at the + beginning of each billing cycle. +

+ + {#if !$currentPlan.budgeting} + + + Budget caps are a Pro plan feature -
- {#if capActive} - - {/if} -
- {/if} -
+ Upgrade to a Pro plan to set a budget cap for your organization. For more information + on what you can do with a Pro plan, + view our pricing guide. + + {:else} + + + + Budget cap limits do not include the base amount of your plan. + + + {#if capActive} + + {/if} + + {/if} + - - {#if $organization?.billingPlan === BillingPlan.FREE} - - {:else} - - {/if} - -
- + + {#if $organization?.billingPlan === BillingPlan.FREE} + + {:else} + + {/if} + + + +
0} /> diff --git a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte index 5d51ce528..b03026f97 100644 --- a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte +++ b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte @@ -6,18 +6,9 @@ import { HeaderAlert } from '$lib/layout'; import { hideBillingHeaderRoutes, readOnly, showBudgetAlert } from '$lib/stores/billing'; import { base } from '$app/paths'; - import { goto } from '$app/navigation'; - $: isSameUrl = $page.url.pathname === redirectUrl; - $: redirectUrl = `${base}/organization-${$organization.$id}/billing`; - - function scrollOrNavigate() { - if (isSameUrl) { - document.querySelector('#update-budget-section')?.scrollIntoView({ block: 'nearest' }); - } else { - goto(`${redirectUrl}#update-budget`); - } - } + $: redirectUrl = `${base}/organization-${$organization.$id}/billing#update-budget`; + $: isSameUrl = $page.url.pathname === new URL(redirectUrl, window.location.origin).pathname; {#if $showBudgetAlert && $organization?.$id && $organization?.billingPlan !== BillingPlan.FREE && $readOnly && !hideBillingHeaderRoutes.includes($page.url.pathname)} @@ -30,7 +21,9 @@ - From b0ad92a19b075e6ec6db3578a61b599dfa6cd266 Mon Sep 17 00:00:00 2001 From: Darshan Date: Tue, 18 Mar 2025 14:22:52 +0530 Subject: [PATCH 12/13] address comments. --- src/lib/elements/forms/button.svelte | 6 ++---- .../billing/budgetCap.svelte | 19 ++----------------- .../billing/store.ts | 4 ---- .../budgetLimitAlert.svelte | 5 +---- 4 files changed, 5 insertions(+), 29 deletions(-) diff --git a/src/lib/elements/forms/button.svelte b/src/lib/elements/forms/button.svelte index b27dd441d..fc643e0af 100644 --- a/src/lib/elements/forms/button.svelte +++ b/src/lib/elements/forms/button.svelte @@ -70,8 +70,7 @@ class={resolvedClasses} style="pointer-events: {internalDisabled ? 'none' : 'auto'};" aria-label={ariaLabel} - use:multiAction={actions} - {...$$restProps}> + use:multiAction={actions}> {:else} @@ -82,8 +81,7 @@ class={resolvedClasses} aria-label={ariaLabel} type={submit ? 'submit' : 'button'} - use:multiAction={actions} - {...$$restProps}> + use:multiAction={actions}> {#if $isSubmitting && submissionLoader} { - const elementTop = formHolderElement.getBoundingClientRect().top + window.scrollY; - // Offset 60 to prevent misalignment. - window.scrollTo({ top: elementTop - 60 }); - }); - } -
+
Budget cap diff --git a/src/routes/(console)/organization-[organization]/billing/store.ts b/src/routes/(console)/organization-[organization]/billing/store.ts index 52cb23cfa..ca081c291 100644 --- a/src/routes/(console)/organization-[organization]/billing/store.ts +++ b/src/routes/(console)/organization-[organization]/billing/store.ts @@ -16,7 +16,3 @@ export const addCreditWizardStore = writable<{ coupon: string; paymentMethodId: export const selectedInvoice = writable(null); export const showRetryModal = writable(false); - -export const scrollToUpdateBudget = derived(page, ($page) => - $page.url.hash.includes('#update-budget') -); diff --git a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte index b03026f97..2d1e25a46 100644 --- a/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte +++ b/src/routes/(console)/organization-[organization]/budgetLimitAlert.svelte @@ -8,7 +8,6 @@ import { base } from '$app/paths'; $: redirectUrl = `${base}/organization-${$organization.$id}/billing#update-budget`; - $: isSameUrl = $page.url.pathname === new URL(redirectUrl, window.location.origin).pathname; {#if $showBudgetAlert && $organization?.$id && $organization?.billingPlan !== BillingPlan.FREE && $readOnly && !hideBillingHeaderRoutes.includes($page.url.pathname)} @@ -21,9 +20,7 @@ - - - From 8e5116b528b09ca9f05a5cdf310f6955059f10bd Mon Sep 17 00:00:00 2001 From: Darshan Date: Tue, 18 Mar 2025 14:42:46 +0530 Subject: [PATCH 13/13] address comments. --- src/lib/components/heading.svelte | 7 + .../billing/budgetCap.svelte | 130 +++++++++--------- 2 files changed, 69 insertions(+), 68 deletions(-) diff --git a/src/lib/components/heading.svelte b/src/lib/components/heading.svelte index 9ef0315f6..a8ce04948 100644 --- a/src/lib/components/heading.svelte +++ b/src/lib/components/heading.svelte @@ -21,3 +21,10 @@ {id}> + + diff --git a/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte b/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte index 6c67bc4ec..479886016 100644 --- a/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte +++ b/src/routes/(console)/organization-[organization]/billing/budgetCap.svelte @@ -49,76 +49,70 @@ } -
- - - Budget cap + + + Budget cap -

- Restrict your resource usage by setting a budget cap. Cap usage is reset at the - beginning of each billing cycle. -

- - {#if !$currentPlan.budgeting} - - - Budget caps are a Pro plan feature +

+ Restrict your resource usage by setting a budget cap. Cap usage is reset at the + beginning of each billing cycle. +

+ + {#if !$currentPlan.budgeting} + + + Budget caps are a Pro plan feature + + Upgrade to a Pro plan to set a budget cap for your organization. For more information + on what you can do with a Pro plan, + view our pricing guide. + + {:else} + + + + Budget cap limits do not include the base amount of your plan. - Upgrade to a Pro plan to set a budget cap for your organization. For more information - on what you can do with a Pro plan, - view our pricing guide. -
- {:else} - - - - Budget cap limits do not include the base amount of your plan. - - - {#if capActive} - - {/if} - - {/if} -
+ + {#if capActive} + + {/if} + + {/if} + - - {#if $organization?.billingPlan === BillingPlan.FREE} - - {:else} - - {/if} - -
- -
+ + {#if $organization?.billingPlan === BillingPlan.FREE} + + {:else} + + {/if} + +
+ 0} />