Skip to content

Commit bf29696

Browse files
authored
Merge pull request #687 from appwrite/refactor-change-plan-to-map
refactor: change plansInfo to a map
2 parents db0774d + f096271 commit bf29696

39 files changed

+173
-130
lines changed

src/lib/components/support.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
import { isSupportOnline, showSupportModal } from '../../routes/console/wizard/support/store';
1111
import { isCloud } from '$lib/system';
1212
import { organization } from '$lib/stores/organization';
13+
import { BillingPlan } from '$lib/constants';
1314
1415
export let show = false;
1516
16-
$: isPaid = $organization?.billingPlan === 'tier-1' || $organization?.billingPlan === 'tier-2';
17+
$: isPaid =
18+
$organization?.billingPlan === BillingPlan.PRO ||
19+
$organization?.billingPlan === BillingPlan.SCALE;
1720
</script>
1821

1922
{#if isCloud}
@@ -39,7 +42,7 @@
3942
</p>
4043
{/if}
4144
</div>
42-
{#if $organization?.billingPlan === 'tier-0'}
45+
{#if $organization?.billingPlan === BillingPlan.STARTER}
4346
<Button fullWidth href="https://appwrite.io/pricing" external>
4447
<span class="text">Get Premium support</span>
4548
</Button>

src/lib/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,9 @@ export const limitRates = {
616616
}
617617
]
618618
};
619+
620+
export enum BillingPlan {
621+
STARTER = 'tier-0',
622+
PRO = 'tier-1',
623+
SCALE = 'tier-2'
624+
}

src/lib/layout/activity.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import { isCloud } from '$lib/system';
2323
import { organization } from '$lib/stores/organization';
2424
import { Button } from '$lib/elements/forms';
25+
import { BillingPlan } from '$lib/constants';
2526
2627
export let logs: Models.LogList;
2728
export let offset = 0;
@@ -51,7 +52,7 @@
5152
Logs are retained in rolling {hoursToDays(limit)} intervals with the
5253
{tierToPlan($organization.billingPlan).name}
5354
plan.
54-
{#if $organization?.billingPlan === 'tier-0'}
55+
{#if $organization?.billingPlan === BillingPlan.STARTER}
5556
<Button link on:click={upgradeMethod}>Upgrade</Button> to increase your log
5657
retention for a longer period.
5758
{/if}

src/lib/layout/containerButton.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<script lang="ts">
22
import { tooltip } from '$lib/actions/tooltip';
3+
import { BillingPlan } from '$lib/constants';
34
import { Button } from '$lib/elements/forms';
45
import { tierToPlan } from '$lib/stores/billing';
56
import { organization } from '$lib/stores/organization';
67
78
export let title: string;
89
export let tooltipContent =
9-
$organization.billingPlan === 'tier-0'
10+
$organization.billingPlan === BillingPlan.STARTER
1011
? `Upgrade to add more ${title.toLocaleLowerCase()}`
1112
: `You've reached the ${title.toLocaleLowerCase()} limit for the ${
1213
tierToPlan($organization.billingPlan).name

src/lib/layout/containerHeader.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import ChangeOrganizationTierCloud from '$routes/console/changeOrganizationTierCloud.svelte';
1818
import { ContainerButton } from '.';
1919
import { Button } from '$lib/elements/forms';
20+
import { BillingPlan } from '$lib/constants';
2021
2122
export let isFlex = true;
2223
export let title: string;
@@ -52,7 +53,7 @@
5253
5354
$: tier = tierToPlan($organization?.billingPlan)?.name;
5455
$: hasProjectLimitation =
55-
checkForProjectLimitation(serviceId) && $organization?.billingPlan === 'tier-0';
56+
checkForProjectLimitation(serviceId) && $organization?.billingPlan === BillingPlan.STARTER;
5657
$: hasUsageFees = hasProjectLimitation
5758
? checkForUsageFees($organization?.billingPlan, serviceId)
5859
: false;
@@ -77,7 +78,7 @@
7778
})
7879
.join(', ')}
7980
<slot name="alert" {limit} {tier} {title} {upgradeMethod} {hasUsageFees} {services}>
80-
{#if $organization?.billingPlan !== 'tier-0' && hasUsageFees}
81+
{#if $organization?.billingPlan !== BillingPlan.STARTER && hasUsageFees}
8182
<Alert type="info" isStandalone>
8283
<span class="text">
8384
You've reached the {services} limit for the {tier} plan.
@@ -121,7 +122,7 @@
121122
<p class="text">
122123
Your are limited to {limit}
123124
{title.toLocaleLowerCase()} per project on the {tier} plan.
124-
{#if $organization?.billingPlan === 'tier-0'}<Button
125+
{#if $organization?.billingPlan === BillingPlan.STARTER}<Button
125126
link
126127
on:click={upgradeMethod}>Upgrade</Button>
127128
for addtional {title.toLocaleLowerCase()}.
@@ -139,7 +140,7 @@
139140
<p class="text">
140141
You are limited to {limit}
141142
{title.toLocaleLowerCase()} per organization on the {tier} plan.
142-
{#if $organization?.billingPlan === 'tier-0'}
143+
{#if $organization?.billingPlan === BillingPlan.STARTER}
143144
<Button link on:click={upgradeMethod}>Upgrade</Button>
144145
for additional {title.toLocaleLowerCase()}.
145146
{/if}

src/lib/layout/header.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import { Pill } from '$lib/elements';
2828
import { showExcess } from '$routes/console/organization-[organization]/store';
2929
import { readOnly } from '$lib/stores/billing';
30+
import { BillingPlan } from '$lib/constants';
3031
3132
let showDropdown = false;
3233
let showSupport = false;
@@ -117,7 +118,7 @@
117118

118119
<div class="main-header-end">
119120
<nav class="u-flex is-only-desktop u-cross-center">
120-
{#if isCloud && $organization?.billingPlan === 'tier-0' && !$page.url.pathname.startsWith('/console/account')}
121+
{#if isCloud && $organization?.billingPlan === BillingPlan.STARTER && !$page.url.pathname.startsWith('/console/account')}
121122
<Button
122123
disabled={$organization?.markedForDeletion}
123124
on:click={() => wizard.start(ChangeOrganizationTierCloud)}>

src/lib/layout/logs.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import { organization } from '$lib/stores/organization';
2121
import { app } from '$lib/stores/app';
2222
import { Button } from '$lib/elements/forms';
23+
import { BillingPlan } from '$lib/constants';
2324
2425
let selectedRequest = 'parameters';
2526
let selectedResponse = 'logs';
@@ -311,7 +312,7 @@
311312
<Alert>
312313
Logs are retained in rolling {hoursToDays(limit)} intervals
313314
with the {tier} plan.
314-
{#if $organization.billingPlan === 'tier-0'}
315+
{#if $organization.billingPlan === BillingPlan.STARTER}
315316
<Button
316317
link
317318
on:click={() =>

src/lib/layout/navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { page } from '$app/stores';
44
import { trackEvent } from '$lib/actions/analytics';
55
import { tooltip } from '$lib/actions/tooltip';
6+
import { BillingPlan } from '$lib/constants';
67
import { isMac } from '$lib/helpers/platform';
78
import { slide } from '$lib/helpers/transition';
89
import { organization } from '$lib/stores/organization';
@@ -180,7 +181,7 @@
180181
</a>
181182

182183
<ul class="drop-list is-only-mobile">
183-
{#if isCloud && $organization?.billingPlan !== 'tier-2'}
184+
{#if isCloud && $organization?.billingPlan !== BillingPlan.SCALE}
184185
<li class="drop-list-item">
185186
<button
186187
class="drop-button"

src/lib/sdk/billing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ export type PlansInfo = {
265265
total: number;
266266
};
267267

268+
export type PlansMap = Map<Tier, Plan>;
269+
268270
export class Billing {
269271
client: Client;
270272

src/lib/stores/billing.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { page } from '$app/stores';
22
import { derived, get, writable } from 'svelte/store';
33
import { sdk } from './sdk';
44
import { organization, type Organization } from './organization';
5-
import type { InvoiceList, AddressesList, Invoice, PaymentList, PlansInfo } from '$lib/sdk/billing';
5+
import type { InvoiceList, AddressesList, Invoice, PaymentList, PlansMap } from '$lib/sdk/billing';
66
import { isCloud } from '$lib/system';
77
import { cachedStore } from '$lib/helpers/cache';
88
import { Query, type Models } from '@appwrite.io/console';
@@ -15,22 +15,23 @@ import { base } from '$app/paths';
1515
import TooManyFreOrgs from '$lib/components/billing/alerts/tooManyFreeOrgs.svelte';
1616
import { activeHeaderAlert, showPostReleaseModal } from '$routes/console/store';
1717
import MarkedForDeletion from '$lib/components/billing/alerts/markedForDeletion.svelte';
18+
import { BillingPlan } from '$lib/constants';
1819

1920
export type Tier = 'tier-0' | 'tier-1' | 'tier-2';
2021

2122
export const paymentMethods = derived(page, ($page) => $page.data.paymentMethods as PaymentList);
2223
export const addressList = derived(page, ($page) => $page.data.addressList as AddressesList);
23-
export const plansInfo = derived(page, ($page) => $page.data.plansInfo as PlansInfo);
24+
export const plansInfo = derived(page, ($page) => $page.data.plansInfo as PlansMap);
2425
export const daysLeftInTrial = writable<number>(0);
2526
export const readOnly = writable<boolean>(false);
2627

2728
export function tierToPlan(tier: Tier) {
2829
switch (tier) {
29-
case 'tier-0':
30+
case BillingPlan.STARTER:
3031
return tierFree;
31-
case 'tier-1':
32+
case BillingPlan.PRO:
3233
return tierPro;
33-
case 'tier-2':
34+
case BillingPlan.SCALE:
3435
return tierScale;
3536
default:
3637
return tierFree;
@@ -63,8 +64,8 @@ export function getServiceLimit(serviceId: PlanServices, tier: Tier = null): num
6364
if (!isCloud) return 0;
6465
if (!serviceId) return 0;
6566
const info = get(plansInfo);
66-
if (!info?.plans) return 0;
67-
const plan = info.plans.find((p) => p.$id === (tier ?? get(organization)?.billingPlan));
67+
if (!info) return 0;
68+
const plan = info.get(tier ?? get(organization)?.billingPlan);
6869
return plan?.[serviceId];
6970
}
7071

@@ -117,7 +118,7 @@ export const tierScale: TierData = {
117118
export const showUsageRatesModal = writable<boolean>(false);
118119

119120
export function checkForUsageFees(plan: Tier, id: PlanServices) {
120-
if (plan === 'tier-1' || plan === 'tier-2') {
121+
if (plan === BillingPlan.PRO || plan === BillingPlan.SCALE) {
121122
switch (id) {
122123
case 'bandwidth':
123124
case 'storage':
@@ -157,7 +158,7 @@ export function isServiceLimited(serviceId: PlanServices, plan: Tier, total: num
157158
}
158159

159160
export function calculateTrialDay(org: Organization) {
160-
if (org?.billingPlan === 'tier-0') return false;
161+
if (org?.billingPlan === BillingPlan.STARTER) return false;
161162
const endDate = new Date(org?.billingStartDate);
162163
const today = new Date();
163164
const days = diffDays(today, endDate);
@@ -190,7 +191,7 @@ export async function checkForUsageLimit(org: Organization) {
190191
}
191192
const { bandwidth, documents, executions, storage, users } = org?.billingLimits ?? {};
192193
const members = await sdk.forConsole.teams.listMemberships(org.$id);
193-
const plan = get(plansInfo).plans.find((plan) => plan.$id === org.billingPlan);
194+
const plan = get(plansInfo)?.get(org.billingPlan);
194195
const membersOverflow =
195196
members?.total > plan.members ? members.total - (plan.members || members.total) : 0;
196197

@@ -207,7 +208,7 @@ export async function checkForUsageLimit(org: Organization) {
207208
}
208209

209210
export async function checkPaymentAuthorizationRequired(org: Organization) {
210-
if (org.billingPlan === 'tier-0') return;
211+
if (org.billingPlan === BillingPlan.STARTER) return;
211212

212213
const invoices = await sdk.forConsole.billing.listInvoices(org.$id, [
213214
Query.equal('status', 'requires_authentication')

0 commit comments

Comments
 (0)