summaryrefslogtreecommitdiff
path: root/makima/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'makima/src/db')
-rw-r--r--makima/src/db/models.rs7
-rw-r--r--makima/src/db/repository.rs53
2 files changed, 56 insertions, 4 deletions
diff --git a/makima/src/db/models.rs b/makima/src/db/models.rs
index 33b9795..f9a34b8 100644
--- a/makima/src/db/models.rs
+++ b/makima/src/db/models.rs
@@ -2860,6 +2860,9 @@ pub struct CreateDirectiveStepRequest {
#[serde(default)]
pub order_index: i32,
pub generation: Option<i32>,
+ /// Optional order ID to auto-link this step to an order.
+ #[serde(default)]
+ pub order_id: Option<Uuid>,
}
/// Request to update a directive step.
@@ -2891,7 +2894,7 @@ pub struct Order {
pub description: Option<String>,
/// Priority: critical, high, medium, low, none
pub priority: String,
- /// Status: open, in_progress, done, archived
+ /// Status: open, in_progress, under_review, done, archived
pub status: String,
/// Type of work: feature, bug, spike, chore, improvement
pub order_type: String,
@@ -2957,7 +2960,7 @@ pub struct OrderListResponse {
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct OrderListQuery {
- /// Filter by status (e.g., "open", "in_progress", "done", "archived")
+ /// Filter by status (e.g., "open", "in_progress", "under_review", "done", "archived")
pub status: Option<String>,
/// Filter by order type (e.g., "feature", "bug", "spike", "chore", "improvement")
#[serde(rename = "type")]
diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs
index d274548..aa1203a 100644
--- a/makima/src/db/repository.rs
+++ b/makima/src/db/repository.rs
@@ -5387,7 +5387,8 @@ pub async fn create_directive_step(
req: CreateDirectiveStepRequest,
) -> Result<DirectiveStep, sqlx::Error> {
let generation = req.generation.unwrap_or(1);
- sqlx::query_as::<_, DirectiveStep>(
+ let order_id = req.order_id;
+ let step = sqlx::query_as::<_, DirectiveStep>(
r#"
INSERT INTO directive_steps (directive_id, name, description, task_plan, depends_on, order_index, generation)
VALUES ($1, $2, $3, $4, $5, $6, $7)
@@ -5402,7 +5403,20 @@ pub async fn create_directive_step(
.bind(req.order_index)
.bind(generation)
.fetch_one(pool)
- .await
+ .await?;
+
+ // If an order_id was provided, auto-link the order to this step
+ if let Some(oid) = order_id {
+ sqlx::query(
+ r#"UPDATE orders SET directive_step_id = $1, updated_at = NOW() WHERE id = $2"#,
+ )
+ .bind(step.id)
+ .bind(oid)
+ .execute(pool)
+ .await?;
+ }
+
+ Ok(step)
}
/// Batch create multiple directive steps.
@@ -6392,3 +6406,38 @@ pub async fn get_orders_by_step_id(
.await
}
+/// Reconcile directive orders by checking linked step statuses.
+/// - Orders linked to completed steps are marked "done"
+/// - Orders linked to running/ready steps are marked "under_review"
+/// Returns the count of orders updated.
+pub async fn reconcile_directive_orders(
+ pool: &PgPool,
+ owner_id: Uuid,
+ directive_id: Uuid,
+) -> Result<i64, sqlx::Error> {
+ let rows: Vec<(Uuid,)> = sqlx::query_as(
+ r#"
+ UPDATE orders o
+ SET status = CASE
+ WHEN ds.status = 'completed' THEN 'done'
+ WHEN ds.status IN ('running', 'ready') THEN 'under_review'
+ ELSE o.status
+ END,
+ updated_at = NOW()
+ FROM directive_steps ds
+ WHERE o.directive_step_id = ds.id
+ AND o.directive_id = $1
+ AND o.owner_id = $2
+ AND o.status NOT IN ('done', 'archived')
+ AND ds.status IN ('completed', 'running', 'ready')
+ RETURNING o.id
+ "#,
+ )
+ .bind(directive_id)
+ .bind(owner_id)
+ .fetch_all(pool)
+ .await?;
+
+ Ok(rows.len() as i64)
+}
+