vinindkoeb/app/models/participant.py
carsten 34ed5c28d7 Add Postal sending, "annoncér runde" action, and MailLog (task 7b)
POST /purchase-rounds/{id}/announce renders the route's round_announced
MailTemplate with simple {{variable}} substitution (app/services/
mail_rendering.py) for every active participant on the route, sends
via Postal's real HTTP API (app/services/postal.py — request/response
shapes verified directly against docs.postalserver.io, not guessed),
and records one MailLog row per attempt (sent/failed, Postal's
message id + token for the future webhook in 7c, denormalized
rendered_subject so the log reflects what was actually sent even if
the template changes later). A failed send for one participant doesn't
abort the rest — each attempt is isolated and committed individually.

Route gained sender_name/sender_email in 7a; both are now populated
for the real route from the historical emails ("Finn Gram - Fælles
Vinindkøb <finn@vinindkoeb.dk>") directly in the DB, since there's no
Route CRUD API yet.

Verified end-to-end against the real Postal instance: a real test
email was sent and received via an isolated throwaway Organization/
Route/Participant/PurchaseRound/MailTemplate sandbox (never the real
route's 308 participants), MailLog captured the correct Postal message
id/token, and a temporarily-invalid API key produced a clean
sent:0/failed:1 result with Postal's actual error message stored,
not a 500. All test data removed afterward; real route/participant
data confirmed untouched.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-28 20:16:04 +02:00

64 lines
1.9 KiB
Python

from typing import TYPE_CHECKING, List, Optional
from pydantic import field_validator
from sqlalchemy import UniqueConstraint
from sqlmodel import Field, Relationship, SQLModel
if TYPE_CHECKING:
from app.models.mail_log import MailLog
from app.models.order import Order
from app.models.route import Route
def _normalize_email(value: str) -> str:
return value.strip().lower()
class ParticipantBase(SQLModel):
name: str
email: str
phone: Optional[str] = Field(default=None)
is_active: bool = Field(default=True)
@field_validator("email")
@classmethod
def _validate_email(cls, v: str) -> str:
return _normalize_email(v)
class Participant(ParticipantBase, table=True):
__tablename__ = "participant"
__table_args__ = (UniqueConstraint("route_id", "email", name="uq_participant_route_email"),)
id: Optional[int] = Field(default=None, primary_key=True)
route_id: int = Field(foreign_key="route.id", ondelete="CASCADE")
route: "Route" = Relationship(back_populates="participants")
orders: List["Order"] = Relationship(
back_populates="participant", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
)
mail_logs: List["MailLog"] = Relationship(
back_populates="participant", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
)
class ParticipantCreate(ParticipantBase):
route_id: int
phone: str # påkrævet ved oprettelse, selvom det er Optional i basen/DB'en
class ParticipantUpdate(SQLModel):
name: Optional[str] = None
email: Optional[str] = None
phone: Optional[str] = None
is_active: Optional[bool] = None
@field_validator("email")
@classmethod
def _validate_email(cls, v: Optional[str]) -> Optional[str]:
return _normalize_email(v) if v is not None else v
class ParticipantPublic(ParticipantBase):
id: int
route_id: int