|
| 1 | +//! POST /api/projects/:project_id/nodes/:id/portal-target-project — create an org project and set this task’s portal in one transaction. |
| 2 | +
|
| 3 | +use axum::{ |
| 4 | + extract::{Path, State}, |
| 5 | + http::StatusCode, |
| 6 | + routing::post, |
| 7 | + Json, Router, |
| 8 | +}; |
| 9 | +use serde::Deserialize; |
| 10 | + |
| 11 | +use crate::app::{ |
| 12 | + db, |
| 13 | + error::AppError, |
| 14 | + session::ApiAuthenticatedSession, |
| 15 | + AppState, |
| 16 | +}; |
| 17 | + |
| 18 | +use super::types::ApiNode; |
| 19 | + |
| 20 | +#[derive(Debug, Deserialize)] |
| 21 | +pub struct CreatePortalTargetProjectRequest { |
| 22 | + pub title: String, |
| 23 | + pub team_id: String, |
| 24 | +} |
| 25 | + |
| 26 | +/// POST /api/projects/:project_id/nodes/:id/portal-target-project |
| 27 | +pub async fn post_create_portal_target_project( |
| 28 | + ApiAuthenticatedSession(session): ApiAuthenticatedSession, |
| 29 | + State(state): State<AppState>, |
| 30 | + Path(params): Path<super::types::NodePathParams>, |
| 31 | + Json(body): Json<CreatePortalTargetProjectRequest>, |
| 32 | +) -> Result<(StatusCode, Json<ApiNode>), AppError> { |
| 33 | + let title = body.title.trim(); |
| 34 | + if title.is_empty() || title.len() > 255 { |
| 35 | + return Err(AppError::Validation( |
| 36 | + "Title must be 1–255 characters".to_string(), |
| 37 | + )); |
| 38 | + } |
| 39 | + if body.team_id.is_empty() { |
| 40 | + return Err(AppError::Validation( |
| 41 | + "team_id must not be empty".to_string(), |
| 42 | + )); |
| 43 | + } |
| 44 | + |
| 45 | + let _project = |
| 46 | + super::helpers::ensure_project_accessible(&state.db, ¶ms.project_id, &session.user_id) |
| 47 | + .await?; |
| 48 | + |
| 49 | + let node = db::nodes::find_by_id(&state.db, ¶ms.id) |
| 50 | + .await? |
| 51 | + .ok_or_else(|| AppError::NotFound("Node not found".to_string()))?; |
| 52 | + |
| 53 | + if node.project_id != params.project_id { |
| 54 | + return Err(AppError::NotFound("Node not found".to_string())); |
| 55 | + } |
| 56 | + |
| 57 | + let org_id = node.organization_id.as_deref().ok_or_else(|| AppError::Internal)?; |
| 58 | + |
| 59 | + let team = db::teams::find_by_id(&state.db, &body.team_id) |
| 60 | + .await? |
| 61 | + .ok_or_else(|| AppError::Validation("Invalid team".to_string()))?; |
| 62 | + |
| 63 | + if team.organization_id != org_id { |
| 64 | + return Err(AppError::Validation("Invalid team".to_string())); |
| 65 | + } |
| 66 | + |
| 67 | + let new_id = ulid::Ulid::new().to_string(); |
| 68 | + let new_project = db::projects::NewProject { |
| 69 | + id: new_id.clone(), |
| 70 | + title: title.to_string(), |
| 71 | + user_id: session.user_id.clone(), |
| 72 | + organization_id: org_id.to_string(), |
| 73 | + team_id: team.id, |
| 74 | + due_utc: None, |
| 75 | + due_timezone: None, |
| 76 | + }; |
| 77 | + |
| 78 | + let mut tx = state.db.begin().await?; |
| 79 | + db::projects::insert(&mut *tx, &new_project).await?; |
| 80 | + |
| 81 | + super::helpers::validate_portal_target_for_node( |
| 82 | + &state.db, |
| 83 | + &mut tx, |
| 84 | + &node, |
| 85 | + &new_id, |
| 86 | + &session.user_id, |
| 87 | + ) |
| 88 | + .await?; |
| 89 | + |
| 90 | + db::node_portal_links::upsert(&mut *tx, &node.id, &new_id) |
| 91 | + .await |
| 92 | + .map_err(super::helpers::map_node_portal_upsert_error)?; |
| 93 | + |
| 94 | + tx.commit().await?; |
| 95 | + |
| 96 | + let updated = db::nodes::find_by_id(&state.db, &node.id) |
| 97 | + .await? |
| 98 | + .ok_or_else(|| AppError::Internal)?; |
| 99 | + |
| 100 | + let response = super::helpers::api_node_with_portal(&state.db, &updated).await?; |
| 101 | + |
| 102 | + Ok((StatusCode::CREATED, Json(response))) |
| 103 | +} |
| 104 | + |
| 105 | +pub fn routes() -> Router<AppState> { |
| 106 | + Router::new().route( |
| 107 | + "/api/projects/:project_id/nodes/:id/portal-target-project", |
| 108 | + post(post_create_portal_target_project), |
| 109 | + ) |
| 110 | +} |
0 commit comments