Mandats : origin→FK identité + nomination auto + boutons + tests intégration
- origin TEXT → origin_id UUID FK duniter_identities (migration e3f4a5b6c7d8) - GET /auth/identities?q= : recherche d'identités par nom/adresse - MandateCreate.nomination_mode : auto (auto-assign auteur), collective, postpone - Wizard new.vue : champ origine = picker identité, checkbox "Démarrer maintenant" - [id].vue : modal "Assigner" = search-picker (résout UUID vs adresse SS58), affiche origin_display_name + mandatee_display_name, inputs natifs (<input>/<textarea>) - Erreurs API visibles dans l'UI (plus de catch silencieux) - test_mandate_flows.py : 17 tests intégration SQLite réels (origin, nomination, assign, lifecycle, revocation, interactions croisées) — 241 tests total OK Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,8 @@ from __future__ import annotations
|
||||
import secrets
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response, status
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.config import settings
|
||||
@@ -232,3 +233,24 @@ async def logout(
|
||||
for session in sessions:
|
||||
await db.delete(session)
|
||||
await db.commit()
|
||||
|
||||
|
||||
@router.get("/identities", response_model=list[IdentityOut])
|
||||
async def search_identities(
|
||||
q: str = Query(..., min_length=1, description="Recherche par adresse ou nom"),
|
||||
limit: int = Query(default=10, ge=1, le=50),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> list[IdentityOut]:
|
||||
"""Search Duniter identities by address prefix or display_name."""
|
||||
result = await db.execute(
|
||||
select(DuniterIdentity)
|
||||
.where(
|
||||
or_(
|
||||
DuniterIdentity.address.ilike(f"{q}%"),
|
||||
DuniterIdentity.display_name.ilike(f"%{q}%"),
|
||||
)
|
||||
)
|
||||
.order_by(DuniterIdentity.display_name)
|
||||
.limit(limit)
|
||||
)
|
||||
return [IdentityOut.model_validate(i) for i in result.scalars().all()]
|
||||
|
||||
@@ -38,10 +38,13 @@ router = APIRouter()
|
||||
|
||||
|
||||
async def _get_mandate(db: AsyncSession, mandate_id: uuid.UUID) -> Mandate:
|
||||
"""Fetch a mandate by ID with its steps eagerly loaded, or raise 404."""
|
||||
result = await db.execute(
|
||||
select(Mandate)
|
||||
.options(selectinload(Mandate.steps))
|
||||
.options(
|
||||
selectinload(Mandate.steps),
|
||||
selectinload(Mandate.origin_identity),
|
||||
selectinload(Mandate.mandatee),
|
||||
)
|
||||
.where(Mandate.id == mandate_id)
|
||||
)
|
||||
mandate = result.scalar_one_or_none()
|
||||
@@ -50,6 +53,13 @@ async def _get_mandate(db: AsyncSession, mandate_id: uuid.UUID) -> Mandate:
|
||||
return mandate
|
||||
|
||||
|
||||
def _mandate_out(mandate: Mandate) -> MandateOut:
|
||||
out = MandateOut.model_validate(mandate)
|
||||
out.origin_display_name = mandate.origin_display_name
|
||||
out.mandatee_display_name = mandate.mandatee_display_name
|
||||
return out
|
||||
|
||||
|
||||
# ── Mandate routes ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -57,13 +67,16 @@ async def _get_mandate(db: AsyncSession, mandate_id: uuid.UUID) -> Mandate:
|
||||
async def list_mandates(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
org_id: uuid.UUID | None = Depends(get_active_org_id),
|
||||
mandate_type: str | None = Query(default=None, description="Filtrer par type de mandat"),
|
||||
status_filter: str | None = Query(default=None, alias="status", description="Filtrer par statut"),
|
||||
mandate_type: str | None = Query(default=None),
|
||||
status_filter: str | None = Query(default=None, alias="status"),
|
||||
skip: int = Query(default=0, ge=0),
|
||||
limit: int = Query(default=50, ge=1, le=200),
|
||||
) -> list[MandateOut]:
|
||||
"""List all mandates with optional filters."""
|
||||
stmt = select(Mandate).options(selectinload(Mandate.steps))
|
||||
stmt = select(Mandate).options(
|
||||
selectinload(Mandate.steps),
|
||||
selectinload(Mandate.origin_identity),
|
||||
selectinload(Mandate.mandatee),
|
||||
)
|
||||
|
||||
if org_id is not None:
|
||||
stmt = stmt.where(Mandate.organization_id == org_id)
|
||||
@@ -76,7 +89,7 @@ async def list_mandates(
|
||||
result = await db.execute(stmt)
|
||||
mandates = result.scalars().unique().all()
|
||||
|
||||
return [MandateOut.model_validate(m) for m in mandates]
|
||||
return [_mandate_out(m) for m in mandates]
|
||||
|
||||
|
||||
@router.post("/", response_model=MandateOut, status_code=status.HTTP_201_CREATED)
|
||||
@@ -86,15 +99,20 @@ async def create_mandate(
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
org_id: uuid.UUID | None = Depends(get_active_org_id),
|
||||
) -> MandateOut:
|
||||
"""Create a new mandate."""
|
||||
mandate = Mandate(**payload.model_dump(), organization_id=org_id)
|
||||
data = payload.model_dump()
|
||||
nomination_mode = data.pop("nomination_mode", "postpone")
|
||||
|
||||
mandate = Mandate(**data, organization_id=org_id)
|
||||
|
||||
if nomination_mode == "auto":
|
||||
mandate.mandatee_id = identity.id
|
||||
|
||||
db.add(mandate)
|
||||
await db.commit()
|
||||
await db.refresh(mandate)
|
||||
|
||||
# Reload with steps (empty at creation)
|
||||
mandate = await _get_mandate(db, mandate.id)
|
||||
return MandateOut.model_validate(mandate)
|
||||
return _mandate_out(mandate)
|
||||
|
||||
|
||||
@router.get("/{id}", response_model=MandateOut)
|
||||
@@ -102,9 +120,8 @@ async def get_mandate(
|
||||
id: uuid.UUID,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> MandateOut:
|
||||
"""Get a single mandate with all its steps."""
|
||||
mandate = await _get_mandate(db, id)
|
||||
return MandateOut.model_validate(mandate)
|
||||
return _mandate_out(mandate)
|
||||
|
||||
|
||||
@router.put("/{id}", response_model=MandateOut)
|
||||
@@ -114,19 +131,14 @@ async def update_mandate(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
) -> MandateOut:
|
||||
"""Update a mandate's metadata."""
|
||||
mandate = await _get_mandate(db, id)
|
||||
|
||||
update_data = payload.model_dump(exclude_unset=True)
|
||||
for field, value in update_data.items():
|
||||
for field, value in payload.model_dump(exclude_unset=True).items():
|
||||
setattr(mandate, field, value)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(mandate)
|
||||
|
||||
# Reload with steps
|
||||
mandate = await _get_mandate(db, mandate.id)
|
||||
return MandateOut.model_validate(mandate)
|
||||
return _mandate_out(mandate)
|
||||
|
||||
|
||||
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, response_class=Response, response_model=None)
|
||||
@@ -135,7 +147,6 @@ async def delete_mandate(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
) -> None:
|
||||
"""Delete a mandate (only if in draft status)."""
|
||||
mandate = await _get_mandate(db, id)
|
||||
|
||||
if mandate.status != "draft":
|
||||
@@ -158,13 +169,9 @@ async def add_step(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
) -> MandateStepOut:
|
||||
"""Add a step to a mandate process."""
|
||||
mandate = await _get_mandate(db, id)
|
||||
|
||||
step = MandateStep(
|
||||
mandate_id=mandate.id,
|
||||
**payload.model_dump(),
|
||||
)
|
||||
step = MandateStep(mandate_id=mandate.id, **payload.model_dump())
|
||||
db.add(step)
|
||||
await db.commit()
|
||||
await db.refresh(step)
|
||||
@@ -177,7 +184,6 @@ async def list_steps(
|
||||
id: uuid.UUID,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> list[MandateStepOut]:
|
||||
"""List all steps for a mandate, ordered by step_order."""
|
||||
mandate = await _get_mandate(db, id)
|
||||
return [MandateStepOut.model_validate(s) for s in mandate.steps]
|
||||
|
||||
@@ -191,17 +197,17 @@ async def advance_mandate_endpoint(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
) -> MandateAdvanceOut:
|
||||
"""Advance a mandate to its next step or status."""
|
||||
try:
|
||||
mandate = await advance_mandate(id, db)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc))
|
||||
|
||||
# Reload with steps for complete output
|
||||
mandate = await _get_mandate(db, mandate.id)
|
||||
data = MandateOut.model_validate(mandate).model_dump()
|
||||
data["message"] = f"Mandat avance au statut : {mandate.status}"
|
||||
return MandateAdvanceOut(**data)
|
||||
out = _mandate_out(mandate)
|
||||
return MandateAdvanceOut(
|
||||
**out.model_dump(),
|
||||
message=f"Mandat avance au statut : {mandate.status}",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{id}/assign", response_model=MandateOut)
|
||||
@@ -211,15 +217,13 @@ async def assign_mandatee_endpoint(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
) -> MandateOut:
|
||||
"""Assign a mandatee to a mandate."""
|
||||
try:
|
||||
mandate = await assign_mandatee(id, payload.mandatee_id, db)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc))
|
||||
|
||||
# Reload with steps
|
||||
mandate = await _get_mandate(db, mandate.id)
|
||||
return MandateOut.model_validate(mandate)
|
||||
return _mandate_out(mandate)
|
||||
|
||||
|
||||
@router.post("/{id}/revoke", response_model=MandateOut)
|
||||
@@ -228,15 +232,13 @@ async def revoke_mandate_endpoint(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
) -> MandateOut:
|
||||
"""Revoke an active mandate."""
|
||||
try:
|
||||
mandate = await revoke_mandate(id, db)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc))
|
||||
|
||||
# Reload with steps
|
||||
mandate = await _get_mandate(db, mandate.id)
|
||||
return MandateOut.model_validate(mandate)
|
||||
return _mandate_out(mandate)
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -250,7 +252,6 @@ async def create_vote_session_for_step_endpoint(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
identity: DuniterIdentity = Depends(get_current_identity),
|
||||
) -> VoteSessionOut:
|
||||
"""Create a vote session linked to a mandate step."""
|
||||
try:
|
||||
session = await create_vote_session_for_step(id, step_id, db)
|
||||
except ValueError as exc:
|
||||
|
||||
Reference in New Issue
Block a user