Skip to content
Merged
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
11 changes: 8 additions & 3 deletions src/lib/sdk/billing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Client, Query } from '@appwrite.io/console';
import type { Organization } from '../stores/organization';
import type { PaymentMethod } from '@stripe/stripe-js';
import type { Tier } from '$lib/stores/billing';

export type PaymentMethodData = {
$id: string;
Expand Down Expand Up @@ -38,6 +39,7 @@ export type Invoice = {
status: string;
dueAt: string;
clientSecret: string;
tier: Tier;
};

export type InvoiceList = {
Expand Down Expand Up @@ -153,12 +155,15 @@ export type Aggregation = {
*/
resources: OrganizationUsage;
};

type UsageMetric = {
date: string;
value: number;
};
export type OrganizationUsage = {
bandwidth: { date: string; value: number }[];
bandwidth: Array<UsageMetric>;
executions: number;
storage: number;
users: { date: string; value: number }[];
users: Array<UsageMetric>;
};

export type AggregationList = {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/stores/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ export type PlanServices =
| 'usersAddon'
| 'webhooks';

export function getServiceLimit(serviceId: PlanServices): number {
export function getServiceLimit(serviceId: PlanServices, tier: Tier = null): number {
if (!isCloud) return 0;
if (!serviceId) return 0;
const plan = get(plansInfo).plans.find((p) => p.$id === get(organization)?.billingPlan);
const plan = get(plansInfo).plans.find(
(p) => p.$id === (tier ?? get(organization)?.billingPlan)
);
return plan[serviceId];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
<script lang="ts">
import { Container } from '$lib/layout';
import { CardGrid, Heading, ProgressBarBig } from '$lib/components';
import { getServiceLimit, showUsageRatesModal, tierToPlan } from '$lib/stores/billing.js';
import ChangeOrganizationTierCloud from '$routes/console/changeOrganizationTierCloud.svelte';
import { wizard } from '$lib/stores/wizard.js';
import { getServiceLimit, showUsageRatesModal, tierToPlan } from '$lib/stores/billing';
import { wizard } from '$lib/stores/wizard';
import { organization } from '$lib/stores/organization';
import { InputDateRange, InputSelect } from '$lib/elements/forms';
import { InputSelect } from '$lib/elements/forms';
import { page } from '$app/stores';
import { goto } from '$app/navigation';
import type { DateRange } from '@melt-ui/svelte';

const tier = tierToPlan($organization?.billingPlan)?.name;
import { toLocaleDate } from '$lib/helpers/date.js';
import ChangeOrganizationTierCloud from '$routes/console/changeOrganizationTierCloud.svelte';

export let data;
let period = 'current';

const tier = data?.currentInvoice?.tier ?? $organization?.billingPlan;
const plan = tierToPlan(tier).name;

let invoice = 'current';

async function handlePeriodChange() {
if (
period === 'current' &&
!$page.url.pathname.includes('usage/current') &&
$page.url.pathname !== `/console/organization-${$organization.$id}/usage`
) {
await goto(`/console/organization-${$organization.$id}/usage/current`);
}
if (period === 'previous' && !$page.url.pathname.includes('usage/previous')) {
await goto(`/console/organization-${$organization.$id}/usage/previous`);
}
if (period === 'custom' && !$page.url.pathname.includes('usage/custom')) {
console.log('test');
// await goto(`/console/organization-${$organization.$id}/usage/custom`);
const target = invoice
? `/console/organization-${$organization.$id}/usage/${invoice}`
: `/console/organization-${$organization.$id}/usage`;
if ($page.url.pathname !== target) {
await goto(target);
}
}

let test: DateRange;
const cycles = data.invoices.invoices.map((invoice) => ({
label: toLocaleDate(invoice.from),
value: invoice.$id
}));
</script>

<InputDateRange id="date" label="test" bind:data={test}></InputDateRange>

<Container>
<Heading tag="h2" size="5">Usage</Heading>
<div class="u-flex u-main-space-between common-section u-cross-center">
Expand All @@ -50,7 +45,7 @@
</p>
{:else}
<p class="text">
If you exceed the limits of the {tier} plan, services for your projects may be disrupted.
If you exceed the limits of the {plan} plan, services for your projects may be disrupted.
<button
on:click={() => wizard.start(ChangeOrganizationTierCloud)}
class="link"
Expand All @@ -59,8 +54,6 @@
</p>
{/if}

<!-- <InputDate id="date" label="test" showLabel={false}></InputDate> -->

<div class="u-flex u-gap-8 u-cross-center">
<p class="text">Usage period:</p>

Expand All @@ -69,21 +62,14 @@
id="period"
label="Usage period"
showLabel={false}
bind:value={period}
bind:value={invoice}
on:change={handlePeriodChange}
options={[
{
label: 'Current billing cycle',
value: 'current'
},
{
label: 'Previous billing cycle',
value: 'previous'
},
{
label: 'Choose dates',
value: 'custom'
}
...cycles
]}></InputSelect>
</div>
</div>
Expand All @@ -98,8 +84,8 @@
<svelte:fragment slot="aside">
<ProgressBarBig
unit="GB"
max={getServiceLimit('bandwidth')}
used={data.organizationUsage.bandwidth[0] ?? 0} />
max={getServiceLimit('bandwidth', tier)}
used={data.organizationUsage.bandwidth[0]?.value ?? 0} />
</svelte:fragment>
</CardGrid>

Expand All @@ -111,8 +97,8 @@
<svelte:fragment slot="aside">
<ProgressBarBig
unit="Users"
max={getServiceLimit('users')}
used={data.organizationUsage.bandwidth[0] ?? 0} />
max={getServiceLimit('users', tier)}
used={data.organizationUsage.bandwidth[0]?.value ?? 0} />
</svelte:fragment>
</CardGrid>

Expand All @@ -126,7 +112,7 @@
<svelte:fragment slot="aside">
<ProgressBarBig
unit="Executions"
max={getServiceLimit('executions')}
max={getServiceLimit('executions', tier)}
used={data.organizationUsage.executions} />
</svelte:fragment>
</CardGrid>
Expand All @@ -141,7 +127,7 @@
<svelte:fragment slot="aside">
<ProgressBarBig
unit="GB"
max={getServiceLimit('storage')}
max={getServiceLimit('storage', tier)}
used={data.organizationUsage.storage} />
</svelte:fragment>
</CardGrid>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { sdk } from '$lib/stores/sdk';
import type { PageLoad } from './$types';
import type { Organization } from '$lib/stores/organization';
import type { Invoice } from '$lib/sdk/billing';

export const load: PageLoad = async ({ params, parent }) => {
const { invoice } = params;
const parentData = await parent();
const org = parentData.organization as Organization;
const today = new Date().toISOString();
let startDate: string;
let endDate: string;
let currentInvoice: Invoice | null = null;

if (invoice) {
currentInvoice = await sdk.forConsole.billing.getInvoice(org.$id, invoice);
startDate = currentInvoice.from;
endDate = currentInvoice.to;
} else {
startDate = org.billingCurrentInvoiceDate;
endDate = today;
}

const [invoices, usage] = await Promise.all([
sdk.forConsole.billing.listInvoices(org.$id),
sdk.forConsole.billing.listUsage(params.organization, startDate, endDate)
]);

return {
organizationUsage: usage,
invoices,
currentInvoice
};
};

This file was deleted.