|
| 1 | +use axum::{ |
| 2 | + extract::{Path, State}, |
| 3 | + http::StatusCode, |
| 4 | + Json, |
| 5 | +}; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use std::sync::Arc; |
| 8 | +use uuid::Uuid; |
| 9 | + |
| 10 | +use crate::AppState; |
| 11 | + |
| 12 | +#[derive(Serialize)] |
| 13 | +pub struct ApprovalResponse { |
| 14 | + pub execution_id: String, |
| 15 | + pub step_key: String, |
| 16 | + pub status: String, |
| 17 | + pub data: Option<serde_json::Value>, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Deserialize)] |
| 21 | +pub struct SubmitApprovalRequest { |
| 22 | + pub data: serde_json::Value, |
| 23 | +} |
| 24 | + |
| 25 | +/// Get approval data for a suspended execution step. |
| 26 | +/// The execution_id in the URL is the root execution ID. |
| 27 | +pub async fn get_approval( |
| 28 | + State(state): State<Arc<AppState>>, |
| 29 | + Path((execution_id, step_key)): Path<(String, String)>, |
| 30 | +) -> Result<Json<ApprovalResponse>, StatusCode> { |
| 31 | + let execution_id_uuid = Uuid::parse_str(&execution_id).map_err(|_| StatusCode::BAD_REQUEST)?; |
| 32 | + |
| 33 | + // Get project_id from execution and set RLS |
| 34 | + let project_id = state |
| 35 | + .db |
| 36 | + .get_project_id_from_execution(&execution_id_uuid) |
| 37 | + .await |
| 38 | + .map_err(|e| { |
| 39 | + tracing::error!("Failed to get project_id from execution: {}", e); |
| 40 | + StatusCode::NOT_FOUND |
| 41 | + })?; |
| 42 | + |
| 43 | + state |
| 44 | + .db |
| 45 | + .set_project_id(&project_id, false) |
| 46 | + .await |
| 47 | + .map_err(|e| { |
| 48 | + tracing::error!("Failed to set project_id: {}", e); |
| 49 | + StatusCode::INTERNAL_SERVER_ERROR |
| 50 | + })?; |
| 51 | + |
| 52 | + let execution = state |
| 53 | + .db |
| 54 | + .get_execution(&execution_id_uuid) |
| 55 | + .await |
| 56 | + .map_err(|_| StatusCode::NOT_FOUND)?; |
| 57 | + |
| 58 | + // If not waiting, return status without data so UI can show "already handled" |
| 59 | + if execution.status != "waiting" { |
| 60 | + return Ok(Json(ApprovalResponse { |
| 61 | + execution_id, |
| 62 | + step_key, |
| 63 | + status: execution.status, |
| 64 | + data: None, |
| 65 | + })); |
| 66 | + } |
| 67 | + |
| 68 | + // Construct event topic using this execution's workflow_id (this is the root) |
| 69 | + let topic = format!("workflow/{}/{}", execution.workflow_id, execution_id_uuid); |
| 70 | + |
| 71 | + let events = state |
| 72 | + .db |
| 73 | + .get_events(&topic, &project_id, None, None, 100) |
| 74 | + .await |
| 75 | + .map_err(|e| { |
| 76 | + tracing::error!("Failed to get events for topic {}: {}", topic, e); |
| 77 | + StatusCode::INTERNAL_SERVER_ERROR |
| 78 | + })?; |
| 79 | + |
| 80 | + // Find the suspend event matching this step_key |
| 81 | + let suspend_event_type = format!("suspend_{}", step_key); |
| 82 | + let suspend_data = events |
| 83 | + .iter() |
| 84 | + .rev() // Most recent first |
| 85 | + .find(|e| { |
| 86 | + e.event_type |
| 87 | + .as_ref() |
| 88 | + .map(|t| t == &suspend_event_type) |
| 89 | + .unwrap_or(false) |
| 90 | + }) |
| 91 | + .map(|e| e.data.clone()); |
| 92 | + |
| 93 | + Ok(Json(ApprovalResponse { |
| 94 | + execution_id, |
| 95 | + step_key, |
| 96 | + status: execution.status, |
| 97 | + data: suspend_data, |
| 98 | + })) |
| 99 | +} |
| 100 | + |
| 101 | +/// Submit approval response for a suspended execution step (unauthenticated). |
| 102 | +/// The execution_id in the URL is the root execution ID. |
| 103 | +pub async fn submit_approval( |
| 104 | + State(state): State<Arc<AppState>>, |
| 105 | + Path((execution_id, step_key)): Path<(String, String)>, |
| 106 | + Json(req): Json<SubmitApprovalRequest>, |
| 107 | +) -> Result<StatusCode, StatusCode> { |
| 108 | + let execution_id_uuid = Uuid::parse_str(&execution_id).map_err(|_| StatusCode::BAD_REQUEST)?; |
| 109 | + |
| 110 | + // Get project_id from execution and set RLS |
| 111 | + let project_id = state |
| 112 | + .db |
| 113 | + .get_project_id_from_execution(&execution_id_uuid) |
| 114 | + .await |
| 115 | + .map_err(|e| { |
| 116 | + tracing::error!("Failed to get project_id from execution: {}", e); |
| 117 | + StatusCode::NOT_FOUND |
| 118 | + })?; |
| 119 | + |
| 120 | + state |
| 121 | + .db |
| 122 | + .set_project_id(&project_id, false) |
| 123 | + .await |
| 124 | + .map_err(|e| { |
| 125 | + tracing::error!("Failed to set project_id: {}", e); |
| 126 | + StatusCode::INTERNAL_SERVER_ERROR |
| 127 | + })?; |
| 128 | + |
| 129 | + // Verify execution is still waiting |
| 130 | + let execution = state |
| 131 | + .db |
| 132 | + .get_execution(&execution_id_uuid) |
| 133 | + .await |
| 134 | + .map_err(|_| StatusCode::NOT_FOUND)?; |
| 135 | + |
| 136 | + if execution.status != "waiting" { |
| 137 | + return Err(StatusCode::CONFLICT); |
| 138 | + } |
| 139 | + |
| 140 | + // Publish resume event |
| 141 | + let topic = format!("workflow/{}/{}", execution.workflow_id, execution_id_uuid); |
| 142 | + let event_type = format!("resume_{}", step_key); |
| 143 | + let events: Vec<(Option<String>, serde_json::Value, Option<Uuid>, i32)> = |
| 144 | + vec![(Some(event_type), req.data, None, 0)]; |
| 145 | + |
| 146 | + state |
| 147 | + .db |
| 148 | + .publish_events_batch(topic, events, None, None, &project_id) |
| 149 | + .await |
| 150 | + .map_err(|e| { |
| 151 | + tracing::error!( |
| 152 | + "Failed to publish resume event for execution {}: {}", |
| 153 | + execution_id_uuid, |
| 154 | + e |
| 155 | + ); |
| 156 | + StatusCode::INTERNAL_SERVER_ERROR |
| 157 | + })?; |
| 158 | + |
| 159 | + Ok(StatusCode::OK) |
| 160 | +} |
0 commit comments