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>
46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
from sqlmodel import select
|
|
|
|
from app.db import SessionDep
|
|
from app.dependencies import CurrentUser
|
|
from app.models.mail_log import MailLog, MailLogPublic
|
|
from app.models.purchase_round import PurchaseRound
|
|
from app.models.route import Route
|
|
|
|
router = APIRouter(prefix="/mail-logs", tags=["mail-logs"])
|
|
|
|
|
|
@router.get("", response_model=list[MailLogPublic])
|
|
def list_mail_logs(
|
|
session: SessionDep,
|
|
current_user: CurrentUser,
|
|
participant_id: Optional[int] = None,
|
|
purchase_round_id: Optional[int] = None,
|
|
) -> list[MailLog]:
|
|
statement = (
|
|
select(MailLog)
|
|
.join(PurchaseRound)
|
|
.join(Route)
|
|
.where(Route.organization_id == current_user.organization_id)
|
|
)
|
|
if participant_id is not None:
|
|
statement = statement.where(MailLog.participant_id == participant_id)
|
|
if purchase_round_id is not None:
|
|
statement = statement.where(MailLog.purchase_round_id == purchase_round_id)
|
|
statement = statement.order_by(MailLog.id.desc())
|
|
return list(session.exec(statement).all())
|
|
|
|
|
|
@router.get("/{log_id}", response_model=MailLogPublic)
|
|
def get_mail_log(log_id: int, session: SessionDep, current_user: CurrentUser) -> MailLog:
|
|
mail_log = session.exec(
|
|
select(MailLog)
|
|
.join(PurchaseRound)
|
|
.join(Route)
|
|
.where(MailLog.id == log_id, Route.organization_id == current_user.organization_id)
|
|
).first()
|
|
if mail_log is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Mail log not found")
|
|
return mail_log
|