feat(campaigns): add get_campaign_timeline service function
This commit is contained in:
@@ -26,6 +26,9 @@ from app.domain.errors import (
|
||||
# Import Campaign, CampaignModificationRequest, CampaignTest from app.models.campaign
|
||||
from app.models.campaign import Campaign, CampaignModificationRequest, CampaignTest
|
||||
|
||||
# Import AuditLog from app.models.audit
|
||||
from app.models.audit import AuditLog
|
||||
|
||||
# Import Technique from app.models.technique
|
||||
from app.models.technique import Technique
|
||||
|
||||
@@ -1133,3 +1136,31 @@ def list_modification_requests(
|
||||
query = query.filter(CampaignModificationRequest.status == status)
|
||||
requests = query.order_by(CampaignModificationRequest.created_at.desc()).all()
|
||||
return [serialize_modification_request(db, r) for r in requests]
|
||||
|
||||
|
||||
def get_campaign_timeline(db: Session, campaign_id: str) -> list[dict]:
|
||||
"""Return the chronological audit-log history for a campaign.
|
||||
|
||||
Raises EntityNotFoundError if the campaign does not exist.
|
||||
"""
|
||||
campaign = db.query(Campaign).filter(Campaign.id == uuid.UUID(campaign_id)).first()
|
||||
if not campaign:
|
||||
raise EntityNotFoundError("Campaign", campaign_id)
|
||||
|
||||
logs = (
|
||||
db.query(AuditLog)
|
||||
.filter(AuditLog.entity_type == "campaign", AuditLog.entity_id == str(campaign_id))
|
||||
.order_by(AuditLog.timestamp.asc())
|
||||
.all()
|
||||
)
|
||||
|
||||
return [
|
||||
{
|
||||
"id": str(log.id),
|
||||
"action": log.action,
|
||||
"user_id": str(log.user_id) if log.user_id else None,
|
||||
"timestamp": log.timestamp.isoformat() if log.timestamp else None,
|
||||
"details": log.details or {},
|
||||
}
|
||||
for log in logs
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user