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).
56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.alert import Alert
|
|
from app.models.enums import SeverityLevel
|
|
|
|
|
|
class AlertRepository:
|
|
def __init__(self, db: AsyncSession) -> None:
|
|
self.db = db
|
|
|
|
async def get(self, alert_id: uuid.UUID) -> Alert | None:
|
|
return await self.db.get(Alert, alert_id)
|
|
|
|
async def get_for_user(self, alert_id: uuid.UUID, user_id: uuid.UUID) -> Alert | None:
|
|
result = await self.db.execute(
|
|
select(Alert).where(Alert.id == alert_id, Alert.user_id == user_id)
|
|
)
|
|
return result.scalar_one_or_none()
|
|
|
|
async def list_for_user(
|
|
self,
|
|
user_id: uuid.UUID,
|
|
*,
|
|
company_id: uuid.UUID | None = None,
|
|
severity: SeverityLevel | None = None,
|
|
read: bool | None = None,
|
|
resolved: bool | None = None,
|
|
since: datetime | None = None,
|
|
limit: int = 200,
|
|
) -> list[Alert]:
|
|
stmt = select(Alert).where(Alert.user_id == user_id)
|
|
if company_id is not None:
|
|
stmt = stmt.where(Alert.company_id == company_id)
|
|
if severity is not None:
|
|
stmt = stmt.where(Alert.severity == severity)
|
|
if read is not None:
|
|
stmt = stmt.where(Alert.read == read)
|
|
if resolved is not None:
|
|
stmt = stmt.where(Alert.resolved == resolved)
|
|
if since is not None:
|
|
stmt = stmt.where(Alert.created_at >= since)
|
|
stmt = stmt.order_by(Alert.created_at.desc()).limit(limit)
|
|
result = await self.db.execute(stmt)
|
|
return list(result.scalars().all())
|
|
|
|
async def create(self, alert: Alert) -> Alert:
|
|
self.db.add(alert)
|
|
await self.db.flush()
|
|
return alert
|