Skip to main content

schwab_cli/commands/
transactions.rs

1use anyhow::Result;
2use serde_json::json;
3
4use crate::cli::TransactionsCommands;
5use crate::config::RuntimeConfig;
6use crate::human;
7use crate::output::ResponseEnvelope;
8
9pub async fn run(runtime: &RuntimeConfig, command: TransactionsCommands) -> Result<()> {
10    let api = runtime.build_api()?;
11
12    match command {
13        TransactionsCommands::List {
14            mut account_number,
15            start_date,
16            end_date,
17            types,
18            symbol,
19        } => {
20            if runtime.is_interactive() && account_number.is_empty() {
21                account_number = human::pick_account_hash(runtime, &api).await?;
22            }
23            let data = api
24                .transactions()
25                .list(
26                    &account_number,
27                    start_date.as_deref(),
28                    end_date.as_deref(),
29                    types.as_deref(),
30                    symbol.as_deref(),
31                )
32                .await?;
33            runtime.emit(ResponseEnvelope::ok("transactions list", json!(data)));
34        }
35        TransactionsCommands::Get {
36            account_number,
37            transaction_id,
38        } => {
39            let data = api
40                .transactions()
41                .get(&account_number, &transaction_id)
42                .await?;
43            runtime.emit(ResponseEnvelope::ok("transactions get", json!(data)));
44        }
45    }
46
47    Ok(())
48}