Skip to main content

cli_core/transaction/
from_template.rs

1//! `transaction create --template`: the template's draft with the given values over it.
2
3use forms::{FormValues, TRANSACTION_CREATE, now_instant};
4use rpc::Session;
5
6use super::{apply_form, eval_text, submit_call};
7use crate::CommandError;
8use crate::flat::load_values;
9use crate::forms::map_references;
10
11/// Create a transaction from the draft of template `template_id`; `overrides`
12/// replace its date and note and merge into its rows, and the date is now
13/// when neither gives one.
14///
15/// # Errors
16/// The server's refusal of the template load, [`CommandError::Argument`] for
17/// an unknown or ambiguous reference, or [`CommandError::FormInvalid`] from the submit.
18pub async fn create_from_template(
19    session: &mut Session,
20    template_id: &str,
21    overrides: &FormValues,
22) -> Result<String, CommandError> {
23    let loaded = load_values(session, &TRANSACTION_CREATE, template_id).await?;
24    let overrides = map_references(session, &TRANSACTION_CREATE, overrides).await?;
25    let mut merged = apply_form(&TRANSACTION_CREATE, &loaded, &overrides, &[])?;
26    if merged.text("date").is_none() {
27        merged.set("date", &now_instant());
28    }
29    eval_text(session, &submit_call(&TRANSACTION_CREATE, &merged)).await
30}