feat: refresh stale last pickup information
Track the successful member-list check time and refresh stale pickup information asynchronously after the GUI loads without triggering dormant warnings.
This commit is contained in:
63
services/lastPickupInfo.js
Normal file
63
services/lastPickupInfo.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const LAST_PICKUP_INFO_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
function parseTimestamp(value) {
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
const timestamp = value instanceof Date ? value.getTime() : new Date(value).getTime();
|
||||
return Number.isFinite(timestamp) ? timestamp : null;
|
||||
}
|
||||
|
||||
function getStaleLastPickupStoreIds(config = [], now = Date.now()) {
|
||||
if (!Array.isArray(config)) {
|
||||
return [];
|
||||
}
|
||||
return config
|
||||
.filter((entry) => {
|
||||
if (!entry?.id || entry.hidden) {
|
||||
return false;
|
||||
}
|
||||
const checkedAt = parseTimestamp(entry.lastPickupCheckedAt);
|
||||
return !checkedAt || now - checkedAt > LAST_PICKUP_INFO_MAX_AGE_MS;
|
||||
})
|
||||
.map((entry) => String(entry.id));
|
||||
}
|
||||
|
||||
function parseLastFetchTimestamp(value) {
|
||||
if (typeof value === 'string') {
|
||||
return parseTimestamp(value);
|
||||
}
|
||||
if (!Number.isFinite(Number(value))) {
|
||||
return null;
|
||||
}
|
||||
const numericValue = Number(value);
|
||||
return numericValue > 1e12 ? numericValue : numericValue * 1000;
|
||||
}
|
||||
|
||||
function updateLastPickupInfo(entry, lastFetchValue, checkedAt = new Date()) {
|
||||
if (!entry || typeof entry !== 'object') {
|
||||
return false;
|
||||
}
|
||||
let changed = false;
|
||||
const checkedAtIso = new Date(checkedAt).toISOString();
|
||||
if (entry.lastPickupCheckedAt !== checkedAtIso) {
|
||||
entry.lastPickupCheckedAt = checkedAtIso;
|
||||
changed = true;
|
||||
}
|
||||
const lastFetchTimestamp = parseLastFetchTimestamp(lastFetchValue);
|
||||
if (lastFetchTimestamp !== null) {
|
||||
const lastPickupAt = new Date(lastFetchTimestamp).toISOString();
|
||||
if (entry.lastPickupAt !== lastPickupAt) {
|
||||
entry.lastPickupAt = lastPickupAt;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
LAST_PICKUP_INFO_MAX_AGE_MS,
|
||||
getStaleLastPickupStoreIds,
|
||||
parseLastFetchTimestamp,
|
||||
updateLastPickupInfo
|
||||
};
|
||||
Reference in New Issue
Block a user