Files
CPLv4.0/services/fetchMessagesService.ts
2025-08-12 12:18:06 +02:00

34 lines
1.1 KiB
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 isLocalHost =
typeof window !== "undefined" &&
["localhost", "127.0.0.1", "::1"].includes(window.location.hostname);
const forceMocks =
typeof process !== "undefined" &&
process.env.NEXT_PUBLIC_USE_MOCKS === "1";
const useMocks = forceMocks || isLocalHost;
const url = useMocks
? `/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;
};