feat(backend): add publish readiness endpoint and enforce publish gate contract
add backend endpoint for publish readiness checks enforce publish gate contract before version publication make publish preconditions explicit and consistent for clients
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from app.core.config import settings
|
||||
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.api_errors import raise_conflict
|
||||
from app.services.scheme_validation import build_scheme_validation_report
|
||||
from app.repositories.scheme_versions import get_current_scheme_version
|
||||
from app.repositories.schemes import get_scheme_record_by_scheme_id, publish_scheme
|
||||
from app.services.api_errors import raise_publish_not_ready
|
||||
from app.services.publish_readiness import build_publish_readiness
|
||||
|
||||
|
||||
async def publish_current_draft_scheme(
|
||||
@@ -11,22 +12,29 @@ 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 = await get_scheme_record_by_scheme_id(scheme_id)
|
||||
version = await get_current_scheme_version(
|
||||
scheme_id=scheme.scheme_id,
|
||||
scheme_version_id=version.scheme_version_id,
|
||||
current_version_number=scheme.current_version_number,
|
||||
)
|
||||
|
||||
if not validation["summary"]["is_publishable"]:
|
||||
raise_conflict(
|
||||
code="publish_validation_failed",
|
||||
message="Scheme is not publishable in current state",
|
||||
scheme_version_id=version.scheme_version_id,
|
||||
validation_summary=validation["summary"],
|
||||
if scheme.status != "draft" or version.status != "draft":
|
||||
raise_publish_not_ready(
|
||||
reason="Current scheme version is not publishable because it is not in draft state",
|
||||
details={
|
||||
"scheme_status": scheme.status,
|
||||
"scheme_version_status": version.status,
|
||||
"scheme_version_id": version.scheme_version_id,
|
||||
},
|
||||
)
|
||||
|
||||
if expected_scheme_version_id and expected_scheme_version_id != version.scheme_version_id:
|
||||
raise_publish_not_ready(
|
||||
reason="Draft scheme version is stale. Reload current draft state before publishing.",
|
||||
details={
|
||||
"expected_scheme_version_id": expected_scheme_version_id,
|
||||
"actual_scheme_version_id": version.scheme_version_id,
|
||||
},
|
||||
)
|
||||
|
||||
snapshot = await replace_scheme_version_pricing_snapshot(
|
||||
@@ -34,6 +42,18 @@ async def publish_current_draft_scheme(
|
||||
scheme_version_id=version.scheme_version_id,
|
||||
)
|
||||
|
||||
readiness = await build_publish_readiness(
|
||||
scheme_id=scheme.scheme_id,
|
||||
scheme_version_id=version.scheme_version_id,
|
||||
require_full_pricing_coverage=settings.publish_require_full_pricing_coverage,
|
||||
)
|
||||
|
||||
if not readiness["readiness"]["is_ready_to_publish"]:
|
||||
raise_publish_not_ready(
|
||||
reason="Scheme is not ready to publish",
|
||||
details=readiness,
|
||||
)
|
||||
|
||||
published_row = await publish_scheme(scheme.scheme_id)
|
||||
|
||||
await create_audit_event(
|
||||
@@ -46,6 +66,7 @@ async def publish_current_draft_scheme(
|
||||
"status": published_row.status,
|
||||
"pricing_snapshot": snapshot,
|
||||
"scheme_version_id": version.scheme_version_id,
|
||||
"publish_readiness": readiness,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -56,5 +77,5 @@ async def publish_current_draft_scheme(
|
||||
"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"],
|
||||
"publish_readiness": readiness,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user