refactor: account billing endpoints

This commit is contained in:
ChiragAgg5k
2025-03-06 17:11:25 +05:30
parent 0b006343af
commit 343858ba9a
12 changed files with 56 additions and 211 deletions
+1 -151
View File
@@ -2,7 +2,7 @@ import type { Tier } from '$lib/stores/billing';
import type { Campaign } from '$lib/stores/campaigns';
import type { Client, Models } from '@appwrite.io/console';
import type { PaymentMethod } from '@stripe/stripe-js';
import type { Organization, OrganizationError, OrganizationList } from '../stores/organization';
import type { Organization, OrganizationError } from '../stores/organization';
export type Invoice = {
$id: string;
@@ -158,22 +158,6 @@ export class Billing {
this.client = client;
}
async listOrganization(queries: string[] = []): Promise<OrganizationList> {
const path = `/organizations`;
const params = {
queries
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'GET',
uri,
{
'content-type': 'application/json'
},
params
);
}
async validateOrganization(organizationId: string, invites: string[]): Promise<Organization> {
const path = `/organizations/${organizationId}/validate`;
const params = {
@@ -247,22 +231,6 @@ export class Billing {
);
}
async deleteOrganization(organizationId: string): Promise<Organization> {
const path = `/organizations/${organizationId}`;
const params = {
organizationId
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'DELETE',
uri,
{
'content-type': 'application/json'
},
params
);
}
async estimationDeleteOrganization(
organizationId: string
): Promise<EstimationDeleteOrganization> {
@@ -954,124 +922,6 @@ export class Billing {
);
}
async listAddresses(queries: string[] = []): Promise<AddressesList> {
const path = `/account/billing-addresses`;
const params = {
queries
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'get',
uri,
{
'content-type': 'application/json'
},
params
);
}
async getAddress(billingAddressId: string): Promise<Address> {
const path = `/account/billing-addresses/${billingAddressId}`;
const params = {
billingAddressId
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'get',
uri,
{
'content-type': 'application/json'
},
params
);
}
async createAddress(
country: string,
streetAddress: string,
city: string,
state: string,
postalCode: string,
addressLine2?: string
): Promise<Address> {
const path = `/account/billing-addresses`;
const params = {
country,
streetAddress,
city,
state,
postalCode,
addressLine2
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'POST',
uri,
{
'content-type': 'application/json'
},
params
);
}
async updateAddress(
billingAddressId: string,
country: string,
streetAddress: string,
city: string,
state: string,
postalCode: string,
addressLine2?: string
): Promise<Address> {
const path = `/account/billing-addresses/${billingAddressId}`;
const params = {
billingAddressId,
country,
streetAddress,
city,
state,
postalCode,
addressLine2
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'PUT',
uri,
{
'content-type': 'application/json'
},
params
);
}
async deleteAddress(billingAddressId: string): Promise<void> {
const path = `/account/billing-addresses/${billingAddressId}`;
const params = {
billingAddressId
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'delete',
uri,
{
'content-type': 'application/json'
},
params
);
}
async listRegions(): Promise<Models.ConsoleRegionList> {
const path = `/console/regions`;
const params = {};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'GET',
uri,
{
'content-type': 'application/json'
},
params
);
}
async getPlansInfo(): Promise<PlanList> {
const path = `/console/plans`;
const params = {};
-5
View File
@@ -34,11 +34,6 @@ export type Organization = Models.Team<Record<string, unknown>> & {
billingInvoiceId: string;
};
export type OrganizationList = {
teams: Organization[];
total: number;
};
export type BillingLimits = {
bandwidth: number;
documents: number;
@@ -8,7 +8,7 @@ export const load: PageLoad = async ({ depends }) => {
const [paymentMethods, addressList] = await Promise.all([
sdk.forConsole.billing.listPaymentMethods(),
sdk.forConsole.billing.listAddresses()
sdk.forConsole.account.listBillingAddresses()
]);
return {
paymentMethods,
@@ -1,6 +1,6 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Modal } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, FormItem, FormList, InputSelect, InputText } from '$lib/elements/forms';
@@ -42,7 +42,7 @@
async function handleSubmit() {
try {
const response = await sdk.forConsole.billing.createAddress(
const response = await sdk.forConsole.account.createBillingAddress(
country,
address,
city,
@@ -1,7 +1,7 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { base } from '$app/paths';
import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Modal } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button } from '$lib/elements/forms';
@@ -17,7 +17,7 @@
async function handleDelete() {
try {
await sdk.forConsole.billing.deleteAddress(selectedAddress.$id);
await sdk.forConsole.account.deleteBillingAddress(selectedAddress.$id);
await invalidate(Dependencies.PAYMENT_METHODS);
showDelete = false;
addNotification({
@@ -1,6 +1,6 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Modal } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, FormItem, FormList, InputSelect, InputText } from '$lib/elements/forms';
@@ -36,7 +36,7 @@
async function handleSubmit() {
try {
await sdk.forConsole.billing.updateAddress(
await sdk.forConsole.account.updateBillingAddress(
selectedAddress.$id,
selectedAddress.country,
selectedAddress.streetAddress,
@@ -1,14 +1,8 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { Pill } from '$lib/elements';
import { Button } from '$lib/elements/forms';
import { Container } from '$lib/layout';
import Create from './createProjectCloud.svelte';
import CreateProject from './createProject.svelte';
import CreateOrganization from '../createOrganization.svelte';
import { wizard } from '$lib/stores/wizard';
import { GRACE_PERIOD_OVERRIDE, isCloud } from '$lib/system';
import { page } from '$app/stores';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { registerCommands } from '$lib/commandCenter';
import {
CardContainer,
@@ -19,20 +13,26 @@
Heading,
PaginationWithLimit
} from '$lib/components';
import { goto } from '$app/navigation';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Pill } from '$lib/elements';
import { Button } from '$lib/elements/forms';
import { checkPricingRefAndRedirect } from '$lib/helpers/pricingRedirect';
import { Container } from '$lib/layout';
import { readOnly } from '$lib/stores/billing';
import { organization } from '$lib/stores/organization';
import { services } from '$lib/stores/project-services';
import { canWriteProjects } from '$lib/stores/roles';
import { sdk } from '$lib/stores/sdk';
import { wizard } from '$lib/stores/wizard';
import { GRACE_PERIOD_OVERRIDE, isCloud } from '$lib/system';
import { regions as regionsStore } from '$routes/(console)/organization-[organization]/store';
import { loading } from '$routes/store';
import type { Models } from '@appwrite.io/console';
import { ID, Region } from '@appwrite.io/console';
import { openImportWizard } from '../project-[project]/settings/migrations/(import)';
import { readOnly } from '$lib/stores/billing';
import { onMount } from 'svelte';
import { organization } from '$lib/stores/organization';
import { canWriteProjects } from '$lib/stores/roles';
import { checkPricingRefAndRedirect } from '$lib/helpers/pricingRedirect';
import { regions as regionsStore } from '$routes/(console)/organization-[organization]/store';
import CreateOrganization from '../createOrganization.svelte';
import { openImportWizard } from '../project-[project]/settings/migrations/(import)';
import CreateProject from './createProject.svelte';
import Create from './createProjectCloud.svelte';
export let data;
@@ -123,7 +123,7 @@
onMount(async () => {
if (isCloud) {
const regions = await sdk.forConsole.billing.listRegions();
const regions = await sdk.forConsole.console.regions();
regionsStore.set(regions);
checkPricingRefAndRedirect($page.url.searchParams);
}
@@ -52,7 +52,7 @@ export const load: PageLoad = async ({ parent, depends }) => {
const [paymentMethods, addressList, billingAddress, creditList, aggregationBillingPlan] =
await Promise.all([
sdk.forConsole.billing.listPaymentMethods(),
sdk.forConsole.billing.listAddresses(),
sdk.forConsole.account.listBillingAddresses(),
billingAddressPromise,
sdk.forConsole.billing.listCredits(organization.$id),
sdk.forConsole.billing.getPlan(billingAggregation?.plan ?? organization.billingPlan)
@@ -1,19 +1,19 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Alert, Modal, RadioBoxes } from '$lib/components';
import { Button, FormItem, FormList, InputSelect, InputText } from '$lib/elements/forms';
import { sdk } from '$lib/stores/sdk';
import { organization } from '$lib/stores/organization';
import { Dependencies } from '$lib/constants';
import { onMount } from 'svelte';
import type { AddressesList } from '$lib/sdk/billing';
import { addNotification } from '$lib/stores/notifications';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Pill } from '$lib/elements';
import { base } from '$app/paths';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Alert, Modal, RadioBoxes } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Pill } from '$lib/elements';
import { Button, FormItem, FormList, InputSelect, InputText } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { organization } from '$lib/stores/organization';
import { sdk } from '$lib/stores/sdk';
import type { Models } from '@appwrite.io/console';
import { onMount } from 'svelte';
export let show = false;
let addresses: AddressesList;
let addresses: Models.BillingAddressList;
let selectedAddress: string;
let error: string;
let country: string;
@@ -30,7 +30,7 @@
];
onMount(async () => {
addresses = await sdk.forConsole.billing.listAddresses();
addresses = await sdk.forConsole.account.listBillingAddresses();
const firstNonCurrentAddress = addresses?.billingAddresses?.find(
(address) => address.$id !== $organization?.billingAddressId
@@ -58,7 +58,7 @@
if (selectedAddress === $organization.billingAddressId) {
show = false;
} else if (selectedAddress === null) {
const address = await sdk.forConsole.billing.createAddress(
const address = await sdk.forConsole.account.createBillingAddress(
country,
streetAddress,
city,
@@ -1,17 +1,10 @@
<script lang="ts">
import { Modal, SecondaryTabs, SecondaryTabsItem } from '$lib/components';
import { Button, FormList, InputText } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdk } from '$lib/stores/sdk';
import { members, organization, organizationList } from '$lib/stores/organization';
import { goto, invalidate } from '$app/navigation';
import { base } from '$app/paths';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { Modal, SecondaryTabs, SecondaryTabsItem } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { projects } from '../store';
import { toLocaleDate } from '$lib/helpers/date';
import { isCloud } from '$lib/system';
import type { EstimationDeleteOrganization } from '$lib/sdk/billing';
import { Button, FormList, InputText } from '$lib/elements/forms';
import {
TableBody,
TableCell,
@@ -20,9 +13,16 @@
TableRow,
TableScroll
} from '$lib/elements/table';
import { onMount } from 'svelte';
import DeleteOrganizationEstimation from './deleteOrganizationEstimation.svelte';
import { toLocaleDate } from '$lib/helpers/date';
import type { EstimationDeleteOrganization } from '$lib/sdk/billing';
import { billingURL } from '$lib/stores/billing';
import { addNotification } from '$lib/stores/notifications';
import { members, organization, organizationList } from '$lib/stores/organization';
import { sdk } from '$lib/stores/sdk';
import { isCloud } from '$lib/system';
import { onMount } from 'svelte';
import { projects } from '../store';
import DeleteOrganizationEstimation from './deleteOrganizationEstimation.svelte';
export let showDelete = false;
let error: string = null;
@@ -39,7 +39,7 @@
async function deleteOrg() {
try {
if (isCloud) {
await sdk.forConsole.billing.deleteOrganization($organization.$id);
await sdk.forConsole.organizations.delete($organization.$id);
} else {
await sdk.forConsole.teams.delete($organization.$id);
}
@@ -1,16 +1,16 @@
<script lang="ts">
import { CustomId } from '$lib/components';
import { Pill } from '$lib/elements';
import { InputText, FormList } from '$lib/elements/forms';
import { FormList, InputText } from '$lib/elements/forms';
import { WizardStep } from '$lib/layout';
import { sdk } from '$lib/stores/sdk';
import { createProject } from './store';
import { regionFlagUrls, regions } from '$routes/(console)/organization-[organization]/store';
import { createProject } from './store';
let showCustomId = false;
if (!$regions?.regions) {
sdk.forConsole.billing.listRegions().then(regions.set);
sdk.forConsole.console.regions().then(regions.set);
}
</script>
@@ -18,7 +18,7 @@
let regions: Models.ConsoleRegionList;
onMount(async () => {
if (isCloud) {
regions = await sdk.forConsole.billing.listRegions();
regions = await sdk.forConsole.console.regions();
}
});