from sqlalchemy.orm import Session from app.models.audit import AuditLog def log_action( db: Session, user_id, action: str, entity_type: str = None, entity_id: str = None, details: dict = None ): """ Log an action to the audit log. Args: db: Database session user_id: UUID of the user performing the action (can be None for system actions) action: Description of the action (e.g., "create_test", "validate_technique") entity_type: Type of entity affected (e.g., "technique", "test", "user") entity_id: ID of the entity affected details: Additional details as a dictionary (stored as JSONB) """ log = AuditLog( user_id=user_id, action=action, entity_type=entity_type, entity_id=str(entity_id) if entity_id else None, details=details, ) db.add(log) db.commit()