Give every year of a repeating period its own row
A repeating period was a single row that the planner expanded into one window per year at read time, so no year had anywhere to differ from the others. Editing one year rewrote them all, and removing a year in the middle had to split the wish in two. Neither survives contact with the thing this app is for: Easter moves, and the cheapest week is not the same week every year. Each year is now its own row, linked by a series id. Editing one year touches one row; editing the series re-projects the rest from it. Removing a year is one delete. The weekday snap is worked out once, when the rows are made, and stored, so a row now says what it means rather than being re-derived on every read. Recurrence stops being a stored field and becomes what it always was in practice — an instruction at create and update time; whether a period repeats is whether it belongs to a series. Migration 004 rebuilds the table and expands the existing rows, keeping each original row as its first year so the days already planned against it still point somewhere. A Feb 29 anchor recurring into a common year used to raise out of the allocator; it clamps now. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ei7gCZzdd751uZtPzb1Pep
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
"""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.
|
||||
to HTTP. Business logic (recurrence materialisation, parent_mode validation,
|
||||
month-day range checks) lives in LeaveWishService. See PLANNING_ENGINE_SPEC.md §4.
|
||||
|
||||
A repeating wish is stored as one row per year (see app/entities/leave_wish.py),
|
||||
so PUT/DELETE address a single year by default. `?scope=series` widens them to
|
||||
every year of the series the addressed row belongs to — the "this year" vs
|
||||
"every year" choice the calendar's period popover offers. On a standalone
|
||||
occurrence the two scopes are the same thing.
|
||||
"""
|
||||
|
||||
from typing import Annotated, Literal
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from fastapi import APIRouter, HTTPException, Query, status
|
||||
|
||||
from app.dependencies import LeaveWishServiceDep, WorkspaceIdDep, WorkspaceScopeDep
|
||||
from app.models.leave_wish import (
|
||||
@@ -17,6 +24,11 @@ from app.models.leave_wish import (
|
||||
)
|
||||
from app.utils.errors import NotFoundError, ValidationError
|
||||
|
||||
ScopeQuery = Annotated[
|
||||
Literal["occurrence", "series"],
|
||||
Query(description="'occurrence' = this year only; 'series' = every year of the wish"),
|
||||
]
|
||||
|
||||
router = APIRouter(prefix="/api/leave-wishes", tags=["leave-wishes"])
|
||||
|
||||
|
||||
@@ -48,6 +60,7 @@ async def create_leave_wish(
|
||||
parent_mode=payload.parent_mode,
|
||||
parent_personal_data_uuid=payload.parent_personal_data_uuid,
|
||||
window_mode=payload.window_mode,
|
||||
series_id=payload.series_id,
|
||||
wants_dubbeldag=payload.wants_dubbeldag,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
@@ -70,17 +83,19 @@ async def update_leave_wish(
|
||||
leave_wish_id: UUID,
|
||||
payload: LeaveWishUpdate,
|
||||
service: LeaveWishServiceDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
workspace_scope: WorkspaceScopeDep,
|
||||
scope: ScopeQuery = "occurrence",
|
||||
) -> LeaveWishResponse:
|
||||
if payload.child_id is not None:
|
||||
await scope.check_child(payload.child_id)
|
||||
await workspace_scope.check_child(payload.child_id)
|
||||
if payload.parent_personal_data_uuid is not None:
|
||||
await scope.check_member(payload.parent_personal_data_uuid)
|
||||
await workspace_scope.check_member(payload.parent_personal_data_uuid)
|
||||
fields_set = payload.model_fields_set
|
||||
try:
|
||||
entity = await service.update(
|
||||
leave_wish_id=leave_wish_id,
|
||||
workspace_id=scope.workspace_id,
|
||||
workspace_id=workspace_scope.workspace_id,
|
||||
scope=scope,
|
||||
child_id=payload.child_id,
|
||||
name=payload.name,
|
||||
name_message_key=payload.name_message_key,
|
||||
@@ -118,9 +133,12 @@ async def update_leave_wish(
|
||||
|
||||
@router.delete("/{leave_wish_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_leave_wish(
|
||||
leave_wish_id: UUID, service: LeaveWishServiceDep, scope: WorkspaceScopeDep
|
||||
leave_wish_id: UUID,
|
||||
service: LeaveWishServiceDep,
|
||||
workspace_scope: WorkspaceScopeDep,
|
||||
scope: ScopeQuery = "occurrence",
|
||||
) -> None:
|
||||
try:
|
||||
await service.delete(leave_wish_id, workspace_id=scope.workspace_id)
|
||||
await service.delete(leave_wish_id, workspace_id=workspace_scope.workspace_id, scope=scope)
|
||||
except NotFoundError as exc:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
||||
|
||||
Reference in New Issue
Block a user