receipts: Attach receipt to Firefly transaction
If a specific transaction is selected on the Add Receipt form, the uploaded receipt image will now be attached to that transaction in Firefly.bugfix/ci-buildah
parent
5c7225f077
commit
0c088e6fc8
|
@ -31,6 +31,7 @@ pub struct ReceiptJson {
|
||||||
|
|
||||||
#[derive(rocket::FromForm)]
|
#[derive(rocket::FromForm)]
|
||||||
pub struct ReceiptPostForm<'r> {
|
pub struct ReceiptPostForm<'r> {
|
||||||
|
pub transaction: Option<String>,
|
||||||
pub date: String,
|
pub date: String,
|
||||||
pub vendor: String,
|
pub vendor: String,
|
||||||
pub amount: String,
|
pub amount: String,
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use rocket::form::Form;
|
use rocket::form::Form;
|
||||||
use rocket::http::{ContentType, MediaType, Status};
|
use rocket::http::{ContentType, MediaType, Status};
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
use rocket::Route;
|
use rocket::{Route, State};
|
||||||
use rocket_db_pools::Connection as DatabaseConnection;
|
use rocket_db_pools::Connection as DatabaseConnection;
|
||||||
use rocket_dyn_templates::{context, Template};
|
use rocket_dyn_templates::{context, Template};
|
||||||
use tracing::{error, info};
|
use tracing::{error, info};
|
||||||
|
|
||||||
use crate::receipts::*;
|
use crate::receipts::*;
|
||||||
use crate::Database;
|
use crate::{Context, Database};
|
||||||
|
|
||||||
#[rocket::get("/")]
|
#[rocket::get("/")]
|
||||||
pub async fn list_receipts(
|
pub async fn list_receipts(
|
||||||
|
@ -45,6 +45,7 @@ pub async fn receipt_form() -> Template {
|
||||||
pub async fn add_receipt(
|
pub async fn add_receipt(
|
||||||
form: Form<ReceiptPostForm<'_>>,
|
form: Form<ReceiptPostForm<'_>>,
|
||||||
db: DatabaseConnection<Database>,
|
db: DatabaseConnection<Database>,
|
||||||
|
ctx: &State<Context>,
|
||||||
) -> (Status, Json<AddReceiptResponse>) {
|
) -> (Status, Json<AddReceiptResponse>) {
|
||||||
let data = match ReceiptPostData::from_form(&form).await {
|
let data = match ReceiptPostData::from_form(&form).await {
|
||||||
Ok(d) => d,
|
Ok(d) => d,
|
||||||
|
@ -56,20 +57,51 @@ pub async fn add_receipt(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let mut repo = ReceiptsRepository::new(db);
|
let mut repo = ReceiptsRepository::new(db);
|
||||||
match repo.add_receipt(&data).await {
|
let receipt = match repo.add_receipt(&data).await {
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
info!("Created new receipt {}", r.id);
|
info!("Created new receipt {}", r.id);
|
||||||
(Status::Ok, Json(AddReceiptResponse::Success(r)))
|
r
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failed to insert new receipt record: {}", e);
|
error!("Failed to insert new receipt record: {}", e);
|
||||||
(
|
return (
|
||||||
Status::InternalServerError,
|
Status::InternalServerError,
|
||||||
Json(AddReceiptResponse::Error(e.to_string())),
|
Json(AddReceiptResponse::Error(e.to_string())),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if let Some(id) = &form.transaction {
|
||||||
|
match ctx.firefly.get_transaction(id).await {
|
||||||
|
Ok(t) => {
|
||||||
|
if let Some(j) = t.data.attributes.transactions.first() {
|
||||||
|
if let Err(e) = ctx
|
||||||
|
.firefly
|
||||||
|
.attach_receipt(
|
||||||
|
&j.transaction_journal_id,
|
||||||
|
data.photo,
|
||||||
|
data.filename,
|
||||||
)
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
error!(
|
||||||
|
"Failed to attach receipt to Firefly transaction {}: {}",
|
||||||
|
id, e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error!(
|
||||||
|
"Could not attach receipt to Firefly transaction {}: no splits",
|
||||||
|
id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
error!("Could not load Firefly transaction {}: {}", id, e);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
(Status::Ok, Json(AddReceiptResponse::Success(receipt)))
|
||||||
|
}
|
||||||
|
|
||||||
#[rocket::get("/<id>")]
|
#[rocket::get("/<id>")]
|
||||||
pub async fn get_receipt(
|
pub async fn get_receipt(
|
||||||
|
|
Loading…
Reference in New Issue