From 5169964ffeafece9aa71c3dc36d73879aa4e0444 Mon Sep 17 00:00:00 2001 From: ChiragAgg5k Date: Tue, 11 Mar 2025 17:28:00 +0530 Subject: [PATCH] refactor: remaining endpoints from billing.ts --- package.json | 2 +- pnpm-lock.yaml | 10 +-- .../billing/alerts/paymentMandate.svelte | 3 +- src/lib/sdk/billing.ts | 77 ------------------- src/lib/stores/billing.ts | 2 +- src/lib/stores/stripe.ts | 4 +- .../billing/paymentMethods.svelte | 8 +- src/routes/+page.ts | 11 ++- 8 files changed, 19 insertions(+), 98 deletions(-) diff --git a/package.json b/package.json index 03b536752..6d0f86cf1 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "e2e:ui": "playwright test tests/e2e --ui" }, "dependencies": { - "@appwrite.io/console": "https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@88c29ea", + "@appwrite.io/console": "https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@6c1a98e", "@appwrite.io/pink": "0.25.0", "@appwrite.io/pink-icons": "0.25.0", "@popperjs/core": "^2.11.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 837d9c5a7..08a91b246 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@appwrite.io/console': - specifier: https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@88c29ea - version: https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@88c29ea + specifier: https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@6c1a98e + version: https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@6c1a98e '@appwrite.io/pink': specifier: 0.25.0 version: 0.25.0 @@ -199,8 +199,8 @@ packages: '@analytics/type-utils@0.6.2': resolution: {integrity: sha512-TD+xbmsBLyYy/IxFimW/YL/9L2IEnM7/EoV9Aeh56U64Ify8o27HJcKjo38XY9Tcn0uOq1AX3thkKgvtWvwFQg==} - '@appwrite.io/console@https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@88c29ea': - resolution: {tarball: https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@88c29ea} + '@appwrite.io/console@https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@6c1a98e': + resolution: {tarball: https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@6c1a98e} version: 1.5.2 '@appwrite.io/pink-icons@0.25.0': @@ -3887,7 +3887,7 @@ snapshots: '@analytics/type-utils@0.6.2': {} - '@appwrite.io/console@https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@88c29ea': {} + '@appwrite.io/console@https://pkg.pr.new/appwrite-labs/cloud/@appwrite.io/console@6c1a98e': {} '@appwrite.io/pink-icons@0.25.0': {} diff --git a/src/lib/components/billing/alerts/paymentMandate.svelte b/src/lib/components/billing/alerts/paymentMandate.svelte index fa7f75a85..8a7e06b16 100644 --- a/src/lib/components/billing/alerts/paymentMandate.svelte +++ b/src/lib/components/billing/alerts/paymentMandate.svelte @@ -8,8 +8,7 @@ import { confirmSetup } from '$lib/stores/stripe'; async function verifyPaymentMethod() { - const method = await sdk.forConsole.billing.setupPaymentMandate( - $organization.$id, + const method = await sdk.forConsole.account.updatePaymentMethodMandateOptions( $paymentMissingMandate.$id ); await confirmSetup(method.clientSecret, method.$id); diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index c91cf7950..782a6ae48 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -1,6 +1,5 @@ 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 } from '../stores/organization'; export type Estimation = { @@ -198,20 +197,6 @@ export class Billing { }); } - async setMembership( - programId: string - ): Promise<{ $createdAt: string } | { error: { code: number; message: string } }> { - const path = `/console/programs/${programId}/memberships`; - const uri = new URL(this.client.config.endpoint + path); - try { - return await this.client.call('POST', uri, { - 'content-type': 'application/json' - }); - } catch (e) { - return { error: { code: e.code, message: e.message } }; - } - } - async setBillingAddress( organizationId: string, billingAddressId: string @@ -249,48 +234,6 @@ export class Billing { ); } - async getOrganizationPaymentMethod( - organizationId: string, - paymentMethodId: string - ): Promise { - const path = `/organizations/${organizationId}/payment-methods/${paymentMethodId}`; - const params = { - organizationId, - paymentMethodId - }; - const uri = new URL(this.client.config.endpoint + path); - return await this.client.call( - 'GET', - uri, - { - 'content-type': 'application/json' - }, - params - ); - } - - async setPaymentMethod( - paymentMethodId: string, - providerMethodId: string | PaymentMethod, - name: string - ): Promise { - const path = `/account/payment-methods/${paymentMethodId}/provider`; - const params = { - paymentMethodId, - providerMethodId, - name - }; - const uri = new URL(this.client.config.endpoint + path); - return await this.client.call( - 'patch', - uri, - { - 'content-type': 'application/json' - }, - params - ); - } - async setDefaultPaymentMethod(paymentMethodId: string): Promise { const path = `/account/payment-methods/${paymentMethodId}/default`; const params = { @@ -306,24 +249,4 @@ export class Billing { params ); } - - async setupPaymentMandate( - organizationId: string, - paymentMethodId: string - ): Promise { - const path = `/account/payment-methods/${paymentMethodId}/setup`; - const params = { - organizationId, - paymentMethodId - }; - const uri = new URL(this.client.config.endpoint + path); - return await this.client.call( - 'patch', - uri, - { - 'content-type': 'application/json' - }, - params - ); - } } diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index f2f424793..4be6907cc 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -361,7 +361,7 @@ export async function checkPaymentAuthorizationRequired(org: Organization) { export async function paymentExpired(org: Organization) { if (!org?.paymentMethodId) return; - const payment = await sdk.forConsole.billing.getOrganizationPaymentMethod( + const payment = await sdk.forConsole.organizations.getPaymentMethod( org.$id, org.paymentMethodId ); diff --git a/src/lib/stores/stripe.ts b/src/lib/stores/stripe.ts index 3df5d3f90..cc14df306 100644 --- a/src/lib/stores/stripe.ts +++ b/src/lib/stores/stripe.ts @@ -93,9 +93,9 @@ export async function submitStripeCard(name: string, organizationId?: string) { } if (setupIntent && setupIntent.status === 'succeeded') { - const method = await sdk.forConsole.billing.setPaymentMethod( + const method = await sdk.forConsole.account.updatePaymentMethodProvider( paymentMethod.$id, - setupIntent.payment_method, + setupIntent.payment_method as string, name ); paymentElement.destroy(); diff --git a/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte b/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte index 0dba26210..a69403298 100644 --- a/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte +++ b/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte @@ -77,14 +77,14 @@ } $: if ($organization?.backupPaymentMethodId) { - sdk.forConsole.billing - .getOrganizationPaymentMethod($organization.$id, $organization.backupPaymentMethodId) + sdk.forConsole.organizations + .getPaymentMethod($organization.$id, $organization.backupPaymentMethodId) .then((res) => (backupPaymentMethod = res)); } $: if ($organization?.paymentMethodId) { - sdk.forConsole.billing - .getOrganizationPaymentMethod($organization.$id, $organization.paymentMethodId) + sdk.forConsole.organizations + .getPaymentMethod($organization.$id, $organization.paymentMethodId) .then((res) => (defaultPaymentMethod = res)); } diff --git a/src/routes/+page.ts b/src/routes/+page.ts index dc0b6e5ea..e8d197c54 100644 --- a/src/routes/+page.ts +++ b/src/routes/+page.ts @@ -1,20 +1,19 @@ import { base } from '$app/paths'; import { sdk } from '$lib/stores/sdk'; import { VARS } from '$lib/system'; +import type { AppwriteException } from '@appwrite.io/console'; import { redirect } from '@sveltejs/kit'; import type { PageLoad } from './$types'; const handleGithubEducationMembership = async (name: string, email: string) => { - const result = await sdk.forConsole.billing.setMembership('github-student-developer'); + const result = await sdk.forConsole.console.createProgramMembership('github-student-developer'); if (result && 'error' in result) { - if (result.error.code === 409) { + const error = result.error as AppwriteException; + if (error.code === 409) { redirect(303, `${base}/account/organizations`); } else { await sdk.forConsole.account.deleteSession('current'); - redirect( - 303, - `${base}/education/error?message=${result.error.message}&code=${result.error.code}` - ); + redirect(303, `${base}/education/error?message=${error.message}&code=${error.code}`); } } else if (result && '$createdAt' in result) { setToGhStudentMailingList(name, email);