61e5487078
Free days (Sat/Sun/red days, subject to DayOverride) can never be vacation, and can only be parental leave when the workspace opts in via take_pl_on_red_days. The allocator already honoured this; the hand-pin path bypassed it entirely. - PlanningService gains optional workspace_repo/day_override_repo deps and _reject_illegal_free_day, enforced in add_planned_day and update. Reuses allocation_service.is_free_day rather than re-deriving it, and no-ops when the deps are unset so existing callers are unaffected. - update_planned_day had no ValidationError handler at all, so a validation failure there 500'd instead of 422'ing. Fixed. - Calendar filters the pin write loop by the day's isFree; the modal explains free days stay off, and that pinning opts a day out of automatic re-planning. Vacation days at risk of forfeiture were computed and sent over the wire but never rendered: an at-risk column with the age-out/over-cap split, and a dashboard key figure. PlanProposal.warnings was likewise parsed but rendered nowhere, for all three kinds. Adds PlanWarningCard in a Risks section above the tips, with parent names resolved and dates formatted. POST /api/plan/sync now fires from the workspace layer on every plan-input change rather than only from Calendar, so a Settings edit or a pinned-day save re-plans day types instead of merely refetching. Overlapping bumps coalesce into one trailing run; a rejection cannot wedge the in-flight guard. eviction_preview was silently dropped by the frontend despite always being on the wire, and is now surfaced through context. Verified: backend 890 passed (91.49% coverage), frontend 1297 passed, ruff/eslint/tsc clean. Browser-checked in Swedish and English. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JVQx4NUvPbJhn6UcvRd2QY
130 lines
4.6 KiB
Python
130 lines
4.6 KiB
Python
"""HTTP routes for planned leave days (calendar planning)."""
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, HTTPException, Query, status
|
|
|
|
from app.dependencies import PlanningServiceDep, WorkspaceIdDep, WorkspaceScopeDep
|
|
from app.models.planned_leave import (
|
|
PlannedLeaveDayCreate,
|
|
PlannedLeaveDayResponse,
|
|
PlannedLeaveDayUpdate,
|
|
)
|
|
from app.utils.errors import NotFoundError, ValidationError
|
|
|
|
router = APIRouter(prefix="/api/planned-days", tags=["planned-days"])
|
|
|
|
|
|
def _to_response(day) -> PlannedLeaveDayResponse:
|
|
return PlannedLeaveDayResponse.from_entity(day)
|
|
|
|
|
|
def _to_http_error(exc: ValidationError) -> HTTPException:
|
|
"""Map a `ValidationError` to a 422, forwarding its `message_key` + `data`
|
|
|
|
(§3.5) as structured `detail` when present — e.g. the free-day hand-pin
|
|
guard (`PlanningService._reject_illegal_free_day`) — so the frontend can
|
|
translate it instead of parsing an English sentence. Falls back to the
|
|
plain message for the (more common) error raised without one. Mirrors
|
|
`app.routers.workspace._to_http_error`.
|
|
"""
|
|
if exc.message_key:
|
|
return HTTPException(
|
|
status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
detail={"message_key": exc.message_key, "data": exc.data},
|
|
)
|
|
return HTTPException(status.HTTP_422_UNPROCESSABLE_ENTITY, detail=str(exc))
|
|
|
|
|
|
@router.post("", response_model=PlannedLeaveDayResponse, status_code=status.HTTP_201_CREATED)
|
|
async def create_planned_day(
|
|
payload: PlannedLeaveDayCreate,
|
|
service: PlanningServiceDep,
|
|
scope: WorkspaceScopeDep,
|
|
) -> PlannedLeaveDayResponse:
|
|
scope.check_workspace(payload.workspace_id)
|
|
await scope.check_child(payload.child_id)
|
|
await scope.check_member(payload.parent_personal_data_uuid)
|
|
try:
|
|
day = await service.add_planned_day(
|
|
workspace_id=payload.workspace_id,
|
|
child_id=payload.child_id,
|
|
parent_personal_data_uuid=payload.parent_personal_data_uuid,
|
|
date=payload.date,
|
|
day_type=payload.day_type,
|
|
is_dubbeldag=payload.is_dubbeldag,
|
|
wish_id=payload.wish_id,
|
|
is_pinned=payload.is_pinned,
|
|
)
|
|
return _to_response(day)
|
|
except ValidationError as exc:
|
|
raise _to_http_error(exc) from exc
|
|
|
|
|
|
@router.get("", response_model=list[PlannedLeaveDayResponse])
|
|
async def list_planned_days(
|
|
service: PlanningServiceDep,
|
|
scope: WorkspaceScopeDep,
|
|
child_id: UUID = Query(...),
|
|
) -> list[PlannedLeaveDayResponse]:
|
|
await scope.check_child(child_id)
|
|
days = await service.list_by_child(child_id)
|
|
return [_to_response(d) for d in days]
|
|
|
|
|
|
@router.get("/{planned_leave_day_id}", response_model=PlannedLeaveDayResponse)
|
|
async def get_planned_day(
|
|
planned_leave_day_id: UUID,
|
|
service: PlanningServiceDep,
|
|
scope: WorkspaceScopeDep,
|
|
) -> PlannedLeaveDayResponse:
|
|
try:
|
|
day = await service.get(planned_leave_day_id, workspace_id=scope.workspace_id)
|
|
return _to_response(day)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
|
|
|
|
@router.get("/workspace/{workspace_id}", response_model=list[PlannedLeaveDayResponse])
|
|
async def list_planned_days_by_workspace(
|
|
workspace_id: WorkspaceIdDep,
|
|
service: PlanningServiceDep,
|
|
) -> list[PlannedLeaveDayResponse]:
|
|
days = await service.list_by_workspace(workspace_id)
|
|
return [_to_response(d) for d in days]
|
|
|
|
|
|
@router.put("/{planned_leave_day_id}", response_model=PlannedLeaveDayResponse)
|
|
async def update_planned_day(
|
|
planned_leave_day_id: UUID,
|
|
payload: PlannedLeaveDayUpdate,
|
|
service: PlanningServiceDep,
|
|
scope: WorkspaceScopeDep,
|
|
) -> PlannedLeaveDayResponse:
|
|
try:
|
|
day = await service.update(
|
|
planned_leave_day_id,
|
|
day_type=payload.day_type,
|
|
is_dubbeldag=payload.is_dubbeldag,
|
|
wish_id=payload.wish_id,
|
|
is_pinned=payload.is_pinned,
|
|
workspace_id=scope.workspace_id,
|
|
)
|
|
return _to_response(day)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
except ValidationError as exc:
|
|
raise _to_http_error(exc) from exc
|
|
|
|
|
|
@router.delete("/{planned_leave_day_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_planned_day(
|
|
planned_leave_day_id: UUID,
|
|
service: PlanningServiceDep,
|
|
scope: WorkspaceScopeDep,
|
|
) -> None:
|
|
try:
|
|
await service.remove_planned_day(planned_leave_day_id, workspace_id=scope.workspace_id)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|