66 lines
2.4 KiB
Python
66 lines
2.4 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.repositories.scheme_versions import get_current_scheme_version
|
|
from app.repositories.schemes import get_scheme_record_by_scheme_id, publish_scheme
|
|
from app.services.scheme_validation import build_scheme_validation_report
|
|
|
|
|
|
async def publish_current_draft_scheme(
|
|
*,
|
|
scheme_id: str,
|
|
) -> dict:
|
|
scheme = await get_scheme_record_by_scheme_id(scheme_id)
|
|
version = await get_current_scheme_version(
|
|
scheme_id=scheme.scheme_id,
|
|
current_version_number=scheme.current_version_number,
|
|
)
|
|
|
|
if scheme.status != "draft" or version.status != "draft":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_409_CONFLICT,
|
|
detail="Current scheme version is not publishable because it is not in draft state",
|
|
)
|
|
|
|
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"],
|
|
}
|