Skip to content

Commit a66ef5e

Browse files
committed
fix: undefined (reading 'budgeting') in budget cap
1 parent 63e9368 commit a66ef5e

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

src/routes/(console)/organization-[organization]/billing/+page.svelte

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script lang="ts">
22
import { Container } from '$lib/layout';
3-
import { organization } from '$lib/stores/organization';
43
import BudgetCap from './budgetCap.svelte';
54
import PlanSummary from './planSummary.svelte';
65
import BillingAddress from './billingAddress.svelte';
@@ -25,14 +24,15 @@
2524
import type { PageData } from './$types';
2625
2726
export let data: PageData;
27+
let organization = data.organization;
2828
2929
// why are these reactive?
3030
$: defaultPaymentMethod = data?.paymentMethods?.paymentMethods?.find(
31-
(method: PaymentMethodData) => method.$id === $organization?.paymentMethodId
31+
(method: PaymentMethodData) => method.$id === organization?.paymentMethodId
3232
);
3333
3434
$: backupPaymentMethod = data?.paymentMethods?.paymentMethods?.find(
35-
(method: PaymentMethodData) => method.$id === $organization?.backupPaymentMethodId
35+
(method: PaymentMethodData) => method.$id === organization?.backupPaymentMethodId
3636
);
3737
3838
onMount(async () => {
@@ -52,10 +52,10 @@
5252
);
5353
5454
await confirmPayment(
55-
$organization.$id,
55+
organization.$id,
5656
invoice.clientSecret,
57-
$organization.paymentMethodId,
58-
`${base}/organization-${$organization.$id}/billing?type=validate-invoice&invoice=${invoice.$id}`
57+
organization.paymentMethodId,
58+
`${base}/organization-${organization.$id}/billing?type=validate-invoice&invoice=${invoice.$id}`
5959
);
6060
}
6161
@@ -64,7 +64,7 @@
6464
page.url.searchParams.get('type') === 'validate-invoice'
6565
) {
6666
const invoiceId = page.url.searchParams.get('invoice');
67-
await sdk.forConsole.billing.updateInvoiceStatus($organization.$id, invoiceId);
67+
await sdk.forConsole.billing.updateInvoiceStatus(organization.$id, invoiceId);
6868
invalidate(Dependencies.INVOICES);
6969
invalidate(Dependencies.ORGANIZATION);
7070
}
@@ -84,7 +84,7 @@
8484
}
8585
if (page.url.searchParams.has('clientSecret')) {
8686
const clientSecret = page.url.searchParams.get('clientSecret');
87-
await confirmPayment($organization.$id, clientSecret, $organization.paymentMethodId);
87+
await confirmPayment(organization.$id, clientSecret, organization.paymentMethodId);
8888
}
8989
});
9090
</script>
@@ -93,7 +93,7 @@
9393
{#if $failedInvoice}
9494
{#if $failedInvoice?.lastError}
9595
<Alert.Inline status="error">
96-
The scheduled payment for {$organization.name} failed due to following error: {$failedInvoice.lastError}
96+
The scheduled payment for {organization.name} failed due to following error: {$failedInvoice.lastError}
9797
<svelte:fragment slot="actions">
9898
<Button
9999
text
@@ -106,7 +106,7 @@
106106
{:else}
107107
<Alert.Inline
108108
status="error"
109-
title={`The scheduled payment for ${$organization.name} failed`}>
109+
title={`The scheduled payment for ${organization.name} failed`}>
110110
To avoid service disruptions in organization's your projects, please verify your
111111
payment details and update them if necessary.
112112
</Alert.Inline>
@@ -115,16 +115,16 @@
115115
{#if defaultPaymentMethod?.failed && !backupPaymentMethod}
116116
<Alert.Inline
117117
status="error"
118-
title={`The default payment method for ${$organization.name} has expired`}>
118+
title={`The default payment method for ${organization.name} has expired`}>
119119
To avoid service disruptions in your organization's projects, please update your payment
120120
details.
121121
</Alert.Inline>
122122
{/if}
123-
{#if $organization?.billingPlanDowngrade}
123+
{#if organization?.billingPlanDowngrade}
124124
<Alert.Inline status="info">
125-
Your organization has changed to {tierToPlan($organization?.billingPlanDowngrade).name} plan.
126-
You will continue to have access to {tierToPlan($organization?.billingPlan).name} plan features
127-
until your billing period ends on {toLocaleDate($organization.billingNextInvoiceDate)}.
125+
Your organization has changed to {tierToPlan(organization?.billingPlanDowngrade).name} plan.
126+
You will continue to have access to {tierToPlan(organization?.billingPlan).name} plan features
127+
until your billing period ends on {toLocaleDate(organization.billingNextInvoiceDate)}.
128128
</Alert.Inline>
129129
{/if}
130130
<Typography.Title>Billing</Typography.Title>
@@ -137,7 +137,7 @@
137137
<PaymentMethods methods={data?.paymentMethods} />
138138
<BillingAddress {data} />
139139
<TaxId />
140-
<BudgetCap />
140+
<BudgetCap organization={data?.organization} currentPlan={data?.currentPlan} />
141141
<AvailableCredit areCreditsSupported={data.areCreditsSupported} />
142142
</Container>
143143

src/routes/(console)/organization-[organization]/billing/budgetAlert.svelte

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
import { Button, Form } from '$lib/elements/forms';
88
import { symmetricDifference } from '$lib/helpers/array';
99
import { addNotification } from '$lib/stores/notifications';
10-
import { currentPlan, organization } from '$lib/stores/organization';
10+
import { type Organization } from '$lib/stores/organization';
1111
import { sdk } from '$lib/stores/sdk';
12-
import { onMount } from 'svelte';
1312
import { Alert, Icon, Table } from '@appwrite.io/pink-svelte';
1413
import { IconTrash } from '@appwrite.io/pink-icons-svelte';
1514
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
15+
import type { Plan } from '$lib/sdk/billing';
1616
17+
export let organization: Organization;
18+
export let currentPlan: Plan;
1719
export let alertsEnabled = false;
1820
1921
let search: string;
2022
let selectedAlert: number;
21-
let alerts: number[] = [];
23+
let alerts: number[] = organization?.budgetAlerts ?? [];
2224
2325
$: options = [
2426
{ value: 25, label: '25%', disabled: false },
@@ -33,10 +35,6 @@
3335
};
3436
});
3537
36-
onMount(() => {
37-
alerts = $organization?.budgetAlerts ?? [];
38-
});
39-
4038
function addAlert() {
4139
if (alerts.some((alert) => alert === selectedAlert)) {
4240
return;
@@ -50,8 +48,8 @@
5048
async function updateBudget() {
5149
try {
5250
await sdk.forConsole.billing.updateBudget(
53-
$organization.$id,
54-
$organization.billingBudget,
51+
organization.$id,
52+
organization.billingBudget,
5553
alerts
5654
);
5755
@@ -60,7 +58,7 @@
6058
addNotification({
6159
type: 'success',
6260
isHtml: true,
63-
message: `<span> ${alerts.length === 0 ? 'Budget alerts removed from' : alerts.length > 1 ? `Budget alerts added to` : 'A budget alert has been added to'} <b>${$organization.name}</b> </span>`
61+
message: `<span> ${alerts.length === 0 ? 'Budget alerts removed from' : alerts.length > 1 ? `Budget alerts added to` : 'A budget alert has been added to'} <b>${organization.name}</b> </span>`
6462
});
6563
trackEvent(Submit.BudgetAlertsUpdate, {
6664
alerts
@@ -75,26 +73,26 @@
7573
}
7674
7775
$: isButtonDisabled =
78-
symmetricDifference(alerts, $organization.budgetAlerts).length === 0 || !alertsEnabled;
76+
symmetricDifference(alerts, organization.budgetAlerts).length === 0 || !alertsEnabled;
7977
</script>
8078

8179
<CardGrid>
8280
<svelte:fragment slot="title">Billing alerts</svelte:fragment>
83-
{#if !$currentPlan.budgeting}
81+
{#if !currentPlan.budgeting}
8482
Get notified by email when your organization meets a percentage of your budget cap. <b
85-
>{tierToPlan($organization.billingPlan).name} organizations will receive one notification
83+
>{tierToPlan(organization.billingPlan).name} organizations will receive one notification
8684
at 75% resource usage.</b>
8785
{:else}
8886
Get notified by email when your organization meets or exceeds a percentage of your specified
8987
billing alert(s).
9088
{/if}
9189
<svelte:fragment slot="aside">
92-
{#if !$currentPlan.budgeting}
90+
{#if !currentPlan.budgeting}
9391
<Alert.Inline status="info" title="Billing alerts are a Pro plan feature">
9492
Upgrade to a Pro plan to manage when you receive billing alerts for your
9593
organization.
9694
</Alert.Inline>
97-
{:else if !$currentPlan.budgetCapEnabled}
95+
{:else if !currentPlan.budgetCapEnabled}
9896
<Alert.Inline status="info" title="Budget cap disabled">
9997
Budget caps are not supported on your current plan. For more information, please
10098
reach out to your customer success manager.
@@ -150,7 +148,7 @@
150148

151149
<svelte:fragment slot="actions">
152150
<Form onSubmit={updateBudget}>
153-
{#if $organization?.billingPlan === BillingPlan.FREE || $organization?.billingPlan === BillingPlan.GITHUB_EDUCATION}
151+
{#if organization?.billingPlan === BillingPlan.FREE || organization?.billingPlan === BillingPlan.GITHUB_EDUCATION}
154152
<Button
155153
secondary
156154
href={$upgradeURL}

src/routes/(console)/organization-[organization]/billing/budgetCap.svelte

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,28 @@
66
import { Button, Form, InputNumber, InputSwitch } from '$lib/elements/forms';
77
import { showUsageRatesModal, upgradeURL } from '$lib/stores/billing';
88
import { addNotification } from '$lib/stores/notifications';
9-
import { organization, currentPlan } from '$lib/stores/organization';
9+
import { type Organization } from '$lib/stores/organization';
1010
import { sdk } from '$lib/stores/sdk';
1111
import { Alert, Link } from '@appwrite.io/pink-svelte';
12-
import { onMount } from 'svelte';
1312
import BudgetAlert from './budgetAlert.svelte';
13+
import type { Plan } from '$lib/sdk/billing';
1414
15-
let capActive = false;
16-
let budget: number;
17-
18-
onMount(() => {
19-
budget = $organization?.billingBudget;
20-
capActive = $organization?.billingBudget !== null;
21-
});
15+
export let currentPlan: Plan;
16+
export let organization: Organization;
17+
let capActive = organization?.billingBudget !== null;
18+
let budget = organization.billingBudget;
2219
2320
async function updateBudget() {
2421
try {
2522
await sdk.forConsole.billing.updateBudget(
26-
$organization.$id,
23+
organization.$id,
2724
budget,
28-
$organization.budgetAlerts
25+
organization.budgetAlerts
2926
);
3027
await invalidate(Dependencies.ORGANIZATION);
3128
addNotification({
3229
type: 'success',
33-
message: `Budget cap enabled for ${$organization.name}`
30+
message: `Budget cap enabled for ${organization.name}`
3431
});
3532
trackEvent(Submit.BudgetCapUpdate, {
3633
budget: capActive ? budget : undefined
@@ -55,7 +52,7 @@
5552
Restrict your resource usage by setting a budget cap. Cap usage is reset at the beginning of
5653
each billing cycle.
5754
<svelte:fragment slot="aside">
58-
{#if !$currentPlan.budgeting}
55+
{#if !currentPlan.budgeting}
5956
<Alert.Inline status="info" title="Budget caps are a Pro plan feature">
6057
Upgrade to a Pro plan to set a budget cap for your organization. For more
6158
information on what you can do with a Pro plan,
@@ -64,7 +61,7 @@
6461
target="_blank"
6562
rel="noopener noreferrer">view our pricing guide.</Link.Anchor>
6663
</Alert.Inline>
67-
{:else if !$currentPlan.budgetCapEnabled}
64+
{:else if !currentPlan.budgetCapEnabled}
6865
<Alert.Inline status="info" title="Budget cap disabled">
6966
Budget caps are not supported on your current plan. For more information, please
7067
reach out to your customer success manager.
@@ -92,7 +89,7 @@
9289
</svelte:fragment>
9390

9491
<svelte:fragment slot="actions">
95-
{#if $organization?.billingPlan === BillingPlan.FREE}
92+
{#if organization?.billingPlan === BillingPlan.FREE}
9693
<Button
9794
secondary
9895
href={$upgradeURL}
@@ -105,10 +102,10 @@
105102
>Upgrade to Pro
106103
</Button>
107104
{:else}
108-
<Button disabled={$organization?.billingBudget === budget} submit>Update</Button>
105+
<Button disabled={organization?.billingBudget === budget} submit>Update</Button>
109106
{/if}
110107
</svelte:fragment>
111108
</CardGrid>
112109
</Form>
113110

114-
<BudgetAlert alertsEnabled={capActive && budget > 0} />
111+
<BudgetAlert {organization} {currentPlan} alertsEnabled={capActive && budget > 0} />

0 commit comments

Comments
 (0)