Give every year of a repeating period its own row

A repeating period was a single row that the planner expanded into one
window per year at read time, so no year had anywhere to differ from the
others. Editing one year rewrote them all, and removing a year in the
middle had to split the wish in two. Neither survives contact with the
thing this app is for: Easter moves, and the cheapest week is not the
same week every year.

Each year is now its own row, linked by a series id. Editing one year
touches one row; editing the series re-projects the rest from it.
Removing a year is one delete. The weekday snap is worked out once, when
the rows are made, and stored, so a row now says what it means rather
than being re-derived on every read. Recurrence stops being a stored
field and becomes what it always was in practice — an instruction at
create and update time; whether a period repeats is whether it belongs
to a series.

Migration 004 rebuilds the table and expands the existing rows, keeping
each original row as its first year so the days already planned against
it still point somewhere. A Feb 29 anchor recurring into a common year
used to raise out of the allocator; it clamps now.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ei7gCZzdd751uZtPzb1Pep
This commit is contained in:
2026-09-19 15:53:25 +02:00
parent c6593062e3
commit 6d92b87bf3
43 changed files with 2735 additions and 638 deletions
+42 -36
View File
@@ -20,7 +20,7 @@ from uuid import uuid4
from dateutil.relativedelta import relativedelta
from app.entities.leave_wish import LeaveWish
from app.entities.leave_wish import LeaveWish, project_window
from app.services.allocation_service import (
AllocationInput,
Allocator,
@@ -82,40 +82,46 @@ def _build_input() -> tuple[AllocationInput, dict]:
}
)
summer_wish = LeaveWish(
id=uuid4(),
workspace_id=uuid4(),
child_id=child.id,
name="Sommar",
window_start_month=6,
window_start_day=15,
window_end_month=7,
window_end_day=26, # 6 weeks
target_weeks=6,
recurrence="yearly",
first_year=CHILD_BIRTH_DATE.year,
last_year=None, # until the child's 4th birthday
priority=1,
parent_mode="either", # lets the allocator swap between parents
parent_personal_data_uuid=None,
)
christmas_wish = LeaveWish(
id=uuid4(),
workspace_id=summer_wish.workspace_id,
child_id=child.id,
name="Jul",
window_start_month=12,
window_start_day=20,
window_end_month=1,
window_end_day=16, # wraps into January; 28 days = 4 weeks
target_weeks=4,
recurrence="yearly",
first_year=CHILD_BIRTH_DATE.year,
last_year=None,
priority=1,
parent_mode="either",
parent_personal_data_uuid=None,
)
# A repeating wish is one row per year now (see app/entities/leave_wish.py);
# `LeaveWishService` does this materialisation for real callers, so this
# script — which builds an `AllocationInput` by hand, with no repositories —
# spells it out with the same `project_window` projection.
def _series(name: str, window: tuple[int, int, int, int], weeks: int) -> list[LeaveWish]:
start_month, start_day, end_month, end_day = window
workspace_id = uuid4()
rows: list[LeaveWish] = []
for year in range(CHILD_BIRTH_DATE.year, CHILD_BIRTH_DATE.year + 5):
start, end = project_window(
start_month=start_month,
start_day=start_day,
end_month=end_month,
end_day=end_day,
anchor_year=CHILD_BIRTH_DATE.year,
year=year,
window_mode="fixed",
)
rows.append(
LeaveWish(
id=uuid4(),
workspace_id=workspace_id,
child_id=child.id,
name=name,
window_start_month=start.month,
window_start_day=start.day,
window_end_month=end.month,
window_end_day=end.day,
target_weeks=weeks,
first_year=year,
last_year=None, # until the child's 4th birthday
priority=1,
parent_mode="either", # lets the allocator swap between parents
parent_personal_data_uuid=None,
)
)
return rows
# Sommar: 6 weeks. Jul: 4 weeks, wrapping into January.
wishes = _series("Sommar", (6, 15, 7, 26), 6) + _series("Jul", (12, 20, 1, 16), 4)
holidays_map: dict[date, str] = {}
for year in range(TODAY.year, horizon_end.year + 2):
@@ -129,7 +135,7 @@ def _build_input() -> tuple[AllocationInput, dict]:
parents=[parent_1, parent_2],
pools=pools,
vacation=vacation,
wishes=[summer_wish, christmas_wish],
wishes=wishes,
existing_planned=[],
holidays=holidays_map,
)
+1 -1
View File
@@ -135,7 +135,7 @@ def _build_services(conn: aiosqlite.Connection) -> DemoServices:
child=ChildService(children),
vacation=VacationDaysService(vacation_repo),
day_override=DayOverrideService(day_overrides),
leave_wish=LeaveWishService(leave_wishes, planned_leave),
leave_wish=LeaveWishService(leave_wishes, planned_leave, children),
manual_fetch=ManualFetchService(parental_leave),
planning=PlanningService(planned_leave, analytics, parental_leave, vacation_repo),
users=users,