2026-05-25 17:39:38 -04:00
|
|
|
"""Consultation du journal d'audit."""
|
2026-05-31 11:06:53 -04:00
|
|
|
from datetime import date, datetime, time, timezone
|
|
|
|
|
|
2026-05-25 17:39:38 -04:00
|
|
|
from fastapi import APIRouter, Depends
|
2026-05-31 11:06:53 -04:00
|
|
|
from sqlalchemy import or_, select
|
2026-05-25 17:39:38 -04:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.database import get_db
|
|
|
|
|
from app.core.security import require_executif
|
|
|
|
|
from app.models.journal_action import JournalAction
|
2026-05-31 10:59:41 -04:00
|
|
|
from app.models.historique_modification import HistoriqueModification
|
|
|
|
|
from app.models.membre import Membre
|
2026-05-25 17:39:38 -04:00
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/journal", tags=["Journal"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/actions")
|
|
|
|
|
async def journal_actions(
|
|
|
|
|
limite: int = 100,
|
|
|
|
|
executif=Depends(require_executif),
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
):
|
|
|
|
|
limite = max(1, min(limite, 500))
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
select(JournalAction)
|
|
|
|
|
.where(JournalAction.groupe_id == executif.groupe_id)
|
|
|
|
|
.order_by(JournalAction.cree_le.desc())
|
|
|
|
|
.limit(limite)
|
|
|
|
|
)
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"id": str(item.id),
|
|
|
|
|
"groupe_id": str(item.groupe_id) if item.groupe_id else None,
|
|
|
|
|
"membre_id": str(item.membre_id) if item.membre_id else None,
|
|
|
|
|
"admin_id": str(item.admin_id) if item.admin_id else None,
|
|
|
|
|
"methode": item.methode,
|
|
|
|
|
"chemin": item.chemin,
|
|
|
|
|
"statut_http": item.statut_http,
|
|
|
|
|
"adresse_ip": item.adresse_ip,
|
|
|
|
|
"user_agent": item.user_agent,
|
|
|
|
|
"details": item.details,
|
|
|
|
|
"cree_le": item.cree_le.isoformat(),
|
|
|
|
|
}
|
|
|
|
|
for item in result.scalars().all()
|
|
|
|
|
]
|
2026-05-31 10:59:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/historique")
|
|
|
|
|
async def historique_modifications(
|
|
|
|
|
limite: int = 100,
|
2026-05-31 11:06:53 -04:00
|
|
|
date_debut: date | None = None,
|
|
|
|
|
date_fin: date | None = None,
|
|
|
|
|
membre_id: str | None = None,
|
|
|
|
|
table_cible: str | None = None,
|
|
|
|
|
action: str | None = None,
|
|
|
|
|
q: str | None = None,
|
2026-05-31 10:59:41 -04:00
|
|
|
executif=Depends(require_executif),
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
):
|
|
|
|
|
limite = max(1, min(limite, 500))
|
2026-05-31 11:06:53 -04:00
|
|
|
query = select(HistoriqueModification).where(HistoriqueModification.groupe_id == executif.groupe_id)
|
|
|
|
|
if date_debut:
|
|
|
|
|
query = query.where(HistoriqueModification.cree_le >= datetime.combine(date_debut, time.min, tzinfo=timezone.utc))
|
|
|
|
|
if date_fin:
|
|
|
|
|
query = query.where(HistoriqueModification.cree_le <= datetime.combine(date_fin, time.max, tzinfo=timezone.utc))
|
|
|
|
|
if membre_id:
|
|
|
|
|
query = query.where(HistoriqueModification.membre_id == membre_id)
|
|
|
|
|
if table_cible:
|
|
|
|
|
query = query.where(HistoriqueModification.table_cible == table_cible)
|
|
|
|
|
if action:
|
|
|
|
|
query = query.where(HistoriqueModification.action == action)
|
|
|
|
|
if q:
|
|
|
|
|
terme = f"%{q.strip()}%"
|
|
|
|
|
query = query.where(or_(
|
|
|
|
|
HistoriqueModification.raison.ilike(terme),
|
|
|
|
|
HistoriqueModification.action.ilike(terme),
|
|
|
|
|
HistoriqueModification.table_cible.ilike(terme),
|
|
|
|
|
))
|
2026-05-31 10:59:41 -04:00
|
|
|
result = await db.execute(
|
2026-05-31 11:06:53 -04:00
|
|
|
query
|
2026-05-31 10:59:41 -04:00
|
|
|
.order_by(HistoriqueModification.cree_le.desc())
|
|
|
|
|
.limit(limite)
|
|
|
|
|
)
|
|
|
|
|
items = result.scalars().all()
|
|
|
|
|
lignes = []
|
|
|
|
|
for item in items:
|
|
|
|
|
membre = await db.get(Membre, item.membre_id) if item.membre_id else None
|
|
|
|
|
lignes.append({
|
|
|
|
|
"id": str(item.id),
|
|
|
|
|
"table_cible": item.table_cible,
|
|
|
|
|
"objet_id": str(item.objet_id),
|
|
|
|
|
"action": item.action,
|
|
|
|
|
"raison": item.raison,
|
|
|
|
|
"membre_id": str(item.membre_id) if item.membre_id else None,
|
|
|
|
|
"membre_prenom": membre.prenom if membre else None,
|
|
|
|
|
"admin_id": str(item.admin_id) if item.admin_id else None,
|
|
|
|
|
"chemin_api": item.chemin_api,
|
|
|
|
|
"avant": item.avant,
|
|
|
|
|
"apres": item.apres,
|
|
|
|
|
"cree_le": item.cree_le.isoformat(),
|
|
|
|
|
})
|
|
|
|
|
return lignes
|