Companies become records of their own, and the stay in Thailand is recorded in full; from these records the app works out each person's deadlines (docs/PLAN.md, phase 2). - Companies: list, page, form, archive; Thai name, the 13-digit registration number with its check digit verified, address and the director who signs. The employer is chosen from them on the person form and on work permits; a company's page adds people to it; the people list and the Deadlines page filter by employer. Migration 0002 turns the free-text employers into companies. - Stay records, each one `Kind` in src/records.rs with one form, one set of routes and the same audit entries: arrivals and departures with the TDAC, extensions of stay (approved, under consideration, refused), 90-day reports with the receipt's next date, visas, re-entry permits and work permits. Passports, visas and companies are chosen from the person's own records; the server checks the choice. - The deadline engine (src/deadlines.rs, a pure function with a unit test per rule): the 90-day report from day 1 of each arrival, reset by a report or an extension, with the in-person and online windows and the in-person rule after a new passport or an extension; the extension window from 30 days before to the last working day; the decision to collect; overstay with its fine; TM.30 after each arrival; passport and work permit expiry; re-entry permits; visas still to be used. - The Deadlines page at /, the person's status and deadlines on the Overview and the new "Visa and stay" tab, the person's Activity tab, and the next deadline in the people list. - The online 90-day window ends 7 days before the due date (the plan said 8; the Immigration Bureau's TM.47 manual says 7). - 69 tests: 45 unit tests and 24 integration tests.
263 lines
9.0 KiB
Rust
263 lines
9.0 KiB
Rust
//! The records of a stay through their forms: every kind is added, edited
|
|
//! and deleted, with the audit log; references stay within the person.
|
|
|
|
use axum::http::StatusCode;
|
|
use sqlx::PgPool;
|
|
|
|
use crate::common::{id_from, local};
|
|
|
|
async fn last_created(pool: &PgPool, person: i64) -> (i64, String) {
|
|
sqlx::query_as(
|
|
"SELECT entity_id, entity FROM audit_log WHERE person_id = $1 AND action = 'create' \
|
|
ORDER BY id DESC LIMIT 1",
|
|
)
|
|
.bind(person)
|
|
.fetch_one(pool)
|
|
.await
|
|
.unwrap()
|
|
}
|
|
|
|
/// A kind's slug and audit entity, the fields of a new record, and a field
|
|
/// to change: its name, the value typed and the value shown.
|
|
type Case<'a> = (
|
|
&'a str,
|
|
&'a str,
|
|
Vec<(&'a str, &'a str)>,
|
|
(&'a str, &'a str, &'a str),
|
|
);
|
|
|
|
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
|
async fn every_kind_is_added_edited_and_deleted(pool: PgPool) {
|
|
let app = local(pool.clone());
|
|
let r = app
|
|
.post("/companies", &[("name", "Test Works Co., Ltd.")])
|
|
.await;
|
|
let company = r.location().trim_start_matches("/companies/").to_string();
|
|
let r = app
|
|
.post("/people", &[("surname", "Doe"), ("first_name", "Jane")])
|
|
.await;
|
|
let id = id_from(r.location());
|
|
|
|
// (slug, entity, the fields of a new record, a field to change)
|
|
let cases: Vec<Case> = vec![
|
|
(
|
|
"visas",
|
|
"visa",
|
|
vec![("category", "non-b"), ("valid_until", "2027-01-01")],
|
|
("number", "v 123", "V123"),
|
|
),
|
|
(
|
|
"arrivals",
|
|
"arrival",
|
|
vec![
|
|
("arrived_on", "2026-09-01"),
|
|
("admission", "exempt"),
|
|
("permitted_until", "2026-10-30"),
|
|
],
|
|
("tdac_no", "tdac 77", "TDAC77"),
|
|
),
|
|
(
|
|
"extensions",
|
|
"extension",
|
|
vec![
|
|
("applied_on", "2026-10-20"),
|
|
("basis", "tourism"),
|
|
("decision", "approved"),
|
|
("permitted_until", "2026-11-29"),
|
|
],
|
|
("fee_thb", "1,900", "1,900 THB"),
|
|
),
|
|
(
|
|
"reports",
|
|
"report",
|
|
vec![("reported_on", "2026-09-10"), ("method", "online")],
|
|
("next_due_on", "2026-12-09", "Dec 9, 2026"),
|
|
),
|
|
(
|
|
"reentry-permits",
|
|
"reentry-permit",
|
|
vec![
|
|
("kind", "single"),
|
|
("issued_on", "2026-09-10"),
|
|
("valid_until", "2026-10-30"),
|
|
],
|
|
(
|
|
"office",
|
|
"Chiang Mai Immigration Office",
|
|
"Chiang Mai Immigration Office",
|
|
),
|
|
),
|
|
(
|
|
"work-permits",
|
|
"work-permit",
|
|
vec![
|
|
("company_id", company.as_str()),
|
|
("valid_until", "2027-09-01"),
|
|
],
|
|
("position", "Designer", "Designer"),
|
|
),
|
|
];
|
|
for (slug, entity, fields, (field, value, shown)) in cases {
|
|
let r = app.post(&format!("/people/{id}/{slug}"), &fields).await;
|
|
assert_eq!(r.status, StatusCode::SEE_OTHER, "{slug}: {}", r.text());
|
|
assert_eq!(r.location(), format!("/people/{id}/stay#{slug}"));
|
|
let (rid, e) = last_created(&pool, id).await;
|
|
assert_eq!(e, entity);
|
|
|
|
let edit = app.get(&format!("/people/{id}/{slug}/{rid}/edit")).await;
|
|
assert_eq!(edit.status, StatusCode::OK, "{slug}");
|
|
let mut changed = fields.clone();
|
|
changed.push((field, value));
|
|
let r = app
|
|
.post(&format!("/people/{id}/{slug}/{rid}"), &changed)
|
|
.await;
|
|
assert_eq!(r.status, StatusCode::SEE_OTHER, "{slug}: {}", r.text());
|
|
let logged: Vec<String> = sqlx::query_scalar(
|
|
"SELECT fields FROM audit_log WHERE entity = $1 AND entity_id = $2 \
|
|
AND action = 'update'",
|
|
)
|
|
.bind(entity)
|
|
.bind(rid)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(logged, vec![field.to_string()], "{slug}");
|
|
let page = app.get(&format!("/people/{id}/stay")).await.text();
|
|
assert!(page.contains(shown), "{slug}: {shown} not shown");
|
|
|
|
// another person's path finds nothing
|
|
assert_eq!(
|
|
app.get(&format!("/people/{}/{slug}/{rid}/edit", id + 1000))
|
|
.await
|
|
.status,
|
|
StatusCode::NOT_FOUND
|
|
);
|
|
let r = app
|
|
.post(&format!("/people/{id}/{slug}/{rid}/delete"), &[])
|
|
.await;
|
|
assert_eq!(r.status, StatusCode::SEE_OTHER, "{slug}");
|
|
}
|
|
let page = app.get(&format!("/people/{id}/stay")).await.text();
|
|
assert!(page.contains("No arrival recorded yet."));
|
|
let deleted: i64 = sqlx::query_scalar("SELECT count(*) FROM audit_log WHERE action = 'delete'")
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(deleted, 6);
|
|
}
|
|
|
|
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
|
async fn forms_say_what_is_missing_or_wrong(pool: PgPool) {
|
|
let app = local(pool.clone());
|
|
let r = app
|
|
.post("/people", &[("surname", "Doe"), ("first_name", "Jane")])
|
|
.await;
|
|
let id = id_from(r.location());
|
|
let r = app.post(&format!("/people/{id}/arrivals"), &[]).await;
|
|
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
let t = r.text();
|
|
assert!(
|
|
t.contains("Enter the arrival date.") && t.contains("Enter the date in the arrival stamp.")
|
|
);
|
|
|
|
let r = app
|
|
.post(
|
|
&format!("/people/{id}/arrivals"),
|
|
&[
|
|
("arrived_on", "2026-09-01"),
|
|
("admission", "visa"),
|
|
("permitted_until", "2026-08-01"),
|
|
("departed_on", "2026-08-15"),
|
|
],
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
let t = r.text();
|
|
assert!(t.contains("The permitted stay cannot end before the arrival."));
|
|
assert!(t.contains("The departure cannot come before the arrival."));
|
|
assert!(r.body.contains("value=\"2026-09-01\""), "typed values stay");
|
|
|
|
let r = app
|
|
.post(
|
|
&format!("/people/{id}/extensions"),
|
|
&[
|
|
("applied_on", "2026-10-20"),
|
|
("basis", "work"),
|
|
("decision", "consideration"),
|
|
],
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
assert!(r.text().contains("Enter the date in the stamp."));
|
|
}
|
|
|
|
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
|
async fn passports_and_visas_are_chosen_from_the_persons_own(pool: PgPool) {
|
|
let app = local(pool.clone());
|
|
let mut people = Vec::new();
|
|
for (surname, number) in [("Doe", "Z00000042"), ("Roe", "Z00000043")] {
|
|
let r = app
|
|
.post("/people", &[("surname", surname), ("first_name", "Test")])
|
|
.await;
|
|
let id = id_from(r.location());
|
|
let r = app
|
|
.post(
|
|
&format!("/people/{id}/passports"),
|
|
&[
|
|
("number", number),
|
|
("issuing_country", "USA"),
|
|
("expires_on", "2031-01-01"),
|
|
],
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
|
|
let pp: i64 = sqlx::query_scalar("SELECT id FROM passports WHERE number = $1")
|
|
.bind(number)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap();
|
|
people.push((id, pp));
|
|
}
|
|
let (doe, doe_pp) = people[0];
|
|
let (_, roe_pp) = people[1];
|
|
|
|
// the new arrival's form offers Doe's passport, already chosen
|
|
let form = app.get(&format!("/people/{doe}/arrivals/new")).await.body;
|
|
assert!(form.contains(&format!(
|
|
"<option value=\"{doe_pp}\" selected>Z00000042 (United States)</option>"
|
|
)));
|
|
assert!(!form.contains("Z00000043"));
|
|
|
|
let arrival = |pp: i64| {
|
|
vec![
|
|
("arrived_on", "2026-09-01".to_string()),
|
|
("admission", "exempt".to_string()),
|
|
("permitted_until", "2026-10-30".to_string()),
|
|
("passport_id", pp.to_string()),
|
|
]
|
|
};
|
|
let other = arrival(roe_pp);
|
|
let other: Vec<(&str, &str)> = other.iter().map(|(k, v)| (*k, v.as_str())).collect();
|
|
let r = app.post(&format!("/people/{doe}/arrivals"), &other).await;
|
|
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
|
assert!(r.text().contains("Choose from the list."));
|
|
|
|
let own = arrival(doe_pp);
|
|
let own: Vec<(&str, &str)> = own.iter().map(|(k, v)| (*k, v.as_str())).collect();
|
|
let r = app.post(&format!("/people/{doe}/arrivals"), &own).await;
|
|
assert_eq!(r.status, StatusCode::SEE_OTHER, "{}", r.text());
|
|
|
|
// deleting the passport leaves the arrival without one
|
|
let r = app
|
|
.post(&format!("/people/{doe}/passports/{doe_pp}/delete"), &[])
|
|
.await;
|
|
assert_eq!(r.status, StatusCode::SEE_OTHER);
|
|
let left: Option<i64> =
|
|
sqlx::query_scalar("SELECT passport_id FROM arrivals WHERE person_id = $1")
|
|
.bind(doe)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(left, None);
|
|
}
|