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>
73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
from datetime import datetime, timezone
|
|
from enum import Enum
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
from sqlalchemy import Column
|
|
from sqlalchemy import Enum as SAEnum
|
|
from sqlmodel import Field, Relationship, SQLModel
|
|
|
|
from app.models.mail_template import MailEventType
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.participant import Participant
|
|
from app.models.purchase_round import PurchaseRound
|
|
|
|
|
|
class MailLogStatus(str, Enum):
|
|
SENT = "sent"
|
|
FAILED = "failed"
|
|
DELIVERED = "delivered" # udfyldes først af webhook i opgave 7c
|
|
BOUNCED = "bounced" # udfyldes først af webhook i opgave 7c
|
|
|
|
|
|
class MailLog(SQLModel, table=True):
|
|
__tablename__ = "mail_log"
|
|
|
|
id: Optional[int] = Field(default=None, primary_key=True)
|
|
participant_id: int = Field(foreign_key="participant.id", ondelete="CASCADE")
|
|
purchase_round_id: int = Field(foreign_key="purchase_round.id", ondelete="CASCADE")
|
|
mail_template_id: Optional[int] = Field(default=None, foreign_key="mail_template.id", ondelete="SET NULL")
|
|
|
|
event_type: MailEventType = Field(
|
|
sa_column=Column(
|
|
SAEnum(
|
|
MailEventType,
|
|
name="mail_event_type",
|
|
native_enum=False,
|
|
values_callable=lambda enum_cls: [member.value for member in enum_cls],
|
|
),
|
|
nullable=False,
|
|
)
|
|
)
|
|
rendered_subject: str
|
|
status: MailLogStatus = Field(
|
|
sa_column=Column(
|
|
SAEnum(
|
|
MailLogStatus,
|
|
name="mail_log_status",
|
|
native_enum=False,
|
|
values_callable=lambda enum_cls: [member.value for member in enum_cls],
|
|
),
|
|
nullable=False,
|
|
)
|
|
)
|
|
postal_message_id: Optional[int] = Field(default=None)
|
|
postal_token: Optional[str] = Field(default=None)
|
|
error_message: Optional[str] = Field(default=None)
|
|
sent_at: Optional[datetime] = Field(default=None)
|
|
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
|
|
|
participant: "Participant" = Relationship(back_populates="mail_logs")
|
|
purchase_round: "PurchaseRound" = Relationship(back_populates="mail_logs")
|
|
|
|
|
|
class MailLogPublic(SQLModel):
|
|
id: int
|
|
participant_id: int
|
|
purchase_round_id: int
|
|
event_type: MailEventType
|
|
rendered_subject: str
|
|
status: MailLogStatus
|
|
error_message: Optional[str]
|
|
sent_at: Optional[datetime]
|
|
created_at: datetime
|