phase-6: alerts engine, scheduler, notifications, calibration popup, and dashboard
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..core.db import get_db
|
||||
from ..core.deps import get_current_user, require_roles
|
||||
from ..core.time_utils import utc_now
|
||||
from ..models import Notification, NotificationRead, User
|
||||
from ..schemas.notifications import (
|
||||
AlertRunOut,
|
||||
NotificationListOut,
|
||||
NotificationOut,
|
||||
NotificationReadAllOut,
|
||||
NotificationReadOut,
|
||||
)
|
||||
from ..services.alerts import (
|
||||
get_visible_notifications_for_user,
|
||||
notification_link_url,
|
||||
run_alert_check,
|
||||
)
|
||||
from ..services.permissions import Role
|
||||
|
||||
router = APIRouter(tags=["notifications"])
|
||||
|
||||
|
||||
@router.post("/alerts/run", response_model=AlertRunOut)
|
||||
def trigger_alert_run(
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_roles(Role.system_admin)),
|
||||
):
|
||||
result = run_alert_check(db)
|
||||
db.commit()
|
||||
return AlertRunOut(**result)
|
||||
|
||||
|
||||
@router.get("/notifications", response_model=NotificationListOut)
|
||||
def list_notifications(
|
||||
notif_type: Optional[str] = Query(None, alias="type"),
|
||||
severity: Optional[str] = None,
|
||||
unread_only: bool = False,
|
||||
limit: Optional[int] = Query(None, ge=1, le=200),
|
||||
page: int = Query(1, ge=1),
|
||||
page_size: int = Query(50, ge=1, le=200),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
visible, read_map = get_visible_notifications_for_user(db, current_user)
|
||||
|
||||
unread_count = sum(1 for n in visible if n.id not in read_map)
|
||||
unread_calibration_count = sum(
|
||||
1 for n in visible if n.id not in read_map and n.type == "calibration_due"
|
||||
)
|
||||
|
||||
filtered: list[Notification] = []
|
||||
for n in visible:
|
||||
is_read = n.id in read_map
|
||||
if notif_type and n.type != notif_type:
|
||||
continue
|
||||
if severity and n.severity != severity:
|
||||
continue
|
||||
if unread_only and is_read:
|
||||
continue
|
||||
filtered.append(n)
|
||||
|
||||
total = len(filtered)
|
||||
eff_size = limit if limit is not None else page_size
|
||||
start = (page - 1) * eff_size
|
||||
page_items = filtered[start : start + eff_size]
|
||||
|
||||
items_out = [
|
||||
NotificationOut(
|
||||
id=n.id,
|
||||
type=n.type,
|
||||
severity=n.severity,
|
||||
title=n.title,
|
||||
message=n.message,
|
||||
entity_type=n.entity_type,
|
||||
entity_id=n.entity_id,
|
||||
due_date=n.due_date,
|
||||
link_url=notification_link_url(n.entity_type, n.entity_id),
|
||||
is_read=n.id in read_map,
|
||||
read_at=read_map.get(n.id),
|
||||
created_at=n.created_at,
|
||||
)
|
||||
for n in page_items
|
||||
]
|
||||
|
||||
return NotificationListOut(
|
||||
items=items_out,
|
||||
total=total,
|
||||
unread_count=unread_count,
|
||||
unread_calibration_count=unread_calibration_count,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/notifications/read-all", response_model=NotificationReadAllOut)
|
||||
def mark_all_notifications_read(
|
||||
notif_type: Optional[str] = Query(None, alias="type"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
visible, read_map = get_visible_notifications_for_user(db, current_user)
|
||||
now = utc_now()
|
||||
marked = 0
|
||||
for n in visible:
|
||||
if n.id in read_map:
|
||||
continue
|
||||
if notif_type and n.type != notif_type:
|
||||
continue
|
||||
db.add(
|
||||
NotificationRead(
|
||||
notification_id=n.id,
|
||||
user_id=current_user.id,
|
||||
read_at=now,
|
||||
)
|
||||
)
|
||||
marked += 1
|
||||
if marked:
|
||||
db.commit()
|
||||
return NotificationReadAllOut(marked_read=marked)
|
||||
|
||||
|
||||
@router.post("/notifications/{notification_id}/read", response_model=NotificationReadOut)
|
||||
def mark_notification_read(
|
||||
notification_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
notif = db.query(Notification).filter(Notification.id == notification_id).first()
|
||||
if notif is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Notification not found.")
|
||||
|
||||
visible, read_map = get_visible_notifications_for_user(db, current_user)
|
||||
if all(n.id != notification_id for n in visible):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Access denied for this notification.",
|
||||
)
|
||||
|
||||
if notification_id not in read_map:
|
||||
db.add(
|
||||
NotificationRead(
|
||||
notification_id=notification_id,
|
||||
user_id=current_user.id,
|
||||
read_at=utc_now(),
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
return NotificationReadOut(notification_id=notification_id, is_read=True)
|
||||
Reference in New Issue
Block a user