From 96b3da75c495ce209a1eda20de7d7f6345d5cf78 Mon Sep 17 00:00:00 2001 From: Arman Date: Mon, 13 Nov 2023 20:38:03 +0100 Subject: [PATCH] feat: organization usage, stipe payment confimation --- src/lib/sdk/billing.ts | 31 ++++++- .../billing/+page.svelte | 26 ++++++ .../billing/+page.ts | 2 +- .../billing/paymentHistory.svelte | 2 +- .../billing/store.ts | 8 -- .../usage/[[period]]/+page.svelte | 87 +++++++++---------- .../usage/[[period]]/+page.ts | 2 +- .../storage/bucket-[bucket]/+page.svelte | 9 +- .../storage/bucket-[bucket]/+page.ts | 8 +- .../cloudOrganizationChangeTier/step1.svelte | 4 +- 10 files changed, 110 insertions(+), 69 deletions(-) delete mode 100644 src/routes/console/organization-[organization]/billing/store.ts diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index 101ca6020..855152779 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -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 { + async listUsage(organizationId: string, queries: string[] = []): Promise { + 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 { const path = `/organizations/${organizationId}/aggregations`; const params = { organizationId, @@ -458,7 +485,7 @@ export class Billing { ); } - async getUsage(organizationId: string, aggregationId: string): Promise { + async getAggregation(organizationId: string, aggregationId: string): Promise { const path = `/organizations/${organizationId}/aggregations/${aggregationId}`; const params = { organizationId, diff --git a/src/routes/console/organization-[organization]/billing/+page.svelte b/src/routes/console/organization-[organization]/billing/+page.svelte index 8046b7ecd..67ed5acac 100644 --- a/src/routes/console/organization-[organization]/billing/+page.svelte +++ b/src/routes/console/organization-[organization]/billing/+page.svelte @@ -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'); + } + } + }); diff --git a/src/routes/console/organization-[organization]/billing/+page.ts b/src/routes/console/organization-[organization]/billing/+page.ts index 2285e7ae0..f122b101a 100644 --- a/src/routes/console/organization-[organization]/billing/+page.ts +++ b/src/routes/console/organization-[organization]/billing/+page.ts @@ -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) }; }; diff --git a/src/routes/console/organization-[organization]/billing/paymentHistory.svelte b/src/routes/console/organization-[organization]/billing/paymentHistory.svelte index c328cff9a..ddb0a36da 100644 --- a/src/routes/console/organization-[organization]/billing/paymentHistory.svelte +++ b/src/routes/console/organization-[organization]/billing/paymentHistory.svelte @@ -62,7 +62,7 @@ payments.

- {#if invoiceList.total} + {#if invoiceList.total - 1 > 0} Due Date diff --git a/src/routes/console/organization-[organization]/billing/store.ts b/src/routes/console/organization-[organization]/billing/store.ts deleted file mode 100644 index 57b6cc703..000000000 --- a/src/routes/console/organization-[organization]/billing/store.ts +++ /dev/null @@ -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 -); diff --git a/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte b/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte index 7a67b43e3..4c8e376b5 100644 --- a/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte +++ b/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte @@ -1,41 +1,41 @@ Usage
-

- If you exceed the limits of the Starter plan, services for your projects may be - disrupted. Upgrade for greater capacity. -

+ {#if $organization.billingPlan === 'tier-2'} +

+ On the Scale plan, you'll be charged only for any usage that exceeds the thresholds + per resource listed below. +

+ {:else} +

+ If you exceed the limits of the {tier} plan, services for your projects may be disrupted. + . +

+ {/if}

Usage period:

@@ -51,16 +51,23 @@

- + + Users

The total number of users across all projects in your organization.

- +
@@ -75,7 +82,7 @@ + used={data.organizationUsage.executions} /> @@ -87,20 +94,10 @@

- - - - - - Realtime connections - -

- Calculated for all realtime concurrent connections and messages sent to all projects in - your organization. -

- - - +
@@ -108,3 +105,5 @@ Metrics are estimates updated every 24 hours and may not accurately reflect your invoice.

+ + diff --git a/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts b/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts index 7273b97b7..52cbd4460 100644 --- a/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts +++ b/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts @@ -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 }; }; diff --git a/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.svelte b/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.svelte index 1783fdd66..5189d0eee 100644 --- a/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.svelte +++ b/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.svelte @@ -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}> @@ -127,7 +124,7 @@

- You are limited to {limit} of storage on the {tier} plan. + You are limited to {limit} GB of storage on the {tier} plan. diff --git a/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.ts b/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.ts index 27a355cab..a2ead3698 100644 --- a/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.ts +++ b/src/routes/console/project-[project]/storage/bucket-[bucket]/+page.ts @@ -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 }; }; diff --git a/src/routes/console/wizard/cloudOrganizationChangeTier/step1.svelte b/src/routes/console/wizard/cloudOrganizationChangeTier/step1.svelte index c4506f966..eeb51b388 100644 --- a/src/routes/console/wizard/cloudOrganizationChangeTier/step1.svelte +++ b/src/routes/console/wizard/cloudOrganizationChangeTier/step1.svelte @@ -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 () => {