Étape 5 : API CRUD, matrice et validation branchée

- CRUD complet, scopé par édition, sur Position, Shift, Volunteer, Assignment
  (routers app/api/*, schémas Pydantic, dépendance get_edition partagée)
- Volunteer : gestion des compétences par codes (création à la volée),
  suppression = effacement complet en cascade (RGPD)
- Shift : contrôle fin > début, poste de la même édition, compétences requises
- Assignment : toute écriture active passe par le moteur de validation ;
  violation bloquante -> 422 error.validation_failed, avertissements renvoyés
  dans la réponse. Endpoint POST .../assignments/validate en simulation.
  app/validation/loader.py construit la ValidationInput depuis la base.
- GET /editions/{id}/matrix : postes -> créneaux -> {min, ideal, confirmed,
  proposed}, source de l'écran principal
- 30 tests d'API (CRUD, 404/409/422, cascade, blocage, avertissements, matrice,
  loader) ; 104 tests au total ; alembic check toujours vert
- Clés de traduction shift.* / assignment.* / volunteer.* ajoutées

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
This commit is contained in:
AdamThomas
2026-09-09 22:54:22 +02:00
co-authored by Claude Sonnet 5
parent c96e85af37
commit 74e35f0195
20 changed files with 1775 additions and 8 deletions
+111
View File
@@ -0,0 +1,111 @@
"""Construit une `ValidationInput` depuis la base pour un couple (bénévole,
créneau). Couche de liaison : c'est le seul endroit du module `validation` qui
connaît la base."""
import uuid
from sqlmodel import Session, select
from app.models import (
ACTIVE_ASSIGNMENT_STATUSES,
Assignment,
Availability,
DayPart,
Edition,
Shift,
ShiftRequiredSkill,
Skill,
Volunteer,
VolunteerSkill,
)
from app.validation.models import (
AvailabilityDeclaration,
DayPartWindow,
ShiftInfo,
ValidationInput,
)
def _volunteer_skill_codes(
session: Session, volunteer_id: uuid.UUID
) -> frozenset[str]:
return frozenset(
session.exec(
select(Skill.code)
.join(VolunteerSkill, VolunteerSkill.skill_id == Skill.id)
.where(VolunteerSkill.volunteer_id == volunteer_id)
)
)
def _shift_required_skill_codes(
session: Session, shift_id: uuid.UUID
) -> frozenset[str]:
return frozenset(
session.exec(
select(Skill.code)
.join(
ShiftRequiredSkill, ShiftRequiredSkill.skill_id == Skill.id
)
.where(ShiftRequiredSkill.shift_id == shift_id)
)
)
def build_validation_input(
*,
session: Session,
edition: Edition,
volunteer: Volunteer,
shift: Shift,
exclude_assignment_id: uuid.UUID | None = None,
) -> ValidationInput:
day_parts = list(
session.exec(select(DayPart).where(DayPart.edition_id == edition.id))
)
code_by_day_part_id = {dp.id: dp.code for dp in day_parts}
stmt = (
select(Shift)
.join(Assignment, Assignment.shift_id == Shift.id)
.where(Assignment.volunteer_id == volunteer.id)
.where(
Assignment.status.in_(tuple(ACTIVE_ASSIGNMENT_STATUSES))
)
.where(Shift.id != shift.id)
)
if exclude_assignment_id is not None:
stmt = stmt.where(Assignment.id != exclude_assignment_id)
existing_shifts = tuple(
ShiftInfo(id=str(s.id), starts_at=s.starts_at, ends_at=s.ends_at)
for s in session.exec(stmt)
)
declarations = tuple(
AvailabilityDeclaration(
date=a.date,
day_part_code=code_by_day_part_id.get(a.day_part_id, ""),
status=a.status,
)
for a in session.exec(
select(Availability).where(
Availability.volunteer_id == volunteer.id
)
)
)
return ValidationInput(
candidate_shift=ShiftInfo(
id=str(shift.id),
starts_at=shift.starts_at,
ends_at=shift.ends_at,
required_skill_codes=_shift_required_skill_codes(session, shift.id),
),
volunteer_skill_codes=_volunteer_skill_codes(session, volunteer.id),
existing_shifts=existing_shifts,
availabilities=declarations,
day_parts=tuple(
DayPartWindow(dp.code, dp.start_time, dp.end_time, dp.order)
for dp in day_parts
),
)