Files

82 lines
3.7 KiB
TypeScript

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";
/** Super admin's company choice; omitted for All companies. */
companyId?: number;
}
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<string, unknown>;
const users = Array.isArray(obj.content) ? (obj.content as Array<Omit<AdminUser, "roles"> & { roles?: Array<AdminRole | number> }>) : [];
// 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<number, AdminRole>();
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<AdminUserListResult> {
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);
if (params.companyId) qs.set("companyId", String(params.companyId));
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<unknown>(`/api/users?${qs.toString()}`);
return normalizeAdminUserPage(raw);
}
export async function getAdminUser(id: number): Promise<AdminUser> {
return apiGet<AdminUser>(`/api/users/${id}`);
}
export async function saveAdminUser(payload: AdminUser): Promise<AdminUser> {
return payload.id
? apiPut<AdminUser>(`/api/users/${payload.id}`, payload)
: apiPost<AdminUser>("/api/users", payload);
}
/** The signed-in admin changes their own password. */
export async function changeOwnPassword(currentPassword: string, newPassword: string, confirmPassword: string): Promise<void> {
await apiPost<void>("/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<void> {
await apiPost<void>(`/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);
}