Files
Арсентьев Александр 07dbde2a0e Initial monorepo structure
2026-08-06 11:23:37 +03:00

152 lines
4.9 KiB
Python

"""Notification templates and saved filters routes."""
import json
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List, Optional, Dict
from app.core.database import get_db
from app.core.auth import get_current_user_id
from app.services.notification_service_extended import NotificationServiceExtended
from app.models.notification_template import NotificationTemplate, SavedFilter
router = APIRouter(prefix="/notifications", tags=["Notifications"])
# Templates
@router.get("/templates", response_model=List[dict])
async def get_templates(
event_type: Optional[str] = None,
token: str = Depends(get_current_user_id),
db: Session = Depends(get_db)
):
"""Get notification templates."""
service = NotificationServiceExtended(db)
templates = service.get_templates(event_type)
result = []
for t in templates:
specialist_ids = []
if t.specialist_ids:
try:
specialist_ids = json.loads(t.specialist_ids)
except:
pass
result.append({
"id": t.id,
"name": t.name,
"event_type": t.event_type,
"channel": t.channel,
"subject_template": t.subject_template,
"body_template": t.body_template,
"is_active": t.is_active,
"send_to_initiator": t.send_to_initiator,
"send_to_specialist": t.send_to_specialist,
"send_to_group": t.send_to_group,
"department_id": t.department_id,
"specialist_ids": specialist_ids
})
return result
@router.post("/templates", status_code=status.HTTP_201_CREATED)
async def create_template(
data: Dict,
token: str = Depends(get_current_user_id),
db: Session = Depends(get_db)
):
"""Create notification template."""
service = NotificationServiceExtended(db)
try:
template = service.create_template(**data)
return {"id": template.id, "message": "Template created"}
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
@router.put("/templates/{template_id}")
async def update_template(
template_id: int,
data: Dict,
token: str = Depends(get_current_user_id),
db: Session = Depends(get_db)
):
"""Update notification template."""
service = NotificationServiceExtended(db)
try:
template = service.update_template(template_id, **data)
return {"id": template.id, "message": "Template updated"}
except ValueError as e:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
@router.delete("/templates/{template_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_template(
template_id: int,
token: str = Depends(get_current_user_id),
db: Session = Depends(get_db)
):
"""Delete notification template."""
service = NotificationServiceExtended(db)
if not service.delete_template(template_id):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Template not found")
# Saved Filters
@router.get("/filters")
async def get_saved_filters(
filter_type: str = "tickets",
token: str = Depends(get_current_user_id),
db: Session = Depends(get_db)
):
"""Get saved filters for current user."""
service = NotificationServiceExtended(db)
filters = service.get_user_filters(token, filter_type)
result = []
for f in filters:
config = f.filter_config
# Parse JSON string if needed
if isinstance(config, str):
try:
config = json.loads(config)
except:
config = {}
result.append({
"id": f.id,
"name": f.name,
"filter_type": f.filter_type,
"filter_config": config
})
return result
@router.post("/filters", status_code=status.HTTP_201_CREATED)
async def create_saved_filter(
data: Dict,
token: str = Depends(get_current_user_id),
db: Session = Depends(get_db)
):
"""Create saved filter."""
service = NotificationServiceExtended(db)
try:
saved_filter = service.create_filter(
user_id=token,
name=data.get('name', 'Unnamed'),
filter_type=data.get('filter_type', 'tickets'),
filter_config=data.get('filter_config', {})
)
return {"id": saved_filter.id, "message": "Filter saved"}
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
@router.delete("/filters/{filter_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_saved_filter(
filter_id: int,
token: str = Depends(get_current_user_id),
db: Session = Depends(get_db)
):
"""Delete saved filter."""
service = NotificationServiceExtended(db)
if not service.delete_filter(filter_id, token):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Filter not found")