1
use scripting::runtime::EvalValue;
2
use server::command::account::CreateAccount;
3
use server::command::commodity::CreateCommodity;
4
use server::command::{CmdResult, FinanceEntity};
5
use sqlx::PgPool;
6
use uuid::Uuid;
7

            
8
use forms::FormValues;
9

            
10
use super::{run_transaction_query, submit_form};
11

            
12
384
pub(super) async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
13
384
    let user_id = Uuid::new_v4();
14
384
    sqlx::query(
15
384
        "INSERT INTO users \
16
384
         (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
17
384
         VALUES ($1, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
18
384
    )
19
384
    .bind(user_id)
20
384
    .bind(format!("u{user_id}"))
21
384
    .bind(format!("{user_id}@test.com"))
22
384
    .execute(pool)
23
384
    .await?;
24
384
    Ok(user_id)
25
384
}
26

            
27
368
pub(super) async fn seed_commodity(user_id: Uuid, symbol: &str) -> anyhow::Result<Uuid> {
28
368
    match CreateCommodity::new()
29
368
        .symbol(symbol.to_string())
30
368
        .name(symbol.to_string())
31
368
        .user_id(user_id)
32
368
        .run()
33
368
        .await?
34
    {
35
368
        Some(CmdResult::String(id)) => Ok(Uuid::parse_str(&id)?),
36
        other => anyhow::bail!("unexpected CreateCommodity result: {other:?}"),
37
    }
38
368
}
39

            
40
442
pub(super) async fn seed_account(user_id: Uuid, name: &str) -> anyhow::Result<Uuid> {
41
442
    match CreateAccount::new()
42
442
        .name(name.to_string())
43
442
        .user_id(user_id)
44
442
        .run()
45
442
        .await?
46
    {
47
442
        Some(CmdResult::Entity(FinanceEntity::Account(a))) => Ok(a.id),
48
        other => anyhow::bail!("unexpected CreateAccount result: {other:?}"),
49
    }
50
442
}
51

            
52
41
pub(super) async fn seed_child_account(
53
41
    user_id: Uuid,
54
41
    name: &str,
55
41
    parent: Uuid,
56
41
) -> anyhow::Result<Uuid> {
57
41
    match CreateAccount::new()
58
41
        .name(name.to_string())
59
41
        .user_id(user_id)
60
41
        .parent(parent)
61
41
        .run()
62
41
        .await?
63
    {
64
41
        Some(CmdResult::Entity(FinanceEntity::Account(a))) => Ok(a.id),
65
        other => anyhow::bail!("unexpected CreateAccount result: {other:?}"),
66
    }
67
41
}
68

            
69
219
pub(super) async fn submit_text(user: Uuid, name: &str, payload: &str) -> wasmtime::Result<String> {
70
219
    match submit_form(user, name, payload).await? {
71
185
        EvalValue::String(text) => Ok(text),
72
        other => Err(wasmtime::Error::msg(format!(
73
            "expected a string, got {other:?}"
74
        ))),
75
    }
76
219
}
77

            
78
125
pub(super) async fn submit_create(user: Uuid, payload: &str) -> wasmtime::Result<String> {
79
125
    submit_text(user, "transaction-create", payload).await
80
125
}
81

            
82
71
pub(super) async fn submit_edit(user: Uuid, payload: &str) -> wasmtime::Result<String> {
83
71
    submit_text(user, "transaction-edit", payload).await
84
71
}
85

            
86
31
pub(super) async fn query_text(user: Uuid, values: &FormValues) -> wasmtime::Result<String> {
87
31
    let page = run_transaction_query(user, values).await?;
88
18
    Ok(nomiscript::format_value(&nomiscript::Value::from(page)))
89
31
}
90

            
91
4
pub(super) async fn seed_price(
92
4
    pool: &PgPool,
93
4
    from: Uuid,
94
4
    to: Uuid,
95
4
    (numer, denom): (i64, i64),
96
4
) -> anyhow::Result<()> {
97
4
    sqlx::query(
98
4
        "INSERT INTO prices (id, commodity_id, currency_id, price_date, value_num, value_denom) \
99
4
         VALUES ($1, $2, $3, NOW(), $4, $5)",
100
4
    )
101
4
    .bind(Uuid::new_v4())
102
4
    .bind(from)
103
4
    .bind(to)
104
4
    .bind(numer)
105
4
    .bind(denom)
106
4
    .execute(pool)
107
4
    .await?;
108
4
    Ok(())
109
4
}