feat(offers): live Lübeck offer prices with pick-to-list and deal matching

Fetch CITTI Lübeck offers from the Tjek API (German dealer 3czmZx only —
never the Danish Flensburg booklet) and surface them in the app.

- New offers + offer_meta tables; defensive hotspots parser; upsert by
  offer id, prune per re-fetched catalog while preserving picked rows
- Daily in-process refresh scheduler (data.db lives on the app's own
  Nomad alloc disk, so a separate periodic job can't share it) plus a
  manual POST /api/offers/refresh and an "Opdater" button
- /tilbud page: browse all offers by catalog, search, EUR + DKK, and tick
  offers onto the shopping list; picks appear in a Lübeck-tilbud section
  on the main list and check off as bought via the existing /api/price
- Match the fixed catalog items against current offers and show a
  "på tilbud" badge with the matched offer name (conservative word-
  boundary token match, >=2 tokens for multi-word names)
- Dockerfile: add ca-certificates for urllib HTTPS to the Tjek API
This commit is contained in:
Henrik Jess Nielsen
2026-10-30 10:35:43 +01:00
parent cf999ae228
commit 65a99d2138
4 changed files with 498 additions and 2 deletions

View File

@@ -60,6 +60,7 @@
.badge { display: inline-block; font-size: 9px; padding: 1px 5px; border-radius: 3px; margin-left: 5px; vertical-align: middle; font-weight: 500; }
.badge-card { background: var(--blue-light); color: var(--blue); }
.badge-kasse { background: var(--amber-light); color: var(--amber); }
.item-name .deal { font-size: 11px; color: var(--green); background: var(--green-light); display: inline-block; margin-top: 3px; padding: 2px 7px; border-radius: 5px; font-weight: 600; text-decoration: none; line-height: 1.3; }
.item-qty { padding: 0.35rem 0.4rem; }
.item-qty input { width: 100%; border: 1px solid var(--border); border-radius: 5px; padding: 6px 5px; font-size: 11px; font-family: var(--mono); background: var(--bg); color: var(--muted); text-align: center; outline: none; transition: border-color 0.15s, background 0.15s; cursor: text; min-height: 32px; }
.item-qty input:focus { border-color: var(--accent); background: white; color: var(--text); }
@@ -174,6 +175,7 @@
<div class="header-actions">
<div style="display:flex;gap:8px;flex-wrap:wrap">
<button class="btn btn-cat" onclick="openCatModal()">+ Kategori</button>
<a href="/tilbud" class="btn">🏷 Tilbud</a>
<a href="/analyse" class="btn">📊 Analyse</a>
<a href="/historik" class="btn">🕑 Historik</a>
<button class="btn" onclick="openArchiveModal()">🗂 Arkivér & ny liste</button>
@@ -234,6 +236,8 @@
{% if item.get('badges') %}{% for b in item.badges %}{% if b == 'card' %}<span class="badge badge-card">CITTI CARD</span>{% endif %}{% if b == 'kasse' %}<span class="badge badge-kasse">Hel kasse</span>{% endif %}{% endfor %}{% endif %}
{% if item.get('de') %}<span class="de">🇩🇪 {{ item.de }}</span>{% endif %}
{% if item.get('note') %}<span class="note">{{ item.note }}</span>{% endif %}
{% set d = deals.get(item.key) %}
{% if d %}<a class="deal" href="/tilbud" title="{{ d.heading }} ({{ d.catalog }})">🏷 på tilbud: {{ "{:.2f}".format(d.price_eur).replace(".",",") }} € ≈ {{ "{:.0f}".format(d.price_eur * eur_dkk) }} kr. · {{ d.heading }}</a>{% endif %}
</div>
<div class="item-qty">
<input type="text" value="{{ s.get('qty') or item.qty }}" placeholder="{{ item.qty }}"
@@ -261,6 +265,34 @@
</div>
{% endfor %}
<!-- LÜBECK-TILBUD (valgt på Tilbud-siden) -->
<div class="section" id="sec-picks"{% if not picks %} style="display:none"{% endif %}>
<div class="section-header" style="background:#2d6a4f">
<span class="emoji">🏷</span> Lübeck-tilbud
<span class="est"><a href="/tilbud" style="color:#fff;text-decoration:underline">vælg flere →</a></span>
</div>
<div class="items-table" id="picks-table">
{% for o in picks %}
{% set ps = saved.get(o.id, {}) %}
<div class="item-row{% if ps.get('checked') %} checked{% endif %}" data-key="{{ o.id }}" id="pick-{{ o.id }}">
<div class="item-check"><input type="checkbox" {% if ps.get('checked') %}checked{% endif %} onchange="toggleOffer(this)"></div>
<div class="item-name">
{{ o.heading }}
<span class="note">{{ o.catalog }}{% if o.page %} · s. {{ o.page }}{% endif %}{% if o.unit %} · {{ o.size_from|int if o.size_from else '' }}{{ o.unit }}{% endif %}</span>
</div>
<div class="item-qty" style="display:flex;align-items:center;color:var(--muted);font-size:12px">
{% if o.pre_price_eur %}<s>{{ "{:.2f}".format(o.pre_price_eur).replace(".",",") }}</s>{% endif %}
</div>
<div class="item-est">{{ "{:.2f}".format(o.price_eur).replace(".",",") }} €</div>
<div class="item-actual" style="display:flex;align-items:center;gap:6px;justify-content:flex-end">
<span style="font-family:var(--mono)">≈ {{ "{:.0f}".format(o.price_eur * eur_dkk) }} kr.</span>
<button class="eir-del" onclick="removeOffer('{{ o.id }}')" title="Fjern fra seddel" style="border:none;background:none;cursor:pointer">×</button>
</div>
</div>
{% endfor %}
</div>
</div>
<!-- DYNAMISKE SEKTIONER -->
<div id="custom-sections">
{% for cs in custom_secs %}
@@ -639,6 +671,26 @@ async function deleteCategory(id) {
document.getElementById(`csec-${id}`).remove();
}
function toggleOffer(cb) {
const row = cb.closest('.item-row');
row.classList.toggle('checked', cb.checked);
saveToServer(row.dataset.key, null, cb.checked ? 1 : 0);
}
async function removeOffer(id) {
await fetch('/api/offers/pick', {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({id, picked: false})
});
const row = document.getElementById('pick-' + id);
if (row) row.remove();
if (!document.querySelectorAll('#picks-table .item-row').length) {
document.getElementById('sec-picks').style.display = 'none';
}
showSaved();
}
function openArchiveModal() {
document.getElementById('archive-modal').classList.add('open');
}

161
templates/tilbud.html Normal file
View File

@@ -0,0 +1,161 @@
<!DOCTYPE html>
<html lang="da">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Citti Tilbud — Lübeck</title>
<link href="https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
<style>
:root {
--bg: #f5f2ed; --card: #ffffff; --border: #e2ddd6; --text: #1a1814;
--muted: #8a857c; --accent: #c84b2f; --green: #2d6a4f; --green-light: #e8f4ee;
--mono: 'DM Mono', monospace; --sans: 'DM Sans', sans-serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: var(--sans); background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5; padding: 1.5rem 1rem 4rem; }
.page { max-width: 860px; margin: 0 auto; }
header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; padding-bottom: 1.25rem; border-bottom: 2px solid var(--text); }
h1 { font-size: 1.75rem; font-weight: 600; letter-spacing: -0.03em; }
h1 span { color: var(--accent); }
.sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.btn { background: var(--card); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 8px 14px; font-size: 13px; font-family: var(--sans); cursor: pointer; text-decoration: none; display: inline-block; font-weight: 500; }
.btn-back { background: var(--text); color: white; border: none; }
.btn-refresh { background: var(--green); color: white; border: none; }
.btn:active { opacity: 0.85; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 1.25rem; }
.search { flex: 1; min-width: 180px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; font-family: var(--sans); font-size: 14px; background: var(--card); }
.pick-count { font-size: 13px; color: var(--green); font-weight: 600; white-space: nowrap; }
.cat { margin-bottom: 1.5rem; }
.cat-title { background: var(--text); color: white; padding: 0.5rem 1rem; font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em; border-radius: 8px 8px 0 0; }
.cat-body { background: var(--card); border: 1px solid var(--border); border-top: none; border-radius: 0 0 8px 8px; overflow: hidden; }
.offer { display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 10px; padding: 0.55rem 1rem; border-bottom: 1px solid var(--border); }
.offer:last-child { border-bottom: none; }
.offer.picked { background: var(--green-light); }
.offer input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--green); cursor: pointer; }
.o-name { font-size: 14px; }
.o-meta { font-size: 11px; color: var(--muted); margin-top: 1px; }
.o-price { text-align: right; white-space: nowrap; }
.o-eur { font-family: var(--mono); font-weight: 600; font-size: 15px; }
.o-pre { font-family: var(--mono); font-size: 12px; color: var(--muted); text-decoration: line-through; margin-right: 4px; }
.o-dkk { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.empty-state { padding: 2.5rem 2rem; text-align: center; color: var(--muted); font-size: 14px; background: var(--card); border: 1px solid var(--border); border-radius: 10px; }
.err { background: #fde8e3; color: var(--accent); font-size: 12px; padding: 8px 12px; border-radius: 8px; margin-bottom: 1rem; }
.cat.hidden, .offer.hidden { display: none; }
@media (max-width: 600px) {
.offer { grid-template-columns: 30px 1fr auto; }
}
</style>
</head>
<body>
<div class="page">
<header>
<div>
<h1>Citti <span>Tilbud</span></h1>
<div class="sub">
CITTI Lübeck · Herrenholz 14 · priser i EUR (≈ ×{{ "{:.2f}".format(eur_dkk).replace(".",",") }} kr.)<br>
{% if last_refresh %}Opdateret {{ last_refresh[:16].replace("T"," ") }} · {% endif %}{{ total }} tilbud
</div>
</div>
<div style="display:flex;flex-direction:column;gap:8px;align-items:flex-end">
<a href="/" class="btn btn-back">← Indkøbsseddel</a>
<button class="btn btn-refresh" id="refresh-btn" onclick="refreshOffers()">⟳ Opdater</button>
</div>
</header>
{% if last_error %}<div class="err">Sidste opdatering fejlede: {{ last_error }}</div>{% endif %}
<div class="toolbar">
<input class="search" id="search" type="search" placeholder="Søg i tilbud (tysk navn)…" oninput="filterOffers()">
<span class="pick-count" id="pick-count">{{ picked_count }} på seddel</span>
</div>
{% if total == 0 %}
<div class="empty-state">
Ingen tilbud hentet endnu. Tryk <strong>⟳ Opdater</strong> for at hente ugens tilbud fra CITTI Lübeck.
</div>
{% endif %}
{% for cat, offers in by_cat.items() %}
<div class="cat" data-cat>
<div class="cat-title">{{ cat }} ({{ offers|length }})</div>
<div class="cat-body">
{% for o in offers %}
<label class="offer{% if o.picked %} picked{% endif %}" data-name="{{ o.heading|lower }}">
<input type="checkbox" {% if o.picked %}checked{% endif %} onchange="togglePick(this, '{{ o.id }}')">
<div>
<div class="o-name">{{ o.heading }}</div>
<div class="o-meta">
{% if o.page %}s. {{ o.page }}{% endif %}
{% if o.unit and o.size_from %} · {{ o.size_from|int }}{% if o.size_to and o.size_to != o.size_from %}–{{ o.size_to|int }}{% endif %} {{ o.unit }}{% endif %}
{% if o.run_till %} · t.o.m. {{ o.run_till[:10] }}{% endif %}
</div>
</div>
<div class="o-price">
<div>
{% if o.pre_price_eur %}<span class="o-pre">{{ "{:.2f}".format(o.pre_price_eur).replace(".",",") }}</span>{% endif %}
<span class="o-eur">{{ "{:.2f}".format(o.price_eur).replace(".",",") }} €</span>
</div>
<div class="o-dkk">≈ {{ "{:.0f}".format(o.price_eur * eur_dkk) }} kr.</div>
</div>
</label>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
<script>
let pickCount = {{ picked_count }};
async function togglePick(cb, id) {
const picked = cb.checked;
cb.closest('.offer').classList.toggle('picked', picked);
pickCount += picked ? 1 : -1;
document.getElementById('pick-count').textContent = pickCount + ' på seddel';
try {
await fetch('/api/offers/pick', {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({id, picked})
});
} catch (e) {
cb.checked = !picked;
cb.closest('.offer').classList.toggle('picked', !picked);
pickCount += picked ? -1 : 1;
document.getElementById('pick-count').textContent = pickCount + ' på seddel';
}
}
function filterOffers() {
const q = document.getElementById('search').value.trim().toLowerCase();
document.querySelectorAll('[data-cat]').forEach(cat => {
let any = false;
cat.querySelectorAll('.offer').forEach(o => {
const match = !q || o.dataset.name.includes(q);
o.classList.toggle('hidden', !match);
if (match) any = true;
});
cat.classList.toggle('hidden', !any);
});
}
async function refreshOffers() {
const btn = document.getElementById('refresh-btn');
btn.textContent = '⟳ Henter…'; btn.disabled = true;
try {
const res = await fetch('/api/offers/refresh', {method: 'POST'});
const data = await res.json();
if (data.ok) { window.location.reload(); return; }
alert('Opdatering fejlede. Prøv igen om lidt.');
} catch (e) {
alert('Opdatering fejlede. Prøv igen om lidt.');
}
btn.textContent = '⟳ Opdater'; btn.disabled = false;
}
</script>
</body>
</html>