(function (I) { const DB_NAME = 'DuimstokInspecties'; const DB_VERSION = 3; let db = null; I.openDB = function () { return new Promise((resolve, reject) => { const req = indexedDB.open(DB_NAME, DB_VERSION); req.onupgradeneeded = (e) => { const d = e.target.result; if (!d.objectStoreNames.contains('inspections')) d.createObjectStore('inspections', { keyPath: 'orderNr' }); if (!d.objectStoreNames.contains('orderStatuses')) d.createObjectStore('orderStatuses', { keyPath: 'orderKey' }); }; req.onsuccess = (e) => { db = e.target.result; resolve(db); }; req.onerror = (e) => reject(e); }); }; async function ensureDb() { if (!db) await I.openDB(); return db; } I.saveInspection = async function (formData) { const d = await ensureDb(); return new Promise((resolve, reject) => { const tx = d.transaction('inspections', 'readwrite'); tx.objectStore('inspections').put(JSON.parse(JSON.stringify(formData))); tx.oncomplete = () => resolve(); tx.onerror = reject; }); }; I.loadInspection = async function (orderNr) { const d = await ensureDb(); return new Promise((resolve, reject) => { const tx = d.transaction('inspections', 'readonly'); const req = tx.objectStore('inspections').get(orderNr); req.onsuccess = () => resolve(req.result); req.onerror = reject; }); }; I.saveOrderStatus = async function (orderKey, status) { const d = await ensureDb(); return new Promise((resolve, reject) => { const tx = d.transaction('orderStatuses', 'readwrite'); tx.objectStore('orderStatuses').put({ orderKey, status, updatedAt: new Date().toISOString() }); tx.oncomplete = resolve; tx.onerror = reject; }); }; I.loadAllOrderStatuses = async function () { const d = await ensureDb(); return new Promise((resolve, reject) => { const tx = d.transaction('orderStatuses', 'readonly'); const req = tx.objectStore('orderStatuses').getAll(); req.onsuccess = () => { const map = {}; (req.result || []).forEach(r => { map[r.orderKey] = r.status; }); resolve(map); }; req.onerror = reject; }); }; })(window.App.Infrastructure);