cleanup
This commit is contained in:
@@ -325,6 +325,7 @@ struct UploadRequest {
|
||||
correspondents: Vec<CorrespondentAssignmentInput>,
|
||||
issued_at_override: Option<NaiveDateTime>,
|
||||
skip_if_existing: bool,
|
||||
document_type_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
enum UploadOutcome {
|
||||
@@ -785,6 +786,7 @@ pub async fn upload_document(
|
||||
let mut issued_at_override: Option<NaiveDateTime> = None;
|
||||
let mut skip_if_existing = false;
|
||||
let mut title_override: Option<String> = None;
|
||||
let mut document_type_id: Option<Uuid> = None;
|
||||
|
||||
while let Some(field) = multipart.next_field().await.map_err(|err| {
|
||||
let msg = format!("invalid multipart data: {err}");
|
||||
@@ -903,6 +905,20 @@ pub async fn upload_document(
|
||||
"1" | "true" | "yes"
|
||||
);
|
||||
}
|
||||
Some("document_type_id") => {
|
||||
let value = field.text().await.map_err(|err| {
|
||||
let msg = format!("invalid document_type_id: {err}");
|
||||
error!(error = %err, "invalid document_type payload");
|
||||
AppError::bad_request(msg)
|
||||
})?;
|
||||
let trimmed = value.trim();
|
||||
if !trimmed.is_empty() {
|
||||
let parsed = Uuid::parse_str(trimmed).map_err(|_| {
|
||||
AppError::bad_request("document_type_id must be a valid UUID")
|
||||
})?;
|
||||
document_type_id = Some(parsed);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -933,6 +949,7 @@ pub async fn upload_document(
|
||||
correspondents,
|
||||
issued_at_override,
|
||||
skip_if_existing,
|
||||
document_type_id,
|
||||
};
|
||||
|
||||
let outcome = match process_upload(&state, request, tenant_id, user_id).await {
|
||||
@@ -2178,8 +2195,23 @@ async fn process_upload(
|
||||
correspondents,
|
||||
issued_at_override,
|
||||
skip_if_existing,
|
||||
document_type_id,
|
||||
} = request;
|
||||
|
||||
if let Some(type_id) = document_type_id {
|
||||
let mut conn = state.db_for_tenant(tenant_id)?;
|
||||
let exists = document_types::table
|
||||
.filter(document_types::tenant_id.eq(tenant_id))
|
||||
.find(type_id)
|
||||
.first::<DocumentType>(&mut conn)
|
||||
.optional()?;
|
||||
if exists.is_none() {
|
||||
return Err(AppError::bad_request(
|
||||
"document_type_id does not exist for this tenant",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(folder) = folder_id {
|
||||
let mut conn = state.db_for_tenant(tenant_id)?;
|
||||
ensure_folder_exists_on_conn(&mut conn, tenant_id, folder)?;
|
||||
@@ -2260,6 +2292,24 @@ async fn process_upload(
|
||||
)?;
|
||||
}
|
||||
|
||||
if let Some(type_id) = document_type_id {
|
||||
if document.document_type_id != Some(type_id) {
|
||||
let now = Utc::now().naive_utc();
|
||||
diesel::update(
|
||||
documents::table
|
||||
.find(document.id)
|
||||
.filter(documents::tenant_id.eq(tenant_id)),
|
||||
)
|
||||
.set((
|
||||
documents::document_type_id.eq(Some(type_id)),
|
||||
documents::updated_at.eq(now),
|
||||
))
|
||||
.execute(&mut conn)?;
|
||||
document.document_type_id = Some(type_id);
|
||||
document.updated_at = now;
|
||||
}
|
||||
}
|
||||
|
||||
if document.deleted_at.is_some() {
|
||||
let now = Utc::now().naive_utc();
|
||||
diesel::update(documents::table.find(document.id))
|
||||
@@ -2336,7 +2386,7 @@ async fn process_upload(
|
||||
title: derived_title.clone(),
|
||||
metadata: metadata_value.clone(),
|
||||
tenant_id,
|
||||
document_type_id: None,
|
||||
document_type_id,
|
||||
};
|
||||
diesel::insert_into(documents::table)
|
||||
.values(&new_document)
|
||||
|
||||
Reference in New Issue
Block a user