Files
co-lite/solvway-admin-portal/app/lib/services/adminEmails.ts
T

50 lines
1.8 KiB
TypeScript

import { apiGet } from "@/app/lib/clientApi";
/** One saved email in the list; the body is never part of it. */
export interface EmailRow {
id: number;
/** Server-local "yyyy-MM-ddTHH:mm:ss". */
datetime: string;
recipients: string[];
subject: string;
/** P pending, N failed, Y success. */
status: string;
}
export interface EmailDetail extends Omit<EmailRow, "recipients"> {
sender: string;
recipients: { recipient: string; type: string | null }[];
body: string;
}
export interface EmailListParams {
recipient?: string;
subject?: string;
/** "yyyy-MM-dd". */
from?: string;
to?: string;
/** 1-based. */
page: number;
pageSize: number;
}
export async function listEmails(params: EmailListParams): Promise<{ items: EmailRow[]; total: number }> {
const qs = new URLSearchParams();
for (const key of ["recipient", "subject", "from", "to"] as const) if (params[key]) qs.set(key, params[key]);
qs.set("page", String(Math.max(0, params.page - 1)));
qs.set("size", String(params.pageSize));
const raw = await apiGet<{ content?: EmailRow[]; totalElements?: number } | null>(`/api/emails?${qs}`);
const items = Array.isArray(raw?.content) ? raw.content : [];
return { items, total: typeof raw?.totalElements === "number" ? raw.totalElements : items.length };
}
export const getEmail = (id: number) => apiGet<EmailDetail>(`/api/emails/${id}`);
/** "2026-11-30T11:44:26" → "30/11/2026 11:44" (server-local time, shown as is). */
export function formatEmailDate(datetime: string): string {
const m = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(datetime ?? "");
return m ? `${m[3]}/${m[2]}/${m[1]} ${m[4]}:${m[5]}` : (datetime ?? "");
}
export const EMAIL_STATUS_KEYS: Record<string, string> = { P: "EmlStatusPending", N: "EmlStatusFailed", Y: "EmlStatusSuccess" };