FastAPI + Celery + Next.js + Postgres/Redis app with company monitoring, source collection, LLM-based change analysis, enrichment, and account security (Turnstile, escalating lockout, email verification).
69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
"""Builds the actual email/SMS/console message bodies for an alert. Kept
|
|
separate from alert_service.py's dispatch loop so the message format can be
|
|
tested in isolation.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app.core.config import Settings
|
|
from app.models.alert import Alert
|
|
from app.models.company import Company
|
|
from app.models.enums import NotificationType
|
|
from app.notifications.base import NotificationMessage
|
|
|
|
|
|
def build_alert_message(
|
|
notification_type: NotificationType,
|
|
destination_value: str,
|
|
company: Company,
|
|
alert: Alert,
|
|
settings: Settings,
|
|
) -> NotificationMessage:
|
|
dashboard_link = f"{settings.frontend_url}/alerts"
|
|
|
|
if notification_type == NotificationType.SMS:
|
|
text = (
|
|
f"CI Alert [{alert.severity.value.upper()}]: {alert.title}. "
|
|
f"Confidence {alert.confidence:.0%}. View details: {dashboard_link}"
|
|
)
|
|
return NotificationMessage(
|
|
destination_value=destination_value, subject=alert.title, body_text=text[:480]
|
|
)
|
|
|
|
subject = f"[{alert.severity.value.upper()}] {company.name}: {alert.title}"
|
|
text_lines = [
|
|
f"Company: {company.name}",
|
|
f"Alert: {alert.title}",
|
|
f"Severity: {alert.severity.value.title()}",
|
|
f"Confidence: {alert.confidence:.0%}",
|
|
"",
|
|
"What changed:",
|
|
alert.summary,
|
|
"",
|
|
"Why it matters:",
|
|
alert.why_it_matters,
|
|
"",
|
|
f"View in dashboard: {dashboard_link}",
|
|
f"Manage notification preferences: {settings.frontend_url}/settings",
|
|
]
|
|
body_text = "\n".join(text_lines)
|
|
|
|
body_html = (
|
|
f"<h2>{subject}</h2>"
|
|
f"<p><strong>Company:</strong> {company.name}<br>"
|
|
f"<strong>Severity:</strong> {alert.severity.value.title()}<br>"
|
|
f"<strong>Confidence:</strong> {alert.confidence:.0%}</p>"
|
|
f"<p><strong>What changed:</strong><br>{alert.summary}</p>"
|
|
f"<p><strong>Why it matters:</strong><br>{alert.why_it_matters}</p>"
|
|
f'<p><a href="{dashboard_link}">View in dashboard</a></p>'
|
|
f'<p style="color:#666;font-size:12px">'
|
|
f'<a href="{settings.frontend_url}/settings">Manage notification preferences</a></p>'
|
|
)
|
|
|
|
return NotificationMessage(
|
|
destination_value=destination_value,
|
|
subject=subject,
|
|
body_text=body_text,
|
|
body_html=body_html,
|
|
)
|