standardize typed error responses across draft, pricing and publish endpoints reduce contract drift between related flows keep client-side handling more predictable and consistent
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
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
|
|
|
|
|
|
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_conflict(
|
|
code="publish_validation_failed",
|
|
message="Scheme is not publishable in current state",
|
|
scheme_version_id=version.scheme_version_id,
|
|
validation_summary=validation["summary"],
|
|
)
|
|
|
|
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"],
|
|
}
|