import { LOARequest, LOAType } from '@shared/types/loa' import { PagedData } from '@shared/types/pagination' // @ts-ignore const addr = import.meta.env.VITE_APIHOST; export async function submitLOA(request: LOARequest): Promise<{ id?: number; error?: string }> { const res = await fetch(`${addr}/loa`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(request), credentials: 'include', }); if (res.ok) { return; } else { throw new Error("Failed to submit LOA"); } } export async function adminSubmitLOA(request: LOARequest): Promise<{ id?: number; error?: string }> { const res = await fetch(`${addr}/loa/admin`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(request), credentials: 'include', }); if (res.ok) { return } else { throw new Error("Failed to submit LOA"); } } export async function getMyLOA(): Promise { const res = await fetch(`${addr}/loa/me`, { method: "GET", headers: { "Content-Type": "application/json", }, credentials: 'include', }); if (res.ok) { const out = res.json(); if (!out) { return null; } return out; } else { return null; } } export async function getAllLOAs(page?: number, pageSize?: number): Promise> { const params = new URLSearchParams(); if (page !== undefined) { params.set("page", page.toString()); } if (pageSize !== undefined) { params.set("pageSize", pageSize.toString()); } return fetch(`${addr}/loa/all?${params}`, { method: "GET", headers: { "Content-Type": "application/json", }, credentials: 'include', }).then((res) => { if (res.ok) { return res.json(); } else { return []; } }); } export function getMyLOAs(page?: number, pageSize?: number): Promise> { const params = new URLSearchParams(); if (page !== undefined) { params.set("page", page.toString()); } if (pageSize !== undefined) { params.set("pageSize", pageSize.toString()); } return fetch(`${addr}/loa/history?${params}`, { method: "GET", credentials: 'include', headers: { "Content-Type": "application/json", }, }).then((res) => { if (res.ok) { return res.json(); } else { return []; } }); } export async function getLoaTypes(): Promise { const res = await fetch(`${addr}/loa/types`, { method: "GET", credentials: 'include', }); if (res.ok) { const out = res.json(); if (!out) { return null; } return out; } else { return null; } }; export async function getLoaPolicy(): Promise { const res = await fetch(`${addr}/loa/policy`, { method: "GET", credentials: 'include', }); if (res.ok) { const out = res.json(); if (!out) { return null; } return out; } else { return null; } } export async function cancelLOA(id: number, admin: boolean = false) { let route = admin ? 'adminCancel' : 'cancel'; const res = await fetch(`${addr}/loa/${route}/${id}`, { method: "POST", credentials: 'include', }); if (res.ok) { return } else { throw new Error("Could not cancel LOA"); } } export async function extendLOA(id: number, to: Date) { const res = await fetch(`${addr}/loa/extend/${id}`, { method: "POST", credentials: 'include', body: JSON.stringify({ to }), headers: { "Content-Type": "application/json", } }); if (res.ok) { return } else { throw new Error("Could not extend LOA"); } }