Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: org creation/upgrade refactor #1328

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/lib/sdk/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,23 @@ export class Billing {
name: string,
billingPlan: string,
paymentMethodId: string,
billingAddressId: string = undefined
billingAddressId: string = undefined,
invites: string[] = undefined,
couponId: string = undefined,
taxId: string = undefined,
billingBudget: number = undefined
): Promise<Organization> {
const path = `/organizations`;
const params = {
organizationId,
name,
billingPlan,
paymentMethodId,
billingAddressId
billingAddressId,
invites,
couponId,
taxId,
billingBudget
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
Expand Down Expand Up @@ -384,14 +392,37 @@ export class Billing {
organizationId: string,
billingPlan: string,
paymentMethodId: string,
billingAddressId: string = undefined
billingAddressId: string = undefined,
invites: string[] = undefined,
couponId: string = undefined,
taxId: string = undefined,
billingBudget: number = undefined
): Promise<Organization> {
const path = `/organizations/${organizationId}/plan`;
const params = {
organizationId,
billingPlan,
paymentMethodId,
billingAddressId
billingAddressId,
invites,
couponId,
taxId,
billingBudget
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'patch',
uri,
{
'content-type': 'application/json'
},
params
);
}
async validateOrganization(organizationId: string): Promise<Organization> {
const path = `/organizations/${organizationId}/validate`;
const params = {
organizationId
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
Expand Down
20 changes: 20 additions & 0 deletions src/lib/stores/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,23 @@ export function calculateResourceSurplus(total: number, limit: number, limitUnit
const realLimit = (limitUnit ? sizeToBytes(limit, limitUnit) : limit) || Infinity;
return total > realLimit ? total - realLimit : 0;
}

export function buildOrgRedirectURL(
base: string,
name: string,
plan: string,
paymentMethodId: string,
id: string,
collaborators: string[] = null,
coupon: string = null
) {
const url = new URL(base);
url.searchParams.append('name', name);
url.searchParams.append('plan', plan);
url.searchParams.append('paymentMethod', paymentMethodId);
url.searchParams.append('validate', id);
if (collaborators?.length) url.searchParams.append('collaborators', collaborators.join(','));
if (coupon) url.searchParams.append('coupon', coupon);

return url.toString();
}
1 change: 1 addition & 0 deletions src/lib/stores/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Organization = Models.Team<Record<string, unknown>> & {
amount: number;
billingTaxId?: string;
billingPlanDowngrade?: Tier;
client_secret?: string;
};

export type OrganizationList = {
Expand Down
88 changes: 62 additions & 26 deletions src/routes/(console)/apply-credit/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
} from '$lib/layout';
import { type PaymentList } from '$lib/sdk/billing';
import { app } from '$lib/stores/app';
import { buildOrgRedirectURL } from '$lib/stores/billing.js';
import { campaigns } from '$lib/stores/campaigns';
import { addNotification } from '$lib/stores/notifications';
import { organizationList, type Organization } from '$lib/stores/organization';
import { sdk } from '$lib/stores/sdk';
import { confirmPayment } from '$lib/stores/stripe.js';
import { ID } from '@appwrite.io/console';
import { onMount } from 'svelte';
import { writable } from 'svelte/store';
Expand Down Expand Up @@ -83,6 +85,19 @@
if (campaign?.plan) {
billingPlan = campaign.plan;
}
if ($page.url.searchParams.has('name')) {
name = $page.url.searchParams.get('name');
}
if ($page.url.searchParams.has('paymentMethod')) {
paymentMethodId = $page.url.searchParams.get('paymentMethod');
}
if ($page.url.searchParams.has('collaborators')) {
collaborators = $page.url.searchParams.get('collaborators')?.split(',') ?? [];
}
if ($page.url.searchParams.has('validate')) {
const id = $page.url.searchParams.get('validate');
await sdk.forConsole.billing.validateOrganization(id);
}
});

async function loadPaymentMethods() {
Expand All @@ -105,7 +120,12 @@
newOrgId,
name,
billingPlan,
paymentMethodId
paymentMethodId,
null,
collaborators?.length ? collaborators : null,
couponData?.code ? couponData.code : null,
taxId ?? null,
billingBudget ?? null
);
}
// Upgrade existing org
Expand All @@ -114,42 +134,58 @@
selectedOrg.$id,
billingPlan,
paymentMethodId,
null
null,
collaborators?.length ? collaborators : null,
couponData?.code ? couponData.code : null,
taxId ?? null,
billingBudget ?? null
);
}
// Existing pro org
else {
org = selectedOrg;
}

// Add coupon
if (couponData?.code) {
await sdk.forConsole.billing.addCredit(org.$id, couponData.code);
}
if (couponData?.code) {
await sdk.forConsole.billing.addCredit(org.$id, couponData.code);
}
if (collaborators?.length) {
collaborators.forEach(async (collaborator) => {
await sdk.forConsole.teams.createMembership(
org.$id,
['owner'],
collaborator,
undefined,
undefined,
`${$page.url.origin}/${base}/organization-${org.$id}`
);
});
}

// Add budget
if (billingBudget) {
await sdk.forConsole.billing.updateBudget(org.$id, billingBudget, [75]);
}
if (billingBudget) {
await sdk.forConsole.billing.updateBudget(org.$id, billingBudget, [75]);
}

// Add collaborators
if (collaborators?.length) {
collaborators.forEach(async (collaborator) => {
await sdk.forConsole.teams.createMembership(
org.$id,
['owner'],
collaborator,
undefined,
undefined,
`${$page.url.origin}/${base}/organization-${org.$id}`
);
});
if (taxId) {
await sdk.forConsole.billing.updateTaxId(org.$id, taxId);
}
}

// Add tax ID
if (taxId) {
await sdk.forConsole.billing.updateTaxId(org.$id, taxId);
// Confirm payment if required
if (org?.client_secret) {
let redirectURL = buildOrgRedirectURL(
`${base}/apply-credit`,
name,
billingPlan,
paymentMethodId,
org.$id,
collaborators,
couponData?.code
);
await confirmPayment(org.$id, org.client_secret, paymentMethodId, redirectURL);

await sdk.forConsole.billing.validateOrganization(org.$id);
}

trackEvent(Submit.CreditRedeem, {
coupon: couponData.code,
campaign: couponData?.campaign
Expand Down
63 changes: 32 additions & 31 deletions src/routes/(console)/create-organization/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
WizardSecondaryFooter
} from '$lib/layout';
import type { Coupon, PaymentList } from '$lib/sdk/billing';
import { tierToPlan } from '$lib/stores/billing';
import { buildOrgRedirectURL, tierToPlan } from '$lib/stores/billing';
import { addNotification } from '$lib/stores/notifications';
import { organizationList, type Organization } from '$lib/stores/organization';
import { sdk } from '$lib/stores/sdk';
import { confirmPayment } from '$lib/stores/stripe';
import { ID } from '@appwrite.io/console';
import { onMount } from 'svelte';
import { writable } from 'svelte/store';
Expand Down Expand Up @@ -79,6 +80,16 @@
billingPlan = plan as BillingPlan;
}
}
if ($page.url.searchParams.has('paymentMethod')) {
paymentMethodId = $page.url.searchParams.get('paymentMethod');
}
if ($page.url.searchParams.has('collaborators')) {
collaborators = $page.url.searchParams.get('collaborators')?.split(',') ?? [];
}
if ($page.url.searchParams.has('validate')) {
const id = $page.url.searchParams.get('validate');
await sdk.forConsole.billing.validateOrganization(id);
}
if (anyOrgFree) {
billingPlan = BillingPlan.PRO;
}
Expand Down Expand Up @@ -107,44 +118,34 @@
name,
billingPlan,
paymentMethodId,
null
null,
collaborators?.length ? collaborators : null,
couponData?.code ?? null,
taxId ?? null,
billingBudget ?? null
);

//Add budget
if (billingBudget) {
await sdk.forConsole.billing.updateBudget(org.$id, billingBudget, [75]);
}

//Add coupon
if (couponData?.code) {
await sdk.forConsole.billing.addCredit(org.$id, couponData.code);
trackEvent(Submit.CreditRedeem);
}

//Add collaborators
if (collaborators?.length) {
collaborators.forEach(async (collaborator) => {
await sdk.forConsole.teams.createMembership(
org.$id,
['developer'],
collaborator,
undefined,
undefined,
`${$page.url.origin}/${base}/organization-${org.$id}`
);
});
}

// Add tax ID
if (taxId) {
await sdk.forConsole.billing.updateTaxId(org.$id, taxId);
if (org?.client_secret) {
let redirectURL = buildOrgRedirectURL(
`${base}/create-organization`,
name,
billingPlan,
paymentMethodId,
org.$id,
collaborators,
couponData?.code
);
await confirmPayment(org.$id, org.client_secret, paymentMethodId, redirectURL);

await sdk.forConsole.billing.validateOrganization(org.$id);
}
}

trackEvent(Submit.OrganizationCreate, {
plan: tierToPlan(billingPlan)?.name,
budget_cap_enabled: !!billingBudget,
members_invited: collaborators?.length
members_invited: collaborators?.length,
coupon: couponData?.code ?? null
});

await invalidate(Dependencies.ACCOUNT);
Expand Down
Loading
Loading