"""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 ( Assignment, Edition, Shift, Volunteer, ) from app.validation.models import ( ShiftInfo, ValidationInput, ) def build_validation_input( *, session: Session, edition: Edition, volunteer: Volunteer, shift: Shift, exclude_assignment_id: uuid.UUID | None = None, ) -> ValidationInput: # Toute affectation existante du bénévole compte (une affectation = une # décision, toujours active). stmt = ( select(Shift) .join(Assignment, Assignment.shift_id == Shift.id) .where(Assignment.volunteer_id == volunteer.id) .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) ) return ValidationInput( candidate_shift=ShiftInfo( id=str(shift.id), starts_at=shift.starts_at, ends_at=shift.ends_at, ), existing_shifts=existing_shifts, )