import { apiGet, apiPost, apiPut } from "@/app/lib/clientApi"; import type { AdminRole, AdminUser } from "@/app/lib/types"; export const USER_STATUS_FILTER_OPTIONS = [{ value: "true", label: "Enabled" }, { value: "false", label: "Disabled" }]; export interface AdminUserListParams { searchKey?: string; /** Combined username/name/email search; `searchKey` is kept for the notifications picker. */ q?: string; /** "true" | "false"; empty = both. */ enabled?: string; /** 1-based, matching this portal's other list pages. */ page: number; pageSize: number; /** Server-side sort: attribute name + direction; sent only when present. */ sortBy?: string; sortDir?: "asc" | "desc"; } export interface AdminUserListResult { items: AdminUser[]; total: number; } export function normalizeAdminUserPage(raw: unknown): AdminUserListResult { if (!raw || typeof raw !== "object") return { items: [], total: 0 }; const obj = raw as Record; const users = Array.isArray(obj.content) ? (obj.content as Array & { roles?: Array }>) : []; // ponytail: the backend User.roles still carries @JsonIdentityInfo, so a role seen earlier in the // response arrives as its bare id. Resolve against the first full object; drop unresolvable ids. // Real fix: remove the annotation from the backend (as done for Module, PR #535/#539). const seen = new Map(); users.forEach((u) => u.roles?.forEach((r) => typeof r === "object" && r?.id != null && !seen.has(r.id) && seen.set(r.id, r))); const items = users.map((u) => Array.isArray(u.roles) ? { ...u, roles: u.roles.flatMap((r) => { const role = typeof r === "number" ? seen.get(r) : r; return role ? [role] : []; }) } : u, ) as AdminUser[]; const total = typeof obj.totalElements === "number" ? obj.totalElements : items.length; return { items, total }; } export async function listAdminUsers(params: AdminUserListParams): Promise { const qs = new URLSearchParams(); if (params.searchKey) qs.set("searchKey", params.searchKey); if (params.q) qs.set("q", params.q); if (params.enabled) qs.set("enabled", params.enabled); qs.set("page", String(Math.max(0, params.page - 1))); qs.set("size", String(params.pageSize)); if (params.sortBy) qs.set("sortBy", params.sortBy); if (params.sortDir && params.sortBy) qs.set("sortDir", params.sortDir); const raw = await apiGet(`/api/users?${qs.toString()}`); return normalizeAdminUserPage(raw); } export async function getAdminUser(id: number): Promise { return apiGet(`/api/users/${id}`); } export async function saveAdminUser(payload: AdminUser): Promise { return payload.id ? apiPut(`/api/users/${payload.id}`, payload) : apiPost("/api/users", payload); } /** The signed-in admin changes their own password. */ export async function changeOwnPassword(currentPassword: string, newPassword: string, confirmPassword: string): Promise { await apiPost("/api/auth/change-password", { currentPassword, newPassword, confirmPassword }); } /** Admin-typed temporary password; the user must change it at next sign-in. */ export async function setAdminUserPassword(id: number, password: string, confirmPassword: string): Promise { await apiPost(`/api/users/${id}/password`, { password, confirmPassword }); } export async function sendAdminUserPasswordReset(id: number): Promise<{ updated: number }> { return apiPost<{ updated: number }>(`/api/users/${id}/reset-password`, undefined); }