- getMessagesAPIHandler.ts in messages.ts umbenannt - API ist nun unter /api/cpl/messages erreichbar - Dateiname entspricht Next.js- und REST-Konventionen
28 lines
948 B
TypeScript
28 lines
948 B
TypeScript
// services/fetchMessagesService.ts
|
|
export const fetchMessagesService = async (
|
|
fromDate: string,
|
|
toDate: string
|
|
) => {
|
|
const from = new Date(fromDate);
|
|
const to = new Date(toDate);
|
|
|
|
const fy = from.getFullYear();
|
|
const fm = String(from.getMonth() + 1).padStart(2, "0");
|
|
const fd = String(from.getDate()).padStart(2, "0");
|
|
const ty = to.getFullYear();
|
|
const tm = String(to.getMonth() + 1).padStart(2, "0");
|
|
const td = String(to.getDate()).padStart(2, "0");
|
|
|
|
const isDev =
|
|
typeof window !== "undefined" && window.location.hostname === "localhost";
|
|
const url = isDev
|
|
? `/api/cpl/messages?fromDate=${fromDate}&toDate=${toDate}`
|
|
: `/CPL?Service/ae.ACP&MSS1=${fy};${fm};${fd};${ty};${tm};${td};All`;
|
|
|
|
const response = await fetch(url);
|
|
const raw = await response.json();
|
|
const data = Array.isArray(raw) ? raw : raw.data;
|
|
if (!response.ok) throw new Error("Fehler beim Laden der Meldungen");
|
|
return data;
|
|
};
|