Add rename and remove for a workspace's placeholder partner
Settings is gaining a partner display, which hides the add-form once a
partner exists. Without these routes a typo'd partner name would be
permanently unfixable, since no endpoint could change or drop one.
PATCH /workspace/{id}/parents/{uuid} renames; DELETE soft-deletes.
Soft rather than hard delete because PersonalData holds PII and the
repository is a SoftDeleteRepository specifically so erasure stays
auditable.
Removal refuses (422) when the member still has leave wishes, planned
leave, parental-leave rows or vacation days, naming the blockers, so a
removal can never orphan plan data. It also refuses for the caller's own
profile, and for a member who has their own login -- the invite/redeem
flow re-points a real user's personal_data into the inviter's workspace,
so soft-deleting that row would silently break their login.
Errors carry message_key + data rather than prose, per the no-Swedish-on
-the-wire rule; NotFoundError/ValidationError gained the same optional
shape DubbeldagError already had, defaulting empty so existing call
sites are unaffected.
Also fixes seed_demo.py constructing WorkspaceService with a stale
3-arg signature, which would have crashed at runtime -- invisible to
pytest since it is a standalone script.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
This commit is contained in:
@@ -5,6 +5,7 @@ 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,
|
||||
@@ -20,6 +21,35 @@ 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,
|
||||
@@ -71,17 +101,7 @@ async def list_members(
|
||||
_current_user: CurrentUserDep,
|
||||
) -> list[PersonalDataResponse]:
|
||||
members = await service.list_members(workspace_id)
|
||||
return [
|
||||
PersonalDataResponse(
|
||||
uuid=m.uuid,
|
||||
workspace_id=m.workspace_id,
|
||||
name=m.name,
|
||||
salary_type=m.salary_type,
|
||||
employment_status=m.employment_status,
|
||||
created_at=m.created_at,
|
||||
)
|
||||
for m in members
|
||||
]
|
||||
return [_to_response(m) for m in members]
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -97,16 +117,45 @@ async def add_placeholder_parent(
|
||||
) -> PersonalDataResponse:
|
||||
try:
|
||||
pd = await service.add_placeholder_parent(workspace_id, payload.display_name)
|
||||
except NotFoundError as exc:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
||||
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,
|
||||
)
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user