-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_transaction.rs
More file actions
55 lines (48 loc) · 1.6 KB
/
Copy pathcreate_transaction.rs
File metadata and controls
55 lines (48 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use rust_ynab::Client;
use rust_ynab::NewTransaction;
use rust_ynab::PlanId;
use rust_ynab::milliunits_to_amount;
/// Creates a single transaction and prints the result.
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let token = std::env::var("YNAB_TOKEN")?;
let client = Client::new(&token)?;
let plan_id_str = std::env::var("YNAB_TEST_PLAN_ID")?;
let (plan, _) = client
.get_plan(PlanId::Id(plan_id_str.parse()?))
.send()
.await?;
let (accounts, _) = client.get_accounts(plan.id()).send().await?;
if accounts.is_empty() {
println!("no accounts found");
return Ok(());
}
let account_id = accounts.last().unwrap().id;
let tx = NewTransaction {
account_id,
date: chrono::Local::now().date_naive(),
amount: 1000,
payee_id: None,
payee_name: None,
category_id: None,
memo: Some("test transaction".to_string()),
cleared: Some(rust_ynab::ClearedStatus::Uncleared),
approved: Some(false),
flag_color: None,
import_id: None,
subtransactions: None,
};
let resp = client.create_transaction(plan.id(), tx).await?;
let tx = resp.transaction;
println!("Created Transaction\n");
println!(" {:<10} {}", "ID:", tx.id);
println!(" {:<10} {}", "Account:", tx.account_name);
println!(" {:<10} {}", "Date:", tx.date);
println!(
" {:<10} ${:.2}",
"Amount:",
milliunits_to_amount(tx.amount)
);
println!(" {:<10} {}", "Memo:", tx.memo.as_deref().unwrap_or(""));
Ok(())
}