1
//! The list page, its lazy detail and delete, served on their production
2
//! paths.
3

            
4
use axum::{
5
    Router,
6
    body::Body,
7
    http::{Method, Request},
8
    routing::{get, post},
9
};
10
use sqlx::types::Uuid;
11
use tower::ServiceExt;
12
use web::model::User;
13
use web::pages::transaction::browse::{LIST_PATH, transaction_detail, transaction_list_page};
14
use web::pages::transaction::delete::submit::transaction_delete_submit;
15

            
16
use super::new_page::{Reply, created_id, post_form};
17
use super::seed::{account_id, commodity_id};
18
use super::{authenticated, create_test_app_state};
19

            
20
29
async fn send(user: &User, method: Method, uri: &str) -> Reply {
21
29
    let router = Router::new()
22
29
        .route(LIST_PATH, get(transaction_list_page))
23
29
        .route("/api/transaction/detail/{id}", get(transaction_detail))
24
29
        .route(
25
29
            "/api/transaction/delete/{id}",
26
29
            post(transaction_delete_submit),
27
29
        );
28
29
    let request = Request::builder()
29
29
        .method(method)
30
29
        .uri(uri)
31
29
        .body(Body::empty())
32
29
        .expect("request");
33
29
    let response = authenticated(router, create_test_app_state().await, user.clone())
34
29
        .oneshot(request)
35
29
        .await
36
29
        .expect("response");
37
29
    let (parts, body) = response.into_parts();
38
29
    let bytes = axum::body::to_bytes(body, usize::MAX).await.expect("body");
39
29
    Reply {
40
29
        status: parts.status,
41
29
        headers: parts.headers,
42
29
        body: String::from_utf8_lossy(&bytes).into_owned(),
43
29
    }
44
29
}
45

            
46
21
pub async fn list(user: &User, query: &str) -> Reply {
47
21
    send(user, Method::GET, &format!("{LIST_PATH}?{query}")).await
48
21
}
49

            
50
6
pub async fn detail(user: &User, id: &str) -> Reply {
51
6
    send(user, Method::GET, &format!("/api/transaction/detail/{id}")).await
52
6
}
53

            
54
2
pub async fn delete(user: &User, id: &str) -> Reply {
55
2
    send(user, Method::POST, &format!("/api/transaction/delete/{id}")).await
56
2
}
57

            
58
/// Create `Assets:Checking` → `to` for `value` USD at `date`.
59
22
pub async fn add(user: &User, date: &str, note: &str, to: &str, value: &str) -> Uuid {
60
    let (checking, to) = (
61
22
        account_id(user.id, "Assets:Checking").await,
62
22
        account_id(user.id, to).await,
63
    );
64
22
    let usd = commodity_id(user.id, "USD").await;
65
22
    let pairs = [
66
22
        ("date", date),
67
22
        ("note", note),
68
22
        ("tags", ""),
69
22
        ("splits", ""),
70
22
        ("splits.0.key", "n1"),
71
22
        ("splits.0.from", checking.as_str()),
72
22
        ("splits.0.to", to.as_str()),
73
22
        ("splits.0.from-commodity", usd.as_str()),
74
22
        ("splits.0.value", value),
75
22
        ("splits.0.hidden", ""),
76
22
        ("split-tags", ""),
77
22
        ("split-tags.0.row", "n1"),
78
22
        ("split-tags.0.name", "memo"),
79
22
        ("split-tags.0.value", "lunch"),
80
22
    ];
81
22
    created_id(&post_form(user, &pairs).await)
82
22
}
83

            
84
/// The note cells of the listed rows, in order of appearance.
85
11
pub fn listed_notes(body: &str) -> Vec<&str> {
86
11
    body.split("<summary>")
87
11
        .skip(1)
88
13
        .filter_map(|tail| tail.split_once('>')?.1.split('<').next())
89
        .collect()
90
11
}