summaryrefslogtreecommitdiff
path: root/transformers/actualbudget_transformer/actualbudget_queries/tests/common/payee_matchers.rs
blob: ccba2044ac3d60fce0ef9c60f5a8ef134b7f96da (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::collections::HashMap;

use actualbudget_models::{ActualbudgetAccount, ActualbudgetPayee};
use anyhow::Context;

use super::{account_names::ACCOUNT_NAMES_DICT, payee_names::PAYEE_NAMES_DICT};

pub fn get_payee_matchers(
) -> Vec<Box<dyn Fn(&ActualbudgetPayee, &HashMap<String, ActualbudgetAccount>) -> bool>> {
    vec![
        Box::new(|p, a| matches_bank_account_transfer_account(p, a)),
        Box::new(|p, a| matches_cash_account_transfer_account(p, a)),
        Box::new(|p, a| matches_category_transfers_transfer_account(p, a)),
        Box::new(|p, a| matches_starting_balances(p, a)),
        Box::new(|p, a| matches_employer(p, a)),
        Box::new(|p, a| matches_tesco(p, a)),
    ]
}

fn matches_bank_account_transfer_account(
    payee: &ActualbudgetPayee,
    accounts: &HashMap<String, ActualbudgetAccount>,
) -> bool {
    let bank_account = accounts
        .get(ACCOUNT_NAMES_DICT.bank_account)
        .with_context(|| "failed to get bank account")
        .unwrap();

    match payee {
        ActualbudgetPayee {
            name,
            transfer_account_id,
            ..
        } => name == ""
            && transfer_account_id.clone().is_some_and(
                |account_id| account_id == bank_account.id,
            ),
    }
}

fn matches_cash_account_transfer_account(
    payee: &ActualbudgetPayee,
    accounts: &HashMap<String, ActualbudgetAccount>,
) -> bool {
    let cash_account = accounts
        .get(ACCOUNT_NAMES_DICT.cash_account)
        .with_context(|| "failed to find cash account")
        .unwrap();

    match payee {
        ActualbudgetPayee {
            name,
            transfer_account_id,
            ..
        } => name == ""
            && transfer_account_id.clone().is_some_and(
                |account_id| account_id == cash_account.id,
            ),
    }
}

fn matches_category_transfers_transfer_account(
    payee: &ActualbudgetPayee,
    accounts: &HashMap<String, ActualbudgetAccount>,
) -> bool {
    let category_transfers_account = accounts
        .get(ACCOUNT_NAMES_DICT.transfers_account)
        .with_context(|| "failed to find category transfers account")
        .unwrap();

    match payee {
        ActualbudgetPayee {
            name,
            transfer_account_id,
            ..
        } => name == ""
            && transfer_account_id.clone().is_some_and(
                |account_id| account_id == category_transfers_account.id,
            ),
    }
}

fn matches_starting_balances(
    payee: &ActualbudgetPayee,
    _accounts: &HashMap<String, ActualbudgetAccount>,
) -> bool {
    match payee {
        ActualbudgetPayee {
            name,
            transfer_account_id,
            ..
        } => transfer_account_id.is_none()
            && name.clone() == PAYEE_NAMES_DICT.starting_balances,
    }
}

fn matches_employer(
    payee: &ActualbudgetPayee,
    _accounts: &HashMap<String, ActualbudgetAccount>,
) -> bool {
    match payee {
        ActualbudgetPayee {
            name,
            transfer_account_id,
            ..
        } => transfer_account_id.is_none()
            && name == PAYEE_NAMES_DICT.employer,
    }
}


fn matches_tesco(
    payee: &ActualbudgetPayee,
    _accounts: &HashMap<String, ActualbudgetAccount>,
) -> bool {
    match payee {
        ActualbudgetPayee {
            name,
            transfer_account_id,
            ..
        } => transfer_account_id.is_none()
            && name == PAYEE_NAMES_DICT.tesco,
    }
}