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

+96 -21
View File
@@ -21,6 +21,7 @@ use crate::people::{
PersonInput, SEXES, TITLES,
};
use crate::session::User;
use crate::{companies, deadlines};
// ----------------------------------------------------------------- views
@@ -38,7 +39,9 @@ pub struct PersonView {
pub nationality: String,
pub nationality_name: String,
pub occupation: String,
/// The employer's name.
pub employer: String,
pub employer_id: Option<i64>,
pub phone: String,
pub email: String,
pub notes: String,
@@ -48,7 +51,12 @@ pub struct PersonView {
pub updated: String,
}
fn person_view(p: &Person, current_passport: Option<&str>, today: NaiveDate) -> PersonView {
fn person_view(
p: &Person,
current_passport: Option<&str>,
employer: Option<String>,
today: NaiveDate,
) -> PersonView {
PersonView {
id: p.id,
name: p.display_name(),
@@ -64,7 +72,8 @@ fn person_view(p: &Person, current_passport: Option<&str>, today: NaiveDate) ->
.unwrap_or(&p.nationality)
.to_string(),
occupation: p.occupation.clone(),
employer: p.employer.clone(),
employer: employer.unwrap_or_default(),
employer_id: p.employer_id,
phone: p.phone.clone(),
email: p.email.clone(),
notes: p.notes.clone(),
@@ -133,14 +142,23 @@ fn address_view(a: &Address) -> AddressView {
}
}
/// The person with the number of their current passport, for the header
/// every page of the person shares.
async fn load_person(st: &AppState, id: i64) -> anyhow::Result<Option<(Person, PersonView)>> {
/// The person with the number of their current passport and the name of
/// their employer, for the head every page of the person shares.
pub async fn load_person(st: &AppState, id: i64) -> anyhow::Result<Option<(Person, PersonView)>> {
let Some(p) = people::get_person(&st.db, id).await? else {
return Ok(None);
};
let pps = people::passports(&st.db, id).await?;
let view = person_view(&p, pps.first().map(|x| x.number.as_str()), dates::today());
let employer = match p.employer_id {
Some(c) => companies::name(&st.db, c).await?,
None => None,
};
let view = person_view(
&p,
pps.first().map(|x| x.number.as_str()),
employer,
dates::today(),
);
Ok(Some((p, view)))
}
@@ -217,6 +235,9 @@ fn zone_note(m: &Mrz, errors: &mut Errors) -> Option<String> {
pub struct ListQuery {
#[serde(default)]
archived: Option<String>,
/// A company's id: only the people it employs.
#[serde(default)]
company: Option<String>,
}
pub async fn list(
@@ -224,21 +245,42 @@ pub async fn list(
Extension(user): Extension<User>,
Query(q): Query<ListQuery>,
) -> Res {
let today = dates::today();
let archived = q.archived.as_deref() == Some("1");
let rows = people::list(&st.db, archived, dates::today()).await?;
let company: Option<i64> = q.company.as_deref().and_then(|c| c.parse().ok());
let mut rows = people::list(&st.db, archived, company, today).await?;
let ids: Vec<i64> = rows.iter().map(|r| r.id).collect();
let mut found = deadlines::for_people(&st.db, &ids, today).await?;
for r in &mut rows {
r.next = found.remove(&r.id).and_then(|a| a.next().cloned());
}
let (n_active, n_archived) = people::counts(&st.db).await?;
let company_name = match company {
Some(c) => companies::name(&st.db, c).await?,
None => None,
};
let heading = match (&company_name, archived) {
(Some(n), _) => format!("People at {n}"),
(None, true) => "Archived people".to_string(),
(None, false) => "People".to_string(),
};
Ok(page(
&st,
&user,
"people.html",
if archived {
"Archived people"
} else {
"People"
},
&heading,
"people",
StatusCode::OK,
context! { people => rows, archived, n_active, n_archived },
context! {
people => rows,
archived,
n_active,
n_archived,
heading,
companies => companies::choices(&st.db, company).await?,
company => company.map(|c| c.to_string()).unwrap_or_default(),
company_name,
},
))
}
@@ -277,6 +319,7 @@ pub async fn show(
.iter()
.map(address_view)
.collect();
let assessment = deadlines::for_person(&st.db, id, today).await?;
audit::viewed(&st.db, &user.login, Entity::Person, id, id).await?;
let recent = audit::recent(&st.db, Some(id), 6).await?;
let title = view.name.clone();
@@ -287,7 +330,15 @@ pub async fn show(
&title,
"people",
StatusCode::OK,
context! { p => view, passports, addresses, recent },
context! {
p => view,
tab => "overview",
passports,
addresses,
recent,
stay => assessment.stay,
deadlines => assessment.items,
},
))
}
@@ -300,6 +351,7 @@ fn person_form_page(
pf: &PassportForm,
errors: &Errors,
mrz_note: Option<String>,
companies: &[(String, String)],
status: StatusCode,
) -> Response {
let (title, mode, action) = match p {
@@ -317,11 +369,26 @@ fn person_form_page(
&title,
"people",
status,
context! { p, f, pf, errors, mrz_note, mode, action, ..select_lists() },
context! {
p, tab => "overview", f, pf, errors, mrz_note, mode, action, companies,
..select_lists()
},
)
}
/// The companies the employer is chosen from (the active ones and the one
/// already chosen), and their ids for the check.
async fn employer_choices(
st: &AppState,
current: Option<i64>,
) -> anyhow::Result<(Vec<(String, String)>, Vec<i64>)> {
let list = companies::choices(&st.db, current).await?;
let ids = list.iter().filter_map(|(id, _)| id.parse().ok()).collect();
Ok((list, ids))
}
pub async fn new_form(State(st): State<Arc<AppState>>, Extension(user): Extension<User>) -> Res {
let (companies, _) = employer_choices(&st, None).await?;
Ok(person_form_page(
&st,
&user,
@@ -330,6 +397,7 @@ pub async fn new_form(State(st): State<Arc<AppState>>, Extension(user): Extensio
&PassportForm::default(),
&Errors::new(),
None,
&companies,
StatusCode::OK,
))
}
@@ -340,6 +408,7 @@ pub async fn create(
Form(f): Form<Fields>,
) -> Res {
let today = dates::today();
let (companies, company_ids) = employer_choices(&st, None).await?;
let mut pf = PersonForm::from_fields(&f);
let mut ppf = PassportForm::from_fields(&f);
let mut errors = Errors::new();
@@ -360,7 +429,7 @@ pub async fn create(
}
let status = status_for(&errors);
return Ok(person_form_page(
&st, &user, None, &pf, &ppf, &errors, note, status,
&st, &user, None, &pf, &ppf, &errors, note, &companies, status,
));
}
@@ -368,7 +437,7 @@ pub async fn create(
pf.take_mrz(m, false);
ppf.take_mrz(m, false);
}
let person = pf.validate(today);
let person = pf.validate(today, &company_ids);
let passport = (!ppf.is_blank()).then(|| ppf.validate(zone.as_ref()));
for e in [
person.as_ref().err(),
@@ -390,6 +459,7 @@ pub async fn create(
&ppf,
&errors,
None,
&companies,
StatusCode::UNPROCESSABLE_ENTITY,
));
};
@@ -412,6 +482,7 @@ pub async fn create(
&ppf,
&errors,
None,
&companies,
StatusCode::UNPROCESSABLE_ENTITY,
));
}
@@ -441,6 +512,7 @@ pub async fn edit_form(
let Some((p, view)) = load_person(&st, id).await? else {
return Ok(not_found_page(&st));
};
let (companies, _) = employer_choices(&st, p.employer_id).await?;
audit::viewed(&st.db, &user.login, Entity::Person, id, id).await?;
Ok(person_form_page(
&st,
@@ -450,6 +522,7 @@ pub async fn edit_form(
&PassportForm::default(),
&Errors::new(),
None,
&companies,
StatusCode::OK,
))
}
@@ -460,11 +533,12 @@ pub async fn update(
Path(id): Path<i64>,
Form(f): Form<Fields>,
) -> Res {
let Some((_, view)) = load_person(&st, id).await? else {
let Some((p, view)) = load_person(&st, id).await? else {
return Ok(not_found_page(&st));
};
let (companies, company_ids) = employer_choices(&st, p.employer_id).await?;
let pf = PersonForm::from_fields(&f);
let input = match pf.validate(dates::today()) {
let input = match pf.validate(dates::today(), &company_ids) {
Ok(i) => i,
Err(errors) => {
return Ok(person_form_page(
@@ -475,6 +549,7 @@ pub async fn update(
&PassportForm::default(),
&errors,
None,
&companies,
StatusCode::UNPROCESSABLE_ENTITY,
));
}
@@ -569,7 +644,7 @@ fn passport_form_page(
&format!("{title}: {}", p.name),
"people",
status,
context! { p, pf, errors, mrz_note, name_hint, mode, action, heading => title, ..select_lists() },
context! { p, tab => "overview", pf, errors, mrz_note, name_hint, mode, action, heading => title, ..select_lists() },
)
}
@@ -824,7 +899,7 @@ fn address_form_page(
&format!("{heading}: {}", p.name),
"people",
status,
context! { p, af, errors, mode, action, heading, ..select_lists() },
context! { p, tab => "overview", af, errors, mode, action, heading, ..select_lists() },
)
}