41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""Alert endpoints."""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from app.database import get_db
|
|
from app.schemas.alert import AlertResponse
|
|
from app.services.alert_service import AlertService
|
|
from app.api.deps import get_current_user
|
|
from app.models.user import User
|
|
|
|
router = APIRouter(prefix="/alerts")
|
|
|
|
|
|
@router.get("", response_model=List[AlertResponse])
|
|
def list_alerts(
|
|
is_read: Optional[bool] = None,
|
|
user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
return AlertService.list_alerts(db, user.tenant_id, is_read)
|
|
|
|
|
|
@router.get("/unread-count")
|
|
def unread_count(
|
|
user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
return {"count": AlertService.unread_count(db, user.tenant_id)}
|
|
|
|
|
|
@router.put("/{alert_id}/read", response_model=AlertResponse)
|
|
def mark_read(
|
|
alert_id: int,
|
|
user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
alert = AlertService.mark_read(db, user.tenant_id, alert_id)
|
|
if not alert:
|
|
raise HTTPException(status_code=404, detail="Alert not found")
|
|
return alert
|