"""Routes postes et affectations.""" from datetime import date from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from app.core.database import get_db from app.core.security import get_current_membre, require_executif from app.models.poste import Poste from app.models.affectation import Affectation from app.schemas.schemas import PosteOut, AffectationOut, AffectationCreate, TerminerMandatRequest router = APIRouter(prefix="/postes", tags=["Postes et affectations"]) @router.get("/", response_model=list[PosteOut]) async def liste_postes(db: AsyncSession = Depends(get_db), _=Depends(get_current_membre)): result = await db.execute(select(Poste).order_by(Poste.ordre_affichage)) return [PosteOut.model_validate(p) for p in result.scalars().all()] @router.get("/affectations") async def mes_affectations(membre=Depends(get_current_membre), db: AsyncSession = Depends(get_db)): """Retourne les affectations actives du membre connecté.""" result = await db.execute( select(Affectation).join(Poste).where( Affectation.membre_id == membre.id, Affectation.statut == "actif", ) ) affectations = result.scalars().all() out = [] for a in affectations: poste = await db.get(Poste, a.poste_id) out.append({ "id": str(a.id), "poste_nom": poste.nom if poste else "?", "poste_categorie": poste.categorie if poste else "?", "date_debut": str(a.date_debut), "statut": a.statut, }) return out @router.get("/affectations/groupe") async def affectations_groupe( executif=Depends(require_executif), db: AsyncSession = Depends(get_db), ): """G3 — Toutes les affectations actives du groupe.""" result = await db.execute( select(Affectation).where( Affectation.groupe_id == executif.groupe_id, Affectation.statut == "actif", ) ) affectations = result.scalars().all() out = [] for a in affectations: from app.models.membre import Membre poste = await db.get(Poste, a.poste_id) mbr = await db.get(Membre, a.membre_id) out.append({ "id": str(a.id), "membre_prenom": mbr.prenom if mbr else "?", "poste_nom": poste.nom if poste else "?", "poste_categorie": poste.categorie if poste else "?", "date_debut": str(a.date_debut), }) return out @router.post("/affectations") async def creer_affectation( req: AffectationCreate, executif=Depends(require_executif), db: AsyncSession = Depends(get_db), ): """G3 — Affecter un membre à un poste.""" affectation = Affectation( groupe_id=executif.groupe_id, membre_id=req.membre_id, poste_id=req.poste_id, date_debut=req.date_debut, statut="actif", ) db.add(affectation) await db.flush() return {"message": "Affectation créée", "id": str(affectation.id)} @router.post("/affectations/{affectation_id}/terminer") async def terminer_mandat( affectation_id: str, req: TerminerMandatRequest, executif=Depends(require_executif), db: AsyncSession = Depends(get_db), ): """G4 — Terminer un mandat manuellement.""" result = await db.execute( select(Affectation).where( Affectation.id == affectation_id, Affectation.groupe_id == executif.groupe_id, Affectation.statut == "actif", ) ) affectation = result.scalar_one_or_none() if not affectation: raise HTTPException(status_code=404, detail="Affectation introuvable") affectation.statut = "termine" affectation.date_fin = date.today() affectation.note_transition = req.note_transition affectation.termine_par = executif.id await db.flush() return {"message": "Mandat terminé"}