Lines
96.67 %
Functions
67.86 %
Branches
100 %
//! A stored transaction's splits with their complete tag lists.
use std::collections::HashMap;
use finance::price::Price;
use finance::split::Split;
use finance::tag::Tag;
use finance::transaction::Transaction;
use server::logical::{is_note, pick_note};
use server::user::User;
use uuid::Uuid;
use super::{Entity, Failure};
use crate::host_failure::cmd_failure;
use crate::resolve::Directory;
use crate::resolve::options::DirectorySource;
/// One stored split with its account path, commodity symbol and all its
/// tags, sorted by name then value.
#[derive(Debug)]
pub struct SplitDetail {
pub split: Split,
pub account: String,
pub commodity: String,
pub tags: Vec<Tag>,
}
/// One stored split with all its tags, sorted by name then value.
pub struct StoredSplit {
fn labels(directory: &Directory, source: DirectorySource) -> HashMap<Uuid, String> {
directory
.options(source)
.into_iter()
.map(|option| (option.id, option.label))
.collect()
/// A stored transaction as one complete read: header, prices, its own tags
/// (the `note` tag excluded, it is `note`) and the splits with their tags.
pub struct TransactionDetail {
pub transaction: Transaction,
pub note: Option<String>,
pub prices: Vec<Price>,
pub splits: Vec<StoredSplit>,
fn sorted(mut tags: Vec<Tag>) -> Vec<Tag> {
tags.sort_by(|a, b| (&a.tag_name, &a.tag_value).cmp(&(&b.tag_name, &b.tag_value)));
tags
/// The user's transaction as one snapshot, without any labels; `Missing`
/// when it does not exist.
pub async fn detail(user_id: Uuid, transaction_id: Uuid) -> Result<TransactionDetail, Failure> {
let snapshot = User { id: user_id }
.transaction_snapshot(transaction_id)
.await
.map_err(|err| cmd_failure("transaction-detail", err.into()))?
.ok_or(Failure::Missing(Entity::Transaction))?;
let note = pick_note(snapshot.tags.iter()).map(|tag| tag.normalized().1.to_string());
let mut split_tags = snapshot.split_tags;
let tx_tags = sorted(
snapshot
.tags
.filter(|tag| !is_note(tag))
.collect(),
);
let splits = snapshot
.splits
.map(|split| StoredSplit {
tags: sorted(split_tags.remove(&split.id).unwrap_or_default()),
split,
})
.collect();
Ok(TransactionDetail {
transaction: snapshot.transaction,
note,
prices: snapshot.prices,
tags: tx_tags,
splits,
/// The splits of the transaction with account and commodity labels, for the
/// web row fragment.
pub async fn labelled_splits(
user_id: Uuid,
transaction_id: Uuid,
) -> Result<Vec<SplitDetail>, Failure> {
let stored = detail(user_id, transaction_id).await?;
let directory = Directory::load("transaction-detail", user_id).await?;
let accounts = labels(&directory, DirectorySource::Accounts);
let commodities = labels(&directory, DirectorySource::Commodities);
Ok(stored
.map(|stored| SplitDetail {
account: accounts
.get(&stored.split.account_id)
.cloned()
.unwrap_or_else(|| format!("Account {}", stored.split.account_id)),
commodity: commodities
.get(&stored.split.commodity_id)
.unwrap_or_default(),
tags: stored.tags,
split: stored.split,
.collect())