all working
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
Document Management API endpoints - QDROs, Templates, and General Documents
|
||||
"""
|
||||
from typing import List, Optional, Dict, Any
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query, UploadFile, File, Form
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query, UploadFile, File, Form, Request
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from sqlalchemy import or_, func, and_, desc, asc, text
|
||||
from datetime import date, datetime
|
||||
@@ -18,6 +18,8 @@ from app.models.lookups import FormIndex, FormList, Footer, Employee
|
||||
from app.models.user import User
|
||||
from app.auth.security import get_current_user
|
||||
from app.models.additional import Document
|
||||
from app.core.logging import get_logger
|
||||
from app.services.audit import audit_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -666,6 +668,78 @@ def _merge_template_variables(content: str, variables: Dict[str, Any]) -> str:
|
||||
return merged
|
||||
|
||||
|
||||
# --- Client Error Logging (for Documents page) ---
|
||||
class ClientErrorLog(BaseModel):
|
||||
"""Payload for client-side error logging"""
|
||||
message: str
|
||||
action: Optional[str] = None
|
||||
stack: Optional[str] = None
|
||||
url: Optional[str] = None
|
||||
line: Optional[int] = None
|
||||
column: Optional[int] = None
|
||||
user_agent: Optional[str] = None
|
||||
extra: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
@router.post("/client-error")
|
||||
async def log_client_error(
|
||||
payload: ClientErrorLog,
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Optional[User] = Depends(lambda: None)
|
||||
):
|
||||
"""Accept client-side error logs from the Documents page.
|
||||
|
||||
This endpoint is lightweight and safe to call; it records the error to the
|
||||
application logs and best-effort to the audit log without interrupting the UI.
|
||||
"""
|
||||
logger = get_logger("client.documents")
|
||||
client_ip = request.headers.get("x-forwarded-for")
|
||||
if client_ip:
|
||||
client_ip = client_ip.split(",")[0].strip()
|
||||
else:
|
||||
client_ip = request.client.host if request.client else None
|
||||
|
||||
logger.error(
|
||||
"Client error reported",
|
||||
action=payload.action,
|
||||
message=payload.message,
|
||||
stack=payload.stack,
|
||||
page="/documents",
|
||||
url=payload.url or str(request.url),
|
||||
line=payload.line,
|
||||
column=payload.column,
|
||||
user=getattr(current_user, "username", None),
|
||||
user_id=getattr(current_user, "id", None),
|
||||
user_agent=payload.user_agent or request.headers.get("user-agent"),
|
||||
client_ip=client_ip,
|
||||
extra=payload.extra,
|
||||
)
|
||||
|
||||
# Best-effort audit log; do not raise on failure
|
||||
try:
|
||||
audit_service.log_action(
|
||||
db=db,
|
||||
action="CLIENT_ERROR",
|
||||
resource_type="DOCUMENTS",
|
||||
user=current_user,
|
||||
resource_id=None,
|
||||
details={
|
||||
"action": payload.action,
|
||||
"message": payload.message,
|
||||
"url": payload.url or str(request.url),
|
||||
"line": payload.line,
|
||||
"column": payload.column,
|
||||
"extra": payload.extra,
|
||||
},
|
||||
request=request,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {"status": "logged"}
|
||||
|
||||
|
||||
@router.post("/upload/{file_no}")
|
||||
async def upload_document(
|
||||
file_no: str,
|
||||
|
||||
Reference in New Issue
Block a user