Lines
98.13 %
Functions
92 %
Branches
100 %
//! The edit page over the shared forms, served on its production paths.
use axum::{
Router,
body::Body,
http::Request,
routing::{get, post},
};
use forms::{FormValues, TRANSACTION_CREATE};
use sqlx::types::Uuid;
use web::model::User;
use web::pages::transaction::update::{
SUBMIT_PATH, transaction_update_page, transaction_update_submit,
use crate::common::controls;
use crate::common::create_mock_user;
use crate::common::new_page::{Reply, send_to};
use crate::common::seed::{commodity_id, named_account, named_commodity};
use crate::common::tenant::{provision_tenant, tenant_connection};
pub const POST_DATE: &str = "2026-10-27T09:15:42.123456Z";
/// A tenant with the accounts `Cash`, `Food` and the commodities `USD`, `EUR`.
pub struct Ledger {
pub user: User,
pub cash: Uuid,
pub food: Uuid,
pub usd: Uuid,
}
pub async fn ledger() -> Ledger {
let user = create_mock_user();
provision_tenant(user.id).await;
let cash = named_account(user.id, "Cash").await;
let food = named_account(user.id, "Food").await;
let usd = named_commodity(user.id, "USD").await;
named_commodity(user.id, "EUR").await;
Ledger {
user,
cash,
food,
usd,
async fn send(user: &User, request: Request<Body>) -> Reply {
let router = Router::new()
.route("/transaction/{id}/edit", get(transaction_update_page))
.route_layer(axum::middleware::from_fn(web::error_render::full_page))
.route(SUBMIT_PATH, post(transaction_update_submit));
send_to(router, user, request).await
pub async fn get_edit(user: &User, id: &str) -> Reply {
let request = Request::builder()
.uri(format!("/transaction/{id}/edit"))
.body(Body::empty())
.expect("request");
send(user, request).await
pub async fn post_edit(user: &User, pairs: &[(String, String)]) -> Reply {
let body = serde_urlencoded::to_string(pairs).expect("urlencoded");
.method("POST")
.uri(SUBMIT_PATH)
.header("content-type", "application/x-www-form-urlencoded")
.header("hx-request", "true")
.body(Body::from(body))
/// The rendered edit form, from its id to its end.
pub fn edit_form(html: &str) -> &str {
let start = html
.find("id=\"nf-transaction-edit\"")
.unwrap_or_else(|| panic!("no edit form in {html}"));
let form = &html[start..];
&form[..form.find("</form>").expect("form end")]
/// What a browser posts from the rendered edit form: every named, enabled
/// control outside `<template>`, line breaks normalised to CRLF.
pub fn browser_pairs(html: &str) -> Vec<(String, String)> {
controls::pairs(edit_form(html))
.into_iter()
.map(|(name, value)| (name, value.replace('\n', "\r\n")))
.collect()
/// `pairs` with the value of `name` replaced.
pub fn with_value(pairs: &[(String, String)], name: &str, value: &str) -> Vec<(String, String)> {
assert!(
pairs.iter().any(|(n, _)| n == name),
"no {name} in {pairs:?}"
);
pairs
.iter()
.map(|(n, v)| match n == name {
true => (n.clone(), value.to_string()),
false => (n.clone(), v.clone()),
})
/// The value posted under `name`.
pub fn posted<'a>(pairs: &'a [(String, String)], name: &str) -> &'a str {
.find(|(n, _)| n == name)
.map(|(_, v)| v.as_str())
.unwrap_or_else(|| panic!("no {name} in {pairs:?}"))
/// An FX transaction `Cash` → `Food`, 10 USD for 9 EUR, with a transaction
/// tag and a split tag.
pub async fn fx_transaction(ledger: &Ledger, note: &str) -> Uuid {
let [cash, food, usd] = [ledger.cash, ledger.food, ledger.usd].map(|id| id.to_string());
let eur = commodity_id(ledger.user.id, "EUR").await;
let pairs = [
("date", POST_DATE),
("note", note),
("tags", ""),
("tags.0.name", "trip"),
("tags.0.value", "japan"),
("tags.0.description", "spring"),
("splits", ""),
("splits.0.key", "n1"),
("splits.0.from", cash.as_str()),
("splits.0.to", food.as_str()),
("splits.0.from-commodity", usd.as_str()),
("splits.0.to-commodity", eur.as_str()),
("splits.0.value", "10"),
("splits.0.to-amount", "9"),
("splits.0.hidden", ""),
("split-tags", ""),
("split-tags.0.row", "n1"),
("split-tags.0.name", "memo"),
("split-tags.0.value", "m"),
("split-tags.0.description", "split memo"),
]
.map(|(n, v)| (n.to_string(), v.to_string()));
let values = FormValues::from_pairs(&TRANSACTION_CREATE, pairs).expect("pairs");
rpc::forms::submit(ledger.user.id, &TRANSACTION_CREATE, &values)
.await
.expect("create")
.id
/// Run `sql` with `$1` bound to `transaction` in the tenant of `user`.
pub async fn alter(user: &User, sql: &str, transaction: Uuid) {
let mut conn = tenant_connection(user.id).await;
sqlx::query(sqlx::AssertSqlSafe(sql.to_string()))
.bind(transaction)
.execute(&mut *conn)
.expect("alter");