249e277696
Second real bug found by driving the running app while the suites were green. Flexible wishes were funded twice across propose -> apply -> re-propose. Because planned_leave_day carries no wish_id, the engine could not tell that applied days were that wish's own prior occurrence; it treated them as occupied space, routed the candidate search around them, and funded the year again on shifted dates while reporting weeks_funded 1.0. Applying twice turned "one week each spring" into 18 paid days instead of 9. Fixed wishes were unaffected and stay unaffected. Fix (allocation_service.py), per the approved approach of counting already-applied days inside the bounding range rather than adding a wish_id column: - _Ledger._persisted_lookup snapshots existing days BEFORE this run folds in its own freshly-computed windows, so a fixed wish's new placement is never mistaken for a flexible wish's history - _reuse_already_funded_window finds this wish's persisted dates within its bound and reuses the window containing them, via the ordinary fund_window path - full funding therefore yields zero new days and partial funding tops up exactly the remainder, with no duplication and no false shortfall - with no persisted dates, or if the reused window is no longer collision-free, it falls through to the original search, so an unsatisfied year is never frozen Calendar cache extended 2026-2035 -> 2026-2055 (39KB -> 116KB) and the allocator switched to strict, so an uncached year raises UncachedYearError -> ValidationError -> HTTP 422 instead of silently treating Julafton, Nyarsafton and Midsommarafton as working days. Also fixed normalize_day dropping the second name when two holidays share a date (Nationaldagen was being lost). 2049 and 2055 legitimately have 17 red days, not 18: Pingstdagen coincides with Nationaldagen. New backend/tests/integration/test_user_journey.py drives the real app over HTTP across seven journeys: the double-booking guard, the fixed-wish control, apply idempotency, fixed-never-displaced-by-flexible, flexible wishes tracking Easter per year, age-4 arithmetic self-consistency, and a recursive check that no Swedish text reaches the wire now that everything is message_key + data. New backend/scripts/seed_demo.py seeds a realistic workspace through the real service layer for manual testing: demo@example.com / DemoPass123! Backend 414 -> 435 tests. Frontend unchanged at 705. Still not verified in a browser. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
58 lines
2.4 KiB
Python
58 lines
2.4 KiB
Python
"""HTTP routes for the planning engine.
|
|
|
|
Routers stay thin: validate via Pydantic, call the service, map domain errors
|
|
to HTTP. All allocation logic lives in `app.services.allocation_service`, and
|
|
all bulk-apply logic (idempotency, collisions, `replace`, validation) lives in
|
|
`app.services.planning_service.PlanningService.apply_proposed_days`. See
|
|
PLANNING_ENGINE_SPEC.md §4.
|
|
"""
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
|
|
from app.dependencies import PlanningEngineServiceDep, PlanningServiceDep
|
|
from app.models.plan import (
|
|
PlanApplyRequest,
|
|
PlanApplyResponse,
|
|
PlanProposalResponse,
|
|
PlanProposeRequest,
|
|
)
|
|
from app.utils.errors import NotFoundError, ValidationError
|
|
|
|
router = APIRouter(prefix="/api/plan", tags=["plan"])
|
|
|
|
|
|
@router.post("/propose", response_model=PlanProposalResponse)
|
|
async def propose_plan(
|
|
payload: PlanProposeRequest, service: PlanningEngineServiceDep
|
|
) -> PlanProposalResponse:
|
|
"""Compute a plan proposal. Read-only — persists nothing, safe to spam."""
|
|
try:
|
|
proposal = await service.propose(payload.workspace_id)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
except ValidationError as exc:
|
|
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
|
|
return PlanProposalResponse.from_domain(proposal)
|
|
|
|
|
|
@router.post("/apply", response_model=PlanApplyResponse)
|
|
async def apply_plan(payload: PlanApplyRequest, service: PlanningServiceDep) -> PlanApplyResponse:
|
|
"""Accept a (possibly user-edited) set of proposed days as real plan.
|
|
|
|
Always returns 200 with `accepted: false` and a `rejections`/`collisions`
|
|
breakdown when the batch is refused — refusal (a collision without
|
|
`replace`, an invalid day type, a balance/eligibility violation) is an
|
|
expected, structured outcome for the UI to render, not an HTTP error.
|
|
A genuinely missing workspace/child (404) is the one case still raised
|
|
as an HTTP error, since that's not something the batch report can explain.
|
|
"""
|
|
try:
|
|
result = await service.apply_proposed_days(
|
|
workspace_id=payload.workspace_id,
|
|
days=[item.to_domain() for item in payload.days],
|
|
replace=payload.replace,
|
|
)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
return PlanApplyResponse.from_domain(result)
|