46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use rocket::serde::json::Json;
|
|
use rocket::Route;
|
|
use rocket::State;
|
|
use tracing::error;
|
|
|
|
use crate::transactions::*;
|
|
use crate::{Context, ErrorResponse};
|
|
|
|
#[rocket::get("/", format = "json")]
|
|
async fn transaction_list(
|
|
ctx: &State<Context>,
|
|
) -> Result<Json<Vec<Transaction>>, ErrorResponse> {
|
|
let result = ctx
|
|
.firefly
|
|
.search_transactions(&ctx.config.firefly.search_query)
|
|
.await
|
|
.map_err(|e| {
|
|
error!("Error fetching transaction list: {}", e);
|
|
ErrorResponse::new(
|
|
"Failed to fetch transaction list from Firefly III",
|
|
)
|
|
})?;
|
|
|
|
let restaurant_tag = Some(&ctx.config.firefly.restaurant_tag);
|
|
|
|
Ok(Json(
|
|
result
|
|
.data
|
|
.into_iter()
|
|
.filter_map(|t| {
|
|
match Transaction::from_firefly(t, restaurant_tag) {
|
|
Ok(t) => Some(t),
|
|
Err(e) => {
|
|
error!("Error parsing transaction details: {}", e);
|
|
None
|
|
},
|
|
}
|
|
})
|
|
.collect(),
|
|
))
|
|
}
|
|
|
|
pub fn routes() -> Vec<Route> {
|
|
rocket::routes![transaction_list]
|
|
}
|