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,63 +4,61 @@ import { redirect } from 'next/navigation';
import { SupabaseClient } from '@supabase/supabase-js';
import { enhanceAction } from '@kit/next/actions';
import { Database } from '@kit/supabase/database';
import { requireUser } from '@kit/supabase/require-user';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { DeleteTeamAccountSchema } from '../../schema/delete-team-account.schema';
import { createDeleteTeamAccountService } from '../services/delete-team-account.service';
export async function deleteTeamAccountAction(formData: FormData) {
const params = DeleteTeamAccountSchema.parse(
Object.fromEntries(formData.entries()),
);
export const deleteTeamAccountAction = enhanceAction(
async (formData: FormData, user) => {
const params = DeleteTeamAccountSchema.parse(
Object.fromEntries(formData.entries()),
);
const client = getSupabaseServerActionClient();
const auth = await requireUser(client);
const client = getSupabaseServerActionClient();
const userId = user.id;
const accountId = params.accountId;
if (auth.error) {
throw new Error('Authentication required');
}
// Check if the user has the necessary permissions to delete the team account
await assertUserPermissionsToDeleteTeamAccount(client, {
accountId,
userId,
});
// Check if the user has the necessary permissions to delete the team account
await assertUserPermissionsToDeleteTeamAccount(client, params.accountId);
// Get the Supabase client and create a new service instance.
const service = createDeleteTeamAccountService();
// Get the Supabase client and create a new service instance.
const service = createDeleteTeamAccountService();
// Delete the team account and all associated data.
await service.deleteTeamAccount(
getSupabaseServerActionClient({
admin: true,
}),
{
accountId,
userId,
},
);
// Delete the team account and all associated data.
await service.deleteTeamAccount(
getSupabaseServerActionClient({
admin: true,
}),
{
accountId: params.accountId,
userId: auth.data.id,
},
);
return redirect('/home');
}
return redirect('/home');
},
{},
);
async function assertUserPermissionsToDeleteTeamAccount(
client: SupabaseClient<Database>,
accountId: string,
params: {
accountId: string;
userId: string;
},
) {
const auth = await requireUser(client);
if (auth.error ?? !auth.data.id) {
throw new Error('Authentication required');
}
const userId = auth.data.id;
const { data, error } = await client
.from('accounts')
.select('id')
.eq('primary_owner_user_id', userId)
.eq('primary_owner_user_id', params.userId)
.eq('is_personal_account', false)
.eq('id', accountId);
.eq('id', params.accountId);
if (error ?? !data) {
throw new Error('Account not found');