Refactor code and simplify billing services

Code for billing services has been refactored for simplicity and improved organization. This includes updated locations for the 'loadTeamWorkspace' function and adjusted component imports. Redundant scripts have been eliminated and new schemas have been introduced for input data validation.
This commit is contained in:
giancarlo
2024-04-06 13:37:38 +08:00
parent 7112560efe
commit d3bd8fb033
13 changed files with 315 additions and 170 deletions

View File

@@ -4,183 +4,39 @@ import { redirect } from 'next/navigation';
import { z } from 'zod';
import { getBillingGatewayProvider } from '@kit/billing-gateway';
import { requireUser } from '@kit/supabase/require-user';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import appConfig from '~/config/app.config';
import billingConfig from '~/config/billing.config';
import pathsConfig from '~/config/paths.config';
import { TeamBillingPortalSchema } from '~/(dashboard)/home/[account]/_lib/schema/team-billing-portal.schema';
import { TeamCheckoutSchema } from '../_lib/schema/team-checkout.schema';
import { TeamBillingService } from '../_lib/server/team-billing.service';
/**
* Creates a checkout session for a team account.
*
* @param {object} params - The parameters for creating the checkout session.
* @param {string} params.planId - The ID of the plan to be associated with the account.
* @name createTeamAccountCheckoutSession
* @description Creates a checkout session for a team account.
*/
export async function createTeamAccountCheckoutSession(params: {
productId: string;
planId: string;
accountId: string;
slug: string;
}) {
const client = getSupabaseServerActionClient();
export async function createTeamAccountCheckoutSession(
params: z.infer<typeof TeamCheckoutSchema>,
) {
const data = TeamCheckoutSchema.parse(params);
// we parse the plan ID from the parameters
// no need in continuing if the plan ID is not valid
const planId = z.string().min(1).parse(params.planId);
const productId = z.string().min(1).parse(params.productId);
const service = new TeamBillingService(getSupabaseServerActionClient());
// we require the user to be authenticated
const { data: user } = await requireUser(client);
if (!user) {
throw new Error('Authentication required');
}
const userId = user.id;
const accountId = params.accountId;
const hasPermission = await getPermissionsForAccountId(userId, accountId);
// if the user does not have permission to manage billing for the account
// then we should not proceed
if (!hasPermission) {
throw new Error('Permission denied');
}
// here we have confirmed that the user has permission to manage billing for the account
// so we go on and create a checkout session
const service = await getBillingGatewayProvider(client);
const product = billingConfig.products.find(
(product) => product.id === productId,
);
if (!product) {
throw new Error('Product not found');
}
const plan = product?.plans.find((plan) => plan.id === planId);
if (!plan) {
throw new Error('Plan not found');
}
// find the customer ID for the account if it exists
// (eg. if the account has been billed before)
const customerId = await getCustomerIdFromAccountId(client, accountId);
const customerEmail = user.email;
// the return URL for the checkout session
const returnUrl = getCheckoutSessionReturnUrl(params.slug);
// call the payment gateway to create the checkout session
const { checkoutToken } = await service.createCheckoutSession({
accountId,
plan,
returnUrl,
customerEmail,
customerId,
});
// return the checkout token to the client
// so we can call the payment gateway to complete the checkout
return {
checkoutToken,
};
return service.createCheckout(data);
}
/**
* @name createBillingPortalSession
* @description Creates a Billing Session Portal and redirects the user to the
* provider's hosted instance
*/
export async function createBillingPortalSession(formData: FormData) {
const client = getSupabaseServerActionClient();
const params = TeamBillingPortalSchema.parse(Object.fromEntries(formData));
const { accountId, slug } = z
.object({
accountId: z.string().min(1),
slug: z.string().min(1),
})
.parse(Object.fromEntries(formData));
const service = new TeamBillingService(getSupabaseServerActionClient());
const { data: user, error } = await requireUser(client);
// get url to billing portal
const url = await service.createBillingPortalSession(params);
if (error ?? !user) {
throw new Error('Authentication required');
}
const userId = user.id;
// we require the user to have permissions to manage billing for the account
const hasPermission = await getPermissionsForAccountId(userId, accountId);
// if the user does not have permission to manage billing for the account
// then we should not proceed
if (!hasPermission) {
throw new Error('Permission denied');
}
const service = await getBillingGatewayProvider(client);
const customerId = await getCustomerIdFromAccountId(client, accountId);
const returnUrl = getBillingPortalReturnUrl(slug);
if (!customerId) {
throw new Error('Customer not found');
}
const { url } = await service.createBillingPortalSession({
customerId,
returnUrl,
});
// redirect the user to the billing portal
return redirect(url);
}
function getCheckoutSessionReturnUrl(accountSlug: string) {
return new URL(pathsConfig.app.accountBillingReturn, appConfig.url)
.toString()
.replace('[account]', accountSlug);
}
function getBillingPortalReturnUrl(accountSlug: string) {
return new URL(pathsConfig.app.accountBilling, appConfig.url)
.toString()
.replace('[account]', accountSlug);
}
/**
* Retrieves the permissions for a user on an account for managing billing.
* @param userId
* @param accountId
*/
async function getPermissionsForAccountId(userId: string, accountId: string) {
const client = getSupabaseServerActionClient();
const { data, error } = await client.rpc('has_permission', {
account_id: accountId,
user_id: userId,
permission_name: 'billing.manage',
});
if (error) {
throw error;
}
return data;
}
async function getCustomerIdFromAccountId(
client: ReturnType<typeof getSupabaseServerActionClient>,
accountId: string,
) {
const { data, error } = await client
.from('billing_customers')
.select('customer_id')
.eq('account_id', accountId)
.maybeSingle();
if (error) {
throw error;
}
return data?.customer_id;
}