Skip to content

Project changes: fetch plan values #2026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 30 additions & 64 deletions src/lib/components/billing/planSelection.svelte
Original file line number Diff line number Diff line change
@@ -1,81 +1,47 @@
<script lang="ts">
import { BASE_BILLING_PLANS, BillingPlan } from '$lib/constants';
import { BillingPlan } from '$lib/constants';
import { formatCurrency } from '$lib/helpers/numbers';
import { plansInfo, type Tier, tierFree, tierPro, tierScale } from '$lib/stores/billing';
import { type Tier } from '$lib/stores/billing';
import { currentPlan, organization } from '$lib/stores/organization';
import { Badge, Layout, Typography } from '@appwrite.io/pink-svelte';
import { LabelCard } from '..';
import type { Plan } from '$lib/sdk/billing';
import { page } from '$app/state';

export let billingPlan: Tier;
export let anyOrgFree = false;
export let isNewOrg = false;
export let selfService = true;

$: freePlan = $plansInfo.get(BillingPlan.FREE);
$: proPlan = $plansInfo.get(BillingPlan.PRO);
$: scalePlan = $plansInfo.get(BillingPlan.SCALE);

$: isBasePlan = BASE_BILLING_PLANS.includes($currentPlan?.$id);
$: plans = Object.values(page.data.plans.plans) as Plan[];
$: currentPlanInList = plans.filter((plan) => plan.$id === $currentPlan?.$id).length > 0;
</script>

<Layout.Stack>
<LabelCard
name="plan"
bind:group={billingPlan}
disabled={anyOrgFree || !selfService}
value={BillingPlan.FREE}
title={tierFree.name}
tooltipShow={anyOrgFree}
tooltipText="You are limited to 1 Free organization per account."
tooltipWidth="100%">
<svelte:fragment slot="action">
{#if $organization?.billingPlan === BillingPlan.FREE && !isNewOrg}
<Badge variant="secondary" size="xs" content="Current plan" />
{/if}
</svelte:fragment>
<Typography.Caption variant="400">
{tierFree.description}
</Typography.Caption>
<Typography.Text>
{formatCurrency(freePlan?.price ?? 0)}
</Typography.Text>
</LabelCard>
<LabelCard
name="plan"
disabled={!selfService}
bind:group={billingPlan}
value={BillingPlan.PRO}
title={tierPro.name}>
<svelte:fragment slot="action">
{#if $organization?.billingPlan === BillingPlan.PRO && !isNewOrg}
<Badge variant="secondary" size="xs" content="Current plan" />
{/if}
</svelte:fragment>
<Typography.Caption variant="400">
{tierPro.description}
</Typography.Caption>
<Typography.Text>
{formatCurrency(proPlan?.price ?? 0)} per month + usage
</Typography.Text>
</LabelCard>
<LabelCard
name="plan"
bind:group={billingPlan}
value={BillingPlan.SCALE}
title={tierScale.name}>
<svelte:fragment slot="action">
{#if $organization?.billingPlan === BillingPlan.SCALE && !isNewOrg}
<Badge variant="secondary" size="xs" content="Current plan" />
{/if}
</svelte:fragment>
<Typography.Caption variant="400">
{tierScale.description}
</Typography.Caption>
<Typography.Text>
{formatCurrency(scalePlan?.price ?? 0)} per month + usage
</Typography.Text>
</LabelCard>
{#if $currentPlan && !isBasePlan}
{#each plans as plan}
<LabelCard
name="plan"
bind:group={billingPlan}
disabled={!selfService || (plan.$id === BillingPlan.FREE && anyOrgFree)}
tooltipShow={plan.$id === BillingPlan.FREE && anyOrgFree}
value={plan.$id}
title={plan.name}>
<svelte:fragment slot="action">
{#if $organization?.billingPlan === plan.$id && !isNewOrg}
<Badge variant="secondary" size="xs" content="Current plan" />
{/if}
</svelte:fragment>
<Typography.Caption variant="400">
{plan.desc}
</Typography.Caption>
<Typography.Text>
{@const isZeroPrice = (plan.price ?? 0) <= 0}
{@const price = formatCurrency(plan.price ?? 0)}
{isZeroPrice ? price : `${price} per month + usage`}
</Typography.Text>
</LabelCard>
{/each}
{#if $currentPlan && !currentPlanInList}
<LabelCard
name="plan"
bind:group={billingPlan}
Expand Down
94 changes: 92 additions & 2 deletions src/lib/sdk/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,78 @@ export type CreditList = {
total: number;
};

export type AggregationTeam = {
$id: string;
/**
* Aggregation creation time in ISO 8601 format.
*/
$createdAt: string;
/**
* Aggregation update date in ISO 8601 format.
*/
$updatedAt: string;
/**
* Beginning date of the invoice.
*/
from: string;
/**
* End date of the invoice.
*/
to: string;
/**
* Total amount of the invoice.
*/
amount: number;
additionalMembers: number;

/**
* Price for additional members
*/
additionalMemberAmount: number;
/**
* Total storage usage.
*/
usageStorage: number;
/**
* Total active users for the billing period.
*/
usageUsers: number;
/**
* Total number of executions for the billing period.
*/
usageExecutions: number;
/**
* Total bandwidth usage for the billing period.
*/
usageBandwidth: number;
/**
* Total realtime usage for the billing period.
*/
usageRealtime: number;
/**
* Usage logs for the billing period.
*/
resources: InvoiceUsage[];
/**
* Aggregation billing plan
*/
plan: string;
projectBreakdown: ProjectBreakdown[]
};

export type ProjectBreakdown = {
$id: string;
name: string;
amount: number;
resources: InvoiceUsage[];
}

export type InvoiceUsage = {
resourceId: string;
value: number;
amount: number;
}

export type AvailableCredit = {
available: number;
};
Expand Down Expand Up @@ -333,6 +405,7 @@ export type Plan = {
};
addons: {
seats: PlanAddon;
projects: PlanAddon;
};
trialDays: number;
budgetCapEnabled: boolean;
Expand All @@ -348,14 +421,15 @@ export type Plan = {
supportsOrganizationRoles: boolean;
buildSize: number; // in MB
deploymentSize: number; // in MB
usagePerProject: boolean;
};

export type PlanList = {
plans: Plan[];
total: number;
};

export type PlansMap = Map<Tier, Plan>;
export type PlansMap = Map<string, Plan>;

export type Roles = {
scopes: string[];
Expand Down Expand Up @@ -492,6 +566,22 @@ export class Billing {
});
}

async listPlans(queries: string[] = []): Promise<PlanList> {
const path = `/console/plans`;
const uri = new URL(this.client.config.endpoint + path);
const params = {
queries
};
return await this.client.call(
'get',
uri,
{
'content-type': 'application/json'
},
params
);
}

async getPlan(planId: string): Promise<Plan> {
const path = `/console/plans/${planId}`;
const uri = new URL(this.client.config.endpoint + path);
Expand Down Expand Up @@ -835,7 +925,7 @@ export class Billing {
);
}

async getAggregation(organizationId: string, aggregationId: string): Promise<Aggregation> {
async getAggregation(organizationId: string, aggregationId: string): Promise<AggregationTeam> {
const path = `/organizations/${organizationId}/aggregations/${aggregationId}`;
const params = {
organizationId,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/stores/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function getServiceLimit(serviceId: PlanServices, tier: Tier = null, plan
// plan > addons > seats/others
if (serviceId === 'members') {
// some don't include `limit`, so we fallback!
return plan?.['addons']['seats']['limit'] ?? 1;
return (plan?.['addons']['seats'] || [])['limit'] ?? 1;
}

return plan?.[serviceId] ?? 0;
Expand Down Expand Up @@ -323,7 +323,8 @@ export async function checkForProjectsLimit(org: Organization, projects: number)
const plan = await sdk.forConsole.billing.getOrganizationPlan(org.$id);
if (!plan) return;
if (plan.$id !== BillingPlan.FREE) return;
if (org.projects?.length > 0) return;
if (!org.projects) return;
if (org.projects.length > 0) return;

if (plan.projects > 0 && projects > plan.projects) {
headerAlert.add({
Expand Down
3 changes: 2 additions & 1 deletion src/routes/(console)/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dependencies } from '$lib/constants';
import { sdk } from '$lib/stores/sdk';
import { isCloud } from '$lib/system';
import type { LayoutLoad } from './$types';
import { Dependencies } from '$lib/constants';
import type { Tier } from '$lib/stores/billing';
import type { Plan, PlanList } from '$lib/sdk/billing';
import { Query } from '@appwrite.io/console';
Expand Down Expand Up @@ -50,6 +50,7 @@ export const load: LayoutLoad = async ({ depends, parent }) => {
plansInfo,
roles: [],
scopes: [],
projects,
preferences,
currentOrgId,
organizations,
Expand Down
7 changes: 4 additions & 3 deletions src/routes/(console)/create-organization/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import type { Organization } from '$lib/stores/organization';
export const load: PageLoad = async ({ url, parent, depends }) => {
const { organizations } = await parent();
depends(Dependencies.ORGANIZATIONS);

const [coupon, paymentMethods] = await Promise.all([
const [coupon, paymentMethods, plans] = await Promise.all([
getCoupon(url),
sdk.forConsole.billing.listPaymentMethods()
sdk.forConsole.billing.listPaymentMethods(),
sdk.forConsole.billing.listPlans()
]);
let plan = getPlanFromUrl(url);
const hasFreeOrganizations = organizations.teams?.some(
Expand All @@ -24,6 +24,7 @@ export const load: PageLoad = async ({ url, parent, depends }) => {
return {
plan,
coupon,
plans,
hasFreeOrganizations,
paymentMethods,
name: url.searchParams.get('name') ?? ''
Expand Down
Loading
Loading