Refactor account server actions using the enhanced action helper

The enhanced action helper has been utilized to refactor account-related server actions across the codebase. This change aims to streamline the server-side handling of user accounts, team accounts, and related functionality. As a result, various account-related server actions have now been wrapped with the helper, providing uniformity and consistency in action handling.
This commit is contained in:
giancarlo
2024-04-27 18:31:11 +07:00
parent ec59d02fb0
commit 0616d3b288
14 changed files with 388 additions and 409 deletions

View File

@@ -4,7 +4,7 @@ import { redirect } from 'next/navigation';
import { z } from 'zod';
import { requireUser } from '@kit/supabase/require-user';
import { enhanceAction } from '@kit/next/actions';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { CreateTeamSchema } from '../../schema/create-team.schema';
@@ -17,31 +17,25 @@ const TEAM_ACCOUNTS_HOME_PATH = z
.min(1)
.parse(process.env.TEAM_ACCOUNTS_HOME_PATH);
export async function createOrganizationAccountAction(
params: z.infer<typeof CreateTeamSchema>,
) {
const { name: accountName } = CreateTeamSchema.parse(params);
export const createOrganizationAccountAction = enhanceAction(
async (params, user) => {
const client = getSupabaseServerActionClient();
const service = createCreateTeamAccountService(client);
const client = getSupabaseServerActionClient();
const auth = await requireUser(client);
const { data, error } = await service.createNewOrganizationAccount({
name: params.name,
userId: user.id,
});
if (auth.error) {
redirect(auth.redirectTo);
}
if (error) {
throw new Error('Error creating team account');
}
const userId = auth.data.id;
const service = createCreateTeamAccountService(client);
const accountHomePath = TEAM_ACCOUNTS_HOME_PATH + '/' + data.slug;
const { data, error } = await service.createNewOrganizationAccount({
name: accountName,
userId,
});
if (error) {
throw new Error('Error creating team account');
}
const accountHomePath = TEAM_ACCOUNTS_HOME_PATH + '/' + data.slug;
redirect(accountHomePath);
}
redirect(accountHomePath);
},
{
schema: CreateTeamSchema,
},
);