Files
warren/frontend/lib/api/admin/editUser.ts
2025-07-21 09:37:53 +02:00

47 lines
1.2 KiB
TypeScript

import { toast } from 'vue-sonner';
import type { ApiResponse } from '#shared/types/api';
import type { AuthUser } from '#shared/types/auth';
import { getApiHeaders } from '..';
/** Admin function to edit an existing user */
export async function editUser(
user: AuthUser & { password: string | null }
): Promise<{ success: true; user: AuthUser } | { success: false }> {
const { data, error } = await useFetch<ApiResponse<AuthUser>>(
getApiUrl('admin/users'),
{
method: 'PATCH',
headers: getApiHeaders(),
body: JSON.stringify({
id: user.id,
name: user.name,
email: user.email,
password: user.password,
admin: user.admin,
}),
responseType: 'json',
}
);
if (data.value == null) {
toast.error('Edit user', {
description: error.value?.data ?? 'Failed to edit user',
});
return {
success: false,
};
}
await refreshNuxtData('admin-resources');
toast.success('Edit user', {
description: 'Successfully edited user',
});
return {
success: true,
user: data.value.data,
};
}