La vue bénévole reprenait les infos essentielles (poste, jour, horaire,
lieu) mais affichait un badge de validation factice (« TU ES ATTENDU
SUR CE CRÉNEAU ») sans plus-value, sans jamais donner le nom ni le
téléphone du référent du poste à contacter — pourtant déjà modélisés
(`Position.lead_account_id`) depuis la gestion des référents.
Un compte VOLUNTEER n'a pas accès à `positions`/`shifts` (403, cf. § 2
matrice des accès) : ces infos ne pouvaient donc pas être jointes côté
front à partir des listes existantes. Nouvel endpoint dédié et
minimal, réservé à la lecture du planning propre du bénévole :
GET /editions/{id}/volunteers/{vid}/planning
Renvoie, par affectation : poste, lieu, et référent (nom + téléphone,
`null` si le poste n'a pas de référent ou si son compte n'est pas
rattaché à une fiche bénévole) — sans exposer la liste complète des
postes/créneaux de l'édition. Tests : accès (soi-même / autre
bénévole 403 / COORD+LEAD), présence/absence du référent, tri par
heure de début.
Front : retrait du badge de validation, ajout des lignes responsable/
téléphone par créneau, et un repère horizontal « MAINTENANT » qui se
positionne dans la liste triée des créneaux selon l'heure réelle (juste
avant le prochain créneau à venir) pour situer le bénévole dans son
planning.
Non inclus dans ce commit (édités en parallèle par l'utilisateur,
laissés en l'état) : backend/app/seed.py, frontend/tailwind.config.js.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
315 lines
10 KiB
Python
315 lines
10 KiB
Python
"""CRUD des bénévoles. La suppression est un effacement complet (RGPD).
|
|
|
|
L'email n'est pas unique : plusieurs bénévoles peuvent en partager un même
|
|
(couple, famille) — voir `app/models/domain.py:Volunteer`."""
|
|
|
|
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
|
from sqlalchemy.orm import aliased
|
|
from sqlmodel import Session, select
|
|
|
|
from app.api._common import owned_or_404, replace_links
|
|
from app.api.deps import get_edition
|
|
from app.api.schemas import (
|
|
VolunteerCreate,
|
|
VolunteerPlanningEntry,
|
|
VolunteerRead,
|
|
VolunteerUpdate,
|
|
)
|
|
from app.auth.deps import (
|
|
current_account_in_edition,
|
|
require_coordinator,
|
|
require_staff,
|
|
)
|
|
from app.db import get_session
|
|
from app.models import (
|
|
Account,
|
|
Artist,
|
|
Assignment,
|
|
Edition,
|
|
Position,
|
|
Role,
|
|
Shift,
|
|
Volunteer,
|
|
VolunteerArtist,
|
|
VolunteerCompanionWish,
|
|
VolunteerPositionWish,
|
|
)
|
|
|
|
router = APIRouter(
|
|
prefix="/editions/{edition_id}/volunteers", tags=["volunteers"]
|
|
)
|
|
|
|
|
|
def _ensure_can_read(account: Account, volunteer_id: uuid.UUID) -> None:
|
|
if account.role in (Role.COORDINATOR, Role.LEAD):
|
|
return
|
|
if account.volunteer_id == volunteer_id:
|
|
return
|
|
raise HTTPException(status_code=403, detail={"code": "error.forbidden"})
|
|
|
|
|
|
def _artist_ids(session: Session, volunteer_id: uuid.UUID) -> list[uuid.UUID]:
|
|
return list(
|
|
session.exec(
|
|
select(Artist.id)
|
|
.join(VolunteerArtist, VolunteerArtist.artist_id == Artist.id)
|
|
.where(VolunteerArtist.volunteer_id == volunteer_id)
|
|
.order_by(Artist.performance_date, Artist.name)
|
|
)
|
|
)
|
|
|
|
|
|
def _wished_position_ids(
|
|
session: Session, volunteer_id: uuid.UUID
|
|
) -> list[uuid.UUID]:
|
|
return list(
|
|
session.exec(
|
|
select(Position.id)
|
|
.join(
|
|
VolunteerPositionWish,
|
|
VolunteerPositionWish.position_id == Position.id,
|
|
)
|
|
.where(VolunteerPositionWish.volunteer_id == volunteer_id)
|
|
.order_by(Position.name)
|
|
)
|
|
)
|
|
|
|
|
|
def _companion_ids(session: Session, volunteer_id: uuid.UUID) -> list[uuid.UUID]:
|
|
return list(
|
|
session.exec(
|
|
select(Volunteer.id)
|
|
.join(
|
|
VolunteerCompanionWish,
|
|
VolunteerCompanionWish.companion_id == Volunteer.id,
|
|
)
|
|
.where(VolunteerCompanionWish.volunteer_id == volunteer_id)
|
|
.order_by(Volunteer.last_name, Volunteer.first_name)
|
|
)
|
|
)
|
|
|
|
|
|
def _read(session: Session, volunteer: Volunteer) -> VolunteerRead:
|
|
return VolunteerRead(
|
|
**volunteer.model_dump(),
|
|
artist_ids=_artist_ids(session, volunteer.id),
|
|
wished_position_ids=_wished_position_ids(session, volunteer.id),
|
|
wished_companion_ids=_companion_ids(session, volunteer.id),
|
|
)
|
|
|
|
|
|
def _resolve_owned_ids(
|
|
session: Session, model, edition_id: uuid.UUID, ids: list[uuid.UUID], code: str
|
|
) -> list[uuid.UUID]:
|
|
"""Ordre d'entrée conservé, doublons retirés ; 422 si un id est étranger."""
|
|
wanted = list(dict.fromkeys(ids))
|
|
if wanted:
|
|
found = set(
|
|
session.exec(
|
|
select(model.id).where(
|
|
model.edition_id == edition_id,
|
|
model.id.in_(wanted),
|
|
)
|
|
)
|
|
)
|
|
if len(found) != len(wanted):
|
|
raise HTTPException(status_code=422, detail={"code": code})
|
|
return wanted
|
|
|
|
|
|
def _set_artists(
|
|
session: Session, volunteer: Volunteer, artist_ids: list[uuid.UUID]
|
|
) -> None:
|
|
ids = _resolve_owned_ids(
|
|
session, Artist, volunteer.edition_id, artist_ids, "artist.unknown"
|
|
)
|
|
replace_links(
|
|
session,
|
|
VolunteerArtist,
|
|
filter_column=VolunteerArtist.volunteer_id,
|
|
filter_value=volunteer.id,
|
|
pairs=[
|
|
{"volunteer_id": volunteer.id, "artist_id": aid} for aid in ids
|
|
],
|
|
)
|
|
|
|
|
|
def _set_wished_positions(
|
|
session: Session, volunteer: Volunteer, position_ids: list[uuid.UUID]
|
|
) -> None:
|
|
ids = _resolve_owned_ids(
|
|
session,
|
|
Position,
|
|
volunteer.edition_id,
|
|
position_ids,
|
|
"position.unknown",
|
|
)
|
|
replace_links(
|
|
session,
|
|
VolunteerPositionWish,
|
|
filter_column=VolunteerPositionWish.volunteer_id,
|
|
filter_value=volunteer.id,
|
|
pairs=[
|
|
{"volunteer_id": volunteer.id, "position_id": pid} for pid in ids
|
|
],
|
|
)
|
|
|
|
|
|
def _set_companions(
|
|
session: Session, volunteer: Volunteer, companion_ids: list[uuid.UUID]
|
|
) -> None:
|
|
ids = _resolve_owned_ids(
|
|
session, Volunteer, volunteer.edition_id, companion_ids, "volunteer.unknown"
|
|
)
|
|
if volunteer.id in ids:
|
|
raise HTTPException(
|
|
status_code=422, detail={"code": "volunteer.cannot_wish_self"}
|
|
)
|
|
replace_links(
|
|
session,
|
|
VolunteerCompanionWish,
|
|
filter_column=VolunteerCompanionWish.volunteer_id,
|
|
filter_value=volunteer.id,
|
|
pairs=[
|
|
{"volunteer_id": volunteer.id, "companion_id": cid} for cid in ids
|
|
],
|
|
)
|
|
|
|
|
|
@router.post("", response_model=VolunteerRead, status_code=status.HTTP_201_CREATED)
|
|
def create_volunteer(
|
|
payload: VolunteerCreate,
|
|
edition: Edition = Depends(get_edition),
|
|
_guard: Account = Depends(require_coordinator),
|
|
session: Session = Depends(get_session),
|
|
) -> VolunteerRead:
|
|
payload.first_name = payload.first_name.title()
|
|
payload.last_name = payload.last_name.title()
|
|
volunteer = Volunteer(
|
|
edition_id=edition.id,
|
|
**payload.model_dump(
|
|
exclude={"artist_ids", "wished_position_ids", "wished_companion_ids"}
|
|
),
|
|
)
|
|
session.add(volunteer)
|
|
session.flush()
|
|
_set_artists(session, volunteer, payload.artist_ids)
|
|
_set_wished_positions(session, volunteer, payload.wished_position_ids)
|
|
_set_companions(session, volunteer, payload.wished_companion_ids)
|
|
session.commit()
|
|
session.refresh(volunteer)
|
|
return _read(session, volunteer)
|
|
|
|
|
|
@router.get("", response_model=list[VolunteerRead])
|
|
def list_volunteers(
|
|
edition: Edition = Depends(get_edition),
|
|
_guard: Account = Depends(require_staff),
|
|
session: Session = Depends(get_session),
|
|
) -> list[VolunteerRead]:
|
|
volunteers = session.exec(
|
|
select(Volunteer)
|
|
.where(Volunteer.edition_id == edition.id)
|
|
.order_by(Volunteer.last_name, Volunteer.first_name)
|
|
)
|
|
return [_read(session, v) for v in volunteers]
|
|
|
|
|
|
@router.get("/{volunteer_id}", response_model=VolunteerRead)
|
|
def get_volunteer(
|
|
volunteer_id: uuid.UUID,
|
|
edition: Edition = Depends(get_edition),
|
|
account: Account = Depends(current_account_in_edition),
|
|
session: Session = Depends(get_session),
|
|
) -> VolunteerRead:
|
|
_ensure_can_read(account, volunteer_id)
|
|
volunteer = owned_or_404(session.get(Volunteer, volunteer_id), edition.id)
|
|
return _read(session, volunteer)
|
|
|
|
|
|
@router.get("/{volunteer_id}/planning", response_model=list[VolunteerPlanningEntry])
|
|
def get_volunteer_planning(
|
|
volunteer_id: uuid.UUID,
|
|
edition: Edition = Depends(get_edition),
|
|
account: Account = Depends(current_account_in_edition),
|
|
session: Session = Depends(get_session),
|
|
) -> list[VolunteerPlanningEntry]:
|
|
"""Planning enrichi (poste, lieu, référent + téléphone à contacter) d'un
|
|
bénévole — permet à l'écran bénévole (`/mobile-benevole`) d'afficher ces
|
|
infos sans jamais donner à un compte VOLUNTEER un accès en lecture aux
|
|
postes/créneaux de toute l'édition (réservé à COORDINATOR/LEAD)."""
|
|
_ensure_can_read(account, volunteer_id)
|
|
owned_or_404(session.get(Volunteer, volunteer_id), edition.id)
|
|
|
|
Referent = aliased(Volunteer)
|
|
rows = session.exec(
|
|
select(Assignment, Shift, Position, Referent)
|
|
.join(Shift, Shift.id == Assignment.shift_id)
|
|
.join(Position, Position.id == Shift.position_id)
|
|
.join(Account, Account.id == Position.lead_account_id, isouter=True)
|
|
.join(Referent, Referent.id == Account.volunteer_id, isouter=True)
|
|
.where(
|
|
Assignment.volunteer_id == volunteer_id,
|
|
Assignment.edition_id == edition.id,
|
|
)
|
|
.order_by(Shift.starts_at)
|
|
).all()
|
|
|
|
return [
|
|
VolunteerPlanningEntry(
|
|
assignment_id=a.id,
|
|
shift_id=s.id,
|
|
starts_at=s.starts_at,
|
|
ends_at=s.ends_at,
|
|
position_name=p.name,
|
|
position_location=p.location,
|
|
referent_name=f"{r.first_name} {r.last_name}" if r else None,
|
|
referent_phone=r.phone if r else None,
|
|
)
|
|
for a, s, p, r in rows
|
|
]
|
|
|
|
|
|
@router.patch("/{volunteer_id}", response_model=VolunteerRead)
|
|
def update_volunteer(
|
|
volunteer_id: uuid.UUID,
|
|
payload: VolunteerUpdate,
|
|
edition: Edition = Depends(get_edition),
|
|
_guard: Account = Depends(require_coordinator),
|
|
session: Session = Depends(get_session),
|
|
) -> VolunteerRead:
|
|
volunteer = owned_or_404(session.get(Volunteer, volunteer_id), edition.id)
|
|
changes = payload.model_dump(exclude_unset=True)
|
|
artist_ids = changes.pop("artist_ids", None)
|
|
wished_position_ids = changes.pop("wished_position_ids", None)
|
|
wished_companion_ids = changes.pop("wished_companion_ids", None)
|
|
for field, value in changes.items():
|
|
setattr(volunteer, field, value)
|
|
session.flush()
|
|
if artist_ids is not None:
|
|
_set_artists(session, volunteer, artist_ids)
|
|
if wished_position_ids is not None:
|
|
_set_wished_positions(session, volunteer, wished_position_ids)
|
|
if wished_companion_ids is not None:
|
|
_set_companions(session, volunteer, wished_companion_ids)
|
|
session.commit()
|
|
session.refresh(volunteer)
|
|
return _read(session, volunteer)
|
|
|
|
|
|
@router.delete("/{volunteer_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_volunteer(
|
|
volunteer_id: uuid.UUID,
|
|
edition: Edition = Depends(get_edition),
|
|
_guard: Account = Depends(require_coordinator),
|
|
session: Session = Depends(get_session),
|
|
) -> Response:
|
|
volunteer = owned_or_404(session.get(Volunteer, volunteer_id), edition.id)
|
|
# Effacement complet : la cascade base supprime disponibilités,
|
|
# affectations, vœux et compte éventuel.
|
|
session.delete(volunteer)
|
|
session.commit()
|
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|