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

@@ -3,33 +3,29 @@
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { requireUser } from '@kit/supabase/require-user';
import { enhanceAction } from '@kit/next/actions';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { LeaveTeamAccountSchema } from '../../schema/leave-team-account.schema';
import { createLeaveTeamAccountService } from '../services/leave-team-account.service';
export async function leaveTeamAccountAction(formData: FormData) {
const body = Object.fromEntries(formData.entries());
const params = LeaveTeamAccountSchema.parse(body);
export const leaveTeamAccountAction = enhanceAction(
async (formData: FormData, user) => {
const body = Object.fromEntries(formData.entries());
const params = LeaveTeamAccountSchema.parse(body);
const client = getSupabaseServerActionClient();
const auth = await requireUser(client);
const service = createLeaveTeamAccountService(
getSupabaseServerActionClient({ admin: true }),
);
if (auth.error) {
throw new Error('Authentication required');
}
await service.leaveTeamAccount({
accountId: params.accountId,
userId: user.id,
});
const service = createLeaveTeamAccountService(
getSupabaseServerActionClient({ admin: true }),
);
revalidatePath('/home/[account]', 'layout');
await service.leaveTeamAccount({
accountId: params.accountId,
userId: auth.data.id,
});
revalidatePath('/home/[account]', 'layout');
return redirect('/home');
}
return redirect('/home');
},
{},
);