aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: c3fa324ea1dae0bd156808d0cd757d9a34b8af94 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
fn client(key: &str) -> ynab_api::apis::client::APIClient {
    let mut ynab_config = ynab_api::apis::configuration::Configuration::new();
    ynab_config.api_key = Some(ynab_api::apis::configuration::ApiKey {
        prefix: Some("Bearer".to_string()),
        key: key.to_string(),
    });
    ynab_api::apis::client::APIClient::new(ynab_config)
}

fn format_amount(amount: i64) -> String {
    let dollars = amount.abs() / 1000;
    let cents = (amount.abs() % 1000) / 10;
    let sign = if amount < 0 { "-" } else { "" };
    format!("{}{}.{:02}", sign, dollars, cents)
}

fn main() {
    let key = std::env::args().nth(1).unwrap();
    let client = client(&key);

    let budgets = client.budgets_api().get_budgets().unwrap().data.budgets;
    let budget = budgets.iter().next().unwrap();
    println!("budget is {} ({})", budget.name, budget.id);

    let reimbursables_id = client
        .categories_api()
        .get_categories(&budget.id, 0)
        .unwrap()
        .data
        .category_groups
        .iter()
        .map(|group| {
            group
                .categories
                .iter()
                .map(|c| (c.id.clone(), c.name.clone()))
        })
        .flat_map(|cs| cs)
        .find(|(_, name)| name == "Reimbursables")
        .map(|(id, _)| id)
        .unwrap();
    println!("found reimbursables category: {}", reimbursables_id);

    let full_budget = client
        .budgets_api()
        .get_budget_by_id(&budget.id, 0)
        .unwrap()
        .data
        .budget;
    println!("got full budget");

    let transactions = full_budget.transactions.unwrap();
    let payees = full_budget.payees.unwrap();

    for t in transactions {
        if let Some(category_id) = t.category_id {
            if category_id != reimbursables_id {
                continue;
            }
        } else {
            continue;
        }

        if t.flag_color.is_some() {
            continue;
        }

        let payee = t
            .payee_id
            .map(|id| {
                payees.iter().find(|p| p.id == id).unwrap().name.clone()
            })
            .unwrap_or_else(|| "(none)".to_string());
        println!("{} | {} | {}", t.date, payee, format_amount(t.amount))
    }
}