This commit is contained in:
@@ -3,7 +3,7 @@ use axum::{
|
||||
Json, Router,
|
||||
extract::{Path, Query, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
routing::{delete, post},
|
||||
routing::{delete, patch, post},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
@@ -34,6 +34,10 @@ pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
|
||||
"/api/v1/tenants/{tenant}/devices/{asset}/archive",
|
||||
post(archive_device),
|
||||
)
|
||||
.route(
|
||||
"/api/v1/tenants/{tenant}/devices/{asset}/purpose",
|
||||
patch(update_device_purpose),
|
||||
)
|
||||
.route(
|
||||
"/api/v1/tenants/{tenant}/operation-plans/{plan}/revocation",
|
||||
post(revoke_plan),
|
||||
@@ -132,6 +136,66 @@ async fn archive_device(
|
||||
tx.commit().await?;
|
||||
Ok(Json(json!({"asset_id":asset, "archived":input.archived})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Purpose {
|
||||
purpose: String,
|
||||
}
|
||||
|
||||
fn normalize_purpose(raw: &str) -> Result<Option<String>, Error> {
|
||||
let normalized = raw.replace("\r\n", "\n");
|
||||
let text = normalized.trim();
|
||||
if text.chars().count() > 500
|
||||
|| normalized
|
||||
.chars()
|
||||
.any(|c| c.is_control() && !matches!(c, '\n' | '\t'))
|
||||
{
|
||||
return Err(Error(StatusCode::BAD_REQUEST, "VALIDATION_ERROR"));
|
||||
}
|
||||
Ok((!text.is_empty()).then(|| text.to_owned()))
|
||||
}
|
||||
|
||||
async fn update_device_purpose(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Path((tenant, asset)): Path<(Uuid, Uuid)>,
|
||||
Json(input): Json<Purpose>,
|
||||
) -> Result<Json<Value>, Error> {
|
||||
const EVENT: &str = "device_purpose_updated";
|
||||
let (p, mut tx) = context(&s, &h, tenant, "devices:revoke", EVENT).await?;
|
||||
crate::permissions::require_principal(&mut tx, &p, "inventory:read").await?;
|
||||
let purpose = normalize_purpose(&input.purpose)?;
|
||||
let current: Option<String> = sqlx::query_scalar(
|
||||
"SELECT purpose FROM assets WHERE tenant_id=$1 AND asset_id=$2
|
||||
AND EXISTS (SELECT 1 FROM agent_installations a WHERE a.tenant_id=$1 AND a.asset_id=$2
|
||||
AND NOT a.customer_transfer_pending)
|
||||
FOR UPDATE",
|
||||
)
|
||||
.bind(tenant)
|
||||
.bind(asset)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
||||
if current != purpose {
|
||||
sqlx::query("UPDATE assets SET purpose=$3 WHERE tenant_id=$1 AND asset_id=$2")
|
||||
.bind(tenant)
|
||||
.bind(asset)
|
||||
.bind(&purpose)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
audit(
|
||||
&mut tx,
|
||||
&p,
|
||||
EVENT,
|
||||
json!({"asset_id":asset,"cleared":purpose.is_none()}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
Ok(Json(json!({"asset_id":asset,"purpose":purpose})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Pairing {
|
||||
|
||||
Reference in New Issue
Block a user