Files
F-r-ldraledigplannerare/backend/app/models/parental_leave.py
T
jacobh2 97a7200c1a Parse transferable days from FK instead of deriving them
The scraper never read "överlåtbara dagar" from Försäkringskassan. It only
visited ansok-och-planera-foraldrapenning#/valjdagar and derived the
reserved/transferable split in build_snapshot, assuming transferable days
are spent before reserved ones. On real data that gave 49 for the user and
0 for the partner where FK states 129 and 71.

The assumption cannot be repaired: reserved-first, transferable-first,
proportional and cap-based models were all checked against real figures and
none reproduces FK's accounting (reserved-first fits the partner but is 10
days off for the user). FK publishes the real numbers per parent per tier on
the min-foraldrapenning page, which the scraper had never visited.

- Scrape the "Dagar ni kan föra över" panel after the valjdagar extraction,
  summing sjukpenningnivå + lägstanivå per parent. The totals are not
  literals on the page. Parse only direct-child <p> elements: the first
  group embeds a tooltip with decoy "45 dagar"/"90 dagar" text.
- build_snapshot accepts the reported value and derives reserved days from
  it; the old arithmetic remains only as a fallback. An implausible FK value
  degrades to that fallback rather than failing the fetch.
- Record provenance in transferable_days_reported (migration 006) and badge
  derived values as estimates in the dashboard breakdown. Drop the footnote's
  claim that at most 150 days are transferable — real data contradicts it as
  a per-family figure.
- Select the child's section by name when FK lists several; never guess.

Verified against a live BankID scrape: scraped 240/101/139 with 129
transferable, deduced partner 240/169/71 with 71, both flagged as reported.

Incidental fixes found along the way:

- The fetch router never forwarded the partner's transferable figure to
  persist_partner_deduction, so the partner's stored snapshot would have kept
  the flawed split even once the parse landed.
- 'deduced' snapshots were labelled "Manually entered" by a two-way ternary
  over a three-value union. Replaced with a compiler-checked exhaustive map.
- Alembic resolved its database from alembic.ini alone, ignoring
  APP_SQLITE_PATH, so migrations silently targeted the developer's real
  database even when pointed elsewhere. env.py now resolves it, preferring
  -x sqlalchemy.url, then a real APP_SQLITE_PATH, then the ini value.
- backend/captures/ was not ignored: the root pattern was anchored as
  /captures/ despite a comment saying scraper dumps must never be tracked.
- CLAUDE.md quoted test counts and coverage percentages that had gone stale,
  which made a correct verification report look suspect. Gate thresholds kept.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DXPBfr1v7F71t6QcbHCaaT
2026-09-20 09:11:53 +02:00

94 lines
3.2 KiB
Python

"""Pydantic schemas for parental leave fetch endpoints."""
from datetime import datetime
from typing import Literal
from uuid import UUID
from pydantic import BaseModel, Field
from app.entities.parental_leave import ParentalLeaveFetch, TierData
class TierDataResponse(BaseModel):
sjukpenning_total: int
sjukpenning_used: int
sjukpenning_left: int
lagsta_niva_total: int
lagsta_niva_used: int
lagsta_niva_left: int
@classmethod
def from_entity(cls, td: TierData) -> "TierDataResponse":
return cls(
sjukpenning_total=td.sjukpenning_total,
sjukpenning_used=td.sjukpenning_used,
sjukpenning_left=td.sjukpenning_left,
lagsta_niva_total=td.lagsta_niva_total,
lagsta_niva_used=td.lagsta_niva_used,
lagsta_niva_left=td.lagsta_niva_left,
)
class ManualFetchRequest(BaseModel):
"""Per-parent "days left" manual entry — everything else (tier totals,
days used, reserved/transferable split) is derived by the same shared
function the FK scraper uses (`app.services.leave_snapshot.build_snapshot`),
so a manually-entered snapshot and a scraped one can never disagree on
how the numbers are derived.
"""
personal_data_uuid: UUID
workspace_id: UUID
child_id: UUID
sjukpenning_left: int = Field(ge=0, le=390)
lagsta_niva_left: int = Field(ge=0, le=90)
dubbeldagar_left: int = Field(ge=0, le=60)
# None -> derived from the transferable-first assumption in `build_snapshot`.
reserved_days_left: int | None = Field(default=None, ge=0, le=90)
# None -> defaults to `rules.total_days_per_child // parent_count`
# (480 solo, 240 with a partner) — see `ManualFetchService.save_manual_entry`.
total_days: int | None = Field(default=None, ge=0, le=480)
source: Literal["manual", "deduced"] = "manual"
class ParentalLeaveFetchResponse(BaseModel):
id: UUID
personal_data_uuid: UUID
workspace_id: UUID
child_id: UUID
source: str
fetched_at: datetime
total_days: int
days_used: int
days_left: int
reserved_days_total: int
reserved_days_left: int
transferable_days_left: int
transferable_days_reported: bool
dubbeldagar_left: int
tier_data: TierDataResponse
created_at: datetime | None
updated_at: datetime | None
@classmethod
def from_entity(cls, fetch: ParentalLeaveFetch) -> "ParentalLeaveFetchResponse":
return cls(
id=fetch.id,
personal_data_uuid=fetch.personal_data_uuid,
workspace_id=fetch.workspace_id,
child_id=fetch.child_id,
source=fetch.source,
fetched_at=fetch.fetched_at,
total_days=fetch.total_days,
days_used=fetch.days_used,
days_left=fetch.days_left,
reserved_days_total=fetch.reserved_days_total,
reserved_days_left=fetch.reserved_days_left,
transferable_days_left=fetch.transferable_days_left,
transferable_days_reported=fetch.transferable_days_reported,
dubbeldagar_left=fetch.dubbeldagar_left,
tier_data=TierDataResponse.from_entity(fetch.tier_data),
created_at=fetch.created_at,
updated_at=fetch.updated_at,
)