Files
F-r-ldraledigplannerare/backend/app/routers/leave_wishes.py
T
jacobh2 cc3ced905e 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.
2026-09-15 20:28:06 +02:00

112 lines
4.5 KiB
Python

"""HTTP routes for leave wishes.
Routers stay thin: validate via Pydantic, call the service, map domain errors
to HTTP. Business logic (recurrence/parent_mode validation, month-day range
checks) lives in LeaveWishService. See PLANNING_ENGINE_SPEC.md §4.
"""
from uuid import UUID
from fastapi import APIRouter, HTTPException, status
from app.dependencies import LeaveWishServiceDep
from app.models.leave_wish import (
LeaveWishCreate,
LeaveWishResponse,
LeaveWishUpdate,
)
from app.utils.errors import NotFoundError, ValidationError
router = APIRouter(prefix="/api/leave-wishes", tags=["leave-wishes"])
@router.post("", response_model=LeaveWishResponse, status_code=status.HTTP_201_CREATED)
async def create_leave_wish(
payload: LeaveWishCreate, service: LeaveWishServiceDep
) -> LeaveWishResponse:
try:
entity = await service.create(
workspace_id=payload.workspace_id,
child_id=payload.child_id,
name=payload.name,
name_message_key=payload.name_message_key,
window_start_month=payload.window_start_month,
window_start_day=payload.window_start_day,
window_end_month=payload.window_end_month,
window_end_day=payload.window_end_day,
target_weeks=payload.target_weeks,
target_days=payload.target_days,
recurrence=payload.recurrence,
first_year=payload.first_year,
last_year=payload.last_year,
priority=payload.priority,
parent_mode=payload.parent_mode,
parent_personal_data_uuid=payload.parent_personal_data_uuid,
window_mode=payload.window_mode,
)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
return LeaveWishResponse.from_entity(entity)
@router.get("/{workspace_id}", response_model=list[LeaveWishResponse])
async def list_leave_wishes(
workspace_id: UUID, service: LeaveWishServiceDep
) -> list[LeaveWishResponse]:
"""List all leave wishes for a workspace. Matches PLANNING_ENGINE_SPEC.md §4
(`GET /api/leave-wishes/{workspace_id}` — list-by-workspace, not get-by-id)."""
entities = await service.list_for_workspace(workspace_id)
return [LeaveWishResponse.from_entity(e) for e in entities]
@router.put("/{leave_wish_id}", response_model=LeaveWishResponse)
async def update_leave_wish(
leave_wish_id: UUID,
payload: LeaveWishUpdate,
service: LeaveWishServiceDep,
) -> LeaveWishResponse:
fields_set = payload.model_fields_set
try:
entity = await service.update(
leave_wish_id=leave_wish_id,
child_id=payload.child_id,
name=payload.name,
name_message_key=payload.name_message_key,
window_start_month=payload.window_start_month,
window_start_day=payload.window_start_day,
window_end_month=payload.window_end_month,
window_end_day=payload.window_end_day,
target_weeks=payload.target_weeks,
target_days=payload.target_days,
recurrence=payload.recurrence,
first_year=payload.first_year,
last_year=payload.last_year,
priority=payload.priority,
parent_mode=payload.parent_mode,
parent_personal_data_uuid=payload.parent_personal_data_uuid,
window_mode=payload.window_mode,
_clear_child_id="child_id" in fields_set and payload.child_id is None,
_clear_last_year="last_year" in fields_set and payload.last_year is None,
_clear_parent_personal_data_uuid=(
"parent_personal_data_uuid" in fields_set
and payload.parent_personal_data_uuid is None
),
_clear_name_message_key=(
"name_message_key" in fields_set and payload.name_message_key is None
),
_clear_target_days="target_days" in fields_set and payload.target_days is None,
)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, str(exc)) from exc
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
return LeaveWishResponse.from_entity(entity)
@router.delete("/{leave_wish_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_leave_wish(leave_wish_id: UUID, service: LeaveWishServiceDep) -> None:
try:
await service.delete(leave_wish_id)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc