Files
waste_api/app/routes/notifications.py

45 lines
1.3 KiB
Python

from fastapi import APIRouter, HTTPException
from ..schemas import (
NotifyRequest,
NotifyResponse,
NotificationsResponse,
DeleteResponse,
)
from ..core.scheduler import NotificationScheduler
router = APIRouter(prefix="/notifications", tags=["notifications"])
# singleton scheduler
_scheduler = NotificationScheduler()
@router.post("", response_model=NotifyResponse)
def create_notification(body: NotifyRequest):
entry = _scheduler.add(
source_id=body.source_id,
collection=body.collection.model_dump(),
notify_at_days_before=body.notify_at_days_before,
channels=body.channels,
chat_id=body.chat_id,
)
return NotifyResponse(
ok=True,
notification_id=entry["id"],
message=f"Reminder set for {entry['collection_date']}: {entry['collection_type']}",
)
@router.get("", response_model=NotificationsResponse)
def list_notifications():
notes = _scheduler.list_all()
return NotificationsResponse(count=len(notes), notifications=notes)
@router.delete("/{notification_id}", response_model=DeleteResponse)
def delete_notification(notification_id: str):
ok = _scheduler.delete(notification_id)
if not ok:
raise HTTPException(status_code=404, detail=f"Notification '{notification_id}' not found")
return DeleteResponse(ok=True, deleted=notification_id)