67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
const { readStoreStatus, writeStoreStatus } = require('./storeStatusStore');
|
|
|
|
const storeStatusCache = new Map();
|
|
|
|
function normalizeStatusEntry(entry = {}) {
|
|
const status = Number(entry.teamSearchStatus);
|
|
const fetchedAt = Number(entry.fetchedAt) || 0;
|
|
return {
|
|
teamSearchStatus: Number.isFinite(status) ? status : null,
|
|
fetchedAt
|
|
};
|
|
}
|
|
|
|
(function bootstrapStoreStatusCache() {
|
|
try {
|
|
const cached = readStoreStatus();
|
|
Object.entries(cached || {}).forEach(([storeId, entry]) => {
|
|
if (!storeId) {
|
|
return;
|
|
}
|
|
storeStatusCache.set(String(storeId), normalizeStatusEntry(entry));
|
|
});
|
|
} catch (error) {
|
|
console.error('[STORE-STATUS] Bootstrap fehlgeschlagen:', error.message);
|
|
}
|
|
})();
|
|
|
|
function getStoreStatus(storeId) {
|
|
if (!storeId) {
|
|
return null;
|
|
}
|
|
return storeStatusCache.get(String(storeId)) || null;
|
|
}
|
|
|
|
function setStoreStatus(storeId, data = {}) {
|
|
if (!storeId) {
|
|
return null;
|
|
}
|
|
const normalized = normalizeStatusEntry(data);
|
|
storeStatusCache.set(String(storeId), normalized);
|
|
return normalized;
|
|
}
|
|
|
|
function bulkSetStoreStatus(entries = []) {
|
|
entries.forEach((entry) => {
|
|
if (!entry || !entry.storeId) {
|
|
return;
|
|
}
|
|
setStoreStatus(entry.storeId, entry);
|
|
});
|
|
}
|
|
|
|
function persistStoreStatusCache() {
|
|
const payload = {};
|
|
storeStatusCache.forEach((value, key) => {
|
|
payload[key] = value;
|
|
});
|
|
writeStoreStatus(payload);
|
|
}
|
|
|
|
module.exports = {
|
|
getStoreStatus,
|
|
setStoreStatus,
|
|
bulkSetStoreStatus,
|
|
persistStoreStatusCache
|
|
};
|