summaryrefslogtreecommitdiff
path: root/makima/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'makima/src/db')
-rw-r--r--makima/src/db/models.rs2
-rw-r--r--makima/src/db/repository.rs42
2 files changed, 40 insertions, 4 deletions
diff --git a/makima/src/db/models.rs b/makima/src/db/models.rs
index 2951159..33b9795 100644
--- a/makima/src/db/models.rs
+++ b/makima/src/db/models.rs
@@ -2897,7 +2897,7 @@ pub struct Order {
pub order_type: String,
/// Flexible labels as JSON array of strings
pub labels: serde_json::Value,
- /// Linked directive (optional)
+ /// Linked directive (required for new orders, nullable for legacy rows)
pub directive_id: Option<Uuid>,
/// Linked directive step (optional)
pub directive_step_id: Option<Uuid>,
diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs
index cb6a0c6..d274548 100644
--- a/makima/src/db/repository.rs
+++ b/makima/src/db/repository.rs
@@ -6305,19 +6305,21 @@ pub async fn convert_order_to_step(
// Order Pickup
// =============================================================================
-/// Get available orders for pickup: open orders with no directive assigned,
+/// Get available orders for pickup: open orders with no directive assigned
+/// OR orders already linked to this specific directive that are not yet done,
/// sorted by priority (critical first) then creation date.
pub async fn get_available_orders_for_pickup(
pool: &PgPool,
owner_id: Uuid,
+ directive_id: Uuid,
) -> Result<Vec<Order>, sqlx::Error> {
sqlx::query_as::<_, Order>(
r#"
SELECT *
FROM orders
WHERE owner_id = $1
- AND status = 'open'
- AND directive_id IS NULL
+ AND status IN ('open', 'in_progress')
+ AND (directive_id IS NULL OR directive_id = $2)
ORDER BY CASE priority
WHEN 'critical' THEN 0
WHEN 'high' THEN 1
@@ -6328,6 +6330,7 @@ pub async fn get_available_orders_for_pickup(
"#,
)
.bind(owner_id)
+ .bind(directive_id)
.fetch_all(pool)
.await
}
@@ -6356,3 +6359,36 @@ pub async fn bulk_link_orders_to_directive(
Ok(result.rows_affected() as i64)
}
+/// Bulk update order status for a set of order IDs.
+/// Returns the count of updated rows.
+pub async fn bulk_update_order_status(
+ pool: &PgPool,
+ owner_id: Uuid,
+ order_ids: &[Uuid],
+ status: &str,
+) -> Result<i64, sqlx::Error> {
+ let result = sqlx::query(
+ r#"UPDATE orders SET status = $1, updated_at = NOW()
+ WHERE id = ANY($2) AND owner_id = $3"#,
+ )
+ .bind(status)
+ .bind(order_ids)
+ .bind(owner_id)
+ .execute(pool)
+ .await?;
+ Ok(result.rows_affected() as i64)
+}
+
+/// Get orders linked to a specific directive step.
+pub async fn get_orders_by_step_id(
+ pool: &PgPool,
+ step_id: Uuid,
+) -> Result<Vec<Order>, sqlx::Error> {
+ sqlx::query_as::<_, Order>(
+ r#"SELECT * FROM orders WHERE directive_step_id = $1"#,
+ )
+ .bind(step_id)
+ .fetch_all(pool)
+ .await
+}
+