Link planned days to their wish, fix the age-4 cap, drop the planning goal

Three changes that together unblock treating a calendar period as one
editable object.

Migration 008 adds `planned_leave_day.wish_id` (nullable, ON DELETE SET NULL)
so a persisted day knows which `leave_wish` produced it, `is_pinned` to mark a
day the user hand-edited, and `leave_wish.target_days` so a period drawn on a
calendar need not be a whole number of weeks. Apply-time identity still keys on
day_type/is_dubbeldag only, so a wish_id-only difference is a metadata update
rather than a collision.

The age-4 savable ceiling was applying the per-child figure of 96 to each
parent's pool, modelling a two-parent family as able to save 192 days past the
child's fourth birthday. `resolve_age4_savable_cap` is now the single place
that decides 48 (couple with two parents) versus 96 (everyone else), and both
AnalyticsService and the allocator read it.

Migration 009 drops `workspace.planning_goal` and `leave_wish.goal_override`.
The allocator's tier-3 policy collapses to one rule; `salary_type` still
branches, since that reflects what a vacation day actually costs a parent
rather than a user preference.
This commit is contained in:
2026-09-15 20:28:06 +02:00
parent 7f516f2cab
commit cc3ced905e
59 changed files with 1427 additions and 740 deletions
+26 -23
View File
@@ -1,9 +1,8 @@
"""Business logic for leave wishes (family recurring home-together intent).
See PLANNING_ENGINE_SPEC.md §2.1. This service owns the validation the
allocator (Phase B) depends on: valid recurrence/parent_mode/goal_override,
sane month/day ranges, and the parent_mode <-> parent_personal_data_uuid
pairing rule.
allocator (Phase B) depends on: valid recurrence/parent_mode, sane month/day
ranges, and the parent_mode <-> parent_personal_data_uuid pairing rule.
"""
from datetime import date, datetime
@@ -15,7 +14,6 @@ from app.utils.errors import NotFoundError, ValidationError
_VALID_RECURRENCE = {"yearly", "once"}
_VALID_PARENT_MODE = {"either", "both", "specific"}
_VALID_GOAL_OVERRIDE = {"max_time", "max_money", "balanced"}
_VALID_WINDOW_MODE = {"fixed", "flexible"}
# A leap year, so day=29 for month=2 (Feb 29) is accepted when validating ranges.
@@ -38,12 +36,12 @@ class LeaveWishService:
first_year: int,
child_id: UUID | None = None,
name_message_key: str | None = None,
target_days: int | None = None,
recurrence: str = "once",
last_year: int | None = None,
priority: int = 1,
parent_mode: str = "either",
parent_personal_data_uuid: UUID | None = None,
goal_override: str | None = None,
window_mode: str = "fixed",
) -> LeaveWish:
_validate(
@@ -52,12 +50,12 @@ class LeaveWishService:
window_end_month=window_end_month,
window_end_day=window_end_day,
target_weeks=target_weeks,
target_days=target_days,
recurrence=recurrence,
first_year=first_year,
last_year=last_year,
parent_mode=parent_mode,
parent_personal_data_uuid=parent_personal_data_uuid,
goal_override=goal_override,
window_mode=window_mode,
)
now = datetime.now()
@@ -72,13 +70,13 @@ class LeaveWishService:
window_end_month=window_end_month,
window_end_day=window_end_day,
target_weeks=target_weeks,
target_days=target_days,
recurrence=recurrence,
first_year=first_year,
last_year=last_year,
priority=priority,
parent_mode=parent_mode,
parent_personal_data_uuid=parent_personal_data_uuid,
goal_override=goal_override,
window_mode=window_mode,
created_at=now,
updated_at=now,
@@ -105,19 +103,19 @@ class LeaveWishService:
window_end_month: int | None = None,
window_end_day: int | None = None,
target_weeks: int | None = None,
target_days: int | None = None,
recurrence: str | None = None,
first_year: int | None = None,
last_year: int | None = None,
priority: int | None = None,
parent_mode: str | None = None,
parent_personal_data_uuid: UUID | None = None,
goal_override: str | None = None,
window_mode: str | None = None,
_clear_child_id: bool = False,
_clear_last_year: bool = False,
_clear_parent_personal_data_uuid: bool = False,
_clear_goal_override: bool = False,
_clear_name_message_key: bool = False,
_clear_target_days: bool = False,
) -> LeaveWish:
"""Partial update — only non-None fields are applied.
@@ -130,6 +128,9 @@ class LeaveWishService:
regardless of what was passed for it, since a caller (frontend bug,
future API client, ...) forgetting to clear it would silently make a
user's edited name revert to a stale preset label.
`_clear_target_days` resets a day-granular wish back to being sized
by `target_weeks` alone.
"""
entity = await self.get(leave_wish_id)
original_name = entity.name
@@ -156,6 +157,10 @@ class LeaveWishService:
entity.window_end_day = window_end_day
if target_weeks is not None:
entity.target_weeks = target_weeks
if _clear_target_days:
entity.target_days = None
elif target_days is not None:
entity.target_days = target_days
if recurrence is not None:
entity.recurrence = recurrence
if first_year is not None:
@@ -172,10 +177,6 @@ class LeaveWishService:
entity.parent_personal_data_uuid = None
elif parent_personal_data_uuid is not None:
entity.parent_personal_data_uuid = parent_personal_data_uuid
if _clear_goal_override:
entity.goal_override = None
elif goal_override is not None:
entity.goal_override = goal_override
if window_mode is not None:
entity.window_mode = window_mode
@@ -185,12 +186,12 @@ class LeaveWishService:
window_end_month=entity.window_end_month,
window_end_day=entity.window_end_day,
target_weeks=entity.target_weeks,
target_days=entity.target_days,
recurrence=entity.recurrence,
first_year=entity.first_year,
last_year=entity.last_year,
parent_mode=entity.parent_mode,
parent_personal_data_uuid=entity.parent_personal_data_uuid,
goal_override=entity.goal_override,
window_mode=entity.window_mode,
)
return await self._repo.update(leave_wish_id, entity)
@@ -207,20 +208,18 @@ def _validate(
window_end_month: int,
window_end_day: int,
target_weeks: int,
target_days: int | None,
recurrence: str,
first_year: int,
last_year: int | None,
parent_mode: str,
parent_personal_data_uuid: UUID | None,
goal_override: str | None,
window_mode: str,
) -> None:
if recurrence not in _VALID_RECURRENCE:
raise ValidationError(f"recurrence must be one of {_VALID_RECURRENCE}")
if parent_mode not in _VALID_PARENT_MODE:
raise ValidationError(f"parent_mode must be one of {_VALID_PARENT_MODE}")
if goal_override is not None and goal_override not in _VALID_GOAL_OVERRIDE:
raise ValidationError(f"goal_override must be one of {_VALID_GOAL_OVERRIDE} or None")
if window_mode not in _VALID_WINDOW_MODE:
raise ValidationError(f"window_mode must be one of {_VALID_WINDOW_MODE}")
@@ -229,6 +228,8 @@ def _validate(
if target_weeks <= 0:
raise ValidationError("target_weeks must be > 0")
if target_days is not None and target_days < 1:
raise ValidationError("target_days must be >= 1")
if last_year is not None and last_year < first_year:
raise ValidationError("last_year must be >= first_year")
@@ -244,18 +245,20 @@ def _validate(
if window_mode == "flexible":
# PLANNING_ENGINE_SPEC.md §10.1 — the bounding range's raw length
# (before any horizon clipping) must be able to fit target_weeks;
# "find a 6-week window inside this 2-week range" is a user-input
# error, not a legitimate future shortfall.
# (before any horizon clipping) must be able to fit the wanted
# duration; "find a 6-week window inside this 2-week range" is a
# user-input error, not a legitimate future shortfall. Mirrors
# `LeaveWish.target_span_days()`: `target_days` wins when set,
# otherwise `target_weeks * 7` (no entity exists yet in `create()`).
bound_days = _bound_length_days(
window_start_month, window_start_day, window_end_month, window_end_day, first_year
)
needed_days = target_weeks * 7
needed_days = target_days if target_days is not None else target_weeks * 7
if bound_days < needed_days:
raise ValidationError(
"flexible wish bounding range "
f"({bound_days} days) is shorter than target_weeks "
f"({target_weeks} weeks = {needed_days} days)"
f"({bound_days} days) is shorter than the wanted duration "
f"({needed_days} days)"
)