Filter orders before pagination and expose recorded decisions and result links
This commit is contained in:
@@ -1348,12 +1348,17 @@ enum OrderSort {
|
||||
Recent,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Default, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct OrderListQuery {
|
||||
sort: Option<OrderSort>,
|
||||
cursor: Option<String>,
|
||||
limit: Option<u16>,
|
||||
q: Option<String>,
|
||||
device: Option<String>,
|
||||
status: Option<String>,
|
||||
from: Option<DateTime<Utc>>,
|
||||
until: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
async fn operations_http(
|
||||
@@ -1363,16 +1368,48 @@ async fn operations_http(
|
||||
axum::extract::Query(query): axum::extract::Query<OrderListQuery>,
|
||||
) -> Result<Json<Value>> {
|
||||
let p = principal(&s, &h, false).await?;
|
||||
order_list(&s, &p, t, query).await.map(Json)
|
||||
}
|
||||
|
||||
async fn order_list(
|
||||
s: &crate::State,
|
||||
p: &Principal,
|
||||
t: Uuid,
|
||||
query: OrderListQuery,
|
||||
) -> Result<Value> {
|
||||
scope(&p, t, false)?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
||||
crate::permissions::require_principal(&mut tx, &p, "operations:read").await?;
|
||||
let recent = matches!(query.sort, Some(OrderSort::Recent));
|
||||
let q = query.q.as_deref().map(str::trim).filter(|q| !q.is_empty());
|
||||
let device = query.device.as_deref().map(str::trim).filter(|q| !q.is_empty());
|
||||
let status = query.status.as_deref();
|
||||
if [q, device].into_iter().flatten().any(|s| s.len() > 256 || s.chars().any(char::is_control))
|
||||
|| status.is_some_and(|s| !matches!(s, "pending_approval" | "approved" | "rejected" | "expired"
|
||||
| "revoked" | "queued" | "running" | "succeeded" | "partially_succeeded"
|
||||
| "failed" | "cancelled" | "needs_attention"))
|
||||
|| query.from.zip(query.until).is_some_and(|(start, end)| start >= end)
|
||||
{
|
||||
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
||||
}
|
||||
let filtered = q.is_some() || device.is_some() || status.is_some() || query.from.is_some() || query.until.is_some();
|
||||
let recent = filtered || matches!(query.sort, Some(OrderSort::Recent));
|
||||
let ordering = if recent { "operations:created_at:desc" } else { "operations:operation_id:asc" };
|
||||
let mut page = crate::pagination::Cursor::parse(&s, &p, ordering, crate::pagination::PageQuery {
|
||||
let device_names = device.is_some() && p.permits_scope("inventory:read")
|
||||
&& crate::permissions::allowed(&mut tx, &p.name, "inventory:read").await?;
|
||||
let cursor_scope = if filtered {
|
||||
format!("{ordering}:{}", hash(json!([q,device,status,query.from,query.until,device_names]).to_string().as_bytes()))
|
||||
} else { ordering.to_owned() };
|
||||
let mut page = crate::pagination::Cursor::parse(&s, &p, &cursor_scope, crate::pagination::PageQuery {
|
||||
cursor: query.cursor, limit: query.limit,
|
||||
}).map_err(|status| Error(status, "VALIDATION_ERROR"))?;
|
||||
let limit = i64::from(page.limit) + 1;
|
||||
let ids: Vec<(Uuid, DateTime<Utc>)> = if recent {
|
||||
let ids: Vec<(Uuid, DateTime<Utc>)> = if filtered {
|
||||
sqlx::query_as(include_str!("order_filter.sql"))
|
||||
.bind(page.as_of).bind(page.last_created_at).bind(page.last)
|
||||
.bind(query.from).bind(query.until).bind(q).bind(status).bind(device)
|
||||
.bind(device.and_then(|s| Uuid::parse_str(s).ok())).bind(device_names).bind(limit)
|
||||
.fetch_all(&mut *tx).await?
|
||||
} else if recent {
|
||||
sqlx::query_as("SELECT operation_id,created_at FROM approval_requests WHERE created_at<=$1
|
||||
AND ($2::timestamptz IS NULL OR (created_at,operation_id)<($2,$3))
|
||||
ORDER BY created_at DESC,operation_id DESC LIMIT $4")
|
||||
@@ -1399,9 +1436,9 @@ async fn operations_http(
|
||||
for (id, _) in ids.into_iter().take(usize::from(page.limit)) {
|
||||
items.push(operation(&s, &p, t, id).await?)
|
||||
}
|
||||
Ok(Json(
|
||||
Ok(
|
||||
json!({"items":items,"next_cursor":next,"completeness":if truncated{"partial"}else{"complete"},"truncated":truncated,"as_of":page.as_of}),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user