"""Notification logs listing with filters.""" from __future__ import annotations import json from typing import List, Optional from fastapi import APIRouter, Depends, Query from sqlalchemy.orm import Session from app.database import get_db from app.models.notification_log import NotificationLog from app.schemas.log import NotificationLogResponse from app.utils.auth import get_current_user router = APIRouter(prefix="/api/notification-logs", tags=["logs"]) def _to_response(log: NotificationLog) -> NotificationLogResponse: return NotificationLogResponse( id=log.id, webhook_config_id=log.webhook_config_id, webhook_name=log.webhook_name, event_type=log.event_type, payload=json.loads(log.payload_json or "{}"), status=log.status, response_text=log.response_text, created_at=log.created_at, ) @router.get("", response_model=List[NotificationLogResponse]) def list_logs( status: Optional[str] = Query(None), event_type: Optional[str] = Query(None), limit: int = Query(100, le=500), offset: int = Query(0), db: Session = Depends(get_db), _=Depends(get_current_user), ): q = db.query(NotificationLog) if status: q = q.filter(NotificationLog.status == status) if event_type: q = q.filter(NotificationLog.event_type == event_type) logs = q.order_by(NotificationLog.created_at.desc()).offset(offset).limit(limit).all() return [_to_response(log) for log in logs]