26 lines
1.5 KiB
TypeScript
26 lines
1.5 KiB
TypeScript
export type DocumentKind = "delivery" | "purchase-order";
|
|
export const DOCUMENT_LABELS: Record<DocumentKind, string> = {
|
|
delivery: "Delivery note", "purchase-order": "Purchase order",
|
|
};
|
|
export const DOCUMENT_ACCEPT = "application/pdf,image/jpeg,image/png,image/webp";
|
|
|
|
export function fitScannerFrame(width: number, height: number, shape: "qr" | "document") {
|
|
const ratio = shape === "qr" ? 1 : 0.75;
|
|
const frameWidth = Math.max(0, Math.min(width - 16, (height - 16) * ratio, shape === "qr" ? 320 : 360));
|
|
return { width: frameWidth, height: frameWidth / ratio };
|
|
}
|
|
|
|
export async function uploadClockDocument(kind: DocumentKind, site: string, file: Blob, name: string): Promise<{ duplicate: boolean }> {
|
|
if (!site.trim()) throw new Error("Choose a site before sending");
|
|
if (!file.size) throw new Error("The selected file is empty");
|
|
if (file.size > 20 * 1024 * 1024) throw new Error("Choose a file smaller than 20 MB");
|
|
if (!DOCUMENT_ACCEPT.split(",").includes(file.type)) throw new Error("Choose a PDF, JPG, PNG, or WebP file");
|
|
const form = new FormData();
|
|
form.append("site", site.trim());
|
|
form.append("file", file, name);
|
|
const endpoint = kind === "delivery" ? "/api/delivery-notes/upload" : "/api/purchase-orders/upload";
|
|
const response = await fetch(endpoint, { method: "POST", body: form });
|
|
const body = await response.json().catch(() => ({}));
|
|
if (!response.ok) throw new Error(body.error ?? `Upload failed (${response.status})`);
|
|
return { duplicate: body.duplicate === true };
|
|
}
|