Files
visa/migrations/0013_originals.sql
T
grabowski ff716eb9d3
CI / secret scan (push) Successful in 8s
CI / fmt, clippy, tests (push) Successful in 1m23s
CI / release (push) Skipped
Originals in our care (phase 12, step 1)
What clients hand Lanna Desk for a while (a passport for an extension, a
signed contract, a title deed) gets a code and a label (OR-2026-0001), and
a record of where it is: held at a place in the office, out with someone
such as Chiang Mai Immigration until a date, or returned.

- Migration 0013: table originals, owned by a client or a person; the
  database refuses a delete and a change of what it is, whose it is and
  how it came.
- Every intake and move is a journal entry starting with the code; the
  item's page reads its history from there. The audit log has the new
  entity "original", with a view when a person's original is shown.
- Pages: Legal, Originals (in our care, out, held, returned, all; a find
  box for a typed or scanned code), the item's page with the move form,
  intake from a client's or a person's page, and the label with a QR code
  to the item's page. Cards on the client's and the person's pages.
- A field can show only for some choices of a selection
  (data-shown-when), so the move form asks "Where it lies", "With whom"
  or "Returned to" as fits.
- Demo data: a passport out at Immigration, a title deed and a house
  registration held, a company certificate returned.
- PLAN: phase 12, with step 2 (the inbox that files a pile of scans) and
  step 3 planned.
2026-11-23 14:06:50 +07:00

80 lines
3.7 KiB
PL/PgSQL

-- Originals in Lanna Desk's care (phase 12): what clients hand over for a
-- while, such as a passport for an extension, a signed contract or a title
-- deed, and where each one is now. The code goes on its label; every intake
-- and move is a journal entry, with the code first.
CREATE SEQUENCE original_codes;
CREATE TABLE originals (
id BIGSERIAL PRIMARY KEY,
-- OR-2026-0001, on the label and in its QR code
code TEXT NOT NULL UNIQUE CHECK (code ~ '^OR-[0-9]{4}-[0-9]{4,}$') DEFAULT (
'OR-' || to_char(now() AT TIME ZONE 'Asia/Bangkok', 'YYYY') || '-'
|| lpad(nextval('original_codes')::text, 4, '0')
),
-- whose it is: a client of the legal practice, or a person of the visa
-- side (until phase 10 makes every person a client)
client_id BIGINT REFERENCES clients (id),
person_id BIGINT REFERENCES people (id),
CONSTRAINT originals_one_owner CHECK (num_nonnulls(client_id, person_id) = 1),
kind TEXT NOT NULL CHECK (kind IN (
'passport', 'id-card', 'house-registration', 'title-deed', 'contract',
'certificate', 'bank-book', 'work-permit', 'other'
)),
-- in words: "Passport C01X00T47, Germany", "Chanote no. 12345"
what TEXT NOT NULL CHECK (what <> ''),
-- the contract it belongs to, if any
contract_id BIGINT REFERENCES contracts (id),
received_at TIMESTAMPTZ NOT NULL DEFAULT now(),
-- who handed it over, and who took it in
received_from TEXT NOT NULL CHECK (received_from <> ''),
received_by TEXT NOT NULL,
state TEXT NOT NULL DEFAULT 'held' CHECK (state IN ('held', 'out', 'returned')),
-- where it lies while held: "Office, safe, tray 2"
location TEXT NOT NULL DEFAULT '',
-- with whom while out ("Chiang Mai Immigration"), to whom once returned
holder TEXT NOT NULL DEFAULT '',
-- when it should be back (out), or go back to the client (held)
due_on DATE,
moved_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT originals_held_somewhere CHECK (state <> 'held' OR location <> ''),
CONSTRAINT originals_out_with_someone CHECK (state = 'held' OR holder <> '')
);
CREATE INDEX originals_client ON originals (client_id) WHERE client_id IS NOT NULL;
CREATE INDEX originals_person ON originals (person_id) WHERE person_id IS NOT NULL;
CREATE INDEX originals_open ON originals (state) WHERE state <> 'returned';
-- An original stays on record. What it is, whose it is and how it came
-- never change; where it is does.
CREATE FUNCTION originals_fixed() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
RAISE EXCEPTION 'an original stays on record: mark it returned';
END IF;
IF NEW.code <> OLD.code
OR NEW.client_id IS DISTINCT FROM OLD.client_id
OR NEW.person_id IS DISTINCT FROM OLD.person_id
OR NEW.kind <> OLD.kind OR NEW.what <> OLD.what
OR NEW.contract_id IS DISTINCT FROM OLD.contract_id
OR NEW.received_at <> OLD.received_at
OR NEW.received_from <> OLD.received_from
OR NEW.received_by <> OLD.received_by THEN
RAISE EXCEPTION 'what an original is, whose it is and how it came never change';
END IF;
RETURN NEW;
END
$$;
CREATE TRIGGER originals_no_change
BEFORE UPDATE OR DELETE ON originals
FOR EACH ROW EXECUTE FUNCTION originals_fixed();
ALTER TABLE audit_log DROP CONSTRAINT audit_log_entity_check;
ALTER TABLE audit_log ADD CONSTRAINT audit_log_entity_check CHECK (
entity IN (
'person', 'passport', 'address', 'session', 'company', 'visa', 'arrival',
'extension', 'reentry-permit', 'report', 'work-permit', 'document', 'form', 'link',
'agreement', 'signature', 'lawyer', 'client', 'contract', 'contract-version',
'paper-copy', 'contract-party', 'original'
)
);