Lines
68.57 %
Functions
60.87 %
Branches
100 %
use server::command::transaction::{
CreateTransaction, DeleteTransaction, GetTransaction, GetTransactionTag, ListTransactions,
SetTransactionTag, UpdateTransaction,
};
use server::command::{CmdResult, FinanceEntity};
use uuid::Uuid;
use wasmtime::{Caller, Rooted, StructRef};
use super::parse::{
parse_account_filter_arg, parse_create_transaction_payload, parse_transaction_id_arg,
parse_update_transaction_payload,
use super::render::{
TransactionEntry, alloc_transaction_entity, list_transaction_entries, render_full_transaction,
use crate::forms::{self, Failure, user_zone};
use crate::host_failure::cmd_failure;
use crate::natives::args::{args_error, present, required, tag_name_arg, uuid_arg};
use crate::natives::forms::native_failure;
use crate::session::SessionData;
/// `set-transaction-tag` upsert (idempotent). Mirrors `set-split-tag`
/// and `set-account-tag` — the wasm-side native returns `t` on success
/// (ADR-0033 Phase 5); this host fn's own return stays `i32` (1), which
/// the compiler's `bool` token maps onto the same wire ABI.
pub(super) async fn run_set_transaction_tag(
user_id: Uuid,
id_arg: Option<String>,
name_arg: Option<String>,
value_arg: Option<String>,
) -> wasmtime::Result<i32> {
let transaction_id = uuid_arg("set-transaction-tag", "transaction-id", id_arg)?;
let tag_name = tag_name_arg("set-transaction-tag", name_arg)?;
let tag_value = present("set-transaction-tag", "tag-value", value_arg)?;
SetTransactionTag::new()
.user_id(user_id)
.transaction_id(transaction_id)
.tag_name(tag_name)
.tag_value(tag_value)
.run()
.await
.map(|_| 1)
.map_err(|err| cmd_failure("set-transaction-tag", err))
}
/// `get-transaction-tag` lookup. Empty-string return when the tag is unset —
/// same shape as `get-split-tag`; a missing transaction fails as not-found.
pub(super) async fn run_get_transaction_tag(
) -> wasmtime::Result<String> {
let transaction_id = uuid_arg("get-transaction-tag", "transaction-id", id_arg)?;
let tag_name = tag_name_arg("get-transaction-tag", name_arg)?;
match GetTransactionTag::new()
{
Ok(Some(CmdResult::String(s))) => Ok(s),
Ok(Some(other)) => Err(wasmtime::Error::msg(format!(
"get-transaction-tag: expected String, got {other:?}"
))),
Ok(None) => Ok(String::new()),
Err(err) => Err(cmd_failure("get-transaction-tag", err)),
/// Companion to create-transaction. Payload mirrors the create shape but
/// `:transaction-id` is required (the row being updated) and every other
/// field is optional — caller only supplies what they want changed.
/// Splits when present REPLACE the existing set; a replacement with a stored
/// split's `:id` keeps the reconcile state, reconcile date and lot it omits.
pub(super) async fn run_update_transaction(
payload_arg: Option<String>,
let payload = required("update-transaction", "payload", payload_arg)?;
let input = parse_update_transaction_payload(&payload)
.map_err(|err| args_error(format!("update-transaction: {err}")))?;
let mut runner = UpdateTransaction::new()
.transaction_id(input.transaction_id);
if let Some(post_date) = input.post_date {
runner = runner.post_date(post_date);
if let Some(enter_date) = input.enter_date {
runner = runner.enter_date(enter_date);
if let Some(note) = input.note {
runner = runner.note(note);
if let Some(splits) = input.splits {
let keep_stored = splits.iter().map(|(s, keep)| (s.id, *keep)).collect();
let entities: Vec<FinanceEntity> = splits
.into_iter()
.map(|(mut s, _)| {
s.tx_id = input.transaction_id;
FinanceEntity::Split(s)
})
.collect();
runner = runner.splits(entities).keep_stored(keep_stored);
if !input.prices.is_empty() {
let price_entities: Vec<FinanceEntity> =
input.prices.into_iter().map(FinanceEntity::Price).collect();
runner = runner.prices(price_entities);
match runner.run().await {
Ok(Some(CmdResult::Entity(FinanceEntity::Transaction(tx)))) => Ok(tx.id.to_string()),
"update-transaction: expected Transaction entity, got {other:?}"
Ok(None) => Err(wasmtime::Error::msg(
"update-transaction: command returned no entity",
)),
Err(err) => Err(cmd_failure("update-transaction", err)),
pub(super) async fn run_create_transaction(
let payload = required("create-transaction", "payload", payload_arg)?;
let input = parse_create_transaction_payload(&payload)
.map_err(|err| args_error(format!("create-transaction: {err}")))?;
let splits: Vec<FinanceEntity> = input
.splits
.map(|mut s| {
s.tx_id = input.id;
let mut runner = CreateTransaction::new()
.id(input.id)
.post_date(input.post_date)
.enter_date(input.enter_date)
.splits(splits);
"create-transaction: expected Transaction entity, got {other:?}"
"create-transaction: command returned no entity",
Err(err) => Err(cmd_failure("create-transaction", err)),
/// Removes the transaction by UUID along with its splits, prices, and
/// owned tags. Not idempotent — server returns an error when the row
/// isn't there. Returns `t` on success.
pub(super) async fn run_delete_transaction(
let transaction_id = uuid_arg("delete-transaction", "transaction-id", id_arg)?;
DeleteTransaction::new()
.map_err(|err| cmd_failure("delete-transaction", err))
pub(super) async fn run_get_transaction(
caller: &mut Caller<'_, SessionData>,
) -> wasmtime::Result<Option<Rooted<StructRef>>> {
let transaction_id = parse_transaction_id_arg(id_arg)?;
let result = GetTransaction::new()
.await;
let entries = list_transaction_entries("get-transaction", result)?;
match entries.into_iter().next() {
Some((id, note, amount, post_date)) => Ok(Some(
alloc_transaction_entity(
caller,
&id,
note.as_deref(),
amount.as_deref(),
Some(&post_date),
)
.await?,
None => Ok(None),
/// `get-transaction-detail` — returns the full transaction as a string
/// plist (`:id :post-date :enter-date :note :splits :prices`) so a client
/// can reconstruct + edit it. Empty `:prices ()` when single-currency.
pub(super) async fn run_get_transaction_detail(
) -> wasmtime::Result<Option<String>> {
match forms::detail(user_id, transaction_id).await {
Ok(detail) => {
let zone = user_zone(user_id).await.map_err(native_failure)?;
render_full_transaction("get-transaction-detail", &detail, zone).map(Some)
Err(Failure::Missing(_)) => Ok(None),
Err(other) => Err(native_failure(other)),
pub(super) async fn run_list_transactions(
account_arg: Option<String>,
) -> wasmtime::Result<Vec<TransactionEntry>> {
let mut cmd = ListTransactions::new().user_id(user_id);
if let Some(account_id) = parse_account_filter_arg(account_arg)? {
cmd = cmd.account(account_id);
let result = cmd.run().await;
list_transaction_entries("list-transactions", result)