One shared draft a contract, locked when all agree (phase 13, step 2)
Until a contract is set, it is one open text for every side, as Alex asked: our team edits it in the app, each other party on its link. Edits are tracked changes in the author's color (crossed out, added, highlighted with a comment) until the other side accepts or rejects them. When no change or highlight is open and Lanna Desk and every party agree, the text becomes a contract version that never changes, and the draft locks. - One side edits at a time: Edit takes the draft for 45 seconds, which every save renews; Done editing, leaving the page or silence gives it up. The editor saves 0.7 s after the last keystroke; the other pages ask for news every 3 s, so each side sees edits, who edits, agreements and the lock without reloading. - A save may only touch the saver's own marks (reviews::check_save); accept, reject (or withdraw) and resolve are their own steps, journaled with the text concerned. A later change voids the agreements given. - Notices: the other side's edits (one a half hour of editing), settled changes, agreements, the lock. The bell in the top bar counts the unseen (every 15 s); Notices lists them and marks them seen. - The draft as a diff: the standing text against every open change taken. - Migration 0015: contract_drafts, draft_saves and draft_agreements (both kept unchanged by a trigger), notices, notice_reads; drops step 1's review_rounds (only the demo held any). The review links stay as each party's way in. - Property tests for the save rule and settling, the fuzz text target runs the parser, the save check and settling; integration tests play both sides to the lock. The demo's recording agreement has an open draft with the label's changes and a notice. - PLAN: phase 13 rewritten; step 3 is the PDF and signing.
This commit is contained in:
1 parent
a4155548b8
commit
3dd9ab60e8
36 files changed
+3812
-2161
No files matched your search
+353
@@ -0,0 +1,353 @@
|
||||
//! The shared draft (phase 13): opened from a version, edited with tracked
|
||||
//! changes by our team and a party on its link, one editor at a time;
|
||||
//! changes accepted, agreement voided by a later change, and the text
|
||||
//! locked as a version once every side agrees. The other side's work
|
||||
//! shows on the bell.
|
||||
|
||||
use axum::http::StatusCode;
|
||||
use serde_json::Value;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::common::{App, local};
|
||||
|
||||
const SAME: &[(&str, &str)] = &[("sec-fetch-site", "same-origin")];
|
||||
|
||||
async fn performance(pool: &PgPool) -> (i64, i64, String) {
|
||||
sqlx::query_as(
|
||||
"SELECT c.id, p.id, v.body FROM contracts c \
|
||||
JOIN contract_parties p ON p.contract_id = c.id AND p.removed_at IS NULL \
|
||||
JOIN contract_versions v ON v.contract_id = c.id \
|
||||
WHERE c.title LIKE 'Performance agreement%' AND v.body <> '' \
|
||||
ORDER BY v.version DESC LIMIT 1",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn json(text: &str) -> Value {
|
||||
serde_json::from_str(text).unwrap_or_else(|e| panic!("{e}: {text}"))
|
||||
}
|
||||
|
||||
/// The JSON in the editor's data block.
|
||||
fn data_of(page: &str) -> Value {
|
||||
let at = page.find("id=\"dr-data\">").expect("the editor's data") + 13;
|
||||
let end = at + page[at..].find("</script>").unwrap();
|
||||
json(&page[at..end])
|
||||
}
|
||||
|
||||
struct Party<'a> {
|
||||
app: &'a App,
|
||||
path: String,
|
||||
csrf: String,
|
||||
}
|
||||
|
||||
impl Party<'_> {
|
||||
async fn act(&self, action: &str, fields: &[(&str, &str)]) -> (StatusCode, Value) {
|
||||
let mut pairs = vec![("csrf", self.csrf.as_str())];
|
||||
pairs.extend_from_slice(fields);
|
||||
let r = self
|
||||
.app
|
||||
.post_raw(&format!("{}/{action}", self.path), &pairs, SAME)
|
||||
.await;
|
||||
(r.status, json(&r.text()))
|
||||
}
|
||||
}
|
||||
|
||||
async fn staff(app: &App, c: i64, action: &str, fields: &[(&str, &str)]) -> (StatusCode, Value) {
|
||||
let r = app
|
||||
.post(&format!("/contracts/{c}/draft/{action}"), fields)
|
||||
.await;
|
||||
(r.status, json(&r.text()))
|
||||
}
|
||||
|
||||
fn rev(v: &Value) -> String {
|
||||
v["state"]["rev"]
|
||||
.as_i64()
|
||||
.or(v["rev"].as_i64())
|
||||
.unwrap()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn both_sides_edit_one_draft_until_all_agree_and_it_locks(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let today = visa::dates::today();
|
||||
visa::seed::demo(&pool, today).await.unwrap();
|
||||
visa::seed::legal::demo(&pool, None, today).await.unwrap();
|
||||
let (c, party, body) = performance(&pool).await;
|
||||
let versions_before: i64 =
|
||||
sqlx::query_scalar("SELECT count(*) FROM contract_versions WHERE contract_id = $1")
|
||||
.bind(c)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// the demo's own notice is seen already
|
||||
app.get("/notices").await;
|
||||
|
||||
// the contract page offers the draft; it opens from version 1
|
||||
let t = app.get(&format!("/contracts/{c}")).await.text();
|
||||
assert!(t.contains("id=\"draft-open-form\""), "{t}");
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/contracts/{c}/draft/open"),
|
||||
&[("draft_version", "1")],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.location(), format!("/contracts/{c}/draft"));
|
||||
let page = app.get(&format!("/contracts/{c}/draft")).await.text();
|
||||
let state = &data_of(&page)["state"];
|
||||
assert_eq!(state["state"], "open");
|
||||
assert_eq!(state["need"], 2, "Lanna Desk and the festival");
|
||||
|
||||
// the festival's link
|
||||
let made = app
|
||||
.post(
|
||||
&format!("/contracts/{c}/invites"),
|
||||
&[("party", &party.to_string())],
|
||||
)
|
||||
.await
|
||||
.text();
|
||||
let at = made.find("/r/").expect("the link is shown");
|
||||
let token: String = made[at + 3..]
|
||||
.chars()
|
||||
.take_while(|ch| ch.is_ascii_alphanumeric() || *ch == '-' || *ch == '_')
|
||||
.collect();
|
||||
let path = format!("/r/{token}");
|
||||
let r = app.get(&path).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.text());
|
||||
let page = r.text();
|
||||
assert!(!page.contains("/desk"), "nothing of the back office");
|
||||
let data = data_of(&page);
|
||||
let them = Party {
|
||||
app: &app,
|
||||
path: path.clone(),
|
||||
csrf: data["csrf"].as_str().unwrap().to_string(),
|
||||
};
|
||||
let mut now = data["state"]["rev"].as_i64().unwrap().to_string();
|
||||
|
||||
// one editor at a time
|
||||
let (s, _) = staff(&app, c, "take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK);
|
||||
let (s, v) = them.act("take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
assert!(v["error"].as_str().unwrap().contains("is editing"), "{v}");
|
||||
staff(&app, c, "release", &[("rev", &now)]).await;
|
||||
let (s, _) = them.act("take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK);
|
||||
|
||||
// the festival's tracked changes: the fee, and a highlight
|
||||
let side = format!("party:{party}");
|
||||
let (head, rest) = body.split_once("45,000").unwrap();
|
||||
let (mid, tail) = rest.split_once("flight from Berlin").unwrap();
|
||||
let pieces = serde_json::json!([
|
||||
["same", head, "", ""],
|
||||
["del", "45,000", "", side],
|
||||
["ins", "60,000", "", side],
|
||||
["same", mid, "", ""],
|
||||
[
|
||||
"mark",
|
||||
"flight from Berlin",
|
||||
"Business class, please.",
|
||||
side
|
||||
],
|
||||
["same", tail, "", ""]
|
||||
])
|
||||
.to_string();
|
||||
// a change to the text as it stands is refused
|
||||
let sneaky =
|
||||
serde_json::json!([["same", body.replace("45,000", "90,000"), "", ""]]).to_string();
|
||||
let (s, _) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &sneaky)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
// marks made in our name too
|
||||
let forged = pieces.replace(&side, "us");
|
||||
let (s, _) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &forged)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let (s, v) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &pieces)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
now = rev(&v);
|
||||
// we cannot save while they edit
|
||||
let (s, _) = staff(&app, c, "save", &[("rev", &now), ("pieces", &pieces)]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
them.act("release", &[("rev", &now)]).await;
|
||||
|
||||
// the bell rings for us, and the list marks it seen
|
||||
let n = json(&app.get("/notices/count").await.text());
|
||||
assert_eq!(n["unread"], 1, "{n}");
|
||||
let t = app.get("/notices").await.text();
|
||||
assert!(t.contains("changed the draft of"), "{t}");
|
||||
assert_eq!(json(&app.get("/notices/count").await.text())["unread"], 0);
|
||||
|
||||
// our view has their change; we take it in
|
||||
let st = json(&app.get(&format!("/contracts/{c}/draft/state")).await.text());
|
||||
assert_eq!(st["rev"].as_i64().unwrap().to_string(), now);
|
||||
let (s, v) = staff(&app, c, "accept", &[("rev", &now), ("n", "1")]).await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
now = rev(&v);
|
||||
// they cannot accept their own; nothing is left to accept anyway
|
||||
let (s, _) = them.act("accept", &[("rev", &now), ("n", "1")]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
|
||||
// an open highlight blocks an agreement; resolved, we agree
|
||||
let (s, _) = staff(&app, c, "agree", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let (_, v) = staff(&app, c, "resolve", &[("rev", &now), ("k", "1")]).await;
|
||||
now = rev(&v);
|
||||
// we agree; their next edit voids it
|
||||
let (s, v) = staff(&app, c, "agree", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
assert_eq!(v["state"]["have"], 1);
|
||||
them.act("take", &[("rev", &now)]).await;
|
||||
let added = format!(
|
||||
"{},[\"ins\",\"\\nSound check at 18:00.\",\"\",\"{side}\"]]",
|
||||
&pieces[..pieces.len() - 1]
|
||||
);
|
||||
let accepted = added
|
||||
.replace(&format!("[\"del\",\"45,000\",\"\",\"{side}\"],"), "")
|
||||
.replace(
|
||||
&format!("[\"ins\",\"60,000\",\"\",\"{side}\"]"),
|
||||
"[\"same\",\"60,000\",\"\",\"\"]",
|
||||
);
|
||||
let (s, v) = them
|
||||
.act("save", &[("rev", &now), ("pieces", &accepted)])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
now = rev(&v);
|
||||
assert_eq!(v["state"]["have"], 0, "a change asks everyone again");
|
||||
them.act("release", &[("rev", &now)]).await;
|
||||
// open changes block an agreement
|
||||
let (s, _) = them.act("agree", &[("rev", &now), ("name", "Ann")]).await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let (_, v) = staff(&app, c, "reject", &[("rev", &now), ("n", "1")]).await;
|
||||
now = rev(&v);
|
||||
let (_, v) = them.act("resolve", &[("rev", &now), ("k", "1")]).await;
|
||||
now = rev(&v);
|
||||
|
||||
// both agree: the text locks as a new version
|
||||
let (s, _) = staff(&app, c, "agree", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::OK);
|
||||
let (s, _) = them.act("agree", &[("rev", &now), ("name", "")]).await;
|
||||
assert_eq!(s, StatusCode::UNPROCESSABLE_ENTITY, "a name goes with it");
|
||||
let (s, v) = them
|
||||
.act("agree", &[("rev", &now), ("name", "Ann Festival")])
|
||||
.await;
|
||||
assert_eq!(s, StatusCode::OK, "{v}");
|
||||
assert_eq!(v["state"]["state"], "locked");
|
||||
let (count, text): (i64, String) = sqlx::query_as(
|
||||
"SELECT (SELECT count(*) FROM contract_versions WHERE contract_id = $1), \
|
||||
(SELECT body FROM contract_versions WHERE contract_id = $1 ORDER BY version DESC LIMIT 1)",
|
||||
)
|
||||
.bind(c)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(count, versions_before + 1);
|
||||
assert!(text.contains("60,000") && !text.contains("45,000") && !text.contains("Sound check"));
|
||||
let events: Vec<String> = sqlx::query_scalar(
|
||||
"SELECT event FROM journal WHERE contract_id = $1 AND seq > 0 ORDER BY seq",
|
||||
)
|
||||
.bind(c)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
for e in [
|
||||
"draft opened",
|
||||
"draft link made",
|
||||
"change accepted",
|
||||
"change rejected",
|
||||
"highlight resolved",
|
||||
"text agreed",
|
||||
"draft locked",
|
||||
] {
|
||||
assert!(events.iter().any(|x| x == e), "{e} missing in {events:?}");
|
||||
}
|
||||
assert!(visa::journal::verify(&pool).await.unwrap().whole());
|
||||
let t = app.get(&format!("/contracts/{c}")).await.text();
|
||||
assert!(t.contains("Every side agreed") && t.contains("Start a new draft"));
|
||||
|
||||
// locked is locked
|
||||
let (s, _) = staff(&app, c, "take", &[("rev", &now)]).await;
|
||||
assert_eq!(s, StatusCode::CONFLICT);
|
||||
assert!(
|
||||
sqlx::query("UPDATE contract_drafts SET pieces = '[]' WHERE contract_id = $1")
|
||||
.bind(c)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
sqlx::query("DELETE FROM draft_saves")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
sqlx::query("DELETE FROM draft_agreements")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
|
||||
// the link's form token is its own, and a closed link ends
|
||||
let r = app
|
||||
.post_raw(
|
||||
&format!("{path}/take"),
|
||||
&[("csrf", &"x".repeat(43)), ("rev", &now)],
|
||||
SAME,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::FORBIDDEN);
|
||||
let link: i64 =
|
||||
sqlx::query_scalar("SELECT id FROM review_links WHERE party_id = $1 AND closed_at IS NULL")
|
||||
.bind(party)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
app.post(&format!("/contracts/{c}/review-links/{link}/close"), &[])
|
||||
.await;
|
||||
assert_eq!(app.get(&path).await.status, StatusCode::GONE);
|
||||
assert_eq!(
|
||||
app.get(&format!("/r/{}", "a".repeat(43))).await.status,
|
||||
StatusCode::NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn the_demo_draft_shows_the_labels_changes_as_a_diff(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let today = visa::dates::today();
|
||||
visa::seed::demo(&pool, today).await.unwrap();
|
||||
visa::seed::legal::demo(&pool, None, today).await.unwrap();
|
||||
let c: i64 = sqlx::query_scalar("SELECT contract_id FROM contract_drafts LIMIT 1")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let st = json(&app.get(&format!("/contracts/{c}/draft/state")).await.text());
|
||||
let pieces = st["pieces"].as_array().unwrap();
|
||||
let changes = pieces
|
||||
.windows(2)
|
||||
.filter(|w| {
|
||||
let k = |p: &Value| p[0].as_str().unwrap().to_string();
|
||||
(k(&w[1]) == "ins" || k(&w[1]) == "del") && !(k(&w[0]) == "ins" || k(&w[0]) == "del")
|
||||
})
|
||||
.count();
|
||||
assert_eq!(changes, 4, "{st}");
|
||||
let t = app.get(&format!("/contracts/{c}/draft/diff")).await.text();
|
||||
assert!(
|
||||
t.contains("class=\"gitdiff\"") && t.contains("id=\"change-4\""),
|
||||
"{t}"
|
||||
);
|
||||
let t = app.get(&format!("/contracts/{c}")).await.text();
|
||||
assert!(
|
||||
t.contains("4 open changes") && t.contains("0 of 2 sides agree"),
|
||||
"{t}"
|
||||
);
|
||||
assert_eq!(json(&app.get("/notices/count").await.text())["unread"], 1);
|
||||
}
|
||||
Reference in new issue
Block a user