89ce523019
- badge « Archivé » et libellé barré dans la liste admin - popup de confirmation avant archivage (rappelle que c'est réversible) - bouton de restauration (PATCH archived:false) pour les archivés - case « Afficher les utilisateurs archivés » (filtre ?archived=true) - masque l'action d'archivage sur son propre compte (évite le 403) - service users : getArchived/restore, toast remove -> users.archived - i18n FR : clés archived/restored/badge/confirmation
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import type { UserData, UserWrite } from './dto/user-data'
|
|
import type { HydraCollection } from '~/utils/api'
|
|
import { extractHydraMembers } from '~/utils/api'
|
|
|
|
export function useUserService() {
|
|
const api = useApi()
|
|
|
|
async function getAll(): Promise<UserData[]> {
|
|
const data = await api.get<HydraCollection<UserData>>('/users')
|
|
return extractHydraMembers(data)
|
|
}
|
|
|
|
// Archived users are hidden from the default collection; an admin lists
|
|
// them explicitly via the `archived` filter (handled server-side).
|
|
async function getArchived(): Promise<UserData[]> {
|
|
const data = await api.get<HydraCollection<UserData>>('/users?archived=true')
|
|
return extractHydraMembers(data)
|
|
}
|
|
|
|
async function getById(id: number): Promise<UserData> {
|
|
return api.get<UserData>(`/users/${id}`)
|
|
}
|
|
|
|
async function create(payload: UserWrite): Promise<UserData> {
|
|
return api.post<UserData>('/users', payload as Record<string, unknown>, {
|
|
toastSuccessKey: 'users.created',
|
|
})
|
|
}
|
|
|
|
async function update(id: number, payload: Partial<UserWrite>): Promise<UserData> {
|
|
return api.patch<UserData>(`/users/${id}`, payload as Record<string, unknown>, {
|
|
toastSuccessKey: 'users.updated',
|
|
})
|
|
}
|
|
|
|
// Deleting a user is a soft delete server-side: the account is archived
|
|
// (kept for referential integrity) rather than removed.
|
|
async function remove(id: number): Promise<void> {
|
|
await api.delete(`/users/${id}`, {}, {
|
|
toastSuccessKey: 'users.archived',
|
|
})
|
|
}
|
|
|
|
async function restore(id: number): Promise<UserData> {
|
|
return api.patch<UserData>(`/users/${id}`, { archived: false }, {
|
|
toastSuccessKey: 'users.restored',
|
|
})
|
|
}
|
|
|
|
return { getAll, getArchived, getById, create, update, remove, restore }
|
|
}
|