Skip to content

Commit ffc5538

Browse files
authored
Merge pull request #3054 from appwrite/fix-billing-null-guards
Fix billing null guards
2 parents a15c452 + b88f43b commit ffc5538

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/lib/components/billing/alerts/newDevUpgradePro.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
</script>
2525

26-
{#if show && $organization?.$id && !$organization?.billingPlanDetails.supportsCredits && !page.url.pathname.includes(base + '/account')}
26+
{#if show && $organization?.$id && !$organization?.billingPlanDetails?.supportsCredits && !page.url.pathname.includes(base + '/account')}
2727
<GradientBanner on:close={handleClose}>
2828
<Layout.Stack
2929
gap="m"

src/lib/components/tab.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@
4848
function handleKeyDown(e: KeyboardEvent) {
4949
const tabBtn = e.target as HTMLElement;
5050
const tabItem = tabBtn.closest('.tabs-item') as HTMLElement;
51+
if (!tabItem) return;
52+
5153
const tabsList = tabItem.closest('.tabs') as HTMLElement;
54+
if (!tabsList) return;
55+
5256
const tabItems = Array.from(tabsList.querySelectorAll('.tabs-item'));
5357
const currentIdx = tabItems.indexOf(tabItem);
58+
if (currentIdx === -1) return;
5459
5560
const dir = getElementDir(tabsList);
5661

src/lib/layout/containerHeader.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
.join(', ')}
9797

9898
{#if services.length}
99-
{@const supportsUsage = Object.keys($currentPlan.usage).length > 0}
99+
{@const supportsUsage = Object.keys($currentPlan?.usage ?? {}).length > 0}
100100
<slot
101101
name="alert"
102102
{limit}

src/lib/stores/billing.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ export function getBasePlanFromGroup(billingPlanGroup: BillingPlanGroup): Models
151151
return proPlans.sort((a, b) => a.order - b.order)[0];
152152
}
153153

154-
export function billingIdToPlan(billingId: string): Models.BillingPlan | null {
154+
export function billingIdToPlan(billingId: string | null | undefined): Models.BillingPlan | null {
155+
if (!billingId) {
156+
return null;
157+
}
158+
155159
const plansInfoStore = getPlansInfoStore();
156160
if (plansInfoStore.has(billingId)) {
157161
return plansInfoStore.get(billingId);
@@ -287,9 +291,9 @@ export const actionRequiredInvoices = writable<Models.InvoiceList>(null);
287291
export const showUsageRatesModal = writable<boolean>(false);
288292
export const useNewPricingModal = derived(currentPlan, ($plan) => $plan?.usagePerProject === true);
289293

290-
export function checkForUsageFees(plan: string, id: PlanServices) {
294+
export function checkForUsageFees(plan: string | null | undefined, id: PlanServices) {
291295
const billingPlan = billingIdToPlan(plan);
292-
const supportsUsage = Object.keys(billingPlan.usage).length > 0;
296+
const supportsUsage = Object.keys(billingPlan?.usage ?? {}).length > 0;
293297

294298
if (supportsUsage) {
295299
switch (id) {
@@ -307,10 +311,10 @@ export function checkForUsageFees(plan: string, id: PlanServices) {
307311
} else return false;
308312
}
309313

310-
export function checkForProjectLimitation(plan: string, id: PlanServices) {
314+
export function checkForProjectLimitation(plan: string | null | undefined, id: PlanServices) {
311315
if (id === 'members') {
312316
const billingPlan = billingIdToPlan(plan);
313-
const hasUnlimitedProjects = billingPlan.projects === 0;
317+
const hasUnlimitedProjects = billingPlan?.projects === 0;
314318

315319
if (hasUnlimitedProjects) {
316320
return false; // No project limitation for members on Pro/Scale plans
@@ -629,7 +633,7 @@ export async function checkForMissingPaymentMethod() {
629633
// Display upgrade banner for new users after 1 week for 30 days
630634
export async function checkForNewDevUpgradePro(org: Models.Organization) {
631635
// browser or plan check.
632-
if (!browser || !org.billingPlanDetails.supportsCredits) return;
636+
if (!browser || !org?.billingPlanDetails?.supportsCredits) return;
633637

634638
// already dismissed by user!
635639
if (localStorage.getItem('newDevUpgradePro')) return;
@@ -640,6 +644,8 @@ export async function checkForNewDevUpgradePro(org: Models.Organization) {
640644

641645
const now = new Date().getTime();
642646
const account = get(user);
647+
if (!account?.$createdAt) return;
648+
643649
const accountCreated = new Date(account.$createdAt).getTime();
644650
if (now - accountCreated < 1000 * 60 * 60 * 24 * 7) return;
645651

0 commit comments

Comments
 (0)