Lines
100 %
Functions
Branches
//! `load-form "transaction-edit"`: the stored transaction as submit payload
//! values with hidden row data.
use chrono::SecondsFormat;
use finance::price::Price;
use finance::tag::Tag;
use forms::{FormValues, Row};
use server::logical::RaisedRow;
use uuid::Uuid;
use super::hidden::Hidden;
use super::transaction_rows::to_commodity_cell;
use super::transaction_split_tags::split_tag_rows;
use super::transaction_stored::{Stored, load_stored};
use super::{Entity, Failure};
use crate::resolve::ResolvedIds;
pub(super) async fn load_transaction_edit(user_id: Uuid, id: &str) -> Result<FormValues, Failure> {
let transaction_id = Uuid::parse_str(id.trim()).map_err(|_| Failure::MalformedId {
entity: Entity::Transaction,
id: id.to_string(),
})?;
let stored = load_stored(user_id, transaction_id)
.await?
.ok_or(Failure::Missing(Entity::Transaction))?;
Ok(edit_values(&stored))
}
/// The commodity price of the row's split pair carries its observation date.
fn price_date(prices: &[Price], raised: &RaisedRow) -> Option<chrono::DateTime<chrono::Utc>> {
prices
.iter()
.find(|price| {
price.currency_split == Some(raised.from_split)
&& price.commodity_split == Some(raised.to_split)
})
.map(|price| price.date)
fn hidden_of(raised: &RaisedRow, prices: &[Price]) -> Hidden {
Hidden {
ids: ResolvedIds {
from: raised.from_account,
to: raised.to_account,
from_commodity: raised.from_commodity,
to_commodity: raised.to_commodity,
},
value: raised.value,
to_amount: raised.to_amount,
price_date: price_date(prices, raised),
reconcile: [raised.from_reconcile, raised.to_reconcile],
fn row_values(key: &str, raised: &RaisedRow, prices: &[Price]) -> Row {
let hidden = hidden_of(raised, prices);
let mut row = Row::from([
("key".to_string(), key.to_string()),
("from".to_string(), raised.from_account.to_string()),
("to".to_string(), raised.to_account.to_string()),
(
"from-commodity".to_string(),
raised.from_commodity.to_string(),
),
"to-commodity".to_string(),
to_commodity_cell(&raised.from_commodity, &raised.to_commodity),
"value".to_string(),
amount::display(*hidden.value.numer(), *hidden.value.denom()),
("hidden".to_string(), hidden.to_sexpr()),
]);
if let Some(to_amount) = hidden.to_amount {
row.insert(
"to-amount".to_string(),
amount::display(*to_amount.numer(), *to_amount.denom()),
);
row
fn tag_row(tag: &Tag) -> Row {
("name".to_string(), tag.normalized().0.to_string()),
("value".to_string(), tag.normalized().1.to_string()),
if let Some(description) = &tag.description {
row.insert("description".to_string(), description.clone());
pub(super) fn edit_values(stored: &Stored) -> FormValues {
let transaction = &stored.detail.transaction;
let mut values = FormValues::default();
values.set("id", &transaction.id.to_string());
values.set("revision", &stored.revision);
values.set(
"date",
&transaction
.post_date
.to_rfc3339_opts(SecondsFormat::AutoSi, true),
values.set("note", stored.detail.note.as_deref().unwrap_or_default());
values.set_rows("tags");
for tag in &stored.tags {
values.push_row("tags", tag_row(tag));
values.set_rows("split-tags");
for (index, (raised, tags)) in stored.rows.iter().enumerate() {
let key = format!("r{}", index + 1);
let row = row_values(&key, raised, &stored.detail.prices);
values.push_row("splits", row);
for row in split_tag_rows(&key, tags, tag_row) {
values.push_row("split-tags", row);
values