1 Commits
Author SHA1 Message Date
grabowski 506221ecd0 Client link: read and sign the agreements first, then the details
CI / secret scan (push) Successful in 5s
CI / fmt, clippy, tests (push) Successful in 1m1s
CI / release (push) Skipped
While an active agreement is unsigned in its current version, the
link's other parts are locked: the overview shows them dashed with
"After signing" and no Send button, and every other page of the link
(About you, Contact, Passport, Address, Documents, Send) redirects to the
agreement and takes nothing posted to it. A new version locks them again
until it is signed.

Checks: fmt, clippy, 105 unit and 56 integration tests (tests/agreements.rs
asserts the redirects, that nothing posted before signing is saved, and
that the parts open after signing and close at a new version); the
locked overview checked on a phone in light and dark.
2026-11-20 20:40:26 +07:00
6 changed files with 120 additions and 17 deletions

No files matched your search

+2 -1
View File
@@ -43,7 +43,8 @@ Data Protection Act (PDPA) asks.
staff close it. staff close it.
- Agreements the client signs on the link: staff paste the lawyers' text - Agreements the client signs on the link: staff paste the lawyers' text
under Agreements and publish it as a version that never changes. Every under Agreements and publish it as a version that never changes. Every
client link asks for each active agreement first; the client ticks that client link asks for each active agreement first, and the other parts
open only once it is signed; the client ticks that
they accept, types their name and signs with a finger. The signed copy is they accept, types their name and signs with a finger. The signed copy is
a PDF with the exact text, the signature, the time and where it was a PDF with the exact text, the signature, the time and where it was
signed from, kept with the person's documents. A changed text is a new signed from, kept with the person's documents. A changed text is a new
+8 -5
View File
@@ -499,8 +499,12 @@ Planned on November 20, 2026, from Alex's answers (decisions table).
paragraphs, `# ` a heading, `- ` a list item) and publish it as paragraphs, `# ` a heading, `- ` a list item) and publish it as
version 1; a published text never changes (the table refuses it), a version 1; a published text never changes (the table refuses it), a
changed one is the next version. Every active agreement is the first changed one is the next version. Every active agreement is the first
step of every client link, in its current version: the client reads step of every client link, in its current version, and the rest of
it, ticks that they accept, types their name and signs with a finger. the link opens only once each is signed (Alex, Nov 20: read and sign
first, then enter the details); every other page of the link leads
to the agreement until then and takes nothing posted to it. The
client reads it, ticks that they accept, types their name and signs
with a finger.
The signed copy is a PDF (Sarabun, so Thai prints) with the text, the The signed copy is a PDF (Sarabun, so Thai prints) with the text, the
name, the drawing, the time, the link, the client's address and name, the drawing, the time, the link, the client's address and
browser and the text's SHA-256, kept as the person's encrypted browser and the text's SHA-256, kept as the person's encrypted
@@ -512,6 +516,5 @@ Planned on November 20, 2026, from Alex's answers (decisions table).
1. B4L's BOI application: once B4L is promoted, its staff extend their stay 1. B4L's BOI application: once B4L is promoted, its staff extend their stay
through the BOI's One Start One Stop center, which changes phase 6's pack. through the BOI's One Start One Stop center, which changes phase 6's pack.
2. The agreements' texts come from B4L's lawyers. Whether a client must 2. The agreements' texts come from B4L's lawyers. Whether a typed name and
sign before entering any data, and whether a typed name and a drawing are a drawing are enough for each kind of agreement is theirs to say.
enough for each kind of agreement, is theirs to say.
+44 -10
View File
@@ -202,6 +202,28 @@ macro_rules! open_or_return {
}; };
} }
/// The first active agreement the client has not signed in its current
/// version: the client reads and signs it before anything else.
async fn unsigned(st: &AppState, c: &Client) -> anyhow::Result<Option<i64>> {
Ok(agreements::for_person(&st.db, c.id())
.await?
.into_iter()
.find(|d| d.signed.is_none())
.map(|d| d.agreement.id))
}
/// Opens the link like `open_or_return!`, and sends a client who still has
/// an agreement to sign there first; what they posted is not taken.
macro_rules! signed_or_return {
($st:expr, $token:expr) => {{
let c = open_or_return!($st, $token);
if let Some(id) = unsigned(&$st, &c).await? {
return Ok(Redirect::to(&format!("{}/agree/{id}", c.base)).into_response());
}
c
}};
}
// ------------------------------------------------------------- overview // ------------------------------------------------------------- overview
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@@ -211,6 +233,8 @@ struct Part {
what: String, what: String,
done: bool, done: bool,
status: String, status: String,
/// Opens only once the agreements are signed.
locked: bool,
} }
fn part(slug: &str, title: &str, what: &str, done: bool, status: String) -> Part { fn part(slug: &str, title: &str, what: &str, done: bool, status: String) -> Part {
@@ -220,6 +244,7 @@ fn part(slug: &str, title: &str, what: &str, done: bool, status: String) -> Part
what: what.to_string(), what: what.to_string(),
done, done,
status, status,
locked: false,
} }
} }
@@ -262,6 +287,8 @@ async fn parts(st: &AppState, c: &Client) -> anyhow::Result<Vec<Part>> {
) )
}) })
.collect(); .collect();
let locked = out.iter().any(|p| !p.done);
let first = out.len();
out.extend([ out.extend([
part( part(
"about", "about",
@@ -304,6 +331,12 @@ async fn parts(st: &AppState, c: &Client) -> anyhow::Result<Vec<Part>> {
}, },
), ),
]); ]);
if locked {
for p in &mut out[first..] {
p.locked = true;
p.status = "After signing".into();
}
}
Ok(out) Ok(out)
} }
@@ -329,6 +362,7 @@ pub async fn overview(
.filter(|p| !p.done) .filter(|p| !p.done)
.map(|p| p.title.as_str()) .map(|p| p.title.as_str())
.collect(); .collect();
let locked = parts.iter().any(|p| p.locked);
let actor = c.link.actor(); let actor = c.link.actor();
audit::record( audit::record(
&st.db, &st.db,
@@ -341,7 +375,7 @@ pub async fn overview(
"client.html", "client.html",
"Your details", "Your details",
StatusCode::OK, StatusCode::OK,
context! { parts, saved, missing }, context! { parts, saved, missing, locked },
)) ))
} }
@@ -436,14 +470,14 @@ fn person_part(st: &AppState, c: &Client, part: &str, f: &PersonForm, errors: &E
} }
async fn person_get(st: Arc<AppState>, token: String, part: &str) -> Res { async fn person_get(st: Arc<AppState>, token: String, part: &str) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
viewed(&st, &c, Entity::Person, c.id()).await?; viewed(&st, &c, Entity::Person, c.id()).await?;
let f = PersonForm::from_person(&c.person); let f = PersonForm::from_person(&c.person);
Ok(person_part(&st, &c, part, &f, &Errors::new())) Ok(person_part(&st, &c, part, &f, &Errors::new()))
} }
async fn person_post(st: Arc<AppState>, token: String, part: &str, f: Fields) -> Res { async fn person_post(st: Arc<AppState>, token: String, part: &str, f: Fields) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
if !token_ok(&c, &f) { if !token_ok(&c, &f) {
return Ok(stale(&st)); return Ok(stale(&st));
} }
@@ -510,7 +544,7 @@ async fn shown_passport(st: &AppState, c: &Client) -> anyhow::Result<Option<peop
} }
pub async fn passport(State(st): State<Arc<AppState>>, Path(token): Path<String>) -> Res { pub async fn passport(State(st): State<Arc<AppState>>, Path(token): Path<String>) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
let pf = match shown_passport(&st, &c).await? { let pf = match shown_passport(&st, &c).await? {
Some(p) => { Some(p) => {
viewed(&st, &c, Entity::Passport, p.id).await?; viewed(&st, &c, Entity::Passport, p.id).await?;
@@ -538,7 +572,7 @@ pub async fn passport_save(
Path(token): Path<String>, Path(token): Path<String>,
Form(f): Form<Fields>, Form(f): Form<Fields>,
) -> Res { ) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
if !token_ok(&c, &f) { if !token_ok(&c, &f) {
return Ok(stale(&st)); return Ok(stale(&st));
} }
@@ -662,7 +696,7 @@ async fn current_address(st: &AppState, c: &Client) -> anyhow::Result<Option<peo
} }
pub async fn address(State(st): State<Arc<AppState>>, Path(token): Path<String>) -> Res { pub async fn address(State(st): State<Arc<AppState>>, Path(token): Path<String>) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
let shown = match made_address(&st, &c).await? { let shown = match made_address(&st, &c).await? {
Some(a) => Some(a), Some(a) => Some(a),
None => current_address(&st, &c).await?, None => current_address(&st, &c).await?,
@@ -689,7 +723,7 @@ pub async fn address_save(
Path(token): Path<String>, Path(token): Path<String>,
Form(f): Form<Fields>, Form(f): Form<Fields>,
) -> Res { ) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
if !token_ok(&c, &f) { if !token_ok(&c, &f) {
return Ok(stale(&st)); return Ok(stale(&st));
} }
@@ -811,7 +845,7 @@ async fn documents_page(
} }
pub async fn documents_get(State(st): State<Arc<AppState>>, Path(token): Path<String>) -> Res { pub async fn documents_get(State(st): State<Arc<AppState>>, Path(token): Path<String>) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
let actor = c.link.actor(); let actor = c.link.actor();
audit::record( audit::record(
&st.db, &st.db,
@@ -882,7 +916,7 @@ pub async fn documents_upload(
Path(token): Path<String>, Path(token): Path<String>,
mp: Multipart, mp: Multipart,
) -> Res { ) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
let user = c.user(); let user = c.user();
let up = match webdocs::read_upload(&user, mp).await { let up = match webdocs::read_upload(&user, mp).await {
Ok(Ok(up)) => up, Ok(Ok(up)) => up,
@@ -931,7 +965,7 @@ pub async fn send(
Path(token): Path<String>, Path(token): Path<String>,
Form(f): Form<Fields>, Form(f): Form<Fields>,
) -> Res { ) -> Res {
let c = open_or_return!(st, token); let c = signed_or_return!(st, token);
if !token_ok(&c, &f) { if !token_ok(&c, &f) {
return Ok(stale(&st)); return Ok(stale(&st));
} }
+8
View File
@@ -1592,3 +1592,11 @@ label.check input {
font-size: 13px; font-size: 13px;
} }
} }
/* a part of the client link that opens once the agreements are signed */
.part.locked {
opacity: 0.72;
border-style: dashed;
}
.part.locked:hover {
border-color: var(--line);
}
+16 -1
View File
@@ -1,23 +1,38 @@
{% extends "client_base.html" %} {% extends "client_base.html" %}
{% block main %} {% block main %}
<h1 class="chead">{% if first_name %}Hello {{ first_name }}{% else %}Hello{% endif %}</h1> <h1 class="chead">{% if first_name %}Hello {{ first_name }}{% else %}Hello{% endif %}</h1>
<p class="lead2">B4L looks after your visa and your stay in Thailand. Fill in the parts below. Each part is saved on its own, so you can stop and come back to this link. When everything is in, press Send.</p> <p class="lead2">B4L looks after your visa and your stay in Thailand. {% if locked %}First read and sign how B4L works with your details; then the other parts open.{% else %}Fill in the parts below.{% endif %} Each part is saved on its own, so you can stop and come back to this link. When everything is in, press Send.</p>
{% if saved %}<div class="notice ok" role="status">Saved: {{ saved }}.</div>{% endif %} {% if saved %}<div class="notice ok" role="status">Saved: {{ saved }}.</div>{% endif %}
<ol class="parts"> <ol class="parts">
{% for p in parts %} {% for p in parts %}
<li> <li>
{% if p.locked %}
<div class="part locked" id="part-{{ p.slug }}" aria-disabled="true">
<span class="pmark" aria-hidden="true">{{ loop.index }}</span>
<span class="pbody"><b>{{ p.title }}</b><span class="muted small">{{ p.what }}</span></span>
<span class="chip plain">{{ p.status }}</span>
</div>
{% else %}
<a class="part{% if p.done %} done{% endif %}" href="{{ base }}/{{ p.slug }}" id="part-{{ p.slug|replace("/", "-") }}"> <a class="part{% if p.done %} done{% endif %}" href="{{ base }}/{{ p.slug }}" id="part-{{ p.slug|replace("/", "-") }}">
<span class="pmark" aria-hidden="true">{% if p.done %}&#10003;{% else %}{{ loop.index }}{% endif %}</span> <span class="pmark" aria-hidden="true">{% if p.done %}&#10003;{% else %}{{ loop.index }}{% endif %}</span>
<span class="pbody"><b>{{ p.title }}</b><span class="muted small">{{ p.what }}</span></span> <span class="pbody"><b>{{ p.title }}</b><span class="muted small">{{ p.what }}</span></span>
<span class="chip {% if p.done %}ok{% else %}warn{% endif %}">{{ p.status }}</span> <span class="chip {% if p.done %}ok{% else %}warn{% endif %}">{{ p.status }}</span>
</a> </a>
{% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ol> </ol>
{% if locked %}
<div class="card sendcard" id="send">
<h2>Send</h2>
<p class="muted">Sign the agreement first; then fill in the rest and send it.</p>
</div>
{% else %}
<form method="post" action="{{ base }}/send" class="card sendcard" id="send"{% if missing %} data-confirm="Still to do: {{ missing|join(", ") }}. Send anyway? The link closes once it is sent."{% else %} data-confirm="Send your details to B4L? The link closes once it is sent."{% endif %}> <form method="post" action="{{ base }}/send" class="card sendcard" id="send"{% if missing %} data-confirm="Still to do: {{ missing|join(", ") }}. Send anyway? The link closes once it is sent."{% else %} data-confirm="Send your details to B4L? The link closes once it is sent."{% endif %}>
<input type="hidden" name="csrf" value="{{ csrf }}"> <input type="hidden" name="csrf" value="{{ csrf }}">
<h2>Send</h2> <h2>Send</h2>
<p class="muted">{% if missing %}Still to do: {{ missing|join(", ") }}. You can send now and give the rest to B4L later.{% else %}Everything is in. Send it, and B4L checks it.{% endif %} After Send the link closes.</p> <p class="muted">{% if missing %}Still to do: {{ missing|join(", ") }}. You can send now and give the rest to B4L later.{% else %}Everything is in. Send it, and B4L checks it.{% endif %} After Send the link closes.</p>
<button type="submit" class="btn pri" id="send-btn">Send to B4L</button> <button type="submit" class="btn pri" id="send-btn">Send to B4L</button>
</form> </form>
{% endif %}
{% endblock %} {% endblock %}
+42
View File
@@ -187,6 +187,39 @@ async fn the_client_signs_and_signs_again_after_a_change(pool: PgPool) {
assert!(t.contains("value=\"Hans Becker\""), "{t}"); assert!(t.contains("value=\"Hans Becker\""), "{t}");
assert!(t.contains("/static/signature.js")); assert!(t.contains("/static/signature.js"));
// nothing else opens before the agreement is signed, and nothing is
// taken: every part leads to it, Send included
assert!(o.contains("First read and sign"), "{o}");
assert!(
o.contains("After signing") && !o.contains("send-btn"),
"{o}"
);
for part in ["about", "contact", "passport", "address", "documents"] {
let r = app.get(&format!("{base}/{part}")).await;
assert_eq!(r.status, StatusCode::SEE_OTHER, "{part}");
assert_eq!(r.location(), uri, "{part}");
}
let r = client_post(
&app,
&format!("{base}/contact"),
&csrf,
&[("phone", "123")],
&[],
)
.await;
assert_eq!(r.location(), uri);
let r = client_post(&app, &format!("{base}/send"), &csrf, &[], &[]).await;
assert_eq!(r.location(), uri);
let (phone, open): (String, bool) = sqlx::query_as(
"SELECT p.phone, l.closed_at IS NULL FROM people p JOIN client_links l ON l.person_id = p.id \
WHERE p.id = $1",
)
.bind(person)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!((phone.as_str(), open), ("", true));
// without the box ticked, or with a broken drawing // without the box ticked, or with a broken drawing
let r = client_post( let r = client_post(
&app, &app,
@@ -270,6 +303,13 @@ async fn the_client_signs_and_signs_again_after_a_change(pool: PgPool) {
assert_eq!(file.status, StatusCode::OK); assert_eq!(file.status, StatusCode::OK);
assert!(file.bytes.starts_with(b"%PDF-")); assert!(file.bytes.starts_with(b"%PDF-"));
// signed: the other parts open
assert_eq!(
app.get(&format!("{base}/about")).await.status,
StatusCode::OK
);
assert!(app.get(&base).await.text().contains("send-btn"));
// signed: the page says so, a second post changes nothing // signed: the page says so, a second post changes nothing
assert!( assert!(
app.get(&uri) app.get(&uri)
@@ -347,6 +387,8 @@ async fn the_client_signs_and_signs_again_after_a_change(pool: PgPool) {
.await; .await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY); assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
assert!(r.text().contains("changed the text while you read it")); assert!(r.text().contains("changed the text while you read it"));
// and the parts close again until it is signed
assert_eq!(app.get(&format!("{base}/about")).await.location(), uri);
// switched off: no longer asked for // switched off: no longer asked for
app.post( app.post(