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.
114 lines
3.5 KiB
Rust
114 lines
3.5 KiB
Rust
//! The Deadlines page at /: every person's coming deadlines, the most
|
|
//! urgent first.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use axum::Extension;
|
|
use axum::extract::{Query, State};
|
|
use axum::http::StatusCode;
|
|
use chrono::TimeDelta;
|
|
use minijinja::context;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use super::{AppState, Res, page};
|
|
use crate::companies;
|
|
use crate::dates;
|
|
use crate::deadlines::{self, Deadline};
|
|
use crate::session::User;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct DashQuery {
|
|
/// "1": also what lies beyond the next 90 days.
|
|
#[serde(default)]
|
|
all: Option<String>,
|
|
/// A company's id: only the people it employs.
|
|
#[serde(default)]
|
|
company: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
struct Row {
|
|
person_id: i64,
|
|
person: String,
|
|
employer: String,
|
|
d: Deadline,
|
|
q: String,
|
|
}
|
|
|
|
/// How far ahead the page looks unless asked for everything; anything
|
|
/// overdue or close (bad, warn) shows regardless.
|
|
const HORIZON_DAYS: i64 = 90;
|
|
|
|
pub async fn page_handler(
|
|
State(st): State<Arc<AppState>>,
|
|
Extension(user): Extension<User>,
|
|
Query(q): Query<DashQuery>,
|
|
) -> Res {
|
|
let today = dates::today();
|
|
let all = q.all.as_deref() == Some("1");
|
|
let company: Option<i64> = q.company.as_deref().and_then(|c| c.parse().ok());
|
|
let people: Vec<(i64, String, String, String, Option<String>)> = sqlx::query_as(
|
|
"SELECT p.id, p.surname, p.first_name, p.middle_name, c.name \
|
|
FROM people p LEFT JOIN companies c ON c.id = p.employer_id \
|
|
WHERE p.archived_at IS NULL AND ($1::bigint IS NULL OR p.employer_id = $1)",
|
|
)
|
|
.bind(company)
|
|
.fetch_all(&st.db)
|
|
.await?;
|
|
let ids: Vec<i64> = people.iter().map(|p| p.0).collect();
|
|
let mut found = deadlines::for_people(&st.db, &ids, today).await?;
|
|
let horizon = today + TimeDelta::days(HORIZON_DAYS);
|
|
let mut rows = Vec::new();
|
|
for (id, surname, first, middle, employer) in &people {
|
|
let Some(a) = found.remove(id) else { continue };
|
|
let given = if middle.is_empty() {
|
|
first.clone()
|
|
} else {
|
|
format!("{first} {middle}")
|
|
};
|
|
let person = format!("{}, {given}", surname.to_uppercase());
|
|
let employer = employer.clone().unwrap_or_default();
|
|
for d in a.items {
|
|
let Some(due) = d.due else { continue };
|
|
let urgent = matches!(d.level, "bad" | "warn");
|
|
if !all && !urgent && due > horizon {
|
|
continue;
|
|
}
|
|
let q = format!("{person} {employer} {} {}", d.title, d.label).to_lowercase();
|
|
rows.push(Row {
|
|
person_id: *id,
|
|
person: person.clone(),
|
|
employer: employer.clone(),
|
|
d,
|
|
q,
|
|
});
|
|
}
|
|
}
|
|
rows.sort_by(|x, y| {
|
|
deadlines::rank(x.d.level)
|
|
.cmp(&deadlines::rank(y.d.level))
|
|
.then(x.d.due.cmp(&y.d.due))
|
|
.then(x.person.cmp(&y.person))
|
|
});
|
|
let overdue = rows.iter().filter(|r| r.d.level == "bad").count();
|
|
let soon = rows.iter().filter(|r| r.d.level == "warn").count();
|
|
Ok(page(
|
|
&st,
|
|
&user,
|
|
"dashboard.html",
|
|
"Deadlines",
|
|
"deadlines",
|
|
StatusCode::OK,
|
|
context! {
|
|
rows,
|
|
overdue,
|
|
soon,
|
|
all,
|
|
n_people => people.len(),
|
|
horizon_days => HORIZON_DAYS,
|
|
companies => companies::choices(&st.db, company).await?,
|
|
company => company.map(|c| c.to_string()).unwrap_or_default(),
|
|
},
|
|
))
|
|
}
|