summaryrefslogtreecommitdiff
path: root/makima/src/db/repository.rs
diff options
context:
space:
mode:
Diffstat (limited to 'makima/src/db/repository.rs')
-rw-r--r--makima/src/db/repository.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs
index b5888c9..2ef3fbc 100644
--- a/makima/src/db/repository.rs
+++ b/makima/src/db/repository.rs
@@ -6319,3 +6319,58 @@ pub async fn convert_order_to_step(
Ok(Some(step))
}
+// =============================================================================
+// Order Pickup
+// =============================================================================
+
+/// Get available orders for pickup: open orders with no directive assigned,
+/// sorted by priority (critical first) then creation date.
+pub async fn get_available_orders_for_pickup(
+ pool: &PgPool,
+ owner_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
+ ORDER BY CASE priority
+ WHEN 'critical' THEN 0
+ WHEN 'high' THEN 1
+ WHEN 'medium' THEN 2
+ WHEN 'low' THEN 3
+ ELSE 4
+ END ASC, created_at ASC
+ "#,
+ )
+ .bind(owner_id)
+ .fetch_all(pool)
+ .await
+}
+
+/// Bulk-link orders to a directive by setting directive_id on matching orders.
+/// Returns the count of updated rows.
+pub async fn bulk_link_orders_to_directive(
+ pool: &PgPool,
+ owner_id: Uuid,
+ order_ids: &[Uuid],
+ directive_id: Uuid,
+) -> Result<i64, sqlx::Error> {
+ let result = sqlx::query(
+ r#"
+ UPDATE orders
+ SET directive_id = $1, updated_at = NOW()
+ WHERE id = ANY($2)
+ AND owner_id = $3
+ "#,
+ )
+ .bind(directive_id)
+ .bind(order_ids)
+ .bind(owner_id)
+ .execute(pool)
+ .await?;
+ Ok(result.rows_affected() as i64)
+}
+