feat: organization usage, stipe payment confimation

This commit is contained in:
Arman committed 2023-11-13 20:38:03 +01:00
1 parent 337df50dc1
commit 96b3da75c4
10 files changed
+110 -69

No files matched your search

+29 -2
View File
@@ -131,6 +131,13 @@ export type Aggregation = {
usageRealtime: number;
};
export type OrganizationUsage = {
bandwidth: [];
executions: number;
storage: number;
users: [];
};
export type AggregationList = {
aggregations: Aggregation[];
total: number;
@@ -441,7 +448,27 @@ export class Billing {
);
}
async listUsage(organizationId: string, queries: string[] = []): Promise<AggregationList> {
async listUsage(organizationId: string, queries: string[] = []): Promise<OrganizationUsage> {
const path = `/organizations/${organizationId}/usage`;
const params = {
organizationId,
queries
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'get',
uri,
{
'content-type': 'application/json'
},
params
);
}
async listAggregation(
organizationId: string,
queries: string[] = []
): Promise<AggregationList> {
const path = `/organizations/${organizationId}/aggregations`;
const params = {
organizationId,
@@ -458,7 +485,7 @@ export class Billing {
);
}
async getUsage(organizationId: string, aggregationId: string): Promise<Aggregation> {
async getAggregation(organizationId: string, aggregationId: string): Promise<Aggregation> {
const path = `/organizations/${organizationId}/aggregations/${aggregationId}`;
const params = {
organizationId,
@@ -12,6 +12,10 @@
import { Alert, Heading } from '$lib/components';
import { failedInvoice, paymentMethods } from '$lib/stores/billing';
import type { PaymentMethodData } from '$lib/sdk/billing';
import { onMount } from 'svelte';
import { page } from '$app/stores';
import { isStripeInitialized, stripe } from '$lib/stores/stripe';
import { base } from '$app/paths';
$: defaultPaymentMethod = $paymentMethods?.paymentMethods?.find(
(method: PaymentMethodData) => method.$id === $organization?.paymentMethodId
@@ -20,6 +24,28 @@
$: backupPaymentMethod = $paymentMethods?.paymentMethods?.find(
(method: PaymentMethodData) => method.$id === $organization?.backupPaymentMethodId
);
onMount(async () => {
if (
$isStripeInitialized &&
$page.url.searchParams.has('invoice') &&
$page.url.searchParams.has('type') &&
$page.url.searchParams.get('type') === 'confirmation'
) {
console.log('test');
console.log($page.url.searchParams.get('invoice'));
const { error } = await $stripe.confirmPayment({
confirmParams: {
// Return URL where the customer should be redirected after the PaymentIntent is confirmed.
return_url: `${base}/console/organization-${$organization.$id}/billing`,
payment_method: $organization.paymentMethodId
}
});
if (error) {
console.log('Something went wrong');
}
}
});
</script>
<Container>
@@ -13,6 +13,6 @@ export const load: PageLoad = async ({ params, parent, depends }) => {
return {
paymentMethods: await sdk.forConsole.billing.listPaymentMethods(),
addressList: await sdk.forConsole.billing.listAddresses(),
aggregationList: await sdk.forConsole.billing.listUsage(params.organization)
oraganizationUsage: await sdk.forConsole.billing.listUsage(params.organization)
};
};
@@ -62,7 +62,7 @@
payments.
</p>
<svelte:fragment slot="aside">
{#if invoiceList.total}
{#if invoiceList.total - 1 > 0}
<Table noMargin noStyles transparent>
<TableHeader>
<TableCellHead>Due Date</TableCellHead>
@@ -1,8 +0,0 @@
import { page } from '$app/stores';
import type { AggregationList } from '$lib/sdk/billing';
import { derived } from 'svelte/store';
export const aggregationList = derived(
page,
($page) => $page.data.aggregationList as AggregationList | null
);
@@ -1,41 +1,41 @@
<script lang="ts">
import { Container } from '$lib/layout';
import { CardGrid, Heading, ProgressBarBig } from '$lib/components';
import { base } from '$app/paths';
import { getServiceLimit } from '$lib/stores/billing.js';
import { getServiceLimit, tierToPlan } from '$lib/stores/billing.js';
import ChangeOrganizationTierCloud from '$routes/console/changeOrganizationTierCloud.svelte';
import { wizard } from '$lib/stores/wizard.js';
import { organization } from '$lib/stores/organization';
import UsageRates from '$routes/console/wizard/cloudOrganization/usageRates.svelte';
const tier = tierToPlan($organization?.billingPlan)?.name;
export let data;
let showRates = false;
$: bandwidth = data.aggregationList.aggregations.reduce(
(acc, curr) => acc + curr.usageBandwidth,
0
);
$: users = data.aggregationList.aggregations.reduce((acc, curr) => acc + curr.usageUsers, 0);
$: executions = data.aggregationList.aggregations.reduce(
(acc, curr) => acc + curr.usageExecutions,
0
);
$: storage = data.aggregationList.aggregations.reduce(
(acc, curr) => acc + curr.usageStorage,
0
);
$: realtime = data.aggregationList.aggregations.reduce(
(acc, curr) => acc + curr.usageRealtime,
0
);
$: console.log(data.organizationUsage.users);
</script>
<Container>
<Heading tag="h2" size="5">Usage</Heading>
<div class="u-flex u-main-space-between common-section">
<p class="text">
If you exceed the limits of the Starter plan, services for your projects may be
disrupted. <a href={`${base}/console/`}>Upgrade for greater capacity</a>.
</p>
{#if $organization.billingPlan === 'tier-2'}
<p class="text">
On the Scale plan, you'll be charged only for any usage that exceeds the thresholds
per resource listed below. <button
on:click={() => (showRates = true)}
class="link"
type="button">Learn more about plan usage limits.</button>
</p>
{:else}
<p class="text">
If you exceed the limits of the {tier} plan, services for your projects may be disrupted.
<button
on:click={() => wizard.start(ChangeOrganizationTierCloud)}
class="link"
type="button">Upgrade for greater capacity</button
>.
</p>
{/if}
<div class="u-flex u-gap-8">
<p class="text">Usage period:</p>
@@ -51,16 +51,23 @@
</p>
<svelte:fragment slot="aside">
<ProgressBarBig unit="GB" max={getServiceLimit('bandwidth')} used={bandwidth} />
<ProgressBarBig
unit="GB"
max={getServiceLimit('bandwidth')}
used={data.organizationUsage.bandwidth[0]} />
</svelte:fragment>
</CardGrid>
<CardGrid>
<Heading tag="h6" size="7">Users</Heading>
<p class="text">The total number of users across all projects in your organization.</p>
<svelte:fragment slot="aside">
<ProgressBarBig unit="Users" max={getServiceLimit('users')} used={users} />
<ProgressBarBig
unit="Users"
max={getServiceLimit('users')}
used={data.organizationUsage.bandwidth[0]} />
</svelte:fragment>
</CardGrid>
@@ -75,7 +82,7 @@
<ProgressBarBig
unit="executions"
max={getServiceLimit('executions')}
used={executions} />
used={data.organizationUsage.executions} />
</svelte:fragment>
</CardGrid>
@@ -87,20 +94,10 @@
</p>
<svelte:fragment slot="aside">
<ProgressBarBig unit="GB" max={getServiceLimit('storage')} used={storage} />
</svelte:fragment>
</CardGrid>
<CardGrid>
<Heading tag="h6" size="7">Realtime connections</Heading>
<p class="text">
Calculated for all realtime concurrent connections and messages sent to all projects in
your organization.
</p>
<svelte:fragment slot="aside">
<ProgressBarBig unit="GB" max={getServiceLimit('storage')} used={realtime} />
<ProgressBarBig
unit="GB"
max={getServiceLimit('storage')}
used={data.organizationUsage.storage} />
</svelte:fragment>
</CardGrid>
@@ -108,3 +105,5 @@
Metrics are estimates updated every 24 hours and may not accurately reflect your invoice.
</p>
</Container>
<UsageRates bind:show={showRates} tier={$organization?.billingPlan} />
@@ -5,6 +5,6 @@ export const load: PageLoad = async ({ params }) => {
const usage = await sdk.forConsole.billing.listUsage(params.organization);
console.log(usage);
return {
aggregationList: usage
organizationUsage: usage
};
};
@@ -57,10 +57,7 @@
const projectId = $page.params.project;
const bucketId = $page.params.bucket;
const totalStorage = data.aggregationList.aggregations.reduce((acc, curr) => {
acc += curr.usageStorage;
return acc;
}, 0);
const usedStorage = data.oraganizationUsage.storage;
const getPreview = (fileId: string) =>
sdk.forProject.storage.getFilePreview(bucketId, fileId, 32, 32).toString() + '&mode=admin';
@@ -96,7 +93,7 @@
title="Files"
serviceId="storage"
isFlex={false}
total={totalStorage}
total={usedStorage}
buttonDisabled={$readOnly.storage}>
<svelte:fragment slot="alert" let:tier let:upgradeMethod>
<Alert type="warning">
@@ -127,7 +124,7 @@
</svelte:fragment>
<svelte:fragment slot="tooltip" let:limit let:tier let:upgradeMethod>
<p class="text">
You are limited to {limit} of storage on the {tier} plan.
You are limited to {limit} GB of storage on the {tier} plan.
<button class="link" type="button" on:click|preventDefault={upgradeMethod}
>Upgrade</button>
@@ -4,7 +4,7 @@ import { getLimit, getPage, getSearch, pageToOffset } from '$lib/helpers/load';
import { Dependencies, PAGE_LIMIT } from '$lib/constants';
import type { PageLoad } from './$types';
import { isCloud } from '$lib/system';
import type { AggregationList } from '$lib/sdk/billing';
import type { OrganizationUsage } from '$lib/sdk/billing';
import { organization } from '$lib/stores/organization';
import { get } from 'svelte/store';
@@ -14,9 +14,9 @@ export const load: PageLoad = async ({ params, depends, url, route }) => {
const search = getSearch(url);
const limit = getLimit(url, route, PAGE_LIMIT);
const offset = pageToOffset(page, limit);
let aggregationList: AggregationList = null;
let oraganizationUsage: OrganizationUsage = null;
if (isCloud && get(organization)) {
aggregationList = await sdk.forConsole.billing.listUsage(get(organization)?.$id);
oraganizationUsage = await sdk.forConsole.billing.listUsage(get(organization)?.$id);
}
return {
@@ -28,6 +28,6 @@ export const load: PageLoad = async ({ params, depends, url, route }) => {
[Query.limit(limit), Query.offset(offset), Query.orderDesc('')],
search
),
aggregationList
oraganizationUsage
};
};
@@ -30,9 +30,9 @@
}
async function checkOverUsage() {
const aggregationList = await sdk.forConsole.billing.listUsage($organization.$id);
const oraganizationUsage = await sdk.forConsole.billing.listUsage($organization.$id);
console.log(aggregationList);
console.log(oraganizationUsage);
}
onMount(async () => {