feat: edit/delete authorities, blocked when referenced (#30)
This commit is contained in:
@@ -3,18 +3,19 @@
|
||||
use auth::{Authorized, EditCatalogue, ViewInternal};
|
||||
use axum::{
|
||||
Json, Router,
|
||||
extract::{Query, State},
|
||||
extract::{Path, Query, State},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
};
|
||||
use domain::{AuditActor, AuthorityKind, LocalizedLabel, NewAuthority};
|
||||
use domain::{AuditActor, AuthorityId, AuthorityKind, LocalizedLabel, NewAuthority};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
use crate::{
|
||||
AppState,
|
||||
admin_objects::LabelView,
|
||||
admin_vocab::{CreatedId, LabelInput},
|
||||
admin_vocab::{CreatedId, InUseView, LabelInput},
|
||||
};
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
@@ -129,9 +130,125 @@ pub(crate) async fn create_authority(
|
||||
Ok((StatusCode::CREATED, Json(CreatedId { id: id.to_string() })))
|
||||
}
|
||||
|
||||
pub(crate) fn routes() -> Router<AppState> {
|
||||
Router::new().route(
|
||||
"/api/admin/authorities",
|
||||
get(list_authorities).post(create_authority),
|
||||
)
|
||||
#[derive(Deserialize, ToSchema)]
|
||||
pub(crate) struct UpdateAuthorityRequest {
|
||||
pub external_uri: Option<String>,
|
||||
pub labels: Vec<LabelInput>,
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
patch, path = "/api/admin/authorities/{id}",
|
||||
request_body = UpdateAuthorityRequest,
|
||||
params(("id" = String, Path, description = "Authority id (UUID)")),
|
||||
responses(
|
||||
(status = 204),
|
||||
(status = 401),
|
||||
(status = 403),
|
||||
(status = 404)
|
||||
)
|
||||
)]
|
||||
pub(crate) async fn update_authority(
|
||||
auth: Authorized<EditCatalogue>,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
Json(req): Json<UpdateAuthorityRequest>,
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
let id = id
|
||||
.parse::<AuthorityId>()
|
||||
.map_err(|_| StatusCode::NOT_FOUND)?;
|
||||
|
||||
let labels: Vec<LocalizedLabel> = req
|
||||
.labels
|
||||
.into_iter()
|
||||
.map(|l| LocalizedLabel {
|
||||
lang: l.lang,
|
||||
label: l.label,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut tx = state
|
||||
.db
|
||||
.pool()
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
let existed = db::authority::update_authority(
|
||||
&mut tx,
|
||||
AuditActor::User(auth.user.id.to_uuid()),
|
||||
id,
|
||||
req.external_uri.as_deref(),
|
||||
&labels,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
if existed {
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
} else {
|
||||
let _ = tx.rollback().await;
|
||||
|
||||
Err(StatusCode::NOT_FOUND)
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
delete, path = "/api/admin/authorities/{id}",
|
||||
params(("id" = String, Path, description = "Authority id (UUID)")),
|
||||
responses(
|
||||
(status = 204),
|
||||
(status = 401),
|
||||
(status = 403),
|
||||
(status = 404),
|
||||
(status = 409, body = InUseView, description = "Referenced by catalogue objects")
|
||||
)
|
||||
)]
|
||||
pub(crate) async fn delete_authority(
|
||||
auth: Authorized<EditCatalogue>,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
) -> Response {
|
||||
let Ok(id) = id.parse::<AuthorityId>() else {
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
};
|
||||
|
||||
let Ok(mut tx) = state.db.pool().begin().await else {
|
||||
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||
};
|
||||
|
||||
match db::authority::delete_authority(&mut tx, AuditActor::User(auth.user.id.to_uuid()), id)
|
||||
.await
|
||||
{
|
||||
Ok(db::DeleteOutcome::Deleted) => match tx.commit().await {
|
||||
Ok(()) => StatusCode::NO_CONTENT.into_response(),
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
},
|
||||
Ok(db::DeleteOutcome::InUse { count }) => {
|
||||
let _ = tx.rollback().await;
|
||||
|
||||
(StatusCode::CONFLICT, Json(InUseView { count })).into_response()
|
||||
}
|
||||
Ok(db::DeleteOutcome::NotFound) => {
|
||||
let _ = tx.rollback().await;
|
||||
|
||||
StatusCode::NOT_FOUND.into_response()
|
||||
}
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn routes() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route(
|
||||
"/api/admin/authorities",
|
||||
get(list_authorities).post(create_authority),
|
||||
)
|
||||
.route(
|
||||
"/api/admin/authorities/{id}",
|
||||
axum::routing::patch(update_authority).delete(delete_authority),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -37,7 +37,9 @@ use crate::{
|
||||
admin_vocab::delete_vocabulary,
|
||||
admin_search::search_objects,
|
||||
admin_authorities::list_authorities,
|
||||
admin_authorities::create_authority
|
||||
admin_authorities::create_authority,
|
||||
admin_authorities::update_authority,
|
||||
admin_authorities::delete_authority
|
||||
),
|
||||
components(schemas(
|
||||
config::ConfigView,
|
||||
@@ -71,6 +73,7 @@ use crate::{
|
||||
admin_search::SearchResultsView,
|
||||
admin_authorities::AuthorityView,
|
||||
admin_authorities::NewAuthorityRequest,
|
||||
admin_authorities::UpdateAuthorityRequest,
|
||||
domain::Visibility,
|
||||
domain::AuthorityKind,
|
||||
domain::DataType
|
||||
|
||||
@@ -587,3 +587,130 @@ async fn delete_vocabulary_with_terms_is_409(pool: PgPool) {
|
||||
serde_json::from_slice(&blocked.into_body().collect().await.unwrap().to_bytes()).unwrap();
|
||||
assert_eq!(body["count"], 1);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "../db/migrations")]
|
||||
async fn delete_authority_referenced_is_409(pool: PgPool) {
|
||||
migrate_sessions(&db::Db::from_pool(pool.clone()))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
seed_user(&pool, "ed@example.com", "pw-editor-123", Role::Editor).await;
|
||||
|
||||
let app = build_app(state(pool));
|
||||
let cookie = login(&app, "ed@example.com", "pw-editor-123").await;
|
||||
|
||||
// create an authority
|
||||
let a = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"POST",
|
||||
"/api/admin/authorities",
|
||||
Some(r#"{"kind":"person","external_uri":null,"labels":[{"lang":"sv","label":"Astrid"}]}"#),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(a.status(), StatusCode::CREATED);
|
||||
|
||||
let aid: serde_json::Value =
|
||||
serde_json::from_slice(&a.into_body().collect().await.unwrap().to_bytes()).unwrap();
|
||||
let aid = aid["id"].as_str().unwrap().to_owned();
|
||||
|
||||
// create an authority-typed field definition
|
||||
send(
|
||||
&app,
|
||||
&cookie,
|
||||
"POST",
|
||||
"/api/admin/field-definitions",
|
||||
Some(
|
||||
r#"{"key":"maker","data_type":"authority","vocabulary_id":null,"authority_kind":"person","required":false,"group":null,"labels":[{"lang":"sv","label":"Tillverkare"}]}"#,
|
||||
),
|
||||
)
|
||||
.await;
|
||||
|
||||
// create an object
|
||||
let obj = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"POST",
|
||||
"/api/admin/objects",
|
||||
Some(
|
||||
r#"{"object_number":"T-1","object_name":"test object","number_of_objects":1,"visibility":"draft"}"#,
|
||||
),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(obj.status(), StatusCode::CREATED);
|
||||
|
||||
let obj_json: serde_json::Value =
|
||||
serde_json::from_slice(&obj.into_body().collect().await.unwrap().to_bytes()).unwrap();
|
||||
let obj_id = obj_json["id"].as_str().unwrap().to_owned();
|
||||
|
||||
// set the object's maker field to the authority id
|
||||
let fields_body = format!(r#"{{"maker":"{aid}"}}"#);
|
||||
let set = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"PUT",
|
||||
&format!("/api/admin/objects/{obj_id}/fields"),
|
||||
Some(&fields_body),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(set.status(), StatusCode::NO_CONTENT);
|
||||
|
||||
// delete the authority — must be blocked
|
||||
let blocked = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"DELETE",
|
||||
&format!("/api/admin/authorities/{aid}"),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(blocked.status(), StatusCode::CONFLICT);
|
||||
|
||||
let body: serde_json::Value =
|
||||
serde_json::from_slice(&blocked.into_body().collect().await.unwrap().to_bytes()).unwrap();
|
||||
assert_eq!(body["count"], 1);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "../db/migrations")]
|
||||
async fn edit_and_delete_authority(pool: PgPool) {
|
||||
migrate_sessions(&db::Db::from_pool(pool.clone()))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
seed_user(&pool, "ed@example.com", "pw-editor-123", Role::Editor).await;
|
||||
|
||||
let app = build_app(state(pool));
|
||||
let cookie = login(&app, "ed@example.com", "pw-editor-123").await;
|
||||
|
||||
let a = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"POST",
|
||||
"/api/admin/authorities",
|
||||
Some(r#"{"kind":"person","external_uri":null,"labels":[{"lang":"sv","label":"Anon"}]}"#),
|
||||
)
|
||||
.await;
|
||||
let aid: serde_json::Value =
|
||||
serde_json::from_slice(&a.into_body().collect().await.unwrap().to_bytes()).unwrap();
|
||||
let aid = aid["id"].as_str().unwrap().to_owned();
|
||||
|
||||
let patched = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"PATCH",
|
||||
&format!("/api/admin/authorities/{aid}"),
|
||||
Some(r#"{"external_uri":"https://viaf.org/1","labels":[{"lang":"sv","label":"Astrid"}]}"#),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(patched.status(), StatusCode::NO_CONTENT);
|
||||
|
||||
let deleted = send(
|
||||
&app,
|
||||
&cookie,
|
||||
"DELETE",
|
||||
&format!("/api/admin/authorities/{aid}"),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(deleted.status(), StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user