import { apiRequestData } from "./http"; import type { AssetStatus } from "./assets"; export type InventoryCheckScopeType = "all" | "legal_entity" | "cost_center" | "location"; export type InventoryCheckStatus = "draft" | "counting" | "completed"; export type InventoryCheckResult = "matched" | "missing" | "extra" | "damaged"; export type InventoryCheckItem = { id: string; assetId: string; result: InventoryCheckResult; note: string | null; asset: { id: string; assetNo: string; name: string; status: AssetStatus; legalEntityId: string; costCenterId: string; location: string | null; assigneeEmployeeId: string | null; } | null; }; export type InventoryCheck = { id: string; checkNo: string; scopeType: InventoryCheckScopeType; scopeRefId: string | null; status: InventoryCheckStatus; startedAt: string | null; completedAt: string | null; createdAt: string | null; updatedAt: string | null; items: InventoryCheckItem[]; }; function toQueryString(query: Record) { const params = new URLSearchParams(); for (const [key, value] of Object.entries(query)) { if (!value) { continue; } params.set(key, value); } const encoded = params.toString(); return encoded ? `?${encoded}` : ""; } export async function fetchInventoryChecks(params: { status?: InventoryCheckStatus; scopeType?: InventoryCheckScopeType } = {}) { return apiRequestData<{ items: InventoryCheck[] }>( `/api/inventory-checks${toQueryString(params as Record)}`, ); } export async function createInventoryCheck(payload: { scopeType: InventoryCheckScopeType; scopeRefId?: string | null }) { return apiRequestData<{ inventoryCheck: InventoryCheck }>("/api/inventory-checks", { method: "POST", body: JSON.stringify(payload), }); } export async function startInventoryCheck(inventoryCheckId: string) { return apiRequestData<{ inventoryCheck: InventoryCheck }>(`/api/inventory-checks/${inventoryCheckId}/start`, { method: "POST", }); } export async function recordInventoryItems( inventoryCheckId: string, payload: { items: Array<{ assetId: string; result: InventoryCheckResult; note?: string | null }> }, ) { return apiRequestData<{ inventoryCheck: InventoryCheck }>(`/api/inventory-checks/${inventoryCheckId}/items`, { method: "POST", body: JSON.stringify(payload), }); } export async function completeInventoryCheck(inventoryCheckId: string) { return apiRequestData<{ inventoryCheck: InventoryCheck }>(`/api/inventory-checks/${inventoryCheckId}/complete`, { method: "POST", }); }