feat(import): complete OpenAPI onboarding and verify delivery
This commit is contained in:
1 parent
bb3e376767
commit
e93de7015a
244 files changed
+57020
-5139
No files matched your search
@@ -1,8 +1,10 @@
|
||||
use crank_core::{
|
||||
OnboardingProjection, OnboardingStepId, ProductEventId, ProductEventKind, WorkspaceId,
|
||||
OnboardingProjection, OnboardingStepId, OperationId, ProductEventId, ProductEventKind,
|
||||
WorkspaceId,
|
||||
};
|
||||
use crank_registry::{
|
||||
OnboardingPresentationMilestone, RecordOnboardingMilestoneRequest, RegistryError,
|
||||
OnboardingImportContext, OnboardingPresentationMilestone, RecordOnboardingMilestoneRequest,
|
||||
RegisterOnboardingImportContextRequest, RegistryError, SelectOnboardingImportDraftRequest,
|
||||
};
|
||||
use serde_json::json;
|
||||
use time::OffsetDateTime;
|
||||
@@ -11,7 +13,10 @@ use crate::{
|
||||
error::ApiError,
|
||||
service::{
|
||||
AdminService, OnboardingEventPayload, OnboardingEventResponse, OnboardingFirstCallEvidence,
|
||||
OnboardingResponse, OnboardingStepView, ResetOnboardingSelectionResponse, format_timestamp,
|
||||
OnboardingImportChoiceView, OnboardingImportContextView, OnboardingImportDriftView,
|
||||
OnboardingResponse, OnboardingStepView, RegisterOnboardingImportContextPayload,
|
||||
RegisterOnboardingImportContextResponse, ResetOnboardingSelectionResponse,
|
||||
SelectOnboardingImportDraftPayload, SelectOnboardingImportDraftResponse, format_timestamp,
|
||||
new_prefixed_id,
|
||||
},
|
||||
};
|
||||
@@ -22,11 +27,12 @@ impl AdminService {
|
||||
workspace_id: &WorkspaceId,
|
||||
) -> Result<OnboardingResponse, ApiError> {
|
||||
self.ensure_workspace_exists(workspace_id).await?;
|
||||
let projection = self
|
||||
let snapshot = self
|
||||
.registry
|
||||
.ensure_onboarding_eligibility(workspace_id, OffsetDateTime::now_utc())
|
||||
.ensure_onboarding_eligibility_snapshot(workspace_id, OffsetDateTime::now_utc())
|
||||
.await?;
|
||||
self.map_onboarding_response(projection).await
|
||||
self.map_onboarding_response(snapshot.projection, snapshot.import_context)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn record_onboarding_event(
|
||||
@@ -67,13 +73,12 @@ impl AdminService {
|
||||
};
|
||||
// Presentation events must never exist outside the server-owned cohort,
|
||||
// including a direct API call before the first UI snapshot GET.
|
||||
let current = self
|
||||
.registry
|
||||
.ensure_onboarding_eligibility(workspace_id, OffsetDateTime::now_utc())
|
||||
self.registry
|
||||
.ensure_onboarding_eligibility_snapshot(workspace_id, OffsetDateTime::now_utc())
|
||||
.await?;
|
||||
let outcome = self
|
||||
.registry
|
||||
.record_onboarding_milestone(RecordOnboardingMilestoneRequest {
|
||||
.record_onboarding_milestone_snapshot(RecordOnboardingMilestoneRequest {
|
||||
workspace_id,
|
||||
event_id: &ProductEventId::new(new_prefixed_id("pe")),
|
||||
milestone,
|
||||
@@ -94,9 +99,18 @@ impl AdminService {
|
||||
.as_ref()
|
||||
.is_some_and(|record| record.event.kind == event_kind)
|
||||
{
|
||||
let latest = self
|
||||
.registry
|
||||
.ensure_onboarding_eligibility_snapshot(
|
||||
workspace_id,
|
||||
OffsetDateTime::now_utc(),
|
||||
)
|
||||
.await?;
|
||||
return Ok(OnboardingEventResponse {
|
||||
accepted: false,
|
||||
onboarding: self.map_onboarding_response(current).await?,
|
||||
onboarding: self
|
||||
.map_onboarding_response(latest.projection, latest.import_context)
|
||||
.await?,
|
||||
});
|
||||
}
|
||||
if replay.is_some() {
|
||||
@@ -122,7 +136,12 @@ impl AdminService {
|
||||
};
|
||||
Ok(OnboardingEventResponse {
|
||||
accepted: outcome.accepted,
|
||||
onboarding: self.map_onboarding_response(outcome.projection).await?,
|
||||
onboarding: self
|
||||
.map_onboarding_response(
|
||||
outcome.snapshot.projection,
|
||||
outcome.snapshot.import_context,
|
||||
)
|
||||
.await?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -132,9 +151,13 @@ impl AdminService {
|
||||
expected_revision: i64,
|
||||
) -> Result<ResetOnboardingSelectionResponse, ApiError> {
|
||||
self.ensure_workspace_exists(workspace_id).await?;
|
||||
let projection = self
|
||||
let snapshot = self
|
||||
.registry
|
||||
.reset_onboarding_selection(workspace_id, expected_revision, OffsetDateTime::now_utc())
|
||||
.reset_onboarding_selection_snapshot(
|
||||
workspace_id,
|
||||
expected_revision,
|
||||
OffsetDateTime::now_utc(),
|
||||
)
|
||||
.await
|
||||
.map_err(|error| match error {
|
||||
RegistryError::OnboardingStaleRevision => ApiError::conflict_with_context(
|
||||
@@ -145,13 +168,76 @@ impl AdminService {
|
||||
})?;
|
||||
Ok(ResetOnboardingSelectionResponse {
|
||||
selection_reset: true,
|
||||
onboarding: self.map_onboarding_response(projection).await?,
|
||||
onboarding: self
|
||||
.map_onboarding_response(snapshot.projection, snapshot.import_context)
|
||||
.await?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn register_onboarding_import_context(
|
||||
&self,
|
||||
workspace_id: &WorkspaceId,
|
||||
payload: RegisterOnboardingImportContextPayload,
|
||||
) -> Result<RegisterOnboardingImportContextResponse, ApiError> {
|
||||
self.ensure_workspace_exists(workspace_id).await?;
|
||||
validate_application_identity(&payload.application_idempotency_key, &payload.plan_digest)?;
|
||||
let snapshot = self
|
||||
.registry
|
||||
.register_onboarding_import_context_snapshot(RegisterOnboardingImportContextRequest {
|
||||
workspace_id,
|
||||
application_idempotency_key: &payload.application_idempotency_key,
|
||||
plan_digest: &payload.plan_digest,
|
||||
expected_revision: payload.expected_revision,
|
||||
occurred_at: OffsetDateTime::now_utc(),
|
||||
})
|
||||
.await?;
|
||||
Ok(RegisterOnboardingImportContextResponse {
|
||||
registered: true,
|
||||
onboarding: self
|
||||
.map_onboarding_response(snapshot.projection, snapshot.import_context)
|
||||
.await?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn select_onboarding_import_draft(
|
||||
&self,
|
||||
workspace_id: &WorkspaceId,
|
||||
payload: SelectOnboardingImportDraftPayload,
|
||||
) -> Result<SelectOnboardingImportDraftResponse, ApiError> {
|
||||
self.ensure_workspace_exists(workspace_id).await?;
|
||||
validate_application_identity(&payload.application_idempotency_key, &payload.plan_digest)?;
|
||||
if payload.operation_id.is_empty() || payload.operation_id.len() > 128 {
|
||||
return Err(ApiError::validation(
|
||||
"operation_id must contain 1..128 bytes",
|
||||
));
|
||||
}
|
||||
if payload.operation_version == 0 {
|
||||
return Err(ApiError::validation("operation_version must be positive"));
|
||||
}
|
||||
let snapshot = self
|
||||
.registry
|
||||
.select_onboarding_import_draft_snapshot(SelectOnboardingImportDraftRequest {
|
||||
workspace_id,
|
||||
application_idempotency_key: &payload.application_idempotency_key,
|
||||
plan_digest: &payload.plan_digest,
|
||||
operation_id: &OperationId::new(payload.operation_id),
|
||||
operation_version: payload.operation_version,
|
||||
expected_revision: payload.expected_revision,
|
||||
occurred_at: OffsetDateTime::now_utc(),
|
||||
})
|
||||
.await?;
|
||||
Ok(SelectOnboardingImportDraftResponse {
|
||||
selected: true,
|
||||
onboarding: self
|
||||
.map_onboarding_response(snapshot.projection, snapshot.import_context)
|
||||
.await?,
|
||||
})
|
||||
}
|
||||
|
||||
async fn map_onboarding_response(
|
||||
&self,
|
||||
projection: OnboardingProjection,
|
||||
import_context: Option<OnboardingImportContext>,
|
||||
) -> Result<OnboardingResponse, ApiError> {
|
||||
let endpoint = if let Some(agent_id) = projection.agent_id.as_ref() {
|
||||
let workspace = self.get_workspace(&projection.workspace_id).await?;
|
||||
@@ -195,6 +281,7 @@ impl AdminService {
|
||||
_ => None,
|
||||
};
|
||||
let first_incomplete = projection.steps.iter().position(|step| !step.completed);
|
||||
let import_context = import_context.map(map_import_context);
|
||||
Ok(OnboardingResponse {
|
||||
schema_version: 1,
|
||||
workspace_id: projection.workspace_id.as_str().to_owned(),
|
||||
@@ -244,10 +331,61 @@ impl AdminService {
|
||||
.map(|id| id.as_str().to_owned()),
|
||||
mcp_endpoint: endpoint,
|
||||
first_call,
|
||||
import_context,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn map_import_context(context: OnboardingImportContext) -> OnboardingImportContextView {
|
||||
OnboardingImportContextView {
|
||||
application_idempotency_key: context.application_idempotency_key,
|
||||
plan_digest: context.plan_digest,
|
||||
report: context.report,
|
||||
choices: context
|
||||
.choices
|
||||
.into_iter()
|
||||
.map(|choice| OnboardingImportChoiceView {
|
||||
key: choice.key,
|
||||
name: choice.name,
|
||||
outcome: choice.outcome,
|
||||
operation_id: choice.operation_id,
|
||||
version: choice.version,
|
||||
reason: choice.reason,
|
||||
selectable: choice.selectable,
|
||||
})
|
||||
.collect(),
|
||||
selected_operation_id: context
|
||||
.selected_operation_id
|
||||
.map(|id| id.as_str().to_owned()),
|
||||
selected_operation_version: context.selected_operation_version,
|
||||
drift: context.drift.map(|drift| OnboardingImportDriftView {
|
||||
reason: drift.reason,
|
||||
current_operation_version: drift.current_operation_version,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_application_identity(
|
||||
application_idempotency_key: &str,
|
||||
plan_digest: &str,
|
||||
) -> Result<(), ApiError> {
|
||||
if application_idempotency_key.is_empty() || application_idempotency_key.len() > 128 {
|
||||
return Err(ApiError::validation(
|
||||
"application_idempotency_key must contain 1..128 bytes",
|
||||
));
|
||||
}
|
||||
let valid_digest = plan_digest.len() == 64
|
||||
&& plan_digest
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte));
|
||||
if !valid_digest {
|
||||
return Err(ApiError::validation(
|
||||
"plan_digest must be 64 lowercase hex characters",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn action_code(step: OnboardingStepId) -> &'static str {
|
||||
match step {
|
||||
OnboardingStepId::Operation => "create_operation",
|
||||
|
||||
Reference in new issue
Block a user