文件
hyb-oa/client/src/mobile/inventoryScanQueue.ts
T

82 行
2.2 KiB
TypeScript

import type { InventoryCheckResult } from "../api/inventoryChecks";
import { createIndexedDbStore, requestPromise } from "./indexedDbStore";
export type QueuedInventoryScanItem = {
id: string;
inventoryCheckId: string;
assetId: string;
assetNo: string;
result: InventoryCheckResult;
note: string | null;
createdAt: string;
lastError: string | null;
};
const STORE_NAME = "inventory_scan_queue";
const database = createIndexedDbStore({
dbName: "hyb-oa-mobile-inventory",
dbVersion: 1,
upgrade(db) {
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME, { keyPath: "id" });
}
},
});
export async function enqueueInventoryScan(input: {
inventoryCheckId: string;
assetId: string;
assetNo: string;
result: InventoryCheckResult;
note?: string | null;
}) {
const id = `${input.inventoryCheckId}:${input.assetId}`;
const item: QueuedInventoryScanItem = {
id,
inventoryCheckId: input.inventoryCheckId,
assetId: input.assetId,
assetNo: input.assetNo,
result: input.result,
note: input.note?.trim() ? input.note.trim() : null,
createdAt: new Date().toISOString(),
lastError: null,
};
await database.withStore(STORE_NAME, "readwrite", async (store) => {
await requestPromise(store.put(item));
return true;
});
return item;
}
export async function listQueuedInventoryScans() {
return database.withStore(STORE_NAME, "readonly", async (store) => {
const records = (await requestPromise(store.getAll())) as QueuedInventoryScanItem[];
return records.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
});
}
export async function removeQueuedInventoryScan(id: string) {
await database.withStore(STORE_NAME, "readwrite", async (store) => {
await requestPromise(store.delete(id));
return true;
});
}
export async function updateQueuedInventoryScanError(id: string, lastError: string | null) {
await database.withStore(STORE_NAME, "readwrite", async (store) => {
const existing = (await requestPromise(store.get(id))) as QueuedInventoryScanItem | undefined;
if (!existing) {
return false;
}
await requestPromise(
store.put({
...existing,
lastError,
}),
);
return true;
});
}