add optimistic concurrency guards via expected scheme version id protect draft editor, pricing snapshot, remap and publish flows from stale mutations protect version creation from stale current version state keep backward compatibility with optional query guards verify 409 conflict behavior for stale clients and 200 for valid flows
60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
from fastapi import HTTPException, status
|
|
|
|
from app.repositories.audit import create_audit_event
|
|
from app.repositories.scheme_version_pricing import replace_scheme_version_pricing_snapshot
|
|
from app.services.draft_guard import get_current_draft_context
|
|
from app.repositories.schemes import publish_scheme
|
|
from app.services.scheme_validation import build_scheme_validation_report
|
|
|
|
|
|
async def publish_current_draft_scheme(
|
|
*,
|
|
scheme_id: str,
|
|
expected_scheme_version_id: str | None = None,
|
|
) -> dict:
|
|
scheme, version = await get_current_draft_context(
|
|
scheme_id=scheme_id,
|
|
expected_scheme_version_id=expected_scheme_version_id,
|
|
)
|
|
|
|
validation = await build_scheme_validation_report(
|
|
scheme_id=scheme.scheme_id,
|
|
scheme_version_id=version.scheme_version_id,
|
|
)
|
|
|
|
if not validation["summary"]["is_publishable"]:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail="Scheme is not publishable in current state",
|
|
)
|
|
|
|
snapshot = await replace_scheme_version_pricing_snapshot(
|
|
scheme_id=scheme.scheme_id,
|
|
scheme_version_id=version.scheme_version_id,
|
|
)
|
|
|
|
published_row = await publish_scheme(scheme.scheme_id)
|
|
|
|
await create_audit_event(
|
|
scheme_id=published_row.scheme_id,
|
|
event_type="scheme.published",
|
|
object_type="scheme",
|
|
object_ref=published_row.scheme_id,
|
|
details={
|
|
"current_version_number": published_row.current_version_number,
|
|
"status": published_row.status,
|
|
"pricing_snapshot": snapshot,
|
|
"scheme_version_id": version.scheme_version_id,
|
|
},
|
|
)
|
|
|
|
return {
|
|
"scheme_id": published_row.scheme_id,
|
|
"scheme_version_id": version.scheme_version_id,
|
|
"status": published_row.status,
|
|
"current_version_number": published_row.current_version_number,
|
|
"published_at": published_row.published_at.isoformat() if published_row.published_at else None,
|
|
"pricing_snapshot": snapshot,
|
|
"validation_summary": validation["summary"],
|
|
}
|