From 4b3e5f2ce44bee1e15a542a74a93f83cf1add63b Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Sun, 29 Mar 2026 19:54:05 +0000 Subject: [PATCH] Cleanup obsolete Bring! items after smart shopping algorithm fix - cleanupObsoleteBringItems(): one-time per session, removes items from Bring! that the updated smart shopping algorithm no longer considers relevant - Cross-references shoppingItems vs smartShoppingItems using exact + token match - Shows toast with count of removed items - Called alongside autoAddCriticalItems after loading smart shopping data --- assets/js/app.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++ data/dispensa.db | Bin 266240 -> 266240 bytes index.html | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/assets/js/app.js b/assets/js/app.js index 1fb7514..b84b127 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -4332,6 +4332,54 @@ async function autoAddCriticalItems() { } catch (e) { /* ignore */ } } +/** + * One-time cleanup: remove items from Bring! that the new smart algorithm no longer considers relevant. + * Compares current shoppingItems vs smartShoppingItems — if a Bring! item has NO match in + * smart predictions (or matches a product with urgency 'none' that was filtered out), remove it. + */ +async function cleanupObsoleteBringItems() { + if (sessionStorage.getItem('_bringCleanupDone')) return; + sessionStorage.setItem('_bringCleanupDone', '1'); + if (!shoppingItems.length || !smartShoppingItems.length) return; + + // Build a set of names that the smart algorithm still considers relevant + const smartNames = new Set(smartShoppingItems.map(i => i.name.toLowerCase())); + + // Find Bring! items that have NO match in smart predictions AND are not manually added + // (We consider items "auto-added" if their name matches a known product in our DB) + const toRemove = []; + for (const item of shoppingItems) { + const nameLower = item.name.toLowerCase(); + // If smart shopping still flags this item, keep it + if (smartNames.has(nameLower)) continue; + // Check with token matching too + const smartMatch = _findSimilarItem(item.name, smartShoppingItems); + if (smartMatch) continue; + // This item is no longer predicted by smart shopping — candidate for removal + toRemove.push(item); + } + + if (toRemove.length === 0) return; + + // Remove them from Bring! + let removed = 0; + for (const item of toRemove) { + try { + const r = await api('bring_remove', {}, 'POST', { + name: item.name, + rawName: item.rawName || '', + listUUID: shoppingListUUID + }); + if (r.success) removed++; + } catch (e) { /* ignore individual failures */ } + } + + if (removed > 0) { + showToast(`🧹 ${removed} prodott${removed === 1 ? 'o non più necessario rimosso' : 'i non più necessari rimossi'} dalla lista`, 'info'); + loadShoppingList(); + } +} + const DEFAULT_SPESA_AI_PROMPT = `Sei un assistente per la spesa online. Ti viene dato il nome di un prodotto che l'utente vuole comprare e una lista di prodotti trovati nel catalogo del supermercato. Regole di selezione: @@ -4685,6 +4733,7 @@ async function loadShoppingList() { // Load smart shopping predictions, then re-render to show badges + auto-add critical loadSmartShopping().then(() => { autoAddCriticalItems(); + cleanupObsoleteBringItems(); renderShoppingItems(); // re-render to apply urgency badges from smart data }); diff --git a/data/dispensa.db b/data/dispensa.db index 639b7a7364957376113960b0a2bb75f9f244b8d4..acd7412f6215b02773a06d4d433dfc8d8391400f 100644 GIT binary patch delta 1894 zcmZuyU2GIp6rMY?bh@)(S5SUL%q;@lUFf<$Q0W@OPm9u6pi%`MG;lk6J9}kj?l3dE z#wN0~m>|)IDkekp!RmvO7>rtUB^pgE@fqR+&n7$@ltg1N#&hrN7DOL5vomwX`mp(O@o?V+9Q50pD_`S8^2{iKgu|qrV8%`y(waBL?^JsAN+?5^us67AMj^$}> zw_css$9mKU^lGZsJ)wV^I+i?>8cXdpe%9^zO$RGW(z>Rq${92)uWzEE)8XM(co@wd zNq1?w((_~TM)ITX5A|!BKEJE?mfAe~bh%4e^Y)8ni#p6@2aa#SA$6%+<=EtI$Z?h0 zHpPKSs??pL*e8t!Gnq?5iYYd06gTRe;krw(&rBMI1bft%_izokO8}7Jpl(^zXMxG# zRUffK>H`F8Gzku<)9d@l1n$6c4>L)7Lb&E?`S z%coUF-H=t}6O3^#*FKQh4aN6yKjc|>iHn5-ws6K|zDck{!-ctQ6ZhlXxM&WCKEXw7 zG48uOXXGT#ClN7SG<5a|I-YqC`^GNg91{Qe7FH+^!jq$g1x~%xo)RE8DIv`s5+BrinAdv_Wn9 zimd8Hej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