summaryrefslogtreecommitdiff
path: root/makima/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'makima/src/db')
-rw-r--r--makima/src/db/models.rs9
-rw-r--r--makima/src/db/repository.rs55
2 files changed, 64 insertions, 0 deletions
diff --git a/makima/src/db/models.rs b/makima/src/db/models.rs
index 19ebb13..bfed942 100644
--- a/makima/src/db/models.rs
+++ b/makima/src/db/models.rs
@@ -2839,6 +2839,15 @@ pub struct CleanupTasksResponse {
pub deleted: i64,
}
+/// Response for pick_up_orders endpoint.
+#[derive(Debug, Serialize, ToSchema)]
+#[serde(rename_all = "camelCase")]
+pub struct PickUpOrdersResponse {
+ pub message: String,
+ pub order_count: i64,
+ pub task_id: Option<Uuid>,
+}
+
/// Request to create a directive step.
#[derive(Debug, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
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)
+}
+