1
use server::command::{
2
    CmdResult, FinanceEntity,
3
    account::{CreateAccount, ListAccounts},
4
    commodity::{CreateCommodity, ListCommodities},
5
};
6
use sqlx::types::Uuid;
7

            
8
272
pub async fn named_account(user_id: Uuid, name: &str) -> Uuid {
9
272
    let result = CreateAccount::new()
10
272
        .name(name.to_string())
11
272
        .user_id(user_id)
12
272
        .run()
13
272
        .await
14
272
        .expect("create account");
15
272
    match result {
16
272
        Some(CmdResult::Entity(FinanceEntity::Account(account))) => account.id,
17
        other => panic!("expected a created account, got {other:?}"),
18
    }
19
272
}
20

            
21
233
pub async fn named_commodity(user_id: Uuid, symbol: &str) -> Uuid {
22
233
    let result = CreateCommodity::new()
23
233
        .symbol(symbol.to_string())
24
233
        .name(symbol.to_string())
25
233
        .user_id(user_id)
26
233
        .run()
27
233
        .await
28
233
        .expect("create commodity");
29
233
    match result {
30
233
        Some(CmdResult::String(id)) => Uuid::parse_str(&id).expect("commodity id"),
31
        other => panic!("expected a created commodity id, got {other:?}"),
32
    }
33
233
}
34

            
35
8
pub async fn commodity_with_name(user_id: Uuid, symbol: &str, name: &str) -> Uuid {
36
8
    let result = CreateCommodity::new()
37
8
        .symbol(symbol.to_string())
38
8
        .name(name.to_string())
39
8
        .user_id(user_id)
40
8
        .run()
41
8
        .await
42
8
        .expect("create commodity");
43
8
    match result {
44
8
        Some(CmdResult::String(id)) => Uuid::parse_str(&id).expect("commodity id"),
45
        other => panic!("expected a created commodity id, got {other:?}"),
46
    }
47
8
}
48

            
49
441
fn tagged_id(result: Option<CmdResult>, tag: &str, wanted: &str) -> Option<Uuid> {
50
441
    let Some(CmdResult::TaggedEntities { entities, .. }) = result else {
51
        return None;
52
    };
53
682
    entities.into_iter().find_map(|(entity, tags)| {
54
682
        let named = matches!(tags.get(tag), Some(FinanceEntity::Tag(t)) if t.tag_value == wanted);
55
682
        match (named, entity) {
56
222
            (true, FinanceEntity::Account(a)) => Some(a.id),
57
219
            (true, FinanceEntity::Commodity(c)) => Some(c.id),
58
241
            _ => None,
59
        }
60
682
    })
61
441
}
62

            
63
/// The id of the user's account whose `name` tag is `name`.
64
222
pub async fn account_id(user_id: Uuid, name: &str) -> String {
65
222
    let listed = ListAccounts::new().user_id(user_id).run().await;
66
222
    match tagged_id(listed.expect("list accounts"), "name", name) {
67
222
        Some(id) => id.to_string(),
68
        None => panic!("no account named {name}"),
69
    }
70
222
}
71

            
72
/// The id of the user's commodity whose `symbol` tag is `symbol`.
73
219
pub async fn commodity_id(user_id: Uuid, symbol: &str) -> String {
74
219
    let listed = ListCommodities::new().user_id(user_id).run().await;
75
219
    match tagged_id(listed.expect("list commodities"), "symbol", symbol) {
76
219
        Some(id) => id.to_string(),
77
        None => panic!("no commodity {symbol}"),
78
    }
79
219
}