26 lines
560 B
Python
26 lines
560 B
Python
import json
|
|
from typing import Any
|
|
from sqlalchemy.orm import Session
|
|
from ..core.time_utils import utc_now
|
|
from ..models.system import AuditLog
|
|
|
|
|
|
def log_audit(
|
|
db: Session,
|
|
entity_type: str,
|
|
entity_id: int,
|
|
action: str,
|
|
changes: dict[str, Any],
|
|
user_id: int | None,
|
|
) -> AuditLog:
|
|
entry = AuditLog(
|
|
entity_type=entity_type,
|
|
entity_id=entity_id,
|
|
action=action,
|
|
changes_json=json.dumps(changes, default=str),
|
|
by_user_id=user_id,
|
|
at=utc_now(),
|
|
)
|
|
db.add(entry)
|
|
return entry
|