Files
myeasycms-v2/apps/web/app/(dashboard)/home/(user)/billing/server-actions.ts
giancarlo fa205ace32 Add UserBillingService for handling checkout and billing portal sessions
This update refactors the handling of checkout and billing portal sessions into a new UserBillingService. The new service centralizes related functions that were previously scattered throughout server-actions and ensures that all related functionality is handled in a single place. This improves maintainability and coherence of the code related to billing sessions.
2024-04-06 13:59:38 +08:00

34 lines
1.0 KiB
TypeScript

'use server';
import { redirect } from 'next/navigation';
import { z } from 'zod';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { PersonalAccountCheckoutSchema } from './_lib/schema/personal-account-checkout.schema';
import { UserBillingService } from './_lib/server/user-billing.service';
/**
* @description Creates a checkout session for a personal account.
*/
export async function createPersonalAccountCheckoutSession(
params: z.infer<typeof PersonalAccountCheckoutSchema>,
) {
// parse the parameters
const data = PersonalAccountCheckoutSchema.parse(params);
const service = new UserBillingService(getSupabaseServerActionClient());
return await service.createCheckoutSession(data);
}
/**
* @description Creates a billing Portal session for a personal account
*/
export async function createPersonalAccountBillingPortalSession() {
const service = new UserBillingService(getSupabaseServerActionClient());
const url = await service.createBillingPortalSession();
return redirect(url);
}