Vœu de compagnon : être affecté avec 0 à N autres bénévoles

Nouveau vœu sur la fiche du bénévole — « je voudrais être affecté avec tel(s)
bénévole(s) » — pris en compte à la fois pour le rendre visible et par le
solveur.

Backend :
- `VolunteerCompanionWish` (`volunteer_id`, `companion_id`, à sens unique,
  `CHECK (volunteer_id <> companion_id)`) — migration 0010.
- API : `wished_companion_ids` sur `VolunteerCreate`/`Update`/`Read`, mêmes
  règles que les autres vœux (ids inconnus → 422 `volunteer.unknown` ; se
  souhaiter soi-même → 422 `volunteer.cannot_wish_self`).
- Solveur (`engine.py`) : bonus (+25 par correspondance) si un des compagnons
  souhaités du candidat est déjà affecté (décision déjà prise) au créneau —
  cumulable si plusieurs compagnons sont présents. Comme les autres termes de
  l'objectif, un bonus/malus, jamais une contrainte.
- Seed de démo : ~20 % des bénévoles ont un vœu de compagnon (couple, amis).

Frontend :
- Fiche bénévole (`ConfigVolunteers.tsx`) : sélecteur de compagnons (ajout et
  édition), avec une bulle d'aide expliquant le sens unique et l'effet sur le
  solveur — c'est ce qui « fait apparaître » le lien entre bénévoles.
- Panneau d'affectation : badge « avec N compagnon(s) souhaité(s) » sur un
  candidat, et bonus correspondant dans le classement par défaut
  (`candidateChanceScore`).
- Mock (`runSolver`) : même bonus, pour que la démonstration se comporte
  pareil que le vrai solveur.

CLAUDE.md à jour (modèle de domaine, section 4, journal).

Vérifié : 194 tests backend (dont 13 nouveaux), ruff, alembic check ;
typecheck/lint/build/smoke frontend ; solveur testé en direct sur le jeu de
démo reseedé (0,25 s, 47 suggestions).

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-15 21:50:30 +02:00
co-authored by Claude Sonnet 5
parent b97b95c606
commit 6a4bcf8782
23 changed files with 469 additions and 42 deletions
+56 -4
View File
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
import { ApiError } from '@/api';
import {
@@ -26,6 +26,7 @@ import type {
ImportReport,
PositionRead,
VolunteerCreate,
VolunteerRead,
} from '@/api/schema';
import { FieldLabel, HelpTip, TextInput } from '@/components/ui';
import { ErrorNote, Loading } from '@/components/state';
@@ -71,7 +72,21 @@ function positionOptions(positions: PositionRead[] | undefined): WishOption[] {
.map((p) => ({ id: p.id, label: p.name, hint: p.location ?? undefined }));
}
/** Sélecteur de vœux : liste déroulante + puces retirables. */
/** Les autres bénévoles, pour le vœu de compagnon (jamais soi-même). */
function volunteerOptions(
volunteers: VolunteerRead[] | undefined,
excludeId?: string,
): WishOption[] {
return [...(volunteers ?? [])]
.filter((v) => v.id !== excludeId)
.sort(
(a, b) =>
a.last_name.localeCompare(b.last_name, 'fr') ||
a.first_name.localeCompare(b.first_name, 'fr'),
)
.map((v) => ({ id: v.id, label: `${v.first_name} ${v.last_name}` }));
}
/**
* Accès de connexion (lien magique) du bénévole. Ajouter un email sur sa
* fiche ne crée aucun compte : sans ce geste explicite, `auth/magic-link` ne
@@ -144,7 +159,7 @@ function WishPicker({
emptyLabel,
addLabel,
}: {
label: string;
label: ReactNode;
options: WishOption[];
selected: string[];
onChange: (ids: string[]) => void;
@@ -214,6 +229,10 @@ export function ConfigVolunteers() {
const artistOpts = useMemo(() => artistOptions(artists.data), [artists.data]);
const positionOpts = useMemo(() => positionOptions(positions.data), [positions.data]);
const companionOpts = useMemo(
() => volunteerOptions(volunteers.data, selectedId ?? undefined),
[volunteers.data, selectedId],
);
const filtered = useMemo(() => {
const needle = search.trim().toLowerCase();
@@ -338,7 +357,10 @@ export function ConfigVolunteers() {
</span>
<span className="truncate font-mono text-[10px] opacity-70">
{t('config.vol.wishesCount', {
count: volunteer.artist_ids.length + volunteer.wished_position_ids.length,
count:
volunteer.artist_ids.length +
volunteer.wished_position_ids.length +
volunteer.wished_companion_ids.length,
})}
</span>
</button>
@@ -427,6 +449,19 @@ export function ConfigVolunteers() {
emptyLabel={t('config.vol.noPositions')}
addLabel={t('config.vol.pickPosition')}
/>
<WishPicker
label={
<>
{t('config.vol.field.companions')}{' '}
<HelpTip text={t('config.vol.companionsHint')} />
</>
}
options={companionOpts}
selected={selected.wished_companion_ids}
onChange={(ids) => patch({ wished_companion_ids: ids })}
emptyLabel={t('config.vol.noCompanions')}
addLabel={t('config.vol.pickCompanion')}
/>
<AvailabilityEditor volunteerId={selected.id} />
@@ -456,13 +491,16 @@ function AddVolunteerForm({ onClose }: { onClose: () => void }) {
const createVolunteer = useCreateVolunteer();
const artists = useArtists();
const positions = usePositions();
const volunteers = useVolunteers();
const [form, setForm] = useState<VolunteerCreate>({ first_name: '', last_name: '' });
const [artistIds, setArtistIds] = useState<string[]>([]);
const [wishedPositionIds, setWishedPositionIds] = useState<string[]>([]);
const [wishedCompanionIds, setWishedCompanionIds] = useState<string[]>([]);
const [localError, setLocalError] = useState<string | null>(null);
const artistOpts = useMemo(() => artistOptions(artists.data), [artists.data]);
const positionOpts = useMemo(() => positionOptions(positions.data), [positions.data]);
const companionOpts = useMemo(() => volunteerOptions(volunteers.data), [volunteers.data]);
const set = <K extends keyof VolunteerCreate>(key: K, value: VolunteerCreate[K]) =>
setForm((f) => ({ ...f, [key]: value }));
@@ -482,6 +520,7 @@ function AddVolunteerForm({ onClose }: { onClose: () => void }) {
comment: form.comment?.trim() || null,
artist_ids: artistIds,
wished_position_ids: wishedPositionIds,
wished_companion_ids: wishedCompanionIds,
});
onClose();
} catch (err) {
@@ -558,6 +597,19 @@ function AddVolunteerForm({ onClose }: { onClose: () => void }) {
emptyLabel={t('config.vol.noPositions')}
addLabel={t('config.vol.pickPosition')}
/>
<WishPicker
label={
<>
{t('config.vol.field.companions')}{' '}
<HelpTip text={t('config.vol.companionsHint')} />
</>
}
options={companionOpts}
selected={wishedCompanionIds}
onChange={setWishedCompanionIds}
emptyLabel={t('config.vol.noCompanions')}
addLabel={t('config.vol.pickCompanion')}
/>
{localError ? (
<div className="border-2 border-ink bg-orange p-[9px] font-display text-[11px] font-bold">