Merge develop: auto-add to Bring! items running out <7 days

This commit is contained in:
dadaloop82
2026-04-19 06:52:19 +00:00
+6 -6
View File
@@ -6721,19 +6721,19 @@ function _isBringPurchased(name, urgency) {
} }
async function autoAddCriticalItems() { async function autoAddCriticalItems() {
// Time-based guard: run at most once every 10 minutes (not session-based, so new critical items get added promptly) // Time-based guard: run at most once every 5 minutes
const lastRun = parseInt(localStorage.getItem('_autoAddedCriticalTs') || '0'); const lastRun = parseInt(localStorage.getItem('_autoAddedCriticalTs') || '0');
if (Date.now() - lastRun < 10 * 60 * 1000) return; if (Date.now() - lastRun < 5 * 60 * 1000) return;
localStorage.setItem('_autoAddedCriticalTs', String(Date.now())); localStorage.setItem('_autoAddedCriticalTs', String(Date.now()));
// Auto-add rules: // Auto-add rules:
// - critical: always // - critical: always
// - high: when qty=0 OR pct_left<20 (almost gone) OR days_left<=3 (imminent) // - high: always (PHP already applies strict criteria for high urgency)
// - any urgency with days_left<=2 and uses_per_month>=5 (running out tomorrow for heavy user) // - medium: when running out within 7 days (<1 week) for items used ≥3x/month
const toAdd = smartShoppingItems.filter(i => { const toAdd = smartShoppingItems.filter(i => {
if (i.on_bring || _isBringPurchased(i.name, i.urgency)) return false; if (i.on_bring || _isBringPurchased(i.name, i.urgency)) return false;
if (i.urgency === 'critical') return true; if (i.urgency === 'critical') return true;
if (i.urgency === 'high' && (i.current_qty === 0 || i.pct_left < 20 || i.days_left <= 3)) return true; if (i.urgency === 'high') return true;
if (i.days_left <= 2 && (i.uses_per_month || 0) >= 5) return true; if (i.urgency === 'medium' && (i.days_left ?? 999) <= 7 && (i.uses_per_month || 0) >= 3) return true;
return false; return false;
}); });
if (toAdd.length === 0) return; if (toAdd.length === 0) return;