Phase 2: companies, stay records, deadlines
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 22s

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.
This commit is contained in:
grabowski committed 2026-11-17 18:20:14 +07:00
1 parent 2e009700a9
commit 83bc481952
37 files changed
+5515 -142

No files matched your search

+50 -21
View File
@@ -46,7 +46,7 @@ pub struct Person {
pub place_of_birth: String,
pub nationality: String,
pub occupation: String,
pub employer: String,
pub employer_id: Option<i64>,
pub phone: String,
pub email: String,
pub notes: String,
@@ -86,6 +86,7 @@ pub struct PersonForm {
pub place_of_birth: String,
pub nationality: String,
pub occupation: String,
/// The employer's company id, from the selection.
pub employer: String,
pub phone: String,
pub email: String,
@@ -104,7 +105,8 @@ pub struct PersonInput {
pub place_of_birth: String,
pub nationality: String,
pub occupation: String,
pub employer: String,
/// The employer's company.
pub employer: Option<i64>,
pub phone: String,
pub email: String,
pub notes: String,
@@ -130,7 +132,7 @@ impl From<&Person> for PersonInput {
place_of_birth: p.place_of_birth.clone(),
nationality: p.nationality.clone(),
occupation: p.occupation.clone(),
employer: p.employer.clone(),
employer: p.employer_id,
phone: p.phone.clone(),
email: p.email.clone(),
notes: p.notes.clone(),
@@ -171,7 +173,7 @@ impl PersonForm {
place_of_birth: p.place_of_birth.clone(),
nationality: p.nationality.clone(),
occupation: p.occupation.clone(),
employer: p.employer.clone(),
employer: p.employer_id.map(|c| c.to_string()).unwrap_or_default(),
phone: p.phone.clone(),
email: p.email.clone(),
notes: p.notes.clone(),
@@ -199,8 +201,17 @@ impl PersonForm {
}
}
pub fn validate(&self, today: NaiveDate) -> Result<PersonInput, Errors> {
/// `companies`: the ids the employer may be chosen from.
pub fn validate(&self, today: NaiveDate, companies: &[i64]) -> Result<PersonInput, Errors> {
let mut e = Errors::new();
let employer = match self.employer.parse::<i64>() {
_ if self.employer.is_empty() => None,
Ok(c) if companies.contains(&c) => Some(c),
_ => {
err(&mut e, "employer", "Choose a company from the list.");
None
}
};
if self.surname.is_empty() {
err(
&mut e,
@@ -248,7 +259,7 @@ impl PersonForm {
place_of_birth: self.place_of_birth.clone(),
nationality: self.nationality.clone(),
occupation: self.occupation.clone(),
employer: self.employer.clone(),
employer,
phone: self.phone.clone(),
email: self.email.clone(),
notes: self.notes.clone(),
@@ -256,7 +267,7 @@ impl PersonForm {
}
}
fn looks_like_email(s: &str) -> bool {
pub fn looks_like_email(s: &str) -> bool {
match s.split_once('@') {
Some((user, host)) => {
!user.is_empty() && host.contains('.') && !host.starts_with('.') && !s.contains(' ')
@@ -295,7 +306,7 @@ pub async fn get_person_in(conn: &mut PgConnection, id: i64) -> Result<Option<Pe
pub async fn create_person(conn: &mut PgConnection, p: &PersonInput) -> Result<i64> {
let id: i64 = sqlx::query_scalar(
"INSERT INTO people (surname, first_name, middle_name, title, sex, date_of_birth, \
place_of_birth, nationality, occupation, employer, phone, email, notes) \
place_of_birth, nationality, occupation, employer_id, phone, email, notes) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING id",
)
.bind(&p.surname)
@@ -307,7 +318,7 @@ pub async fn create_person(conn: &mut PgConnection, p: &PersonInput) -> Result<i
.bind(&p.place_of_birth)
.bind(&p.nationality)
.bind(&p.occupation)
.bind(&p.employer)
.bind(p.employer)
.bind(&p.phone)
.bind(&p.email)
.bind(&p.notes)
@@ -320,7 +331,7 @@ pub async fn update_person(conn: &mut PgConnection, id: i64, p: &PersonInput) ->
sqlx::query(
"UPDATE people SET surname = $2, first_name = $3, middle_name = $4, title = $5, \
sex = $6, date_of_birth = $7, place_of_birth = $8, nationality = $9, \
occupation = $10, employer = $11, phone = $12, email = $13, notes = $14, \
occupation = $10, employer_id = $11, phone = $12, email = $13, notes = $14, \
updated_at = now() WHERE id = $1",
)
.bind(id)
@@ -333,7 +344,7 @@ pub async fn update_person(conn: &mut PgConnection, id: i64, p: &PersonInput) ->
.bind(&p.place_of_birth)
.bind(&p.nationality)
.bind(&p.occupation)
.bind(&p.employer)
.bind(p.employer)
.bind(&p.phone)
.bind(&p.email)
.bind(&p.notes)
@@ -363,7 +374,8 @@ struct ListRow {
middle_name: String,
nationality: String,
date_of_birth: Option<NaiveDate>,
employer: String,
employer_id: Option<i64>,
employer: Option<String>,
passport_number: Option<String>,
passport_expires: Option<NaiveDate>,
province: Option<String>,
@@ -376,30 +388,42 @@ pub struct ListItem {
pub name: String,
pub nationality: String,
pub age: Option<i32>,
pub employer_id: Option<i64>,
pub employer: String,
pub passport_number: String,
pub passport_expires: String,
pub passport_status: Option<dates::Status>,
pub province: String,
/// The most urgent deadline; the page fills it in.
pub next: Option<crate::deadlines::Deadline>,
/// Lower-case text the filter box searches.
pub q: String,
}
pub async fn list(db: &PgPool, archived: bool, today: NaiveDate) -> Result<Vec<ListItem>> {
/// The people in the list or in the archive; with `company`, only those it
/// employs.
pub async fn list(
db: &PgPool,
archived: bool,
company: Option<i64>,
today: NaiveDate,
) -> Result<Vec<ListItem>> {
let rows: Vec<ListRow> = sqlx::query_as(
"SELECT p.id, p.surname, p.first_name, p.middle_name, p.nationality, p.date_of_birth, \
p.employer, pp.number AS passport_number, pp.expires_on AS passport_expires, \
a.province \
p.employer_id, c.name AS employer, pp.number AS passport_number, \
pp.expires_on AS passport_expires, a.province \
FROM people p \
LEFT JOIN companies c ON c.id = p.employer_id \
LEFT JOIN LATERAL (SELECT number, expires_on FROM passports \
WHERE person_id = p.id ORDER BY expires_on DESC, id DESC LIMIT 1) pp ON true \
LEFT JOIN LATERAL (SELECT province FROM addresses \
WHERE person_id = p.id AND moved_out_on IS NULL \
ORDER BY moved_in_on DESC NULLS LAST, id DESC LIMIT 1) a ON true \
WHERE (p.archived_at IS NOT NULL) = $1 \
WHERE (p.archived_at IS NOT NULL) = $1 AND ($2::bigint IS NULL OR p.employer_id = $2) \
ORDER BY lower(p.surname), lower(p.first_name), p.id",
)
.bind(archived)
.bind(company)
.fetch_all(db)
.await?;
Ok(rows
@@ -415,9 +439,10 @@ pub async fn list(db: &PgPool, archived: bool, today: NaiveDate) -> Result<Vec<L
.to_string();
let passport_number = r.passport_number.unwrap_or_default();
let province = r.province.unwrap_or_default();
let employer = r.employer.unwrap_or_default();
let q = format!(
"{name} {nationality} {} {passport_number} {} {province}",
r.nationality, r.employer
"{name} {nationality} {} {passport_number} {employer} {province}",
r.nationality
)
.to_lowercase();
ListItem {
@@ -427,9 +452,11 @@ pub async fn list(db: &PgPool, archived: bool, today: NaiveDate) -> Result<Vec<L
passport_status: r.passport_expires.map(|d| dates::passport_status(d, today)),
name,
nationality,
employer: r.employer,
employer_id: r.employer_id,
employer,
passport_number,
province,
next: None,
q,
}
})
@@ -1085,7 +1112,7 @@ mod tests {
]));
assert_eq!(f.surname, "Mustermann");
assert_eq!(f.nationality, "DEU");
let e = f.validate(today).unwrap_err();
let e = f.validate(today, &[]).unwrap_err();
assert_eq!(
e.keys().map(String::as_str).collect::<Vec<_>>(),
vec!["date_of_birth", "email", "title"]
@@ -1095,9 +1122,11 @@ mod tests {
("first_name", "Erika"),
("date_of_birth", "1984-08-12"),
("nationality", "DEU"),
("employer", "3"),
]))
.validate(today)
.validate(today, &[3])
.unwrap();
assert_eq!(ok.employer, Some(3));
assert_eq!(ok.date_of_birth, Some(d("1984-08-12")));
let mut other = ok.clone();
other.email = "erika@example.com".into();