Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
73 lines
3.2 KiB
TypeScript
73 lines
3.2 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";
|
|
}
|
|
|
|
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);
|
|
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);
|
|
}
|
|
|
|
export async function changeAdminUserPassword(id: number, password: string): Promise<{ updated: number }> {
|
|
return apiPost<{ updated: number }>(`/api/users/${id}/change-password`, { password });
|
|
}
|
|
|
|
export async function sendAdminUserPasswordReset(id: number): Promise<{ updated: number }> {
|
|
return apiPost<{ updated: number }>(`/api/users/${id}/reset-password`, undefined);
|
|
}
|