A Forms tab on each person fills in TM.7, STM.2, STM.9, STM.11 and STM.1 for an extension of stay, TM.47 for a 90-day report and TM.8 for a re-entry permit. The Bureau's blank pages (forms/, with their sources and checksums) go unchanged into a new PDF, and the values are written on the dotted lines in Sarabun; boxes are ticked and the title circled where a form asks. scripts/gen_layout.py measures the blanks into src/papers/layout.rs. The tab says what would stay blank and links to where it is added. What only the application says (the date, the office, the reason, the days, a trip) is carried in the links and never stored. The extension's five forms also come in one PDF. Opening or downloading a form is audited as entity "form" with the forms' codes, never their values. Migration 0005 adds what STM.1 and STM.11 ask of the company (capital, revenue, equity, staff, a coordinator), a work permit's salary and contract dates, and where an arrival came from. The Nix package now takes forms/.
323 lines
22 KiB
Markdown
323 lines
22 KiB
Markdown
# visa.buildfor.life: plan
|
|
|
|
> Phases 1 to 3 built on November 17, 2026, phase 4's app side and phase 5
|
|
> on November 18, 2026. The live app at https://visa.buildfor.life has phases
|
|
> 1 to 3 until cnx-network-clan PR #126 (task 4.6) is deployed and the fleet's
|
|
> `visa` input is updated to the newest commit. **Resume here: phase 6, the
|
|
> Non-B extension pack.**
|
|
|
|
visa.buildfor.life keeps the immigration records of the foreigners that B4L
|
|
looks after: their passports, addresses in Thailand, arrivals with the Thailand
|
|
Digital Arrival Card (TDAC), visas, extensions of stay, re-entry permits,
|
|
90-day reports and work permits. From those records it works out every
|
|
deadline, such as the next 90-day report with its filing windows or the day the
|
|
window for the next extension opens, and it prepares the documents for the
|
|
next extension of stay with the forms filled in from the records.
|
|
|
|
B4L's own foreign staff use it first. It is meant to grow into the internal
|
|
tool of a visa agency in Chiang Mai, the one planned in the business plan in
|
|
`grabowski/julia` (`C:\dev\visa_service_plan`), so the data model treats
|
|
everyone as a client of an agency whose staff see all records.
|
|
|
|
## Decisions
|
|
|
|
The first round of questions was answered on November 17, 2026.
|
|
|
|
| Question | Decision |
|
|
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| Where it runs | cnx01, installed on the host like proplan and lampbench: database `visa` on cnx01's PostgreSQL, files in a ZFS dataset, encrypted by the app, borg backups to juliet |
|
|
| How it is published | bkk01 publishes `visa.buildfor.life` through a `modules/web-sites` app entry |
|
|
| Login | Kanidm at id.cnx.network, OAuth2 client `visa-web`, group `visa` with grabowski and oltmanns |
|
|
| Who sees what | every member of `visa` sees and edits every record, as agency staff would; a view for clients comes later |
|
|
| First extension pack | Non-B with a work permit, employer B4L, at Chiang Mai Immigration |
|
|
| Reminders | the dashboard first; later a Matrix room message and an email to the person concerned |
|
|
| Repository | public `B4L/visa` since Nov 17, 2026: it holds code and invented demo data only, so the fleet fetches it like proplan |
|
|
| Employers | companies are records of their own under Companies; a person's employer and a work permit's employer are chosen from them, and a company's page adds people to it |
|
|
|
|
Kanidm keeps persons, groups and OAuth2 clients in one namespace, so the
|
|
client cannot be called `visa` like the group. It is `visa-web`, as
|
|
lampbench's client is `lampbench-web`.
|
|
|
|
## How it is built
|
|
|
|
The stack is the one of proplan, the fleet overview and lampbench-web: one
|
|
Rust binary (axum 0.8, minijinja templates, sqlx on PostgreSQL) with its
|
|
templates, static files and migrations compiled in. Pages are rendered on the
|
|
server and work without JavaScript; `static/app.js` only switches the theme,
|
|
filters lists and asks before a delete. The Content Security Policy allows no
|
|
inline script or style.
|
|
|
|
- **Login.** OpenID Connect with PKCE and a nonce against Kanidm, ported from
|
|
the fleet overview (`src/oidc.rs`). Only members of `visa` get a session.
|
|
Without a login provider the app starts only with `VISA_INSECURE_NO_LOGIN=1`
|
|
on a loopback address, for development.
|
|
- **Sessions.** In PostgreSQL, keyed by the SHA-256 of the cookie's value, so
|
|
a deploy does not sign anyone out and a database dump holds no usable
|
|
session. The cookie is `__Host-visa` (Secure, HttpOnly, SameSite=Lax).
|
|
- **Forms.** Every POST must come from this site (`Sec-Fetch-Site`, else
|
|
`Origin`) and carry the session's form token (`csrf`).
|
|
- **Audit.** Personal data falls under Thailand's Personal Data Protection Act
|
|
(PDPA). Every view of a person and every change is written to `audit_log`
|
|
with the login, the record and the names of the changed fields, never their
|
|
values. The table refuses updates and deletes. The Activity page lists it.
|
|
- **Documents.** Scans and photos lie in a files directory on the server,
|
|
one file per document, encrypted by the app with AES-256-GCM. Each file has
|
|
a key of its own, derived with HKDF-SHA256 from the app's file key and a
|
|
random salt in the file's header, and the document's id is authenticated
|
|
with it, so a file renamed to another document's id does not open. The
|
|
file key is a systemd credential that is never in the database, so neither
|
|
the files nor their backups can be read without it. The database holds what
|
|
is known about each file: its kind, the record it belongs to, the date on
|
|
it, a note, its name, type, size and SHA-256.
|
|
- **Passport data entry.** The two lines of the machine-readable zone (MRZ)
|
|
at the bottom of the photo page fill the name, nationality, date of birth,
|
|
sex, passport number, issuing state and expiry date. Their check digits
|
|
catch typing errors (ICAO Doc 9303, part 4).
|
|
|
|
## Data model
|
|
|
|
Phase 1 holds people, passports and addresses; phase 2 adds what a stay is
|
|
made of.
|
|
|
|
| Table | Holds | Phase |
|
|
| -------------------- | --------------------------------------------------------------------------------------------------------------------------- | ----- |
|
|
| `people` | names as in the passport, title, sex, birth date and place, nationality, occupation, employer (a company), phone, email, notes; archiving | 1 |
|
|
| `passports` | number, issuing state, issue and expiry dates, issuing authority, the MRZ as read; the current one has the latest expiry | 1 |
|
|
| `addresses` | the address in Thailand in the order Thai forms use, moving dates, the house master and the TM.30 receipt | 1 |
|
|
| `sessions` | signed-in sessions | 1 |
|
|
| `audit_log` | who viewed or changed which record, append-only | 1 |
|
|
| `companies` | name in English and Thai, the 13-digit registration number with its check digit, address, the office's GPS position, the director who signs; archiving | 2, 4 |
|
|
| `visas` | visa category (Non-B, Non-O, DTV and others), number, place and date of issue, validity, entries | 2 |
|
|
| `arrivals` | arrival date, port, flight, visa used, permitted-until date, TDAC number; departure date when the person left | 2 |
|
|
| `extensions` | extensions of stay: approval date, new permitted-until date, reason, office, receipt | 2 |
|
|
| `reentry_permits` | TM.8 permits, single or multiple, valid until | 2 |
|
|
| `ninety_day_reports` | the report date, the way it was filed, the receipt and the next due date printed on it | 2 |
|
|
| `work_permits` | number, employer (a company), position, validity | 2 |
|
|
| `documents` | uploads of a person (tied to one of their records, or none) or of a company: kind, date on the document, note, file name, type, size, SHA-256; the file itself is encrypted on disk | 4 |
|
|
|
|
## Deadline rules
|
|
|
|
The rules below come from the Immigration Bureau's pages. Where the receipt
|
|
or the stamp shows a date, that date wins over any calculation, and the app
|
|
shows a calculated date as an estimate until a receipt confirms it.
|
|
|
|
- **90-day report (TM.47).** The day of arrival counts as day 1, so the first
|
|
report is due on the arrival date plus 89 days. After each report the
|
|
receipt names the next date. Leaving Thailand ends the count; after the
|
|
next arrival it starts again from day 1. A report in person, or by someone
|
|
the person authorizes, is accepted from 15 days before to 7 days after the
|
|
due date. Online it is accepted from 15 days before to 7 days before. The
|
|
first report after a new passport, and the first after an extension of
|
|
stay, is made in person. A late report costs 2,000 THB; being caught
|
|
without one, up to 5,000 THB. An extension of stay starts the count again,
|
|
so when the permission to stay ends before the next report is due, the app
|
|
shows no report: the person either leaves or extends.
|
|
- **Extension of stay (TM.7).** The application must be made before the
|
|
permitted-until date. Chiang Mai accepts it from 30 days before (some
|
|
categories 45); the app opens the window 30 days before and counts down to
|
|
the last working day, the Friday when the permission ends on a weekend.
|
|
An overstay costs 500 THB a day, at most 20,000 THB. A first application
|
|
for work is often stamped "under consideration" for 30 days; the app then
|
|
shows the day to return for the decision.
|
|
- **Passport.** An extension never runs past the passport's expiry, so the
|
|
app warns 12 months before expiry and marks a passport with less than six
|
|
months left.
|
|
- **Re-entry permit (TM.8).** Leaving without one cancels the extension of
|
|
stay; a planned trip without a valid permit raises a warning.
|
|
- **TM.30.** The house master reports the person's address within 24 hours
|
|
of arrival, also after a trip abroad; extensions and 90-day reports ask for
|
|
the receipt.
|
|
- **TDAC.** Submitted online within the three days before each arrival; its
|
|
number replaces the old TM.6 card number on the forms.
|
|
|
|
Sources: Immigration Bureau, TM.47 online manual
|
|
(bangkok.immigration.go.th/en/tm47online-manual); Immigration Division 1 form
|
|
downloads (bangkok.immigration.go.th/en/downloads_en); thailand.go.th,
|
|
"Business visa extension for foreigners working in Thailand"; TDAC manual
|
|
(tdac.immigration.go.th/manual/en).
|
|
|
|
## The Non-B extension pack (phase 6)
|
|
|
|
For an extension of stay based on work, Chiang Mai Immigration asks for the
|
|
following. The pack lists each item with its state (generated, uploaded,
|
|
missing or out of date) and prints them in this order.
|
|
|
|
1. TM.7 with a 4 x 6 cm photo, the acknowledgment of overstay penalties and
|
|
STM.2, all filled in from the records.
|
|
2. STM.1, the employer's certificate of employment.
|
|
3. Copies of the passport: the photo page, the visa, the latest arrival stamp,
|
|
earlier extensions, and the TDAC.
|
|
4. The last 90-day receipt and the TM.30 receipt.
|
|
5. The work permit and its copy.
|
|
6. B4L's company documents: the company certificate and the list of
|
|
shareholders (BOJ.5), both certified within six months; the latest
|
|
balance sheet with PND.50; PND.1 withholding tax and SPS 1-10 social
|
|
security for the last three months; PP.30 VAT returns; the applicant's
|
|
PND.91; a map; photos of the office outside and inside. Every page carries
|
|
the company seal and the director's signature, in two sets.
|
|
|
|
The company documents are shared by every employee's pack, so they are kept
|
|
once per company with their own expiry (a certification older than six months
|
|
counts as out of date).
|
|
|
|
## Phases
|
|
|
|
Each phase ends with the tests, clippy, formatting, screenshots in light and
|
|
dark at desktop and phone width, a commit and a push.
|
|
|
|
| Phase | Content | Status |
|
|
| ----- | --------------------------------------------------------------------------------------------------------------- | ---------------------------- |
|
|
| 1 | App, Kanidm login, people, passports with MRZ reading, addresses in Thailand, audit log, demo data, CI | built Nov 17, 2026 |
|
|
| 2 | Companies; stays: visas, arrivals with TDAC, extensions, re-entry permits, 90-day reports, work permits; deadlines | built Nov 17, 2026 |
|
|
| 3 | On cnx01: flake package, fleet module, Kanidm client and group, bkk01 site, backups; real data from here on | live Nov 17, 2026 |
|
|
| 4 | Documents: encrypted uploads (passport pages, TDAC, receipts, photo) on the dataset `zroot/visa` with a key var, viewing and download, all audited; the office's GPS position and photo | app built Nov 18, 2026; fleet in PR #126 |
|
|
| 5 | Filled-in forms: TM.7, STM.2, the overstay acknowledgment (STM.9), STM.11, STM.1, TM.47 and TM.8 as PDF | built Nov 18, 2026 |
|
|
| 6 | The Non-B extension pack: B4L's company documents, the checklist, one PDF to print | |
|
|
| 7 | Reminders by Matrix room message and by email | |
|
|
| later | Thai user interface, agency cases and fees, a view for clients, other visa categories' packs | |
|
|
|
|
### Phase 1: app, login, people
|
|
|
|
- [x] 1.1 Repository: Cargo crate `visa`, templates and static files compiled
|
|
in, migrations, CI (secret scan, format, clippy, tests against
|
|
PostgreSQL 18), pre-commit secret scan.
|
|
- [x] 1.2 Login through Kanidm (OIDC with PKCE), sessions in PostgreSQL,
|
|
group `visa`; `VISA_INSECURE_NO_LOGIN=1` for loopback development.
|
|
- [x] 1.3 Same-site check and form token on every POST; security headers.
|
|
- [x] 1.4 People: list with filter, page, create, edit, archive, restore.
|
|
- [x] 1.5 Passports: add, edit, delete; the current one marked; expiry
|
|
warnings at 12 and 6 months.
|
|
- [x] 1.6 MRZ reading with check digits, on the new person and the new
|
|
passport forms.
|
|
- [x] 1.7 Addresses in Thailand with house master and TM.30 receipt.
|
|
- [x] 1.8 Audit log with the Activity page.
|
|
- [x] 1.9 Demo data with invented people (`visa seed-demo`, development only).
|
|
|
|
### Phase 2: stays and deadlines
|
|
|
|
- [x] 2.1 Visas and arrivals: arrival date, port, flight, visa used,
|
|
permitted-until date, TDAC number and date; departures.
|
|
- [x] 2.2 Extensions of stay and re-entry permits.
|
|
- [x] 2.3 90-day reports with the next due date from the receipt.
|
|
- [x] 2.4 Work permits.
|
|
- [x] 2.5 The deadline engine as pure functions with unit tests for every
|
|
rule above (arrival plus 89 days, the windows, a trip resetting the
|
|
count, the extension window, passport and work permit expiry).
|
|
- [x] 2.6 Dashboard at `/`: every person's deadlines sorted by urgency, with
|
|
a company filter; the person's status and deadlines on the Overview
|
|
and the "Visa and stay" tab; the next deadline in the people list.
|
|
- [x] 2.7 Companies: list, page, form, archive; the employer chosen from
|
|
them on the person form and on work permits; people added to a
|
|
company from its page; the people list filtered by employer.
|
|
|
|
The six kinds of stay record and companies share one description each
|
|
(`Kind` in `src/records.rs`): one form template, one set of routes and the
|
|
same audit entries serve them all. A new kind of record is a migration and
|
|
one `Kind`.
|
|
|
|
### Phase 3: on cnx01
|
|
|
|
- [x] 3.1 `flake.nix` with `packages.visa` (crane), built in WSL against the
|
|
fleet's nixpkgs (Rust 1.95): a 9.8 MB binary, 58 MB with its closure.
|
|
- [x] 3.2 Fleet module `modules/visa` (roles `enable` on cnx01 and
|
|
`kanidmClient` on web01), database `visa` on cnx01's PostgreSQL, vars
|
|
`visa-oidc` and `postgres-visa`, `visa-admin`, VM test `checks.visa`;
|
|
group `visa` in `identity/`. The dataset `zroot/visa` and the file key
|
|
move to phase 4, which builds the uploads that use them.
|
|
- [x] 3.3 Decided on November 17, 2026: B4L/visa is public. It holds code and
|
|
invented demo data only, so the fleet fetches it like proplan and needs
|
|
no read token.
|
|
- [x] 3.4 `sites.nix` entry `visa` on bkk01 and the A record; deployed to ns1,
|
|
web01, cnx01, bkk01 and control on November 17, 2026
|
|
(cnx-network-clan PR #121).
|
|
|
|
### Phase 4: documents
|
|
|
|
- [x] 4.1 Encrypted store (`src/files.rs`): one file per document,
|
|
`<id>.vdoc`, written to a part file and synced before it takes its
|
|
name, never replaced. The file key is 64 hex digits from
|
|
`VISA_FILES_KEY_FILE` or the credential `files-key`; with the login on,
|
|
the app does not start without it and `VISA_FILES_DIR`.
|
|
- [x] 4.2 Uploads on a new Documents tab of each person: a one-line dashed
|
|
strip that takes dropped files, up to 10 files of 20 MB each and 64 MB
|
|
in all. PDF, JPEG, PNG and WebP are taken, told by their first bytes.
|
|
The kinds follow the Non-B pack, and a document may belong to one of
|
|
the person's records (passport, visa, arrival, extension, re-entry
|
|
permit, 90-day report, address, work permit). The form token is the
|
|
upload's first field and is checked before any file is read; a file
|
|
that is there already for the person is refused.
|
|
- [x] 4.3 Company documents on the company's page: the certificate, BOJ.5,
|
|
the balance sheet with PND.50, PND.1, SPS 1-10, PP.30, a map and photos
|
|
of the office, each with the date on it for phase 6's six-month rule.
|
|
- [x] 4.4 Viewing and download: a file opens in the browser's image or PDF
|
|
viewer under its own Content Security Policy, in which nothing runs, and
|
|
is never cached. A download is named after the person or company, the
|
|
kind and the date. Each record lists its documents next to its Edit
|
|
link, with "+ Document" to add one.
|
|
- [x] 4.5 Audit: upload, view (opening the file), download (a new action),
|
|
change and deletion; a company's entries carry its id
|
|
(`audit_log.company_id`). `visa files-check` reads every file back and
|
|
lists damaged, missing and stray files. `visa seed-demo` adds demo
|
|
documents: PDFs marked as demos and PNGs of shapes.
|
|
- [ ] 4.6 Fleet: the dataset `zroot/visa` at `/var/lib/visa` with the files
|
|
in `files/`, the var `visa-files` with the key, the files in cnx01's
|
|
borg backups, and the VM test uploading a scan through the login. Built
|
|
and checked in cnx-network-clan PR #126 (branch `visa-files`, opened
|
|
Nov 18, 2026); Alex merges and deploys it, together with the flake
|
|
update #125 that cnx01 and control do not run yet. After the deploy:
|
|
`visa-admin files-check` on cnx01 and an upload on the live app.
|
|
- [x] 4.7 A company's office, asked for on November 18, 2026: its GPS
|
|
position on the company form, read from the numbers Google Maps
|
|
copies, a link to the place on Google Maps or OpenStreetMap, or
|
|
degrees, minutes and seconds, kept as decimal degrees and checked to
|
|
lie in Thailand (`src/gps.rs`); an Office card on the company's page
|
|
with the position, links that open it on either map, the newest photo
|
|
of the office, and a strip that adds a photo and returns to the card.
|
|
The photos are documents of the kind "Photo of the office", so they
|
|
are encrypted and audited like every other file.
|
|
|
|
### Phase 5: filled-in forms
|
|
|
|
- [x] 5.1 The blank forms as the Immigration Bureau publishes them, in
|
|
`forms/` with their sources and checksums in `forms/README.md`: TM.7,
|
|
STM.2, STM.9 (the acknowledgment of the penalties for overstay),
|
|
STM.11, STM.1, TM.47 and TM.8. Each page goes unchanged into a new PDF
|
|
and the values are written on its dotted lines in Sarabun, the font of
|
|
the forms; a box is ticked and the person's title circled where the
|
|
form asks for that. Signatures, the photo and the officers' parts stay
|
|
blank.
|
|
- [x] 5.2 Where each value goes: `scripts/gen_layout.py` finds the dotted
|
|
lines in the blank PDFs and writes `src/papers/layout.rs`. TM.47 has no
|
|
dotted lines, so its blanks are measured from the lines drawn on it. A
|
|
new version of a form needs the script run again and every page checked.
|
|
- [x] 5.3 The values (`src/papers/values.rs`) come from the person, the
|
|
passport that runs longest, the current address, the last arrival with
|
|
its visa and permission to stay, the work permit and the employer, and
|
|
the date of the company certificate from the company's documents. They
|
|
are written in capitals with the Western year, as the passport has
|
|
them. What only the application says (the date, the office, the reason,
|
|
the days asked for, which yearly extension it is, a trip for TM.8) is
|
|
on the tab, with defaults from the records, and travels in the links as
|
|
query parameters, so none of it is stored.
|
|
- [x] 5.4 A Forms tab on each person: every form with what it is for, Open
|
|
and Download, and what would stay blank, each with a link to where it
|
|
is added. "The extension's five forms" gives TM.7, STM.2, STM.9, STM.11
|
|
and STM.1 in one PDF, in the order the officer reads them.
|
|
- [x] 5.5 New fields for the forms (`migrations/0005_forms.sql`): a
|
|
company's registered and paid-up capital, the year of its latest
|
|
financial statements with the revenue and the owners' equity, the
|
|
foreign staff's pay for the next year, its Thai and foreign staff, and
|
|
a coordinator for immigration; a work permit's monthly salary and
|
|
contract dates; where an arrival came from. A Thai year such as 2568 is
|
|
taken as 2025.
|
|
- [x] 5.6 Audit: opening or downloading a form is logged as `form` with the
|
|
codes of the forms, never their values.
|
|
|
|
## Open questions
|
|
|
|
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.
|
|
2. Which Chiang Mai office handles B4L's cases (the main office at 71 Moo 3
|
|
Airport Road or the branch at Central Chiangmai) only matters for the
|
|
cover sheet; to be settled in phase 6.
|