diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index 9f7f85c4da..b2d22568cf 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -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; @@ -38,6 +39,7 @@ export type Invoice = { status: string; dueAt: string; clientSecret: string; + tier: Tier; }; export type InvoiceList = { @@ -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; executions: number; storage: number; - users: { date: string; value: number }[]; + users: Array; }; export type AggregationList = { diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index eecc1b7483..1638598236 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -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]; } diff --git a/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte b/src/routes/console/organization-[organization]/usage/[[invoice]]/+page.svelte similarity index 65% rename from src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte rename to src/routes/console/organization-[organization]/usage/[[invoice]]/+page.svelte index fcde6545ef..2ceac055e3 100644 --- a/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte +++ b/src/routes/console/organization-[organization]/usage/[[invoice]]/+page.svelte @@ -1,42 +1,37 @@ - - Usage
@@ -50,7 +45,7 @@

{:else}

- 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.

@@ -98,8 +84,8 @@ + max={getServiceLimit('bandwidth', tier)} + used={data.organizationUsage.bandwidth[0]?.value ?? 0} /> @@ -111,8 +97,8 @@ + max={getServiceLimit('users', tier)} + used={data.organizationUsage.bandwidth[0]?.value ?? 0} /> @@ -126,7 +112,7 @@ @@ -141,7 +127,7 @@ diff --git a/src/routes/console/organization-[organization]/usage/[[invoice]]/+page.ts b/src/routes/console/organization-[organization]/usage/[[invoice]]/+page.ts new file mode 100644 index 0000000000..c2f3584748 --- /dev/null +++ b/src/routes/console/organization-[organization]/usage/[[invoice]]/+page.ts @@ -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 + }; +}; diff --git a/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts b/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts deleted file mode 100644 index ad93286ae2..0000000000 --- a/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts +++ /dev/null @@ -1,34 +0,0 @@ -import type { Organization } from '$lib/stores/organization'; -import { sdk } from '$lib/stores/sdk'; -import type { PageLoad } from './$types'; - -export const load: PageLoad = async ({ params, parent, url }) => { - const { period } = params; - const parentData = await parent(); - const org = parentData.organization as Organization; - const today = new Date().toISOString(); - let startDate: string; - let endDate: string; - - if (period === 'current') { - startDate = org.billingCurrentInvoiceDate; - endDate = today; - } else if (period === 'previous') { - const prevDate = new Date(org.billingCurrentInvoiceDate); - prevDate.setDate(prevDate.getDate() - 30); - startDate = prevDate.toISOString(); - endDate = org.billingCurrentInvoiceDate; - } else if (period === 'custom') { - startDate = url.searchParams.get('start') ?? org.billingCurrentInvoiceDate; - endDate = url.searchParams.get('end') ?? today; - } - const usage = await sdk.forConsole.billing.listUsage( - params.organization, - startDate ?? org.billingCurrentInvoiceDate, - endDate ?? today - ); - console.log(usage); - return { - organizationUsage: usage - }; -};