6 Commits
Author SHA1 Message Date
nils 157f03b655 Skip uploads of existing documents and support checksum preflight in importer 2025-10-26 02:56:35 +02:00
nils 9b7ca3d692 409 on duplicate 2025-10-26 02:33:30 +02:00
nils e51a59a829 upload with more data 2025-10-26 02:28:02 +02:00
nils 62cadbcfa0 openapi 2025-10-25 23:46:01 +02:00
nils d7aefc4110 k8s: disable index creation job 2025-10-25 13:57:56 +02:00
nils e972a8dddb fix folder move to root 2025-10-25 02:11:05 +02:00
16 changed files with 1813 additions and 347 deletions
+87 -25
View File
@@ -67,7 +67,7 @@ checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -635,6 +635,7 @@ dependencies = [
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"url", "url",
"utoipa",
"uuid", "uuid",
] ]
@@ -704,7 +705,7 @@ dependencies = [
"regex", "regex",
"rustc-hash", "rustc-hash",
"shlex", "shlex",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -989,7 +990,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"strsim", "strsim",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -1000,7 +1001,7 @@ checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81"
dependencies = [ dependencies = [
"darling_core", "darling_core",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -1050,7 +1051,7 @@ dependencies = [
"dsl_auto_type", "dsl_auto_type",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -1070,7 +1071,7 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe2444076b48641147115697648dc743c2c00b61adade0f01ce67133c7babe8c" checksum = "fe2444076b48641147115697648dc743c2c00b61adade0f01ce67133c7babe8c"
dependencies = [ dependencies = [
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -1092,7 +1093,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -1118,7 +1119,7 @@ dependencies = [
"heck", "heck",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -1287,7 +1288,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -1812,6 +1813,8 @@ checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown 0.16.0", "hashbrown 0.16.0",
"serde",
"serde_core",
] ]
[[package]] [[package]]
@@ -2267,7 +2270,7 @@ checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -2359,7 +2362,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"syn", "syn 2.0.106",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
] ]
[[package]] [[package]]
@@ -2894,7 +2921,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3082,6 +3109,16 @@ version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"unicode-ident",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.106" version = "2.0.106"
@@ -3110,7 +3147,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3152,7 +3189,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3163,7 +3200,7 @@ checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3259,7 +3296,7 @@ checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3409,7 +3446,7 @@ checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3514,6 +3551,31 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utoipa"
version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5afb1a60e207dca502682537fefcfd9921e71d0b83e9576060f09abc6efab23"
dependencies = [
"indexmap",
"serde",
"serde_json",
"utoipa-gen",
]
[[package]]
name = "utoipa-gen"
version = "4.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20c24e8ab68ff9ee746aad22d39b5535601e6416d1b0feeabf78be986a5c4392"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"syn 2.0.106",
"uuid",
]
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.18.1" version = "1.18.1"
@@ -3615,7 +3677,7 @@ dependencies = [
"log", "log",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@@ -3650,7 +3712,7 @@ checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@@ -3727,7 +3789,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3738,7 +3800,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -3965,7 +4027,7 @@ checksum = "38da3c9736e16c5d3c8c597a9aaa5d1fa565d0532ae05e27c24aa62fb32c0ab6"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
"synstructure", "synstructure",
] ]
@@ -3986,7 +4048,7 @@ checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
@@ -4006,7 +4068,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
"synstructure", "synstructure",
] ]
@@ -4046,7 +4108,7 @@ checksum = "5b96237efa0c878c64bd89c436f661be4e46b2f3eff1ebb976f7ef2321d2f58f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.106",
] ]
[[package]] [[package]]
+1
View File
@@ -45,6 +45,7 @@ quick-xml = "0.32"
futures-util = "0.3" futures-util = "0.3"
url = "2.5" url = "2.5"
once_cell = "1.19" once_cell = "1.19"
utoipa = { version = "4.2", default-features = false, features = ["chrono", "uuid", "preserve_order"] }
# Error handling # Error handling
thiserror = "1.0" thiserror = "1.0"
+8
View File
@@ -0,0 +1,8 @@
use backend::openapi::ApiDoc;
use utoipa::OpenApi;
fn main() {
let spec = ApiDoc::openapi();
let json = serde_json::to_string_pretty(&spec).expect("serialize openapi");
println!("{}", json);
}
+14
View File
@@ -12,6 +12,7 @@ pub type AppResult<T> = Result<T, AppError>;
pub struct AppError { pub struct AppError {
status: StatusCode, status: StatusCode,
message: String, message: String,
code: Option<String>,
} }
impl AppError { impl AppError {
@@ -19,6 +20,7 @@ impl AppError {
Self { Self {
status, status,
message: message.into(), message: message.into(),
code: None,
} }
} }
@@ -26,6 +28,10 @@ impl AppError {
Self::new(StatusCode::BAD_REQUEST, message) Self::new(StatusCode::BAD_REQUEST, message)
} }
pub fn conflict(message: impl Into<String>) -> Self {
Self::new(StatusCode::CONFLICT, message)
}
pub fn unauthorized() -> Self { pub fn unauthorized() -> Self {
Self::new(StatusCode::UNAUTHORIZED, "unauthorized") Self::new(StatusCode::UNAUTHORIZED, "unauthorized")
} }
@@ -37,6 +43,11 @@ impl AppError {
pub fn internal<E: Display>(error: E) -> Self { pub fn internal<E: Display>(error: E) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.to_string()) Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.to_string())
} }
pub fn with_code(mut self, code: impl Into<String>) -> Self {
self.code = Some(code.into());
self
}
} }
impl IntoResponse for AppError { impl IntoResponse for AppError {
@@ -44,6 +55,7 @@ impl IntoResponse for AppError {
let status = self.status; let status = self.status;
let body = Json(ErrorResponse { let body = Json(ErrorResponse {
error: self.message, error: self.message,
code: self.code,
}); });
(status, body).into_response() (status, body).into_response()
} }
@@ -52,6 +64,8 @@ impl IntoResponse for AppError {
#[derive(Serialize)] #[derive(Serialize)]
struct ErrorResponse { struct ErrorResponse {
error: String, error: String,
#[serde(skip_serializing_if = "Option::is_none")]
code: Option<String>,
} }
impl From<diesel::result::Error> for AppError { impl From<diesel::result::Error> for AppError {
+1
View File
@@ -4,6 +4,7 @@ pub mod db;
pub mod error; pub mod error;
pub mod jobs; pub mod jobs;
pub mod models; pub mod models;
pub mod openapi;
pub mod routes; pub mod routes;
pub mod s3; pub mod s3;
pub mod schema; pub mod schema;
+951
View File
@@ -0,0 +1,951 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use utoipa::{IntoParams, OpenApi, ToSchema};
use uuid::Uuid;
#[derive(OpenApi)]
#[openapi(
paths(
doc::health_check,
doc::login,
doc::refresh,
doc::logout,
doc::me,
doc::select_tenant,
doc::list_documents,
doc::check_document,
doc::upload_document,
doc::get_document,
doc::update_document,
doc::delete_document,
doc::download_document,
doc::download_with_token,
doc::move_document,
doc::assign_tags,
doc::remove_tag,
doc::bulk_move_documents,
doc::bulk_update_tags,
doc::bulk_assign_correspondents,
doc::assign_correspondents,
doc::remove_correspondent,
doc::reanalyze_selected_documents,
doc::list_document_assets,
doc::request_document_assets,
doc::get_document_asset,
doc::create_folder,
doc::ensure_folder_path,
doc::get_folder,
doc::list_folder_contents,
doc::delete_folder,
doc::update_folder,
doc::list_tags,
doc::create_tag,
doc::update_tag,
doc::delete_tag,
doc::list_correspondents,
doc::create_correspondent,
doc::update_correspondent,
doc::delete_correspondent,
),
components(
schemas(
schemas::LoginRequest,
schemas::AccessTokenResponse,
schemas::TenantSummary,
schemas::TenantSelectionResponse,
schemas::TenantSelectionRequest,
schemas::LoginResponseVariants,
schemas::DocumentResponse,
schemas::DocumentDetailResponse,
schemas::DocumentVersion,
schemas::DocumentAssetSummary,
schemas::DocumentAssetDetail,
schemas::DocumentAssetObject,
schemas::DocumentCorrespondent,
schemas::DocumentTag,
schemas::DocumentDownloadResponse,
schemas::UpdateDocumentRequest,
schemas::BulkMoveDocumentsRequest,
schemas::BulkMoveDocumentsResponse,
schemas::AssignTagsRequest,
schemas::MoveDocumentRequest,
schemas::BulkTagRequest,
schemas::BulkTagResponse,
schemas::CorrespondentAssignment,
schemas::BulkTagAction,
schemas::BulkCorrespondentsRequest,
schemas::BulkCorrespondentsResponse,
schemas::BulkCorrespondentAction,
schemas::AssignCorrespondentsRequest,
schemas::RemoveCorrespondentParams,
schemas::ReanalyzeRequest,
schemas::ReanalyzeResponse,
schemas::DocumentAssetRequestParams,
schemas::AssetObjectsQuery,
schemas::DocumentCheckQuery,
schemas::DocumentCheckResponse,
schemas::UploadDocumentForm,
schemas::CreateFolderRequest,
schemas::EnsureFolderPathRequest,
schemas::FolderResponse,
schemas::FolderInfo,
schemas::FolderContentsResponse,
schemas::FolderDocumentSummary,
schemas::UpdateFolderRequest,
schemas::FolderContentsParams,
schemas::TagCatalogEntry,
schemas::CreateTagRequest,
schemas::UpdateTagRequest,
schemas::CorrespondentCatalogEntry,
schemas::CreateCorrespondentRequest,
schemas::UpdateCorrespondentRequest,
)
),
tags(
(name = "Health", description = "Service health"),
(name = "Auth", description = "Authentication"),
(name = "Documents", description = "Document management"),
(name = "Assets", description = "Document assets"),
(name = "Folders", description = "Folder management"),
(name = "Tags", description = "Tag catalog"),
(name = "Correspondents", description = "Correspondent catalog")
)
)]
pub struct ApiDoc;
#[allow(dead_code)]
mod doc {
use super::schemas::*;
use uuid::Uuid;
#[allow(dead_code)]
fn __keep_uuid_import() {
let _ = Uuid::nil();
}
#[utoipa::path(
get,
path = "/api/health",
responses((status = 200, description = "Service is healthy")),
tag = "Health"
)]
pub(super) fn health_check() {}
#[utoipa::path(
post,
path = "/api/auth/login",
request_body = LoginRequest,
responses(
(status = 200, description = "Login succeeded", body = LoginResponseVariants),
(status = 401, description = "Invalid credentials")
),
tag = "Auth"
)]
pub(super) fn login() {}
#[utoipa::path(
post,
path = "/api/auth/refresh",
responses(
(status = 200, description = "Refreshed access token", body = AccessTokenResponse),
(status = 401, description = "Missing or invalid refresh token")
),
tag = "Auth"
)]
pub(super) fn refresh() {}
#[utoipa::path(
post,
path = "/api/auth/logout",
responses((status = 204, description = "Session revoked")),
tag = "Auth"
)]
pub(super) fn logout() {}
#[utoipa::path(
get,
path = "/api/auth/me",
responses((status = 200, description = "Authenticated principal", body = AccessTokenResponse)),
tag = "Auth"
)]
pub(super) fn me() {}
#[utoipa::path(
post,
path = "/api/auth/select-tenant",
request_body = TenantSelectionRequest,
responses((status = 200, description = "Tenant selected", body = AccessTokenResponse)),
tag = "Auth"
)]
pub(super) fn select_tenant() {}
#[utoipa::path(
get,
path = "/api/documents",
params(DocumentListQuery),
responses((status = 200, description = "List documents", body = [DocumentResponse])),
tag = "Documents"
)]
pub(super) fn list_documents() {}
#[utoipa::path(
post,
path = "/api/documents",
request_body = UploadDocumentForm,
responses(
(status = 201, description = "Document created", body = DocumentDetailResponse),
(status = 200, description = "Existing document reused", body = DocumentDetailResponse),
(status = 204, description = "Upload skipped because the document already exists")
),
tag = "Documents"
)]
pub(super) fn upload_document() {}
#[utoipa::path(
get,
path = "/api/documents/check",
params(DocumentCheckQuery),
responses((status = 200, description = "Checksum lookup", body = DocumentCheckResponse)),
tag = "Documents"
)]
pub(super) fn check_document() {}
#[utoipa::path(
get,
path = "/api/documents/{id}",
params(("id" = Uuid, Path, description = "Document ID")),
responses((status = 200, description = "Document detail", body = DocumentDetailResponse)),
tag = "Documents"
)]
pub(super) fn get_document() {}
#[utoipa::path(
patch,
path = "/api/documents/{id}",
params(("id" = Uuid, Path, description = "Document ID")),
request_body = UpdateDocumentRequest,
responses((status = 200, description = "Updated document", body = DocumentDetailResponse)),
tag = "Documents"
)]
pub(super) fn update_document() {}
#[utoipa::path(
delete,
path = "/api/documents/{id}",
params(("id" = Uuid, Path, description = "Document ID")),
responses((status = 204, description = "Document deleted")),
tag = "Documents"
)]
pub(super) fn delete_document() {}
#[utoipa::path(
get,
path = "/api/documents/{id}/download",
params(("id" = Uuid, Path, description = "Document ID")),
responses((status = 200, description = "Download metadata", body = DocumentDownloadResponse)),
tag = "Documents"
)]
pub(super) fn download_document() {}
#[utoipa::path(
get,
path = "/download/{token}",
params(("token" = String, Path, description = "Download token")),
responses((status = 302, description = "Redirect to pre-signed URL")),
tag = "Documents"
)]
pub(super) fn download_with_token() {}
#[utoipa::path(
patch,
path = "/api/documents/{id}/folder",
params(("id" = Uuid, Path, description = "Document ID")),
request_body = MoveDocumentRequest,
responses((status = 204, description = "Document moved")),
tag = "Documents"
)]
pub(super) fn move_document() {}
#[utoipa::path(
post,
path = "/api/documents/{id}/tags",
params(("id" = Uuid, Path, description = "Document ID")),
request_body = AssignTagsRequest,
responses((status = 204, description = "Tags assigned")),
tag = "Documents"
)]
pub(super) fn assign_tags() {}
#[utoipa::path(
delete,
path = "/api/documents/{id}/tags/{tag_id}",
params(
("id" = Uuid, Path, description = "Document ID"),
("tag_id" = Uuid, Path, description = "Tag ID")
),
responses((status = 204, description = "Tag removed")),
tag = "Documents"
)]
pub(super) fn remove_tag() {}
#[utoipa::path(
post,
path = "/api/documents/bulk/move",
request_body = BulkMoveDocumentsRequest,
responses((status = 200, description = "Bulk move outcome", body = BulkMoveDocumentsResponse)),
tag = "Documents"
)]
pub(super) fn bulk_move_documents() {}
#[utoipa::path(
post,
path = "/api/documents/bulk/tags",
request_body = BulkTagRequest,
responses((status = 200, description = "Bulk tag outcome", body = BulkTagResponse)),
tag = "Documents"
)]
pub(super) fn bulk_update_tags() {}
#[utoipa::path(
post,
path = "/api/documents/bulk/correspondents",
request_body = BulkCorrespondentsRequest,
responses((status = 200, description = "Bulk correspondents outcome", body = BulkCorrespondentsResponse)),
tag = "Documents"
)]
pub(super) fn bulk_assign_correspondents() {}
#[utoipa::path(
post,
path = "/api/documents/{id}/correspondents",
params(("id" = Uuid, Path, description = "Document ID")),
request_body = AssignCorrespondentsRequest,
responses((status = 204, description = "Correspondents assigned")),
tag = "Documents"
)]
pub(super) fn assign_correspondents() {}
#[utoipa::path(
delete,
path = "/api/documents/{id}/correspondents/{correspondent_id}",
params(
("id" = Uuid, Path, description = "Document ID"),
("correspondent_id" = Uuid, Path, description = "Correspondent ID"),
RemoveCorrespondentParams
),
responses((status = 204, description = "Correspondent removed")),
tag = "Documents"
)]
pub(super) fn remove_correspondent() {}
#[utoipa::path(
post,
path = "/api/documents/bulk/reanalyze",
request_body = ReanalyzeRequest,
responses((status = 200, description = "Reanalyze queued", body = ReanalyzeResponse)),
tag = "Documents"
)]
pub(super) fn reanalyze_selected_documents() {}
#[utoipa::path(
get,
path = "/api/documents/{id}/assets",
params(("id" = Uuid, Path, description = "Document ID")),
responses((status = 200, description = "Document assets", body = [DocumentAssetSummary])),
tag = "Assets"
)]
pub(super) fn list_document_assets() {}
#[utoipa::path(
post,
path = "/api/documents/{id}/assets",
params(
("id" = Uuid, Path, description = "Document ID"),
DocumentAssetRequestParams
),
responses((status = 202, description = "Asset generation requested")),
tag = "Assets"
)]
pub(super) fn request_document_assets() {}
#[utoipa::path(
get,
path = "/api/assets/{asset_id}",
params(
("asset_id" = Uuid, Path, description = "Asset ID"),
AssetObjectsQuery
),
responses((status = 200, description = "Asset detail", body = DocumentAssetDetail)),
tag = "Assets"
)]
pub(super) fn get_document_asset() {}
#[utoipa::path(
post,
path = "/api/folders",
request_body = CreateFolderRequest,
responses((status = 200, description = "Folder created", body = FolderResponse)),
tag = "Folders"
)]
pub(super) fn create_folder() {}
#[utoipa::path(
post,
path = "/api/folders/path",
request_body = EnsureFolderPathRequest,
responses((status = 200, description = "Folder path ensured", body = FolderResponse)),
tag = "Folders"
)]
pub(super) fn ensure_folder_path() {}
#[utoipa::path(
get,
path = "/api/folders/{id}",
params(("id" = Uuid, Path, description = "Folder ID")),
responses((status = 200, description = "Folder detail", body = FolderResponse)),
tag = "Folders"
)]
pub(super) fn get_folder() {}
#[utoipa::path(
get,
path = "/api/folders/{id}/contents",
params(
("id" = Uuid, Path, description = "Folder ID"),
FolderContentsParams
),
responses((status = 200, description = "Folder contents", body = FolderContentsResponse)),
tag = "Folders"
)]
pub(super) fn list_folder_contents() {}
#[utoipa::path(
delete,
path = "/api/folders/{id}",
params(("id" = Uuid, Path, description = "Folder ID")),
responses((status = 204, description = "Folder deleted")),
tag = "Folders"
)]
pub(super) fn delete_folder() {}
#[utoipa::path(
patch,
path = "/api/folders/{id}",
params(("id" = Uuid, Path, description = "Folder ID")),
request_body = UpdateFolderRequest,
responses((status = 204, description = "Folder updated")),
tag = "Folders"
)]
pub(super) fn update_folder() {}
#[utoipa::path(
get,
path = "/api/tags",
responses((status = 200, description = "Tags", body = [TagCatalogEntry])),
tag = "Tags"
)]
pub(super) fn list_tags() {}
#[utoipa::path(
post,
path = "/api/tags",
request_body = CreateTagRequest,
responses((status = 200, description = "Tag created", body = TagCatalogEntry)),
tag = "Tags"
)]
pub(super) fn create_tag() {}
#[utoipa::path(
patch,
path = "/api/tags/{id}",
params(("id" = Uuid, Path, description = "Tag ID")),
request_body = UpdateTagRequest,
responses((status = 200, description = "Tag updated", body = TagCatalogEntry)),
tag = "Tags"
)]
pub(super) fn update_tag() {}
#[utoipa::path(
delete,
path = "/api/tags/{id}",
params(("id" = Uuid, Path, description = "Tag ID")),
responses((status = 204, description = "Tag deleted")),
tag = "Tags"
)]
pub(super) fn delete_tag() {}
#[utoipa::path(
get,
path = "/api/correspondents",
responses((status = 200, description = "Correspondents", body = [CorrespondentCatalogEntry])),
tag = "Correspondents"
)]
pub(super) fn list_correspondents() {}
#[utoipa::path(
post,
path = "/api/correspondents",
request_body = CreateCorrespondentRequest,
responses((status = 200, description = "Correspondent created", body = CorrespondentCatalogEntry)),
tag = "Correspondents"
)]
pub(super) fn create_correspondent() {}
#[utoipa::path(
patch,
path = "/api/correspondents/{id}",
params(("id" = Uuid, Path, description = "Correspondent ID")),
request_body = UpdateCorrespondentRequest,
responses((status = 200, description = "Correspondent updated", body = CorrespondentCatalogEntry)),
tag = "Correspondents"
)]
pub(super) fn update_correspondent() {}
#[utoipa::path(
delete,
path = "/api/correspondents/{id}",
params(("id" = Uuid, Path, description = "Correspondent ID")),
responses((status = 204, description = "Correspondent deleted")),
tag = "Correspondents"
)]
pub(super) fn delete_correspondent() {}
}
#[cfg(test)]
mod tests {
use super::ApiDoc;
use utoipa::OpenApi;
#[test]
fn openapi_serializes() {
let spec = ApiDoc::openapi();
let _ = serde_json::to_string(&spec).expect("serialize openapi");
}
}
pub mod schemas {
use super::*;
#[derive(Serialize, Deserialize, ToSchema)]
pub struct LoginRequest {
pub username: String,
pub password: String,
#[schema(nullable)]
pub preferred_tenant_slug: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct AccessTokenResponse {
pub access_token: String,
pub token_type: String,
pub expires_in: i64,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct TenantSummary {
pub tenant_id: Uuid,
pub slug: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct TenantSelectionResponse {
pub selection_token: String,
pub tenants: Vec<TenantSummary>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct TenantSelectionRequest {
pub tenant_id: Uuid,
}
#[derive(Serialize, Deserialize, ToSchema)]
#[serde(untagged)]
pub enum LoginResponseVariants {
Token(AccessTokenResponse),
Selection(TenantSelectionResponse),
}
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct DocumentListQuery {
pub folder_id: Option<Uuid>,
pub include_deleted: Option<bool>,
pub include_descendants: Option<bool>,
pub query: Option<String>,
pub tags: Option<String>,
pub correspondents: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentTag {
pub id: Uuid,
pub label: String,
#[schema(nullable)]
pub color: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentAssetObject {
pub id: Uuid,
pub ordinal: i32,
pub metadata: Value,
#[schema(nullable)]
pub url: Option<String>,
#[schema(nullable)]
pub expires_at: Option<i64>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentAssetSummary {
pub id: Uuid,
pub asset_type: String,
pub mime_type: String,
pub metadata: Value,
#[schema(nullable)]
pub cardinality: Option<i32>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentAssetDetail {
pub id: Uuid,
pub asset_type: String,
pub mime_type: String,
pub metadata: Value,
pub created_at: String,
#[schema(nullable)]
pub cardinality: Option<i32>,
pub objects: Vec<DocumentAssetObject>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentVersion {
pub id: Uuid,
pub version_number: i32,
pub checksum: String,
pub size_bytes: i64,
pub created_at: String,
pub metadata: Value,
#[schema(nullable)]
pub operations_summary: Option<Value>,
#[schema(nullable)]
pub assets: Option<Vec<DocumentAssetSummary>>,
pub download_path: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentCorrespondent {
pub id: Uuid,
pub name: String,
pub role: String,
pub metadata: Value,
pub assigned_at: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentResponse {
pub id: Uuid,
pub filename: String,
pub title: String,
pub original_name: String,
#[schema(nullable)]
pub content_type: Option<String>,
#[schema(nullable)]
pub folder_id: Option<Uuid>,
pub uploaded_at: String,
pub updated_at: String,
#[schema(nullable)]
pub deleted_at: Option<String>,
#[schema(nullable)]
pub issued_at: Option<String>,
pub metadata: Value,
pub tags: Vec<DocumentTag>,
#[schema(nullable)]
pub correspondents: Option<Vec<DocumentCorrespondent>>,
#[schema(nullable)]
pub current_version: Option<DocumentVersion>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentDetailResponse {
pub document: DocumentResponse,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentDownloadResponse {
pub url: String,
pub expires_in: u64,
pub filename: String,
#[schema(nullable)]
pub content_type: Option<String>,
pub size_bytes: i64,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct UpdateDocumentRequest {
#[schema(nullable)]
pub title: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct BulkMoveDocumentsRequest {
pub document_ids: Vec<Uuid>,
#[schema(nullable)]
pub folder_id: Option<Uuid>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct BulkMoveDocumentsResponse {
pub updated: usize,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct AssignTagsRequest {
pub tag_ids: Vec<Uuid>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct MoveDocumentRequest {
#[schema(nullable)]
pub folder_id: Option<Uuid>,
}
#[derive(Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum BulkTagAction {
Add,
Remove,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct BulkTagRequest {
pub document_ids: Vec<Uuid>,
pub tag_ids: Vec<Uuid>,
pub action: BulkTagAction,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct BulkTagResponse {
pub added: usize,
pub removed: usize,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct CorrespondentAssignment {
pub correspondent_id: Uuid,
pub role: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct AssignCorrespondentsRequest {
pub assignments: Vec<CorrespondentAssignment>,
#[serde(default)]
pub replace: bool,
}
#[derive(Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum BulkCorrespondentAction {
Add,
Remove,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct BulkCorrespondentsRequest {
pub document_ids: Vec<Uuid>,
pub assignments: Vec<CorrespondentAssignment>,
#[serde(default = "default_bulk_correspondent_action")]
pub action: BulkCorrespondentAction,
}
fn default_bulk_correspondent_action() -> BulkCorrespondentAction {
BulkCorrespondentAction::Add
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct BulkCorrespondentsResponse {
pub assigned: usize,
pub removed: usize,
}
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct RemoveCorrespondentParams {
pub role: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct ReanalyzeRequest {
pub document_ids: Vec<Uuid>,
#[serde(default)]
pub force: bool,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct ReanalyzeResponse {
pub queued: usize,
}
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct DocumentAssetRequestParams {
#[serde(default)]
pub force: Option<bool>,
}
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct AssetObjectsQuery {
#[serde(default)]
pub start: Option<i32>,
#[serde(default)]
pub limit: Option<i32>,
}
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct DocumentCheckQuery {
pub checksum: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct DocumentCheckResponse {
pub exists: bool,
#[schema(nullable)]
pub document_id: Option<Uuid>,
#[schema(nullable)]
pub title: Option<String>,
#[schema(nullable)]
pub filename: Option<String>,
#[schema(nullable)]
pub version_id: Option<Uuid>,
#[schema(nullable)]
pub version_number: Option<i32>,
#[schema(nullable)]
pub uploaded_at: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct UploadDocumentForm {
#[schema(value_type = String, format = Binary)]
pub file: String,
#[schema(nullable)]
pub folder_id: Option<Uuid>,
#[schema(nullable)]
pub metadata: Option<Value>,
#[schema(nullable)]
pub title: Option<String>,
#[schema(nullable, value_type = Vec<Uuid>)]
pub tag_ids: Option<Vec<Uuid>>,
#[schema(nullable, value_type = Vec<CorrespondentAssignment>)]
pub correspondents: Option<Vec<CorrespondentAssignment>>,
#[schema(nullable, example = "2024-01-01T00:00:00Z")]
pub issued_at: Option<String>,
#[schema(nullable)]
pub skip_existing: Option<bool>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct CreateFolderRequest {
pub name: String,
#[schema(nullable)]
pub parent_id: Option<Uuid>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct EnsureFolderPathRequest {
#[schema(nullable)]
pub parent_id: Option<Uuid>,
pub segments: Vec<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct FolderInfo {
pub id: Uuid,
pub name: String,
#[schema(nullable)]
pub parent_id: Option<Uuid>,
pub created_at: String,
pub updated_at: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct FolderResponse {
pub folder: FolderInfo,
}
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct FolderContentsParams {
#[serde(default)]
pub include_documents: Option<bool>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct FolderDocumentSummary {
pub id: Uuid,
pub title: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct FolderContentsResponse {
#[schema(nullable)]
pub folder: Option<FolderInfo>,
pub subfolders: Vec<FolderInfo>,
pub documents: Vec<FolderDocumentSummary>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct UpdateFolderRequest {
#[schema(nullable)]
pub parent_id: Option<Option<Uuid>>,
#[schema(nullable)]
pub name: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct TagCatalogEntry {
pub id: Uuid,
pub label: String,
#[schema(nullable)]
pub color: Option<String>,
pub usage_count: i64,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct CreateTagRequest {
pub label: String,
#[schema(nullable)]
pub color: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct UpdateTagRequest {
#[schema(nullable)]
pub label: Option<Option<String>>,
#[schema(nullable)]
pub color: Option<Option<String>>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct CorrespondentCatalogEntry {
pub id: Uuid,
pub name: String,
pub metadata: Value,
pub role_counts: Value,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct CreateCorrespondentRequest {
pub name: String,
#[schema(default, value_type = Object)]
pub metadata: Value,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct UpdateCorrespondentRequest {
#[schema(nullable)]
pub name: Option<String>,
#[schema(nullable)]
pub metadata: Option<Value>,
}
}
+501 -150
View File
@@ -6,7 +6,7 @@ use std::{
use axum::extract::{Json, Multipart, Path, Query, State}; use axum::extract::{Json, Multipart, Path, Query, State};
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use chrono::{NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use diesel::dsl::exists; use diesel::dsl::exists;
use diesel::{prelude::*, result::DatabaseErrorKind, select, PgConnection}; use diesel::{prelude::*, result::DatabaseErrorKind, select, PgConnection};
use reqwest::Client; use reqwest::Client;
@@ -14,6 +14,7 @@ use serde::{Deserialize, Serialize};
use serde_json::{json, Value}; use serde_json::{json, Value};
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
use utoipa::{IntoParams, ToSchema};
use uuid::Uuid; use uuid::Uuid;
use super::folders::gather_descendant_folder_ids; use super::folders::gather_descendant_folder_ids;
@@ -55,7 +56,8 @@ use search_utils::{build_quickwit_query, extract_document_id};
const PRESIGNED_URL_EXPIRY_SECONDS: u64 = 300; const PRESIGNED_URL_EXPIRY_SECONDS: u64 = 300;
const QUICKWIT_MAX_HITS: usize = 200; const QUICKWIT_MAX_HITS: usize = 200;
#[derive(Deserialize)] #[derive(Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct DocumentListQuery { pub struct DocumentListQuery {
pub folder_id: Option<Uuid>, pub folder_id: Option<Uuid>,
#[serde(default)] #[serde(default)]
@@ -67,13 +69,37 @@ pub struct DocumentListQuery {
pub correspondents: Option<String>, pub correspondents: Option<String>,
} }
#[derive(Deserialize)] #[derive(Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct AssetRequestQuery { pub struct AssetRequestQuery {
#[serde(default)] #[serde(default)]
pub force: bool, pub force: bool,
} }
#[derive(Serialize)] #[derive(Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct DocumentCheckQuery {
pub checksum: String,
}
#[derive(Serialize, ToSchema)]
pub struct DocumentCheckResponse {
pub exists: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub document_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub filename: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version_number: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub uploaded_at: Option<String>,
}
#[derive(Serialize, ToSchema)]
pub struct TagResponse { pub struct TagResponse {
pub id: Uuid, pub id: Uuid,
pub label: String, pub label: String,
@@ -90,7 +116,7 @@ impl From<Tag> for TagResponse {
} }
} }
#[derive(Serialize, Clone)] #[derive(Serialize, Clone, ToSchema)]
pub struct DocumentVersionResponse { pub struct DocumentVersionResponse {
pub id: Uuid, pub id: Uuid,
pub version_number: i32, pub version_number: i32,
@@ -103,7 +129,7 @@ pub struct DocumentVersionResponse {
pub operations_summary: Option<Value>, pub operations_summary: Option<Value>,
} }
#[derive(Serialize, Clone)] #[derive(Serialize, Clone, ToSchema)]
pub struct DocumentAssetResponse { pub struct DocumentAssetResponse {
pub id: Uuid, pub id: Uuid,
pub asset_type: String, pub asset_type: String,
@@ -113,7 +139,7 @@ pub struct DocumentAssetResponse {
pub cardinality: Option<i32>, pub cardinality: Option<i32>,
} }
#[derive(Serialize, Clone)] #[derive(Serialize, Clone, ToSchema)]
pub struct DocumentAssetObjectResponse { pub struct DocumentAssetObjectResponse {
pub id: Uuid, pub id: Uuid,
pub ordinal: i32, pub ordinal: i32,
@@ -124,7 +150,7 @@ pub struct DocumentAssetObjectResponse {
pub expires_at: Option<i64>, pub expires_at: Option<i64>,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct DocumentAssetDetailResponse { pub struct DocumentAssetDetailResponse {
pub id: Uuid, pub id: Uuid,
pub asset_type: String, pub asset_type: String,
@@ -137,7 +163,7 @@ pub struct DocumentAssetDetailResponse {
pub objects: Vec<DocumentAssetObjectResponse>, pub objects: Vec<DocumentAssetObjectResponse>,
} }
#[derive(Serialize, Clone)] #[derive(Serialize, Clone, ToSchema)]
pub struct DocumentCurrentVersionResponse { pub struct DocumentCurrentVersionResponse {
#[serde(flatten)] #[serde(flatten)]
pub version: DocumentVersionResponse, pub version: DocumentVersionResponse,
@@ -146,7 +172,7 @@ pub struct DocumentCurrentVersionResponse {
pub download_path: String, pub download_path: String,
} }
#[derive(Serialize, Clone)] #[derive(Serialize, Clone, ToSchema)]
pub struct DocumentCorrespondentResponse { pub struct DocumentCorrespondentResponse {
pub id: Uuid, pub id: Uuid,
pub name: String, pub name: String,
@@ -155,7 +181,7 @@ pub struct DocumentCorrespondentResponse {
pub assigned_at: String, pub assigned_at: String,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct DocumentResponse { pub struct DocumentResponse {
pub id: Uuid, pub id: Uuid,
pub filename: String, pub filename: String,
@@ -174,12 +200,12 @@ pub struct DocumentResponse {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub current_version: Option<DocumentCurrentVersionResponse>, pub current_version: Option<DocumentCurrentVersionResponse>,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct DocumentDetailResponse { pub struct DocumentDetailResponse {
pub document: DocumentResponse, pub document: DocumentResponse,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct DocumentDownloadResponse { pub struct DocumentDownloadResponse {
pub url: String, pub url: String,
pub expires_in: u64, pub expires_in: u64,
@@ -188,67 +214,67 @@ pub struct DocumentDownloadResponse {
pub size_bytes: i64, pub size_bytes: i64,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct BulkReanalyzeResponse { pub struct BulkReanalyzeResponse {
pub queued: usize, pub queued: usize,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct BulkMoveRequest { pub struct BulkMoveRequest {
pub document_ids: Vec<Uuid>, pub document_ids: Vec<Uuid>,
pub folder_id: Option<Uuid>, pub folder_id: Option<Uuid>,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct UpdateDocumentRequest { pub struct UpdateDocumentRequest {
pub title: Option<String>, pub title: Option<String>,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct BulkMoveResponse { pub struct BulkMoveResponse {
pub updated: usize, pub updated: usize,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum BulkTagAction { pub enum BulkTagAction {
Add, Add,
Remove, Remove,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct BulkTagRequest { pub struct BulkTagRequest {
pub document_ids: Vec<Uuid>, pub document_ids: Vec<Uuid>,
pub tag_ids: Vec<Uuid>, pub tag_ids: Vec<Uuid>,
pub action: BulkTagAction, pub action: BulkTagAction,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct BulkTagResponse { pub struct BulkTagResponse {
pub added: usize, pub added: usize,
pub removed: usize, pub removed: usize,
} }
#[derive(Serialize)] #[derive(Serialize, ToSchema)]
pub struct BulkCorrespondentResponse { pub struct BulkCorrespondentResponse {
pub assigned: usize, pub assigned: usize,
pub removed: usize, pub removed: usize,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct CorrespondentAssignmentInput { pub struct CorrespondentAssignmentInput {
pub correspondent_id: Uuid, pub correspondent_id: Uuid,
pub role: String, pub role: String,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct AssignCorrespondentsRequest { pub struct AssignCorrespondentsRequest {
pub assignments: Vec<CorrespondentAssignmentInput>, pub assignments: Vec<CorrespondentAssignmentInput>,
#[serde(default)] #[serde(default)]
pub replace: bool, pub replace: bool,
} }
#[derive(Deserialize, Copy, Clone, PartialEq, Eq)] #[derive(Deserialize, Copy, Clone, PartialEq, Eq, ToSchema)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum BulkCorrespondentAction { pub enum BulkCorrespondentAction {
Add, Add,
@@ -259,7 +285,7 @@ fn default_bulk_correspondent_action() -> BulkCorrespondentAction {
BulkCorrespondentAction::Add BulkCorrespondentAction::Add
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct BulkCorrespondentsRequest { pub struct BulkCorrespondentsRequest {
pub document_ids: Vec<Uuid>, pub document_ids: Vec<Uuid>,
pub assignments: Vec<CorrespondentAssignmentInput>, pub assignments: Vec<CorrespondentAssignmentInput>,
@@ -267,12 +293,13 @@ pub struct BulkCorrespondentsRequest {
pub action: BulkCorrespondentAction, pub action: BulkCorrespondentAction,
} }
#[derive(Deserialize)] #[derive(Deserialize, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct CorrespondentRoleQuery { pub struct CorrespondentRoleQuery {
pub role: String, pub role: String,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct BulkReanalyzeSelectionRequest { pub struct BulkReanalyzeSelectionRequest {
pub document_ids: Vec<Uuid>, pub document_ids: Vec<Uuid>,
#[serde(default = "default_true")] #[serde(default = "default_true")]
@@ -289,24 +316,51 @@ struct UploadRequest {
content_type: Option<String>, content_type: Option<String>,
folder_id: Option<Uuid>, folder_id: Option<Uuid>,
metadata: Value, metadata: Value,
title_override: Option<String>,
tag_ids: Vec<Uuid>,
correspondents: Vec<CorrespondentAssignmentInput>,
issued_at_override: Option<NaiveDateTime>,
skip_if_existing: bool,
} }
struct UploadOutcome { enum UploadOutcome {
detail: DocumentDetailResponse, Created(DocumentDetailResponse),
created: bool, Reused(DocumentDetailResponse),
Skipped { document_id: Uuid },
} }
#[derive(Deserialize)] #[derive(ToSchema)]
pub struct UploadDocumentForm {
#[schema(value_type = String, format = Binary)]
pub file: String,
#[schema(nullable)]
pub folder_id: Option<Uuid>,
#[schema(nullable)]
pub metadata: Option<Value>,
#[schema(nullable)]
pub title: Option<String>,
#[schema(nullable, value_type = Vec<Uuid>)]
pub tag_ids: Option<Vec<Uuid>>,
#[schema(nullable, value_type = Vec<CorrespondentAssignmentInput>)]
pub correspondents: Option<Vec<CorrespondentAssignmentInput>>,
#[schema(nullable, example = "2024-01-01T00:00:00Z")]
pub issued_at: Option<String>,
#[schema(nullable)]
pub skip_existing: Option<bool>,
}
#[derive(Deserialize, ToSchema)]
pub struct MoveDocumentRequest { pub struct MoveDocumentRequest {
pub folder_id: Option<Uuid>, pub folder_id: Option<Uuid>,
} }
#[derive(Deserialize)] #[derive(Deserialize, ToSchema)]
pub struct AssignTagsRequest { pub struct AssignTagsRequest {
pub tag_ids: Vec<Uuid>, pub tag_ids: Vec<Uuid>,
} }
#[derive(Deserialize, Default)] #[derive(Deserialize, Default, IntoParams, ToSchema)]
#[into_params(parameter_in = Query)]
pub struct AssetObjectsQuery { pub struct AssetObjectsQuery {
#[serde(default)] #[serde(default)]
pub start: Option<i32>, pub start: Option<i32>,
@@ -567,6 +621,59 @@ pub async fn list_documents(
Ok(Json(response)) Ok(Json(response))
} }
pub async fn check_document(
Query(query): Query<DocumentCheckQuery>,
TenantScopedConn {
mut conn,
tenant_id,
..
}: TenantScopedConn,
) -> AppResult<Json<DocumentCheckResponse>> {
let checksum_raw = query.checksum.trim();
if checksum_raw.is_empty() {
return Err(AppError::bad_request("checksum must not be empty"));
}
let checksum = checksum_raw.to_ascii_lowercase();
if !checksum.chars().all(|ch| ch.is_ascii_hexdigit()) {
return Err(AppError::bad_request(
"checksum must be a hex-encoded string",
));
}
let record: Option<(Document, DocumentVersion)> = documents::table
.inner_join(
document_versions::table.on(document_versions::id.eq(documents::current_version_id)),
)
.filter(documents::tenant_id.eq(tenant_id))
.filter(document_versions::checksum.eq(&checksum))
.select((documents::all_columns, document_versions::all_columns))
.first(&mut conn)
.optional()?;
if let Some((document, version)) = record {
Ok(Json(DocumentCheckResponse {
exists: true,
document_id: Some(document.id),
title: Some(document.title.clone()),
filename: Some(document.filename.clone()),
version_id: Some(version.id),
version_number: Some(version.version_number),
uploaded_at: Some(to_iso(document.uploaded_at)),
}))
} else {
Ok(Json(DocumentCheckResponse {
exists: false,
document_id: None,
title: None,
filename: None,
version_id: None,
version_number: None,
uploaded_at: None,
}))
}
}
pub async fn get_document( pub async fn get_document(
State(state): State<AppState>, State(state): State<AppState>,
Path(document_id): Path<Uuid>, Path(document_id): Path<Uuid>,
@@ -615,12 +722,17 @@ pub async fn upload_document(
tenant_id, user_id, .. tenant_id, user_id, ..
}: TenantScopedConn, }: TenantScopedConn,
mut multipart: Multipart, mut multipart: Multipart,
) -> AppResult<(StatusCode, Json<DocumentDetailResponse>)> { ) -> AppResult<impl IntoResponse> {
let mut file_bytes: Option<Vec<u8>> = None; let mut file_bytes: Option<Vec<u8>> = None;
let mut original_name: Option<String> = None; let mut original_name: Option<String> = None;
let mut content_type: Option<String> = None; let mut content_type: Option<String> = None;
let mut folder_id: Option<Uuid> = None; let mut folder_id: Option<Uuid> = None;
let mut metadata: Value = Value::Object(Default::default()); let mut metadata: Value = Value::Object(Default::default());
let mut tag_ids: Vec<Uuid> = Vec::new();
let mut correspondents: Vec<CorrespondentAssignmentInput> = Vec::new();
let mut issued_at_override: Option<NaiveDateTime> = None;
let mut skip_if_existing = false;
let mut title_override: Option<String> = None;
while let Some(field) = multipart.next_field().await.map_err(|err| { while let Some(field) = multipart.next_field().await.map_err(|err| {
let msg = format!("invalid multipart data: {err}"); let msg = format!("invalid multipart data: {err}");
@@ -664,6 +776,81 @@ pub async fn upload_document(
AppError::bad_request(msg) AppError::bad_request(msg)
})?; })?;
} }
Some("title") => {
let value = field.text().await.map_err(|err| {
let msg = format!("invalid title: {err}");
error!(error = %err, "invalid title payload");
AppError::bad_request(msg)
})?;
let trimmed = value.trim();
if !trimmed.is_empty() {
title_override = Some(trimmed.to_string());
}
}
Some("tag_ids") => {
let value = field.text().await.map_err(|err| {
let msg = format!("invalid tag_ids: {err}");
error!(error = %err, "invalid tag_ids payload");
AppError::bad_request(msg)
})?;
let parsed: Vec<String> = serde_json::from_str(&value).map_err(|err| {
let msg = format!("tag_ids must be a JSON array of UUID strings: {err}");
error!(error = %err, "invalid tag_ids json");
AppError::bad_request(msg)
})?;
let mut set = HashSet::new();
for raw in parsed {
let trimmed = raw.trim();
if trimmed.is_empty() {
continue;
}
let uuid = Uuid::parse_str(trimmed)
.map_err(|_| AppError::bad_request("tag_ids must contain valid UUIDs"))?;
set.insert(uuid);
}
tag_ids = set.into_iter().collect();
}
Some("correspondents") => {
let value = field.text().await.map_err(|err| {
let msg = format!("invalid correspondents: {err}");
error!(error = %err, "invalid correspondents payload");
AppError::bad_request(msg)
})?;
correspondents = serde_json::from_str(&value).map_err(|err| {
let msg = format!(
"correspondents must be a JSON array of {{correspondent_id, role}} objects: {err}"
);
error!(error = %err, "invalid correspondents json");
AppError::bad_request(msg)
})?;
}
Some("issued_at") => {
let value = field.text().await.map_err(|err| {
let msg = format!("invalid issued_at: {err}");
error!(error = %err, "invalid issued_at payload");
AppError::bad_request(msg)
})?;
let trimmed = value.trim();
if !trimmed.is_empty() {
let parsed = DateTime::parse_from_rfc3339(trimmed).map_err(|err| {
let msg = format!("issued_at must be an RFC3339 timestamp: {err}");
error!(error = %err, "invalid issued_at format");
AppError::bad_request(msg)
})?;
issued_at_override = Some(parsed.naive_utc());
}
}
Some("skip_existing") => {
let value = field.text().await.map_err(|err| {
let msg = format!("invalid skip_existing flag: {err}");
error!(error = %err, "invalid skip_existing payload");
AppError::bad_request(msg)
})?;
skip_if_existing = matches!(
value.trim().to_ascii_lowercase().as_str(),
"1" | "true" | "yes"
);
}
_ => {} _ => {}
} }
} }
@@ -689,31 +876,49 @@ pub async fn upload_document(
content_type, content_type,
folder_id, folder_id,
metadata, metadata,
title_override,
tag_ids,
correspondents,
issued_at_override,
skip_if_existing,
}; };
let outcome = match process_upload(&state, request, tenant_id, user_id).await { let outcome = match process_upload(&state, request, tenant_id, user_id).await {
Ok(outcome) => { Ok(outcome) => outcome,
info!(
document_id = %outcome.detail.document.id,
original_name = %outcome.detail.document.original_name,
created = outcome.created,
reused_existing = !outcome.created,
"document upload succeeded"
);
outcome
}
Err(err) => { Err(err) => {
error!(error = ?err, original_name = %original_name_for_log, "document upload failed"); error!(error = ?err, original_name = %original_name_for_log, "document upload failed");
return Err(err); return Err(err);
} }
}; };
let status = if outcome.created {
StatusCode::CREATED let response = match outcome {
} else { UploadOutcome::Created(detail) => {
StatusCode::OK info!(
document_id = %detail.document.id,
original_name = %detail.document.original_name,
created = true,
reused_existing = false,
"document upload succeeded",
);
(StatusCode::CREATED, Json(detail)).into_response()
}
UploadOutcome::Reused(detail) => {
info!(
document_id = %detail.document.id,
original_name = %detail.document.original_name,
created = false,
reused_existing = true,
"document upload succeeded",
);
(StatusCode::OK, Json(detail)).into_response()
}
UploadOutcome::Skipped { document_id } => {
info!(document_id = %document_id, "document upload skipped by client request");
StatusCode::NO_CONTENT.into_response()
}
}; };
Ok((status, Json(outcome.detail))) Ok(response)
} }
pub async fn request_document_assets( pub async fn request_document_assets(
@@ -1060,9 +1265,10 @@ pub async fn update_document(
match update_result.execute(&mut conn) { match update_result.execute(&mut conn) {
Ok(_) => {} Ok(_) => {}
Err(diesel::result::Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) => { Err(diesel::result::Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) => {
return Err(AppError::bad_request( return Err(AppError::conflict(
"another document in this folder already uses that filename", "another document in this folder already uses that filename",
)); )
.with_code("duplicate_filename"));
} }
Err(err) => return Err(AppError::from(err)), Err(err) => return Err(AppError::from(err)),
} }
@@ -1197,10 +1403,9 @@ pub async fn assign_correspondents(
return Err(AppError::bad_request("assignments must not be empty")); return Err(AppError::bad_request("assignments must not be empty"));
} }
let (normalized_pairs, correspondents_vec, roles_vec) = let (normalized_pairs, _correspondent_ids, roles_vec) =
normalize_correspondent_assignments(&payload.assignments)?; normalize_correspondent_assignments(&payload.assignments)?;
let replace = payload.replace; let replace = payload.replace;
let user_id_val = user_id;
conn.transaction::<(), AppError, _>(|conn| { conn.transaction::<(), AppError, _>(|conn| {
let document: Document = documents::table let document: Document = documents::table
@@ -1211,54 +1416,21 @@ pub async fn assign_correspondents(
return Err(AppError::not_found()); return Err(AppError::not_found());
} }
if !correspondents_vec.is_empty() { let mut deleted = 0;
let existing: Vec<Correspondent> = correspondents::table
.filter(correspondents::id.eq_any(&correspondents_vec))
.filter(correspondents::tenant_id.eq(tenant_id))
.load(conn)?;
if existing.len() != correspondents_vec.len() {
return Err(AppError::bad_request(
"one or more correspondents do not exist",
));
}
}
let mut changed = false;
if replace { if replace {
let deleted = diesel::delete( deleted = diesel::delete(
document_correspondents::table document_correspondents::table
.filter(document_correspondents::document_id.eq(document_id)) .filter(document_correspondents::document_id.eq(document_id))
.filter(document_correspondents::tenant_id.eq(tenant_id)) .filter(document_correspondents::tenant_id.eq(tenant_id))
.filter(document_correspondents::role.eq_any(&roles_vec)), .filter(document_correspondents::role.eq_any(&roles_vec)),
) )
.execute(conn)?; .execute(conn)?;
if deleted > 0 {
changed = true;
}
} }
let new_rows: Vec<NewDocumentCorrespondent> = normalized_pairs let inserted =
.iter() insert_document_correspondents(conn, tenant_id, &document, user_id, &normalized_pairs)?;
.map(|(correspondent_id, role)| NewDocumentCorrespondent {
document_id,
correspondent_id: *correspondent_id,
role: role.clone(),
assigned_by: Some(user_id_val),
tenant_id,
})
.collect();
if !new_rows.is_empty() { if replace && deleted > 0 && inserted == 0 {
let inserted = diesel::insert_into(document_correspondents::table)
.values(&new_rows)
.on_conflict_do_nothing()
.execute(conn)?;
if inserted > 0 {
changed = true;
}
}
if changed {
diesel::update( diesel::update(
documents::table documents::table
.find(document_id) .find(document_id)
@@ -1498,36 +1670,18 @@ pub async fn assign_tags(
return Err(AppError::bad_request("tag_ids must not be empty")); return Err(AppError::bad_request("tag_ids must not be empty"));
} }
// Ensure document exists let document: Document = documents::table
documents::table
.find(document_id) .find(document_id)
.filter(documents::tenant_id.eq(tenant_id)) .filter(documents::tenant_id.eq(tenant_id))
.first::<Document>(&mut conn)?; .first(&mut conn)?;
// Ensure tags exist assign_tags_internal(
let existing_tags: Vec<Tag> = tags::table &mut conn,
.filter(tags::id.eq_any(&payload.tag_ids)) tenant_id,
.filter(tags::tenant_id.eq(tenant_id)) &document,
.load(&mut conn)?; &payload.tag_ids,
if existing_tags.len() != payload.tag_ids.len() { Some(user_id),
return Err(AppError::bad_request("one or more tags do not exist")); )?;
}
let new_tags: Vec<NewDocumentTag> = payload
.tag_ids
.iter()
.map(|tag_id| NewDocumentTag {
document_id,
tag_id: *tag_id,
assigned_by: Some(user_id),
tenant_id,
})
.collect();
diesel::insert_into(document_tags::table)
.values(&new_tags)
.on_conflict_do_nothing()
.execute(&mut conn)?;
Ok(StatusCode::NO_CONTENT) Ok(StatusCode::NO_CONTENT)
} }
@@ -1648,6 +1802,11 @@ async fn process_upload(
content_type, content_type,
folder_id, folder_id,
metadata, metadata,
title_override,
tag_ids,
correspondents,
issued_at_override,
skip_if_existing,
} = request; } = request;
if let Some(folder) = folder_id { if let Some(folder) = folder_id {
@@ -1657,7 +1816,13 @@ async fn process_upload(
let doc_id = Uuid::new_v4(); let doc_id = Uuid::new_v4();
let version_id = Uuid::new_v4(); let version_id = Uuid::new_v4();
let version_number = 1; let version_number = 1;
let stored_filename = original_name.clone(); let derived_title = title_override
.as_ref()
.map(|value| value.trim())
.filter(|value| !value.is_empty())
.map(|value| value.to_string())
.unwrap_or_else(|| derive_document_title(&original_name));
let stored_filename = filename_with_retained_extension(&derived_title, &original_name);
let checksum = Sha256::digest(&bytes); let checksum = Sha256::digest(&bytes);
let checksum_hex = hex::encode(checksum); let checksum_hex = hex::encode(checksum);
@@ -1679,6 +1844,43 @@ async fn process_upload(
.optional()?; .optional()?;
if let Some((mut document, version)) = existing { if let Some((mut document, version)) = existing {
if skip_if_existing {
info!(
document_id = %document.id,
checksum = %checksum_hex,
"upload skipped existing document due to skip flag",
);
return Ok(UploadOutcome::Skipped {
document_id: document.id,
});
}
if let Some(issued_at) = issued_at_override {
if document.issued_at != Some(issued_at) {
diesel::update(
documents::table
.find(document.id)
.filter(documents::tenant_id.eq(tenant_id)),
)
.set((
documents::issued_at.eq(Some(issued_at)),
documents::updated_at.eq(Utc::now().naive_utc()),
))
.execute(&mut conn)?;
document.issued_at = Some(issued_at);
}
}
assign_tags_internal(&mut conn, tenant_id, &document, &tag_ids, Some(user_id))?;
assign_correspondents_internal(
&mut conn,
tenant_id,
&document,
user_id,
&correspondents,
)?;
if document.deleted_at.is_some() { if document.deleted_at.is_some() {
let now = Utc::now().naive_utc(); let now = Utc::now().naive_utc();
diesel::update(documents::table.find(document.id)) diesel::update(documents::table.find(document.id))
@@ -1706,23 +1908,20 @@ async fn process_upload(
"upload deduplicated existing document" "upload deduplicated existing document"
); );
return Ok(UploadOutcome { return Ok(UploadOutcome::Reused(DocumentDetailResponse {
detail: DocumentDetailResponse { document: to_document_response(
document: to_document_response( state,
state, user_id,
user_id, document,
document, tags,
tags, correspondents,
correspondents, Some((version_response, assets)),
Some((version_response, assets)), )?,
)?, }));
},
created: false,
});
} }
} }
let content_disposition = inline_content_disposition(&original_name); let content_disposition = inline_content_disposition(&stored_filename);
let storage = state.storage_for_tenant(tenant_id)?; let storage = state.storage_for_tenant(tenant_id)?;
@@ -1747,7 +1946,7 @@ async fn process_upload(
let (document, version) = { let (document, version) = {
let mut conn = state.db_for_tenant(tenant_id)?; let mut conn = state.db_for_tenant(tenant_id)?;
conn.transaction(|conn| { let transaction_result = conn.transaction(|conn| {
let new_document = NewDocument { let new_document = NewDocument {
id: doc_id, id: doc_id,
filename: stored_filename.clone(), filename: stored_filename.clone(),
@@ -1755,8 +1954,8 @@ async fn process_upload(
content_type: content_type.clone(), content_type: content_type.clone(),
folder_id, folder_id,
current_version_id: version_id, current_version_id: version_id,
issued_at: None, issued_at: issued_at_override,
title: derive_document_title(&original_name), title: derived_title.clone(),
metadata: metadata_value.clone(), metadata: metadata_value.clone(),
tenant_id, tenant_id,
}; };
@@ -1784,18 +1983,43 @@ async fn process_upload(
let version: DocumentVersion = document_versions::table.find(version_id).first(conn)?; let version: DocumentVersion = document_versions::table.find(version_id).first(conn)?;
Ok::<_, diesel::result::Error>((document, version)) Ok::<_, diesel::result::Error>((document, version))
})? });
match transaction_result {
Ok(result) => result,
Err(diesel::result::Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) => {
return Err(AppError::conflict(
"another document in this folder already uses that filename",
)
.with_code("duplicate_filename"))
}
Err(err) => return Err(AppError::from(err)),
}
}; };
let detail = DocumentDetailResponse { let detail = {
document: to_document_response( let mut conn = state.db_for_tenant(tenant_id)?;
state,
user_id, assign_tags_internal(&mut conn, tenant_id, &document, &tag_ids, Some(user_id))?;
document,
None, assign_correspondents_internal(&mut conn, tenant_id, &document, user_id, &correspondents)?;
Vec::new(),
Some((to_version_response(version.clone(), true), Vec::new())), let tags_map = load_tags_for_documents(&mut conn, &[doc_id])?;
)?, let mut correspondents_map = load_correspondents_for_documents(&mut conn, &[doc_id])?;
let tags = tags_map.get(&doc_id).cloned();
let correspondents = correspondents_map.remove(&doc_id).unwrap_or_default();
drop(conn);
DocumentDetailResponse {
document: to_document_response(
state,
user_id,
document,
tags,
correspondents,
Some((to_version_response(version.clone(), true), Vec::new())),
)?,
}
}; };
if let Ok(mut conn) = state.db_for_tenant(tenant_id) { if let Ok(mut conn) = state.db_for_tenant(tenant_id) {
@@ -1816,10 +2040,137 @@ async fn process_upload(
warn!(document_id = %doc_id, "failed to enqueue analyze job due to pool error"); warn!(document_id = %doc_id, "failed to enqueue analyze job due to pool error");
} }
Ok(UploadOutcome { Ok(UploadOutcome::Created(detail))
detail, }
created: true,
}) fn assign_tags_internal(
conn: &mut PgConnection,
tenant_id: Uuid,
document: &Document,
raw_tag_ids: &[Uuid],
assigned_by: Option<Uuid>,
) -> AppResult<usize> {
if raw_tag_ids.is_empty() {
return Ok(0);
}
let mut tag_ids: Vec<Uuid> = raw_tag_ids.iter().copied().collect();
tag_ids.sort_unstable();
tag_ids.dedup();
if tag_ids.is_empty() {
return Ok(0);
}
let existing: Vec<Uuid> = tags::table
.filter(tags::id.eq_any(&tag_ids))
.filter(tags::tenant_id.eq(tenant_id))
.select(tags::id)
.load(conn)?;
if existing.len() != tag_ids.len() {
return Err(AppError::bad_request("one or more tags do not exist"));
}
let new_tags: Vec<NewDocumentTag> = tag_ids
.into_iter()
.map(|tag_id| NewDocumentTag {
document_id: document.id,
tag_id,
assigned_by,
tenant_id,
})
.collect();
if new_tags.is_empty() {
return Ok(0);
}
let inserted = diesel::insert_into(document_tags::table)
.values(&new_tags)
.on_conflict_do_nothing()
.execute(conn)?;
Ok(inserted)
}
fn assign_correspondents_internal(
conn: &mut PgConnection,
tenant_id: Uuid,
document: &Document,
user_id: Uuid,
assignments: &[CorrespondentAssignmentInput],
) -> AppResult<usize> {
if assignments.is_empty() {
return Ok(0);
}
let (normalized_pairs, _correspondent_ids, _roles) =
normalize_correspondent_assignments(assignments)?;
insert_document_correspondents(conn, tenant_id, document, user_id, &normalized_pairs)
}
fn insert_document_correspondents(
conn: &mut PgConnection,
tenant_id: Uuid,
document: &Document,
user_id: Uuid,
normalized_pairs: &[(Uuid, String)],
) -> AppResult<usize> {
if normalized_pairs.is_empty() {
return Ok(0);
}
let mut correspondent_ids: Vec<Uuid> = normalized_pairs.iter().map(|(id, _)| *id).collect();
correspondent_ids.sort_unstable();
correspondent_ids.dedup();
if !correspondent_ids.is_empty() {
let existing: Vec<Uuid> = correspondents::table
.filter(correspondents::id.eq_any(&correspondent_ids))
.filter(correspondents::tenant_id.eq(tenant_id))
.select(correspondents::id)
.load(conn)?;
if existing.len() != correspondent_ids.len() {
return Err(AppError::bad_request(
"one or more correspondents do not exist",
));
}
}
let new_rows: Vec<NewDocumentCorrespondent> = normalized_pairs
.iter()
.map(|(correspondent_id, role)| NewDocumentCorrespondent {
document_id: document.id,
correspondent_id: *correspondent_id,
role: role.clone(),
assigned_by: Some(user_id),
tenant_id,
})
.collect();
if new_rows.is_empty() {
return Ok(0);
}
let inserted = diesel::insert_into(document_correspondents::table)
.values(&new_rows)
.on_conflict_do_nothing()
.execute(conn)?;
if inserted > 0 {
diesel::update(
documents::table
.find(document.id)
.filter(documents::tenant_id.eq(tenant_id)),
)
.set(documents::updated_at.eq(Utc::now().naive_utc()))
.execute(conn)?;
}
Ok(inserted)
} }
fn ensure_folder_exists(state: &AppState, tenant_id: Uuid, folder_id: Uuid) -> AppResult<()> { fn ensure_folder_exists(state: &AppState, tenant_id: Uuid, folder_id: Uuid) -> AppResult<()> {
+71 -30
View File
@@ -4,6 +4,7 @@ use axum::{
}; };
use diesel::{dsl::exists, prelude::*, PgConnection}; use diesel::{dsl::exists, prelude::*, PgConnection};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid; use uuid::Uuid;
use crate::models::{Document, Folder, NewFolder}; use crate::models::{Document, Folder, NewFolder};
@@ -18,7 +19,10 @@ use super::documents::{
load_correspondents_for_documents, load_primary_assets, load_tags_for_documents, load_correspondents_for_documents, load_primary_assets, load_tags_for_documents,
to_document_response, DocumentResponse, to_document_response, DocumentResponse,
}; };
use crate::utils::time::to_iso; use crate::utils::{
json::{classify_nullable, NullableValue},
time::to_iso,
};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct CreateFolderRequest { pub struct CreateFolderRequest {
@@ -32,13 +36,6 @@ pub struct EnsureFolderPathRequest {
pub segments: Vec<String>, pub segments: Vec<String>,
} }
#[derive(Deserialize)]
pub struct UpdateFolderRequest {
#[serde(default)]
pub parent_id: Option<Option<Uuid>>,
pub name: Option<String>,
}
#[derive(Serialize)] #[derive(Serialize)]
pub struct FolderResponse { pub struct FolderResponse {
pub folder: FolderInfo, pub folder: FolderInfo,
@@ -70,6 +67,24 @@ pub struct FolderInfo {
pub updated_at: String, pub updated_at: String,
} }
pub async fn get_folder(
Path(folder_id): Path<Uuid>,
TenantScopedConn {
mut conn,
tenant_id,
..
}: TenantScopedConn,
) -> AppResult<Json<FolderResponse>> {
let folder: Folder = folders::table
.find(folder_id)
.filter(folders::tenant_id.eq(tenant_id))
.first(&mut conn)?;
Ok(Json(FolderResponse {
folder: folder_to_info(folder),
}))
}
pub async fn ensure_folder_path( pub async fn ensure_folder_path(
TenantScopedConn { TenantScopedConn {
mut conn, mut conn,
@@ -322,8 +337,15 @@ pub async fn update_folder(
tenant_id, tenant_id,
.. ..
}: TenantScopedConn, }: TenantScopedConn,
Json(payload): Json<UpdateFolderRequest>, Json(body): Json<Value>,
) -> AppResult<StatusCode> { ) -> AppResult<StatusCode> {
if !body.is_object() {
return Err(AppError::bad_request("request body must be a JSON object"));
}
let parent_class = classify_nullable(body.get("parent_id")).map_err(AppError::bad_request)?;
let name_class = classify_nullable(body.get("name")).map_err(AppError::bad_request)?;
conn.transaction::<(), AppError, _>(|conn| { conn.transaction::<(), AppError, _>(|conn| {
let folder: Folder = folders::table let folder: Folder = folders::table
.find(folder_id) .find(folder_id)
@@ -332,42 +354,61 @@ pub async fn update_folder(
let mut next_parent = folder.parent_id; let mut next_parent = folder.parent_id;
let mut parent_changed = false; let mut parent_changed = false;
match parent_class {
if let Some(parent_request) = payload.parent_id { NullableValue::Omitted => {}
if parent_request == Some(folder_id) { NullableValue::Null => {
return Err(AppError::bad_request("folder cannot be its own parent")); if folder.parent_id.is_some() {
parent_changed = true;
}
next_parent = None;
} }
NullableValue::String(value) => {
let trimmed = value.trim();
if trimmed.is_empty() {
return Err(AppError::bad_request("parent_id must not be empty"));
}
let parent_id = Uuid::parse_str(trimmed)
.map_err(|_| AppError::bad_request("parent_id must be a valid UUID or null"))?;
if parent_id == folder_id {
return Err(AppError::bad_request("folder cannot be its own parent"));
}
if let Some(parent_id) = parent_request {
let _parent: Folder = folders::table let _parent: Folder = folders::table
.find(parent_id) .find(parent_id)
.filter(folders::tenant_id.eq(tenant_id)) .filter(folders::tenant_id.eq(tenant_id))
.first(conn)?; .first(conn)?;
let descendant_ids = gather_descendant_folder_ids(conn, tenant_id, folder_id)?; if folder.parent_id != Some(parent_id) {
if descendant_ids.contains(&parent_id) { let descendant_ids = gather_descendant_folder_ids(conn, tenant_id, folder_id)?;
return Err(AppError::bad_request( if descendant_ids.contains(&parent_id) {
"cannot move folder into itself or a descendant", return Err(AppError::bad_request(
)); "cannot move folder into itself or a descendant",
));
}
parent_changed = true;
} }
}
parent_changed = parent_request != folder.parent_id; next_parent = Some(parent_id);
next_parent = parent_request; }
} }
let mut new_name = folder.name.clone(); let mut new_name = folder.name.clone();
let mut name_changed = false; let mut name_changed = false;
match name_class {
if let Some(name) = payload.name { NullableValue::Omitted => {}
let trimmed = name.trim(); NullableValue::Null => {
if trimmed.is_empty() { return Err(AppError::bad_request("name cannot be null"));
return Err(AppError::bad_request("name must not be empty"));
} }
NullableValue::String(value) => {
let trimmed = value.trim();
if trimmed.is_empty() {
return Err(AppError::bad_request("name must not be empty"));
}
if trimmed != folder.name { if trimmed != folder.name {
new_name = trimmed.to_string(); new_name = trimmed.to_string();
name_changed = true; name_changed = true;
}
} }
} }
+21 -5
View File
@@ -2,12 +2,15 @@ use axum::http::HeaderValue;
use axum::{ use axum::{
extract::DefaultBodyLimit, extract::DefaultBodyLimit,
middleware, middleware,
response::Json,
routing::{delete, get, patch, post}, routing::{delete, get, patch, post},
Router, Router,
}; };
use std::sync::Arc;
use tower_http::cors::{AllowOrigin, CorsLayer}; use tower_http::cors::{AllowOrigin, CorsLayer};
use utoipa::OpenApi;
use crate::{auth::AuthenticatedUser, state::AppState}; use crate::{auth::AuthenticatedUser, openapi::ApiDoc, state::AppState};
pub mod auth; pub mod auth;
pub mod correspondents; pub mod correspondents;
@@ -54,6 +57,7 @@ pub fn create_router(state: AppState) -> Router<()> {
.route("/me", get(auth::me)); .route("/me", get(auth::me));
let documents_routes = Router::new() let documents_routes = Router::new()
.route("/check", get(documents::check_document))
.route( .route(
"/", "/",
get(documents::list_documents).post(documents::upload_document), get(documents::list_documents).post(documents::upload_document),
@@ -97,10 +101,9 @@ pub fn create_router(state: AppState) -> Router<()> {
let folders_routes = Router::new() let folders_routes = Router::new()
.route("/", post(folders::create_folder)) .route("/", post(folders::create_folder))
.route("/path", post(folders::ensure_folder_path)) .route("/path", post(folders::ensure_folder_path))
.route( .route("/:id", get(folders::get_folder))
"/:id", .route("/:id", delete(folders::delete_folder))
delete(folders::delete_folder).patch(folders::update_folder), .route("/:id", patch(folders::update_folder))
)
.route("/:id/contents", get(folders::list_folder_contents)); .route("/:id/contents", get(folders::list_folder_contents));
let tags_routes = Router::new() let tags_routes = Router::new()
@@ -129,9 +132,22 @@ pub fn create_router(state: AppState) -> Router<()> {
.nest("/api/assets", assets_routes) .nest("/api/assets", assets_routes)
.layer(middleware::from_extractor_with_state::<AuthenticatedUser, _>(protected_state)); .layer(middleware::from_extractor_with_state::<AuthenticatedUser, _>(protected_state));
let openapi_arc = Arc::new(ApiDoc::openapi());
let docs_route = Router::new().route(
"/api/docs/openapi.json",
get({
let spec = openapi_arc.clone();
move || {
let spec = spec.clone();
async move { Json((*spec).clone()) }
}
}),
);
Router::new() Router::new()
.merge(download_routes) .merge(download_routes)
.merge(protected_routes) .merge(protected_routes)
.merge(docs_route)
.nest("/api/auth", auth_routes) .nest("/api/auth", auth_routes)
.route("/api/health", get(health::health_check)) .route("/api/health", get(health::health_check))
.with_state(state) .with_state(state)
+41 -2
View File
@@ -165,7 +165,7 @@ impl TestApp {
pub async fn cleanup(&self) -> Result<()> { pub async fn cleanup(&self) -> Result<()> {
let pool = self.state.pool.clone(); let pool = self.state.pool.clone();
tokio::task::spawn_blocking(move || -> Result<()> { let _ = tokio::task::spawn_blocking(move || -> Result<()> {
let mut conn = pool let mut conn = pool
.get() .get()
.map_err(|err| anyhow!("failed to get cleanup connection: {err}"))?; .map_err(|err| anyhow!("failed to get cleanup connection: {err}"))?;
@@ -184,6 +184,7 @@ impl TestApp {
self.storage.clone() self.storage.clone()
} }
#[allow(dead_code)]
pub async fn storage_key_for(&self, key: &str) -> Result<String> { pub async fn storage_key_for(&self, key: &str) -> Result<String> {
let tenant = self let tenant = self
.state .state
@@ -322,7 +323,7 @@ impl TestApp {
#[derive(Deserialize)] #[derive(Deserialize)]
struct TenantSummary { struct TenantSummary {
tenant_id: Uuid, tenant_id: Uuid,
slug: String, _slug: String,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@@ -477,6 +478,30 @@ impl TestApp {
data: &[u8], data: &[u8],
folder_id: Option<Uuid>, folder_id: Option<Uuid>,
token: &str, token: &str,
) -> Result<hyper::Response<Body>> {
self.upload_document_with_options(
path,
filename,
content_type,
data,
folder_id,
None,
None,
token,
)
.await
}
pub async fn upload_document_with_options(
&self,
path: &str,
filename: &str,
content_type: &str,
data: &[u8],
folder_id: Option<Uuid>,
title: Option<&str>,
metadata_json: Option<&str>,
token: &str,
) -> Result<hyper::Response<Body>> { ) -> Result<hyper::Response<Body>> {
let boundary = format!("boundary-{}", Uuid::new_v4()); let boundary = format!("boundary-{}", Uuid::new_v4());
let mut body = Vec::new(); let mut body = Vec::new();
@@ -499,6 +524,20 @@ impl TestApp {
body.extend(b"\r\n"); body.extend(b"\r\n");
} }
if let Some(title_value) = title {
body.extend(format!("--{boundary}\r\n").as_bytes());
body.extend(b"Content-Disposition: form-data; name=\"title\"\r\n\r\n");
body.extend(title_value.as_bytes());
body.extend(b"\r\n");
}
if let Some(metadata_value) = metadata_json {
body.extend(format!("--{boundary}\r\n").as_bytes());
body.extend(b"Content-Disposition: form-data; name=\"metadata\"\r\n\r\n");
body.extend(metadata_value.as_bytes());
body.extend(b"\r\n");
}
body.extend(format!("--{boundary}--\r\n").as_bytes()); body.extend(format!("--{boundary}--\r\n").as_bytes());
let builder = Request::builder() let builder = Request::builder()
+38
View File
@@ -15,6 +15,7 @@ struct DocumentDetail {
struct DocumentInfo { struct DocumentInfo {
id: Uuid, id: Uuid,
title: String, title: String,
filename: String,
original_name: String, original_name: String,
deleted_at: Option<String>, deleted_at: Option<String>,
issued_at: Option<String>, issued_at: Option<String>,
@@ -240,6 +241,43 @@ async fn upload_and_list_document() -> Result<()> {
Ok(()) Ok(())
} }
#[tokio::test]
async fn upload_document_with_custom_title_sets_filename() -> Result<()> {
let _lock = acquire_db_lock().await;
let app = TestApp::new().await?;
let password = "passw0rd";
app.insert_user("nora", password, "admin").await?;
let token = app.login_token("nora", password).await?;
let file_bytes = b"example contract body".to_vec();
let title = "Vendor Contract";
let original_filename = "scan.pdf";
let upload = app
.upload_document_with_options(
"/api/documents",
original_filename,
"application/pdf",
&file_bytes,
None,
Some(title),
None,
&token,
)
.await?;
assert_eq!(upload.status(), StatusCode::CREATED);
let body = body_to_vec(upload.into_body()).await?;
let detail: DocumentDetail = serde_json::from_slice(&body)?;
assert_eq!(detail.document.title, title);
assert_eq!(detail.document.filename, format!("{title}.pdf"));
assert_eq!(detail.document.original_name, original_filename);
app.cleanup().await?;
Ok(())
}
#[tokio::test] #[tokio::test]
async fn duplicate_and_restore_document() -> Result<()> { async fn duplicate_and_restore_document() -> Result<()> {
let _lock = acquire_db_lock().await; let _lock = acquire_db_lock().await;
+75 -12
View File
@@ -5,6 +5,7 @@ use axum::http::StatusCode;
use common::{acquire_db_lock, body_to_vec, TestApp}; use common::{acquire_db_lock, body_to_vec, TestApp};
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use serde_json::json;
use uuid::Uuid; use uuid::Uuid;
#[derive(Deserialize)] #[derive(Deserialize)]
@@ -16,6 +17,7 @@ struct FolderResponse {
struct FolderInfo { struct FolderInfo {
id: Uuid, id: Uuid,
name: String, name: String,
parent_id: Option<Uuid>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@@ -42,14 +44,6 @@ struct EnsureFolderPath<'a> {
segments: &'a [&'a str], segments: &'a [&'a str],
} }
#[derive(Serialize)]
struct UpdateFolderRequest {
#[serde(skip_serializing_if = "Option::is_none")]
parent_id: Option<Option<Uuid>>,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
}
#[derive(Serialize)] #[derive(Serialize)]
struct MoveDocumentRequest { struct MoveDocumentRequest {
folder_id: Option<Uuid>, folder_id: Option<Uuid>,
@@ -150,6 +144,78 @@ async fn folder_move_and_delete_flow() -> Result<()> {
Ok(()) Ok(())
} }
#[tokio::test]
async fn update_folder_parent_to_root() -> Result<()> {
let _lock = acquire_db_lock().await;
let app = TestApp::new().await?;
let password = "rootpass";
app.insert_user("root-admin", password, "admin").await?;
let token = app.login_token("root-admin", password).await?;
// Create a parent folder under root
let parent_resp = app
.post_json(
"/api/folders",
&CreateFolder {
name: "Parent",
parent_id: None,
},
Some(&token),
)
.await?;
assert_eq!(parent_resp.status(), StatusCode::OK);
let parent_body = body_to_vec(parent_resp.into_body()).await?;
let parent: FolderResponse = serde_json::from_slice(&parent_body)?;
// Create a child folder inside the parent
let child_resp = app
.post_json(
"/api/folders",
&CreateFolder {
name: "Child",
parent_id: Some(parent.folder.id),
},
Some(&token),
)
.await?;
assert_eq!(child_resp.status(), StatusCode::OK);
let child_body = body_to_vec(child_resp.into_body()).await?;
let child: FolderResponse = serde_json::from_slice(&child_body)?;
// Move the child back to the root by setting parent_id to null
let update_resp = app
.patch_json(
&format!("/api/folders/{}", child.folder.id),
&json!({ "parent_id": null }),
Some(&token),
)
.await?;
assert_eq!(update_resp.status(), StatusCode::NO_CONTENT);
// Fetch the child folder and ensure parent_id is now null
let updated_resp = app
.get(&format!("/api/folders/{}", child.folder.id), Some(&token))
.await?;
assert_eq!(updated_resp.status(), StatusCode::OK);
let updated_body = body_to_vec(updated_resp.into_body()).await?;
let updated_folder: FolderResponse = serde_json::from_slice(&updated_body)?;
assert!(updated_folder.folder.parent_id.is_none());
// Root contents should include the child folder by name
let root_contents = app.get("/api/folders/root/contents", Some(&token)).await?;
assert_eq!(root_contents.status(), StatusCode::OK);
let root_body = body_to_vec(root_contents.into_body()).await?;
let root: FolderContents = serde_json::from_slice(&root_body)?;
assert!(root
.subfolders
.iter()
.any(|folder| folder.id == child.folder.id));
app.cleanup().await?;
Ok(())
}
#[tokio::test] #[tokio::test]
async fn ensure_path_creates_nested_folders() -> Result<()> { async fn ensure_path_creates_nested_folders() -> Result<()> {
let _lock = acquire_db_lock().await; let _lock = acquire_db_lock().await;
@@ -267,10 +333,7 @@ async fn folder_rename_updates_name_and_child_paths() -> Result<()> {
let rename_resp = app let rename_resp = app
.patch_json( .patch_json(
&format!("/api/folders/{}", parent.folder.id), &format!("/api/folders/{}", parent.folder.id),
&UpdateFolderRequest { &json!({ "name": "Archive" }),
parent_id: None,
name: Some("Archive".to_string()),
},
Some(&token), Some(&token),
) )
.await?; .await?;
+4 -1
View File
@@ -17,7 +17,8 @@ Health
Documents Documents
--------- ---------
- GET /api/documents - List or search documents. Optional filters: `folder_id` (defaults to root when omitted), `include_deleted`, `include_descendants` (defaults to true when a `folder_id` is provided and no other override is supplied), `query` (Quickwit full-text), `tags` (comma-separated tag UUIDs), and `correspondents` (comma-separated correspondent UUIDs). Each entry includes tags, correspondent assignments, and current version info. - GET /api/documents - List or search documents. Optional filters: `folder_id` (defaults to root when omitted), `include_deleted`, `include_descendants` (defaults to true when a `folder_id` is provided and no other override is supplied), `query` (Quickwit full-text), `tags` (comma-separated tag UUIDs), and `correspondents` (comma-separated correspondent UUIDs). Each entry includes tags, correspondent assignments, and current version info.
- POST /api/documents - Upload a document via multipart form-data (`file`, optional metadata/folder fields). - GET /api/documents/check?checksum=<sha256> - Lightweight checksum preflight. Returns `exists=false` when no document with the supplied SHA-256 checksum is present; otherwise returns `exists=true` plus the current document metadata.
- POST /api/documents - Upload a document via multipart form-data. Required field: `file`. Optional fields: `title`, `folder_id`, JSON `metadata`, JSON array `tag_ids`, JSON array `correspondents` (each with `correspondent_id` and `role`), and `issued_at` (RFC3339). When `title` is supplied, the stored filename becomes `<title><original_extension>`. Include `skip_existing=true` to receive `204 No Content` instead of reusing a matching document.
- POST /api/documents/bulk/move - Move multiple documents to a target folder. - POST /api/documents/bulk/move - Move multiple documents to a target folder.
- POST /api/documents/bulk/tags - Add or remove tags across multiple documents. - POST /api/documents/bulk/tags - Add or remove tags across multiple documents.
- POST /api/documents/bulk/correspondents - Bulk correspondent actions. Default `action=add` replaces existing assignments for the provided roles before adding the supplied correspondents; `action=remove` drops the specified correspondent/role pairs. - POST /api/documents/bulk/correspondents - Bulk correspondent actions. Default `action=add` replaces existing assignments for the provided roles before adding the supplied correspondents; `action=remove` drops the specified correspondent/role pairs.
@@ -46,9 +47,11 @@ Folders
------- -------
- POST /api/folders - Create a folder (optionally under a parent). - POST /api/folders - Create a folder (optionally under a parent).
- POST /api/folders/path - Ensure a nested folder path exists, creating missing segments. - POST /api/folders/path - Ensure a nested folder path exists, creating missing segments.
- GET /api/folders/:id - Fetch folder metadata.
- GET /api/folders/:id/contents - List subfolders and documents inside a folder; use `root` for the workspace root. - GET /api/folders/:id/contents - List subfolders and documents inside a folder; use `root` for the workspace root.
- DELETE /api/folders/:id - Soft-delete a folder. - DELETE /api/folders/:id - Soft-delete a folder.
- PATCH /api/folders/:id - Update a folder's parent (`parent_id`) and/or rename it (`name`). - PATCH /api/folders/:id - Update a folder's parent (`parent_id`) and/or rename it (`name`).
- GET /api/docs/openapi.json - Generated OpenAPI specification (JSON).
Tags Tags
---- ----
@@ -1,34 +0,0 @@
{{- if and .Values.quickwit.enabled .Values.quickwit.indexJob.enabled .Values.quickwit.indexJob.indexConfig.create }}
{{- $cfg := .Values.quickwit.indexJob.indexConfig -}}
{{- $config := dict "value" "" -}}
{{- if $cfg.file }}
{{- with $.Files.Get $cfg.file -}}
{{- $_ := set $config "value" . -}}
{{- end -}}
{{- end -}}
{{- if and (not (index $config "value")) $cfg.contents }}
{{- $_ := set $config "value" $cfg.contents -}}
{{- end -}}
{{- if eq (trim (default "" (index $config "value"))) "" -}}
{{- fail "Quickwit index configuration requires either indexConfig.file or indexConfig.contents" -}}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "papercrate.fullname" . }}-quickwit-index
labels:
{{- include "papercrate.labels" . | nindent 4 }}
app.kubernetes.io/component: quickwit
app.kubernetes.io/part-of: quickwit-index
data:
{{ $cfg.key }}: |
{{ index $config "value" | indent 4 }}
{{- $ctx := dict "storageUri" (.Values.quickwit.env.storageUri | default "") -}}
{{- if and (eq (index $ctx "storageUri") "") .Values.global.s3.bucket -}}
{{- $_ := set $ctx "storageUri" (printf "s3://%s/quickwit" .Values.global.s3.bucket) -}}
{{- end -}}
{{- $storageUri := index $ctx "storageUri" -}}
{{- if $storageUri }}
{{ printf "index_uri: %s/documents" (trimSuffix "/" $storageUri) | indent 4 }}
{{- end }}
{{- end }}
@@ -1,78 +0,0 @@
{{- if and .Values.quickwit.enabled .Values.quickwit.indexJob.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "papercrate.fullname" . }}-quickwit-index
labels:
{{- include "papercrate.labels" . | nindent 4 }}
app.kubernetes.io/component: quickwit-index
annotations:
"helm.sh/hook": post-install,post-upgrade
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
backoffLimit: {{ .Values.quickwit.indexJob.backoffLimit }}
template:
metadata:
labels:
{{- include "papercrate.selectorLabels" . | nindent 8 }}
app.kubernetes.io/component: quickwit-index
spec:
restartPolicy: Never
{{- if or .Values.global.imagePullSecrets .Values.imagePullSecrets }}
imagePullSecrets:
{{- range .Values.global.imagePullSecrets }}
- name: {{ . }}
{{- end }}
{{- range .Values.imagePullSecrets }}
- name: {{ . }}
{{- end }}
{{- end }}
{{- $qw := dict "storageUri" .Values.quickwit.env.storageUri "metastoreUri" .Values.quickwit.env.metastoreUri -}}
{{- if and (not (index $qw "storageUri")) .Values.global.s3.bucket -}}
{{- $_ := set $qw "storageUri" (printf "s3://%s/quickwit" .Values.global.s3.bucket) -}}
{{- end -}}
{{- if not (index $qw "storageUri") -}}
{{- fail "quickwit.env.storageUri must be set, or provide global.s3.bucket" -}}
{{- end -}}
{{- if and (not (index $qw "metastoreUri")) .Values.postgres.enabled -}}
{{- $dbUser := required "postgres.auth.username is required when postgres.enabled" .Values.postgres.auth.username -}}
{{- $dbPass := required "postgres.auth.password is required when postgres.enabled" .Values.postgres.auth.password -}}
{{- $dbHost := printf "%s-postgres" (include "papercrate.fullname" .) -}}
{{- $_ := set $qw "metastoreUri" (printf "postgres://%s:%s@%s:5432/quickwit" $dbUser $dbPass $dbHost) -}}
{{- end -}}
{{- if not (index $qw "metastoreUri") -}}
{{- fail "quickwit.env.metastoreUri must be set when not using bundled Postgres" -}}
{{- end }}
containers:
- name: quickwit-index
image: {{ include "papercrate.quickwit.image" . }}:{{ include "papercrate.image.tag" (dict "image" .Values.quickwit.image "Chart" .Chart) }}
imagePullPolicy: {{ .Values.quickwit.image.pullPolicy }}
command:
- quickwit
- index
- create
args:
- --index-config=/config/{{ .Values.quickwit.indexJob.indexConfig.key }}
- --endpoint=http://{{ include "papercrate.fullname" . }}-quickwit:{{ .Values.quickwit.service.port }}
{{- if or .Values.quickwit.envFrom .Values.global.s3.credentialsSecret }}
envFrom:
{{- with .Values.global.s3.credentialsSecret }}
- secretRef:
name: {{ . }}
{{- end }}
{{- range .Values.quickwit.envFrom }}
- {{ toYaml . | nindent 12 | trim }}
{{- end }}
{{- end }}
{{- if or .Values.quickwit.indexJob.indexConfig.existingConfigMap .Values.quickwit.indexJob.indexConfig.create }}
volumeMounts:
- name: index-config
mountPath: /config
{{- end }}
{{- if or .Values.quickwit.indexJob.indexConfig.existingConfigMap .Values.quickwit.indexJob.indexConfig.create }}
volumes:
- name: index-config
configMap:
name: {{ if .Values.quickwit.indexJob.indexConfig.existingConfigMap }}{{ .Values.quickwit.indexJob.indexConfig.existingConfigMap }}{{ else }}{{ include "papercrate.fullname" . }}-quickwit-index{{ end }}
{{- end }}
{{- end }}
-10
View File
@@ -141,16 +141,6 @@ quickwit:
nodeSelector: {} nodeSelector: {}
tolerations: [] tolerations: []
affinity: {} affinity: {}
indexJob:
enabled: true
backoffLimit: 3
indexConfig:
existingConfigMap: ""
key: documents-index.yaml
create: true
file: files/quickwit/documents-index.yaml
contents: ""
imagePullSecrets: [] imagePullSecrets: []
nameOverride: "" nameOverride: ""
fullnameOverride: "" fullnameOverride: ""