POST /public/routes/{id}/orders and GET .../current-round are the
first fully unauthenticated routes in the app — no CurrentUser or
org-scoping helper applies, route_id comes straight from the URL.
Participant identification matches the old system's no-login design:
an existing participant (case-insensitive email match on the route) is
reused, and — per explicit user correction — its name/phone/is_active
are overwritten from the submission every time, since there's no login
or "edit my details" page; the order form *is* how members keep their
contact info current. is_active is a checkbox on the form itself
(default true), not inferred. A new email always creates a new
participant row — that's the deliberate no-login way to "change
email". An IntegrityError race on concurrent same-email submissions
falls back to re-selecting the winner rather than 500ing.
Order confirmation email reuses 7b's render/send/log pattern for a
single participant, but a missing order_confirmed template (or missing
route sender identity) is a silent no-op + logger.warning, never a
blocking error — an admin configuration gap must never stop a real
order, unlike task 7b's admin-triggered /announce which fails fast on
the same conditions.
Per explicit user correction, the confirmation email deliberately
reproduces the old system's full-catalog receipt (every wine in the
round, grouped by category, ordered quantity filled in where
applicable) — confirmed via old-emails/Kvitering.eml discussion to be
a deliberate mimicry of the physical order sheet used when buying wine
at the producer's cellar, not a legacy-template artifact to simplify
away. Currency totals are computed at full Decimal precision (EUR
summed, then × eur_dkk_rate with no intermediate rounding) and only
rounded to 2dp (ROUND_HALF_UP) at final display formatting, per
correction — avoids compounding an early rounding error into the DKK
figure.
Empty order_lines is rejected with 400 (not the Pydantic-level 422 a
schema constraint would give) — the old empty-order signup/unsubscribe
hack is intentionally not resurrected here; a real signup/unsubscribe
flow is a separate future task per the user's own framing.
Verified end-to-end directly against the real route 4: a temporary
open round + wine offerings + order_confirmed template, a real order
submitted and a real confirmation email sent/logged, a second order
with the same (differently-cased) email confirmed to update the
existing participant in place rather than duplicate it, a
different-round wine_offering_id rejected (400), an empty order
rejected (400 not 422), and the missing-template case confirmed to
still return 201 with no new MailLog row. All temporary data removed
afterward; the real participant (carsten@itkon.dk, id 133) — legitimately
touched by the get-or-create-with-update logic during testing, exactly
as designed — was restored to its original name/phone/is_active. The
other 307 real participants were untouched throughout.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
import re
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from decimal import ROUND_HALF_UP, Decimal
|
|
from typing import Optional
|
|
|
|
_PLACEHOLDER_RE = re.compile(r"\{\{\s*(\w+)\s*\}\}")
|
|
|
|
|
|
def render_template(text: str, variables: dict[str, str]) -> str:
|
|
return _PLACEHOLDER_RE.sub(lambda m: str(variables.get(m.group(1), m.group(0))), text)
|
|
|
|
|
|
def format_datetime_da(dt: datetime) -> str:
|
|
return dt.strftime("%d-%m-%Y kl. %H:%M")
|
|
|
|
|
|
@dataclass
|
|
class CatalogRow:
|
|
category_name: str
|
|
wine_name: str
|
|
unit_price_eur: Decimal
|
|
ordered_quantity: Optional[int] # None = ikke bestilt, vises tomt
|
|
|
|
|
|
def render_order_receipt_html(rows: list[CatalogRow]) -> str:
|
|
"""rows skal være for-sorteret efter (kategori.sort_order, vin-id) —
|
|
denne funktion grupperer blot konsekutive ens kategorinavne under
|
|
én overskrift, ligesom den gamle Kvitering-mail."""
|
|
html_parts = ['<table style="border-collapse:collapse;width:100%;">']
|
|
current_category = None
|
|
for row in rows:
|
|
if row.category_name != current_category:
|
|
html_parts.append(
|
|
f'<tr><th colspan="3" style="text-align:left;padding-top:12px;">{row.category_name}</th></tr>'
|
|
)
|
|
current_category = row.category_name
|
|
qty_display = str(row.ordered_quantity) if row.ordered_quantity else ""
|
|
html_parts.append(
|
|
f'<tr><td style="padding:2px 8px;width:40px;">{qty_display}</td>'
|
|
f'<td style="padding:2px 8px;">{row.wine_name}</td>'
|
|
f'<td style="padding:2px 8px;text-align:right;">{row.unit_price_eur:.2f}</td></tr>'
|
|
)
|
|
html_parts.append("</table>")
|
|
return "".join(html_parts)
|
|
|
|
|
|
def compute_order_total_eur(rows: list[CatalogRow]) -> Decimal:
|
|
"""Kun de faktisk bestilte linjer tæller med i totalen — resten af
|
|
kataloget er kun til visning. Ingen afrunding her (fuld præcision
|
|
bevares til evt. DKK-omregning)."""
|
|
return sum(
|
|
(r.unit_price_eur * r.ordered_quantity for r in rows if r.ordered_quantity),
|
|
Decimal("0"),
|
|
)
|
|
|
|
|
|
def format_currency(amount: Decimal) -> str:
|
|
"""Eneste sted afrunding sker — til visning, normal matematisk
|
|
afrunding (ikke Decimal's ROUND_HALF_EVEN-standard)."""
|
|
return str(amount.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
|