
Features: - User authentication with JWT - Client management with CRUD operations - Invoice generation and management - SQLite database with Alembic migrations - Detailed project documentation
53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.logging import app_logger
|
|
from app.models.activity import Activity
|
|
|
|
|
|
def log_activity(
|
|
db: Session,
|
|
user_id: int,
|
|
action: str,
|
|
entity_type: str,
|
|
entity_id: Optional[int] = None,
|
|
details: Optional[str] = None,
|
|
) -> Activity:
|
|
"""
|
|
Log an activity performed by a user
|
|
|
|
Parameters:
|
|
- db: Database session
|
|
- user_id: ID of the user performing the action
|
|
- action: Type of action (e.g., "create", "update", "delete", "view")
|
|
- entity_type: Type of entity being acted on (e.g., "client", "invoice")
|
|
- entity_id: ID of the entity being acted on (optional)
|
|
- details: Additional details about the activity (optional)
|
|
"""
|
|
try:
|
|
activity = Activity(
|
|
user_id=user_id,
|
|
action=action,
|
|
entity_type=entity_type,
|
|
entity_id=entity_id,
|
|
details=details,
|
|
timestamp=datetime.utcnow()
|
|
)
|
|
|
|
db.add(activity)
|
|
db.commit()
|
|
db.refresh(activity)
|
|
|
|
app_logger.info(
|
|
f"Activity logged: user_id={user_id}, action={action}, "
|
|
f"entity_type={entity_type}, entity_id={entity_id}"
|
|
)
|
|
|
|
return activity
|
|
|
|
except Exception as e:
|
|
app_logger.error(f"Error logging activity: {str(e)}")
|
|
db.rollback()
|
|
raise |