Files
CPLv4.0/services/fetchMessagesService.ts

27 lines
841 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/getMessagesAPIHandler`
: `/CPL?Service/ae.ACP&MSS1=${fy};${fm};${fd};${ty};${tm};${td};All`;
const res = await fetch(url);
const raw = await res.json();
const data = Array.isArray(raw) ? raw : raw.data;
return data;
};