Spaces:
Running

File size: 1,112 Bytes
1b99aeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

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