cc3ced905e
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.
208 lines
6.5 KiB
Python
208 lines
6.5 KiB
Python
"""HTTP routes for workspace, members, and invitations."""
|
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
|
|
from app.dependencies import CurrentUserDep, WorkspaceServiceDep
|
|
from app.entities.personal_data import PersonalData
|
|
from app.models.workspace import (
|
|
InvitationResponse,
|
|
InviteResponse,
|
|
PersonalDataCreate,
|
|
PersonalDataResponse,
|
|
RedeemInviteRequest,
|
|
WorkspaceCreate,
|
|
WorkspaceResponse,
|
|
WorkspaceUpdate,
|
|
)
|
|
from app.utils.errors import InvitationError, NotFoundError, ValidationError
|
|
|
|
router = APIRouter(prefix="/api/workspace", tags=["workspace"])
|
|
|
|
|
|
def _to_response(pd: PersonalData) -> PersonalDataResponse:
|
|
return PersonalDataResponse(
|
|
uuid=pd.uuid,
|
|
workspace_id=pd.workspace_id,
|
|
name=pd.name,
|
|
salary_type=pd.salary_type,
|
|
employment_status=pd.employment_status,
|
|
created_at=pd.created_at,
|
|
)
|
|
|
|
|
|
def _to_http_error(exc: NotFoundError | ValidationError) -> HTTPException:
|
|
"""Map a NotFoundError/ValidationError to an HTTPException.
|
|
|
|
Both carry an optional `message_key` + `data` pair (§3.5); when present it
|
|
becomes the structured `detail` so the frontend never has to parse an
|
|
English sentence. Falls back to the plain message for the (rare) error
|
|
raised without one.
|
|
"""
|
|
code = (
|
|
status.HTTP_404_NOT_FOUND
|
|
if isinstance(exc, NotFoundError)
|
|
else status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
)
|
|
if exc.message_key:
|
|
return HTTPException(code, detail={"message_key": exc.message_key, "data": exc.data})
|
|
return HTTPException(code, detail=str(exc))
|
|
|
|
|
|
@router.post("", response_model=WorkspaceResponse, status_code=status.HTTP_201_CREATED)
|
|
async def create_workspace(
|
|
payload: WorkspaceCreate,
|
|
service: WorkspaceServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> WorkspaceResponse:
|
|
ws = await service.create_workspace(payload.name)
|
|
return WorkspaceResponse.from_entity(ws)
|
|
|
|
|
|
@router.get("/{workspace_id}", response_model=WorkspaceResponse)
|
|
async def get_workspace(
|
|
workspace_id: UUID,
|
|
service: WorkspaceServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> WorkspaceResponse:
|
|
try:
|
|
ws = await service.get_workspace(workspace_id)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
return WorkspaceResponse.from_entity(ws)
|
|
|
|
|
|
@router.put("/{workspace_id}", response_model=WorkspaceResponse)
|
|
async def update_workspace(
|
|
workspace_id: UUID,
|
|
payload: WorkspaceUpdate,
|
|
service: WorkspaceServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> WorkspaceResponse:
|
|
try:
|
|
ws = await service.update_workspace(workspace_id, payload.name, payload.planning_mode)
|
|
except (NotFoundError, ValidationError) as exc:
|
|
code = (
|
|
status.HTTP_404_NOT_FOUND
|
|
if isinstance(exc, NotFoundError)
|
|
else status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
)
|
|
raise HTTPException(code, str(exc)) from exc
|
|
return WorkspaceResponse.from_entity(ws)
|
|
|
|
|
|
@router.get("/{workspace_id}/members", response_model=list[PersonalDataResponse])
|
|
async def list_members(
|
|
workspace_id: UUID,
|
|
service: WorkspaceServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> list[PersonalDataResponse]:
|
|
members = await service.list_members(workspace_id)
|
|
return [_to_response(m) for m in members]
|
|
|
|
|
|
@router.post(
|
|
"/{workspace_id}/parents",
|
|
response_model=PersonalDataResponse,
|
|
status_code=status.HTTP_201_CREATED,
|
|
)
|
|
async def add_placeholder_parent(
|
|
workspace_id: UUID,
|
|
payload: PersonalDataCreate,
|
|
service: WorkspaceServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> PersonalDataResponse:
|
|
try:
|
|
pd = await service.add_placeholder_parent(workspace_id, payload.display_name)
|
|
except (NotFoundError, ValidationError) as exc:
|
|
raise _to_http_error(exc) from exc
|
|
return _to_response(pd)
|
|
|
|
|
|
@router.patch(
|
|
"/{workspace_id}/parents/{personal_data_uuid}",
|
|
response_model=PersonalDataResponse,
|
|
)
|
|
async def rename_parent(
|
|
workspace_id: UUID,
|
|
personal_data_uuid: UUID,
|
|
payload: PersonalDataCreate,
|
|
service: WorkspaceServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> PersonalDataResponse:
|
|
try:
|
|
pd = await service.rename_member(workspace_id, personal_data_uuid, payload.display_name)
|
|
except (NotFoundError, ValidationError) as exc:
|
|
raise _to_http_error(exc) from exc
|
|
return _to_response(pd)
|
|
|
|
|
|
@router.delete(
|
|
"/{workspace_id}/parents/{personal_data_uuid}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
)
|
|
async def remove_parent(
|
|
workspace_id: UUID,
|
|
personal_data_uuid: UUID,
|
|
service: WorkspaceServiceDep,
|
|
current_user: CurrentUserDep,
|
|
) -> None:
|
|
try:
|
|
await service.remove_member(
|
|
workspace_id, personal_data_uuid, current_user.personal_data_uuid
|
|
)
|
|
except (NotFoundError, ValidationError) as exc:
|
|
raise _to_http_error(exc) from exc
|
|
|
|
|
|
@router.post(
|
|
"/{workspace_id}/invitations",
|
|
response_model=InviteResponse,
|
|
status_code=status.HTTP_201_CREATED,
|
|
)
|
|
async def create_invitation(
|
|
workspace_id: UUID,
|
|
service: WorkspaceServiceDep,
|
|
current_user: CurrentUserDep,
|
|
) -> InviteResponse:
|
|
try:
|
|
raw_token = await service.invite_partner(workspace_id, current_user.id)
|
|
except NotFoundError as exc:
|
|
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
|
return InviteResponse(token=raw_token)
|
|
|
|
|
|
# Invitation redemption is workspace-agnostic (token carries workspace info).
|
|
_inv_router = APIRouter(prefix="/api/invitations", tags=["invitations"])
|
|
|
|
|
|
@_inv_router.post("/redeem", response_model=InvitationResponse)
|
|
async def redeem_invitation(
|
|
payload: RedeemInviteRequest,
|
|
service: WorkspaceServiceDep,
|
|
current_user: CurrentUserDep,
|
|
) -> InvitationResponse:
|
|
try:
|
|
inv = await service.redeem_invitation(payload.token, current_user.personal_data_uuid)
|
|
except (InvitationError, NotFoundError) as exc:
|
|
raise HTTPException(status.HTTP_400_BAD_REQUEST, str(exc)) from exc
|
|
return InvitationResponse.from_entity(inv)
|
|
|
|
|
|
@_inv_router.delete("/{invitation_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def revoke_invitation(
|
|
invitation_id: UUID,
|
|
service: WorkspaceServiceDep,
|
|
_current_user: CurrentUserDep,
|
|
) -> None:
|
|
try:
|
|
await service.revoke_invitation(invitation_id)
|
|
except (NotFoundError, ValidationError) as exc:
|
|
code = (
|
|
status.HTTP_404_NOT_FOUND
|
|
if isinstance(exc, NotFoundError)
|
|
else status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
)
|
|
raise HTTPException(code, str(exc)) from exc
|