Créneaux types : paramétrage + génération auto des créneaux
Le paramétrage des créneaux se fait dans « Règles & édition » : la coordination
définit des créneaux types (DayPart). À la création d'un poste, un créneau à
besoin 0/0 est généré pour chaque jour de l'édition et chaque créneau type ;
les coordinateurs ajustent ensuite min/idéal.
Back-end :
- DayPart CRUD (GET/POST/PATCH/DELETE /editions/{id}/day-parts). Code figé
après création (slugifié depuis le libellé si absent), unique par édition.
- Shift.day_part_id (nullable, SET NULL) + migration 0004.
- create_position génère les créneaux ; POST .../positions/{pid}/generate-shifts
recrée les manquants (idempotent, ne touche pas aux besoins déjà réglés).
- 13 tests (day-parts, génération, extension d'édition, ajout de type).
Front-end :
- api/hooks/mock : dayParts CRUD + positions.generateShifts.
- ConfigRules : panneau « Créneaux types » (libellé + horaires éditables,
ajout, suppression).
- ConfigGrid : bouton « Générer les créneaux » sur l'en-tête du poste.
- GrillePage : case « à définir » (gris, glyphe ·) quand min=idéal=0, avec
entrée de légende.
155 tests back OK ; typecheck + lint + build + smoke front OK.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
cee581b832
commit
f58ec00e30
@@ -0,0 +1,82 @@
|
||||
import datetime as dt
|
||||
|
||||
from sqlmodel import select
|
||||
|
||||
from app.models import Availability, AvailabilityStatus, Role, Shift
|
||||
from tests.factories import dt_at, make_position, make_shift, make_volunteer
|
||||
|
||||
|
||||
def _url(edition, suffix=""):
|
||||
return f"/editions/{edition.id}/day-parts{suffix}"
|
||||
|
||||
|
||||
def test_list_returns_the_three_fixture_types(client, edition):
|
||||
body = client.get(_url(edition)).json()
|
||||
assert [d["code"] for d in body] == ["MORNING", "AFTERNOON", "EVENING"]
|
||||
|
||||
|
||||
def test_create_slugs_the_code_from_the_label(client, edition):
|
||||
resp = client.post(
|
||||
_url(edition),
|
||||
json={"label": "Nuit profonde", "start_time": "02:00", "end_time": "06:00", "order": 3},
|
||||
)
|
||||
assert resp.status_code == 201
|
||||
assert resp.json()["code"] == "NUIT_PROFONDE"
|
||||
|
||||
|
||||
def test_create_duplicate_code_conflicts(client, edition):
|
||||
body = {
|
||||
"code": "MORNING",
|
||||
"label": "Matin bis",
|
||||
"start_time": "06:00",
|
||||
"end_time": "13:00",
|
||||
}
|
||||
resp = client.post(_url(edition), json=body)
|
||||
assert resp.status_code == 409
|
||||
assert resp.json()["detail"]["code"] == "day_part.duplicate_code"
|
||||
|
||||
|
||||
def test_update_day_part(client, edition):
|
||||
dp_id = client.get(_url(edition)).json()[0]["id"]
|
||||
resp = client.patch(
|
||||
_url(edition, f"/{dp_id}"), json={"label": "Tôt le matin", "end_time": "12:00"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["label"] == "Tôt le matin"
|
||||
assert resp.json()["end_time"] == "12:00:00"
|
||||
|
||||
|
||||
def test_delete_unlinks_shifts_and_removes_availabilities(
|
||||
client, edition, session, day_parts
|
||||
):
|
||||
morning = day_parts["MORNING"]
|
||||
position = make_position(session, edition)
|
||||
shift = make_shift(
|
||||
session, edition, position, start=dt_at(10, 6), end=dt_at(10, 13)
|
||||
)
|
||||
shift.day_part_id = morning.id
|
||||
volunteer = make_volunteer(session, edition, email="v@e.fr")
|
||||
session.add(
|
||||
Availability(
|
||||
edition_id=edition.id, volunteer_id=volunteer.id,
|
||||
date=dt.date(2026, 7, 10), day_part_id=morning.id,
|
||||
status=AvailabilityStatus.AVAILABLE,
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
|
||||
assert client.delete(_url(edition, f"/{morning.id}")).status_code == 204
|
||||
session.expire_all()
|
||||
assert session.get(Shift, shift.id).day_part_id is None
|
||||
assert session.exec(select(Availability)).all() == []
|
||||
|
||||
|
||||
def test_write_is_coordinator_only(edition, make_account, as_account):
|
||||
lead = make_account(Role.LEAD, email="lead@e.fr")
|
||||
client = as_account(lead)
|
||||
assert client.get(_url(edition)).status_code == 200
|
||||
resp = client.post(
|
||||
_url(edition),
|
||||
json={"label": "X", "start_time": "08:00", "end_time": "09:00"},
|
||||
)
|
||||
assert resp.status_code == 403
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Génération des créneaux d'un poste à partir des créneaux types."""
|
||||
|
||||
|
||||
def _positions_url(edition):
|
||||
return f"/editions/{edition.id}/positions"
|
||||
|
||||
|
||||
def _create_position(client, edition, name="Bar"):
|
||||
return client.post(
|
||||
_positions_url(edition), json={"name": name, "category": "BAR"}
|
||||
).json()
|
||||
|
||||
|
||||
def test_creating_a_position_generates_one_shift_per_day_and_type(client, edition):
|
||||
position = _create_position(client, edition)
|
||||
shifts = client.get(
|
||||
f"/editions/{edition.id}/shifts", params={"position_id": position["id"]}
|
||||
).json()
|
||||
# 4 jours (2026-07-09..12) x 3 créneaux types.
|
||||
assert len(shifts) == 12
|
||||
assert all(s["min_staff"] == 0 and s["ideal_staff"] == 0 for s in shifts)
|
||||
assert all(s["day_part_id"] is not None for s in shifts)
|
||||
|
||||
|
||||
def test_generate_is_idempotent(client, edition):
|
||||
position = _create_position(client, edition)
|
||||
resp = client.post(
|
||||
f"{_positions_url(edition)}/{position['id']}/generate-shifts"
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["created"] == 0
|
||||
|
||||
|
||||
def test_extending_the_edition_then_generating_adds_the_new_days(client, edition):
|
||||
position = _create_position(client, edition)
|
||||
client.patch(f"/editions/{edition.id}", json={"end_date": "2026-07-14"})
|
||||
|
||||
resp = client.post(
|
||||
f"{_positions_url(edition)}/{position['id']}/generate-shifts"
|
||||
)
|
||||
# 2 jours de plus x 3 créneaux types.
|
||||
assert resp.json()["created"] == 6
|
||||
shifts = client.get(
|
||||
f"/editions/{edition.id}/shifts", params={"position_id": position["id"]}
|
||||
).json()
|
||||
assert len(shifts) == 18
|
||||
|
||||
|
||||
def test_adding_a_day_part_then_generating_adds_it_everywhere(client, edition):
|
||||
position = _create_position(client, edition)
|
||||
client.post(
|
||||
f"/editions/{edition.id}/day-parts",
|
||||
json={"label": "Nuit", "start_time": "02:00", "end_time": "05:00"},
|
||||
)
|
||||
resp = client.post(
|
||||
f"{_positions_url(edition)}/{position['id']}/generate-shifts"
|
||||
)
|
||||
assert resp.json()["created"] == 4 # 1 par jour
|
||||
|
||||
|
||||
def test_generation_does_not_touch_staffing_already_set(client, edition):
|
||||
position = _create_position(client, edition)
|
||||
shift = client.get(
|
||||
f"/editions/{edition.id}/shifts", params={"position_id": position["id"]}
|
||||
).json()[0]
|
||||
client.patch(
|
||||
f"/editions/{edition.id}/shifts/{shift['id']}",
|
||||
json={"min_staff": 3, "ideal_staff": 5},
|
||||
)
|
||||
client.post(f"{_positions_url(edition)}/{position['id']}/generate-shifts")
|
||||
again = client.get(f"/editions/{edition.id}/shifts/{shift['id']}").json()
|
||||
assert again["min_staff"] == 3 and again["ideal_staff"] == 5
|
||||
Reference in New Issue
Block a user