1
//! The W0a pins of the web transaction edit, flipped in W6: the edit page over
2
//! the shared forms keeps an unchanged FX row's price date and refuses a
3
//! stale load.
4

            
5
use axum::http::StatusCode;
6
use chrono::{DateTime, TimeZone, Utc};
7
use sqlx::types::Uuid;
8
use web::model::User;
9

            
10
use crate::common::edit_page::{
11
    browser_pairs, edit_form, fx_transaction, get_edit, ledger, post_edit, with_value,
12
};
13
use crate::common::tenant::tenant_connection;
14

            
15
2
async fn price_dates(user_id: Uuid) -> Vec<DateTime<Utc>> {
16
2
    let mut conn = tenant_connection(user_id).await;
17
2
    sqlx::query_scalar("SELECT price_date FROM prices ORDER BY price_date")
18
2
        .fetch_all(&mut *conn)
19
2
        .await
20
2
        .expect("prices")
21
2
}
22

            
23
2
async fn notes(user_id: Uuid) -> Vec<String> {
24
2
    let mut conn = tenant_connection(user_id).await;
25
2
    sqlx::query_scalar(
26
2
        "SELECT t.tag_value FROM tags t JOIN transaction_tags tt ON tt.tag_id = t.id \
27
2
         WHERE t.tag_name = 'note' ORDER BY t.tag_value",
28
2
    )
29
2
    .fetch_all(&mut *conn)
30
2
    .await
31
2
    .expect("notes")
32
2
}
33

            
34
4
async fn edit_pairs(user: &User, id: Uuid, note: &str) -> Vec<(String, String)> {
35
4
    let page = get_edit(user, &id.to_string()).await;
36
4
    assert_eq!(page.status, StatusCode::OK, "{}", page.body);
37
4
    with_value(&browser_pairs(&page.body), "note", note)
38
4
}
39

            
40
/// Flipped in W6: a note-only edit of an FX transaction keeps its price date.
41
#[supp_macro::shared_runtime_test]
42
1
async fn note_only_fx_edit_keeps_the_price_date() {
43
1
    let ledger = ledger().await;
44
1
    let id = fx_transaction(&ledger, "first").await;
45
1
    let seeded = Utc
46
1
        .timestamp_opt(1_583_065_815, 654_321_000)
47
1
        .single()
48
1
        .expect("instant");
49
1
    let mut conn = tenant_connection(ledger.user.id).await;
50
1
    sqlx::query("UPDATE prices SET price_date = $1")
51
1
        .bind(seeded)
52
1
        .execute(&mut *conn)
53
1
        .await
54
1
        .expect("seed price date");
55
1
    drop(conn);
56

            
57
1
    let pairs = edit_pairs(&ledger.user, id, "edited").await;
58
1
    let reply = post_edit(&ledger.user, &pairs).await;
59
1
    let notes = notes(ledger.user.id).await;
60
1
    let dates = price_dates(ledger.user.id).await;
61

            
62
1
    assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
63
1
    assert_eq!(notes, vec!["edited"]);
64
1
    assert_eq!(dates, vec![seeded]);
65
}
66

            
67
/// Flipped in W6: of two pages loaded from the same revision, the second
68
/// submit is refused with the reload banner and stores nothing.
69
#[supp_macro::shared_runtime_test]
70
1
async fn concurrent_edit_is_refused_on_revision() {
71
1
    let ledger = ledger().await;
72
1
    let id = fx_transaction(&ledger, "first").await;
73
1
    let first = edit_pairs(&ledger.user, id, "from the first tab").await;
74
1
    let second = edit_pairs(&ledger.user, id, "from the second tab").await;
75

            
76
1
    let accepted = post_edit(&ledger.user, &first).await;
77
1
    let refused = post_edit(&ledger.user, &second).await;
78
1
    let notes = notes(ledger.user.id).await;
79

            
80
1
    assert_eq!(accepted.status, StatusCode::OK, "{}", accepted.body);
81
1
    assert_eq!(
82
1
        refused.status,
83
1
        StatusCode::UNPROCESSABLE_ENTITY,
84
        "{}",
85
        refused.body
86
    );
87
1
    let form = edit_form(&refused.body);
88
1
    assert!(
89
1
        form.contains(&format!(
90
1
            "class=\"form-reload\" href=\"/transaction/{id}/edit\""
91
1
        )),
92
        "{form}"
93
    );
94
1
    assert_eq!(notes, vec!["from the first tab"]);
95
}
96

            
97
/// A rate added after the transaction survives a note-only edit and stays the
98
/// latest.
99
#[supp_macro::shared_runtime_test]
100
1
async fn a_newer_unrelated_rate_survives_a_note_only_edit() {
101
1
    let ledger = ledger().await;
102
1
    let id = fx_transaction(&ledger, "first").await;
103
1
    let newer = Utc.with_ymd_and_hms(2030, 1, 1, 0, 0, 0).unwrap();
104
1
    let mut conn = tenant_connection(ledger.user.id).await;
105
    let (commodity, currency): (Uuid, Uuid) =
106
1
        sqlx::query_as("SELECT commodity_id, currency_id FROM prices")
107
1
            .fetch_one(&mut *conn)
108
1
            .await
109
1
            .expect("transaction price");
110
1
    sqlx::query(
111
1
        "INSERT INTO prices (id, commodity_id, currency_id, price_date, value_num, value_denom) \
112
1
         VALUES ($1, $2, $3, $4, 7, 1)",
113
1
    )
114
1
    .bind(Uuid::new_v4())
115
1
    .bind(commodity)
116
1
    .bind(currency)
117
1
    .bind(newer)
118
1
    .execute(&mut *conn)
119
1
    .await
120
1
    .expect("newer rate");
121
1
    drop(conn);
122

            
123
1
    let pairs = edit_pairs(&ledger.user, id, "edited").await;
124
1
    let reply = post_edit(&ledger.user, &pairs).await;
125
1
    let dates = price_dates(ledger.user.id).await;
126
1
    let mut conn = tenant_connection(ledger.user.id).await;
127
1
    let latest: i64 = sqlx::query_scalar("SELECT value_num FROM prices ORDER BY price_date DESC")
128
1
        .fetch_one(&mut *conn)
129
1
        .await
130
1
        .expect("latest rate");
131
1
    drop(conn);
132

            
133
1
    assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
134
1
    assert_eq!(dates.len(), 2, "{dates:?}");
135
1
    assert_eq!(dates.last(), Some(&newer));
136
1
    assert_eq!(latest, 7);
137
}