30 lines
682 B
Python
30 lines
682 B
Python
import json
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.audit_log import AuditLog
|
|
|
|
|
|
async def log_action(
|
|
db: AsyncSession,
|
|
operator_id: int,
|
|
action: str,
|
|
target_type: str,
|
|
target_id: int | None = None,
|
|
detail: dict | str | None = None,
|
|
) -> AuditLog:
|
|
detail_str = None
|
|
if detail is not None:
|
|
detail_str = json.dumps(detail, ensure_ascii=False) if isinstance(detail, dict) else str(detail)
|
|
|
|
entry = AuditLog(
|
|
operator_id=operator_id,
|
|
action=action,
|
|
target_type=target_type,
|
|
target_id=target_id,
|
|
detail=detail_str,
|
|
)
|
|
db.add(entry)
|
|
await db.flush()
|
|
return entry
|