Files
myeasycms-v2/apps/web/app/(dashboard)/home/[account]/billing/server-actions.ts
giancarlo d3bd8fb033 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.
2024-04-06 13:37:38 +08:00

43 lines
1.3 KiB
TypeScript

'use server';
import { redirect } from 'next/navigation';
import { z } from 'zod';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
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';
/**
* @name createTeamAccountCheckoutSession
* @description Creates a checkout session for a team account.
*/
export async function createTeamAccountCheckoutSession(
params: z.infer<typeof TeamCheckoutSchema>,
) {
const data = TeamCheckoutSchema.parse(params);
const service = new TeamBillingService(getSupabaseServerActionClient());
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 params = TeamBillingPortalSchema.parse(Object.fromEntries(formData));
const service = new TeamBillingService(getSupabaseServerActionClient());
// get url to billing portal
const url = await service.createBillingPortalSession(params);
return redirect(url);
}