from fastapi import APIRouter, Depends from sqlmodel import select from sqlmodel.ext.asyncio.session import AsyncSession from typing import List from datetime import datetime from app.core.database import get_async_session from app.models.system import SystemAnnouncement router = APIRouter(prefix="/system", tags=["System"]) @router.get("/announcements", response_model=List[SystemAnnouncement]) async def get_active_announcements( session: AsyncSession = Depends(get_async_session) ): """ Get active system announcements for all users. """ now = datetime.utcnow() # Explicitly check is_active AND (expires_at is null OR expires_at > now) statement = select(SystemAnnouncement).where( SystemAnnouncement.is_active == True ) result = await session.execute(statement) announcements = result.scalars().all() # Filter by expiration in python if complex OR condition is annoying in SQLModel active = [] for a in announcements: if a.expires_at is None or a.expires_at > now: active.append(a) return active