Features: - FastAPI backend with JWT authentication - MySQL database with SQLAlchemy ORM - KPI workflow: draft → pending → approved → evaluation → completed - Ollama LLM API integration for AI features - Gitea API integration for version control - Complete API endpoints for KPI, dashboard, notifications Tables: KPI_D_* prefix naming convention 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
134 lines
3.6 KiB
Python
134 lines
3.6 KiB
Python
"""
|
|
通知 API
|
|
"""
|
|
from typing import List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_db, get_current_user
|
|
from app.models.employee import Employee
|
|
from app.services.notify_service import NotifyService
|
|
from app.schemas.notification import (
|
|
NotificationResponse,
|
|
NotificationPreferenceResponse,
|
|
NotificationPreferenceUpdate,
|
|
UnreadCountResponse,
|
|
)
|
|
from app.schemas.common import MessageResponse
|
|
|
|
router = APIRouter(prefix="/api/notifications", tags=["通知"])
|
|
|
|
|
|
@router.get("", response_model=List[NotificationResponse])
|
|
def list_notifications(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
取得通知列表
|
|
|
|
返回當前使用者的通知,按時間倒序排列。
|
|
"""
|
|
service = NotifyService(db)
|
|
return service.get_by_recipient(current_user.id, skip, limit)
|
|
|
|
|
|
@router.get("/unread-count", response_model=UnreadCountResponse)
|
|
def get_unread_count(
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
取得未讀數量
|
|
|
|
返回當前使用者的未讀通知數量。
|
|
"""
|
|
service = NotifyService(db)
|
|
count = service.get_unread_count(current_user.id)
|
|
return UnreadCountResponse(count=count)
|
|
|
|
|
|
@router.put("/{notification_id}/read", response_model=MessageResponse)
|
|
def mark_as_read(
|
|
notification_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
標記已讀
|
|
|
|
將指定通知標記為已讀。
|
|
"""
|
|
service = NotifyService(db)
|
|
success = service.mark_as_read(notification_id, current_user.id)
|
|
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"code": "NOTIFY001", "message": "通知不存在"},
|
|
)
|
|
|
|
return MessageResponse(message="已標記為已讀")
|
|
|
|
|
|
@router.put("/read-all", response_model=MessageResponse)
|
|
def mark_all_as_read(
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
全部標記已讀
|
|
|
|
將當前使用者的所有通知標記為已讀。
|
|
"""
|
|
service = NotifyService(db)
|
|
count = service.mark_all_as_read(current_user.id)
|
|
return MessageResponse(message=f"已將 {count} 則通知標記為已讀")
|
|
|
|
|
|
@router.get("/preferences", response_model=NotificationPreferenceResponse)
|
|
def get_preferences(
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
取得通知偏好
|
|
|
|
返回當前使用者的通知偏好設定。
|
|
"""
|
|
service = NotifyService(db)
|
|
pref = service.get_preferences(current_user.id)
|
|
|
|
if not pref:
|
|
# 返回預設值
|
|
return NotificationPreferenceResponse(
|
|
email_enabled=True,
|
|
in_app_enabled=True,
|
|
reminder_days_before=3,
|
|
)
|
|
|
|
return pref
|
|
|
|
|
|
@router.put("/preferences", response_model=NotificationPreferenceResponse)
|
|
def update_preferences(
|
|
data: NotificationPreferenceUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
更新通知偏好
|
|
|
|
更新當前使用者的通知偏好設定。
|
|
"""
|
|
service = NotifyService(db)
|
|
return service.update_preferences(
|
|
current_user.id,
|
|
email_enabled=data.email_enabled,
|
|
in_app_enabled=data.in_app_enabled,
|
|
reminder_days_before=data.reminder_days_before,
|
|
)
|