import { apiRequestData } from "./http"; export type InvoicePoolStatus = "pending_ocr" | "ocr_failed" | "available" | "bound" | "archived"; export type InvoicePoolListItem = { id: string; status: InvoicePoolStatus; ocrType: string; ocrProvider: string; ocrAttempts: number; ocrLastError: string | null; ocrCompletedAt: string | null; invoiceCode: string | null; invoiceNumber: string | null; invoiceDate: string | null; amount: string | null; taxAmount: string | null; totalAmount: string | null; linkedReimbursementId: string | null; linkedItemId: string | null; originalName: string; mimeType: string; fileSize: number; owner: { id: string; employeeNo: string; name: string; } | null; uploadedBy: { id: string; employeeNo: string; name: string; } | null; createdAt: string | null; updatedAt: string | null; }; export type InvoicePoolListResponse = { page: number; pageSize: number; total: number; ownerScope: "own" | "all"; items: InvoicePoolListItem[]; }; export type InvoicePoolDetail = { id: string; status: InvoicePoolStatus; ownerEmployeeId: string; uploadedByEmployeeId: string; ocrType: string; ocrProvider: string; ocrAttempts: number; ocrLastError: string | null; ocrCompletedAt: string | null; invoiceCode: string | null; invoiceNumber: string | null; invoiceDate: string | null; amount: string | null; taxAmount: string | null; totalAmount: string | null; linkedReimbursementId: string | null; linkedItemId: string | null; createdAt: string | null; updatedAt: string | null; }; export type InvoicePoolListQuery = { page?: number; pageSize?: number; status?: InvoicePoolStatus; ownerScope?: "own" | "all"; }; function toQueryString(query: Record) { const params = new URLSearchParams(); for (const [key, value] of Object.entries(query)) { if (value == null || value === "") { continue; } params.set(key, String(value)); } const encoded = params.toString(); return encoded ? `?${encoded}` : ""; } export async function fetchInvoicePoolList(query: InvoicePoolListQuery = {}) { return apiRequestData( `/api/invoice-pool${toQueryString(query as Record)}`, ); } export async function fetchInvoicePoolDetail(invoiceId: string) { return apiRequestData(`/api/invoice-pool/${invoiceId}`); } export async function uploadInvoice(file: File, ocrType?: string, ownerEmployeeId?: string) { const formData = new FormData(); formData.append("file", file); if (ocrType) { formData.append("ocr_type", ocrType); } if (ownerEmployeeId) { formData.append("owner_employee_id", ownerEmployeeId); } return apiRequestData<{ invoicePoolId: string; status: string; fileSha256: string }>( "/api/invoice-pool/upload", { method: "POST", body: formData, }, ); } export async function bindInvoice(invoiceId: string, reimbursementId: string, linkedItemId?: string) { return apiRequestData>(`/api/invoice-pool/${invoiceId}/bind`, { method: "POST", body: JSON.stringify({ reimbursementId, linkedItemId }), }); } export async function fetchInvoiceSignedUrl(invoiceId: string) { return apiRequestData<{ signedUrl: string; expiresAt: string }>( `/api/invoice-pool/${invoiceId}/signed-url`, { method: "POST", body: JSON.stringify({}), }, ); }