Rework balances around per-parent days left and deduce the partner from FK

Manual entry now takes one parent's sjukpenningnivå, lägstanivå and
dubbeldagar left (plus optional reserved days) and derives the rest in
build_snapshot, shared with the scraper. The scraper also reads the
family panel and exposes a partner deduction; with two parents it is
saved as a 'deduced' snapshot (migration 002). Parent count is the single
source of entitlement: one member gets 480 days and 96 savable at age 4,
two get 240 and 48 each. Workspaces are capped at two parents.

Also lands the in-flight squash of migrations 002–011 into the initial
schema and the removal of planning_mode.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-09-19 12:27:29 +02:00
parent 375bc64f2f
commit e4719cb9ae
71 changed files with 5705 additions and 1859 deletions
+105
View File
@@ -11,6 +11,7 @@ for the idempotency/collision/`replace` semantics.
"""
from datetime import date
from typing import Literal
from uuid import UUID
from pydantic import BaseModel, Field
@@ -19,13 +20,22 @@ from app.models.planned_leave import PlannedLeaveDayResponse
from app.services.allocation_service import PlanProposal
from app.services.planning_service import (
DayCollision,
EvictionCandidate,
EvictionPreview,
PlanApplyResult,
PlanChange,
PlanSyncResult,
ProposedDayInput,
Rejection,
ResourceShortage,
)
# Wire-level mirror of `PlanningService`'s `EVICTION_CHOICE_*` constants —
# kept as a `Literal` here (rather than importing the string constants) so
# an invalid value 422s at the Pydantic boundary instead of silently
# reaching the service as a no-op "do nothing".
EvictionChoice = Literal["drop_future_pl", "drop_latest_vacation_this_year"]
class PlanProposeRequest(BaseModel):
workspace_id: UUID
@@ -77,6 +87,9 @@ class WindowOutcomeResponse(BaseModel):
weeks_funded: float
shortfall_days: int
strategy: str
requested_strategy: str
strategy_degraded: bool
dubbeldagar_legally_available: bool
message_key: str
data: dict[str, object]
cost_breakdown: dict[str, int]
@@ -164,6 +177,9 @@ class PlanProposalResponse(BaseModel):
weeks_funded=o.weeks_funded,
shortfall_days=o.shortfall_days,
strategy=o.strategy,
requested_strategy=o.requested_strategy,
strategy_degraded=o.strategy_degraded,
dubbeldagar_legally_available=o.dubbeldagar_legally_available,
message_key=o.message_key,
data=o.data,
cost_breakdown=o.cost_breakdown,
@@ -341,6 +357,15 @@ class PlanApplyResponse(BaseModel):
class PlanSyncRequest(BaseModel):
workspace_id: UUID
# Which of the three user-facing options resolves a would-evict save
# (§ eviction preview). Omitted/`None` — the default — means "do
# nothing": the save is refused and nothing is mutated, exactly as if
# the user picked the explicit cancel option. Only meaningful when the
# response's `eviction_preview.would_evict` was `True` on a prior call
# with the same edit already persisted upstream (e.g. via the leave-wish
# endpoints) — a plain sync that fits within budget ignores this field
# entirely (never asked for a choice in the first place).
eviction_choice: EvictionChoice | None = None
class PlanChangeResponse(BaseModel):
@@ -378,10 +403,89 @@ class PlanChangeResponse(BaseModel):
)
class EvictionCandidateResponse(BaseModel):
"""One previously-persisted day a fresh sync would otherwise silently
delete — see `EvictionCandidate`'s docstring. `wish_name` is `None` when
the owning wish's name couldn't be resolved (never a reason to hide the
day itself).
"""
date: date
parent_personal_data_uuid: UUID
child_id: UUID
day_type: str
is_dubbeldag: bool
wish_id: UUID
wish_name: str | None
@classmethod
def from_domain(cls, candidate: EvictionCandidate) -> "EvictionCandidateResponse":
return cls(
date=candidate.date,
parent_personal_data_uuid=candidate.parent_personal_data_uuid,
child_id=candidate.child_id,
day_type=candidate.day_type,
is_dubbeldag=candidate.is_dubbeldag,
wish_id=candidate.wish_id,
wish_name=candidate.wish_name,
)
class ResourceShortageResponse(BaseModel):
"""One exhausted resource behind an eviction preview — see
`ResourceShortage`'s docstring. `resource_kind == 'parental_leave'`
carries `tier`/`child_id` (`year` is `None`); `resource_kind ==
'vacation'` carries `year` instead (`tier`/`child_id` are `None`).
Structured on purpose — the frontend composes the warning text itself,
never a backend-rendered string.
"""
resource_kind: str
parent_personal_data_uuid: UUID
tier: str | None
child_id: UUID | None
year: int | None
days_evicted: int
@classmethod
def from_domain(cls, shortage: ResourceShortage) -> "ResourceShortageResponse":
return cls(
resource_kind=shortage.resource_kind,
parent_personal_data_uuid=shortage.parent_personal_data_uuid,
tier=shortage.tier,
child_id=shortage.child_id,
year=shortage.year,
days_evicted=shortage.days_evicted,
)
class EvictionPreviewResponse(BaseModel):
"""Always present on `PlanSyncResponse`; `would_evict: false` with empty
lists in the common (fits-within-budget) case — see
`EvictionPreview`'s docstring.
"""
would_evict: bool
candidates: list[EvictionCandidateResponse]
shortages: list[ResourceShortageResponse]
@classmethod
def from_domain(cls, preview: EvictionPreview) -> "EvictionPreviewResponse":
return cls(
would_evict=preview.would_evict,
candidates=[EvictionCandidateResponse.from_domain(c) for c in preview.candidates],
shortages=[ResourceShortageResponse.from_domain(s) for s in preview.shortages],
)
class PlanSyncResponse(BaseModel):
proposal: PlanProposalResponse
apply_result: PlanApplyResponse
changes: list[PlanChangeResponse]
eviction_preview: EvictionPreviewResponse
@classmethod
def from_domain(cls, result: PlanSyncResult) -> "PlanSyncResponse":
@@ -389,4 +493,5 @@ class PlanSyncResponse(BaseModel):
proposal=PlanProposalResponse.from_domain(result.proposal),
apply_result=PlanApplyResponse.from_domain(result.apply_result),
changes=[PlanChangeResponse.from_domain(c) for c in result.changes],
eviction_preview=EvictionPreviewResponse.from_domain(result.eviction_preview),
)