Lanna Desk's larger work is contracts, general ones and artists', for clients who are Thai or foreign, people or organizations, with several lawyers in the team. The back office gets a Legal section (phase 9 in docs/PLAN.md): - Lawyers: license number, contact, the login of one who uses the app (then "My contracts"), in the team or left. - Clients, as record cards: person or organization, names in English and Thai, stage name or brand, a Thai ID number (checked), a passport or a registration number, the lawyer in charge. - Contracts: code LD-<year>-<n>, the general kinds and the artist kinds (recording, management, publishing, performance, license, merchandise), other party, status, lawyer, dates and value; a contract in force shows its notice day and its end ahead. Filters: general or artists, a lawyer, mine, those over. - Versions: a text kept here, a file (PDF or scan, encrypted as the client's document) or both. They never change: the database refuses UPDATE and DELETE, a version's file cannot be deleted and no document's checksum can change. Any version reads against the one before as a line diff. - Paper copies: the id <code>-P<n> to write on the paper or print as a 90 x 50 mm label with a QR code; where it lies, its state and holder, each move journaled; found again by its id. - The journal: every change in words, in the change's transaction, each entry chained to the one before by SHA-256. The database refuses to change, delete or truncate it; /journal checks the chain and every version's text and fingerprints against its entry, shows the last hash and exports CSV. The sidebar groups its sections: Legal, Visas and stays, Office. Demo data (seed-demo) adds three lawyers, six clients and seven contracts with versions, papers and moves. Documents take a third owner, the client (migration 0009). Checks: fmt, clippy, 115 unit and 62 integration tests (new: versions and the journal, the database refusing changes, a change around the app breaking the chain, paper ids and moves, the demo checking whole); screenshots of the new pages in light, dark and on a phone.
225 lines
9.3 KiB
PL/PgSQL
225 lines
9.3 KiB
PL/PgSQL
-- Lanna Desk, phase 9: the legal practice (docs/PLAN.md "Phase 9").
|
|
--
|
|
-- Clients are people or organizations, Thai or foreign: an artist, a label,
|
|
-- a festival, a company, someone buying land. Lawyers are the team; each
|
|
-- client and each contract has a lawyer in charge. A contract (a general
|
|
-- contract, or one of the artist contracts) has versions, and a version's
|
|
-- text and file never change once added (the table refuses it): a new
|
|
-- text is a new version. The paper copies of a contract each carry an id,
|
|
-- written on the paper, and the record of where they lie.
|
|
--
|
|
-- Every change to these tables is also written to the journal: a ledger
|
|
-- whose entries chain by SHA-256, each naming the hash of the one before,
|
|
-- so an entry changed or taken out later breaks the chain where it was.
|
|
-- The database refuses to change or delete journal entries.
|
|
|
|
CREATE TABLE lawyers (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
name TEXT NOT NULL CHECK (name <> ''),
|
|
name_th TEXT NOT NULL DEFAULT '',
|
|
-- the Lawyers Council of Thailand license number
|
|
license_no TEXT NOT NULL DEFAULT '',
|
|
email TEXT NOT NULL DEFAULT '',
|
|
phone TEXT NOT NULL DEFAULT '',
|
|
-- the id.cnx.network login of a lawyer who uses the app: "My contracts"
|
|
login TEXT NOT NULL DEFAULT '',
|
|
-- for the list of lawyers to choose from; leaving keeps the history
|
|
status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'left')),
|
|
notes TEXT NOT NULL DEFAULT '',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE clients (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
kind TEXT NOT NULL CHECK (kind IN ('person', 'organization')),
|
|
name TEXT NOT NULL CHECK (name <> ''),
|
|
name_th TEXT NOT NULL DEFAULT '',
|
|
-- an artist's stage name, a label's brand
|
|
known_as TEXT NOT NULL DEFAULT '',
|
|
nationality TEXT NOT NULL DEFAULT '',
|
|
-- a Thai person's 13-digit ID number, a foreigner's passport number,
|
|
-- an organization's registration (juristic person) number
|
|
thai_id TEXT NOT NULL DEFAULT '',
|
|
passport_no TEXT NOT NULL DEFAULT '',
|
|
registration_no TEXT NOT NULL DEFAULT '',
|
|
-- who speaks for an organization
|
|
contact_person TEXT NOT NULL DEFAULT '',
|
|
email TEXT NOT NULL DEFAULT '',
|
|
phone TEXT NOT NULL DEFAULT '',
|
|
address TEXT NOT NULL DEFAULT '',
|
|
lawyer_id BIGINT REFERENCES lawyers (id),
|
|
status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'former')),
|
|
notes TEXT NOT NULL DEFAULT '',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
-- The numbers of the contract codes: they run on across the years.
|
|
CREATE SEQUENCE contract_codes;
|
|
|
|
CREATE TABLE contracts (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
-- LD-2026-0007, the year it was opened in Chiang Mai: on every page,
|
|
-- label and paper copy of the contract
|
|
code TEXT NOT NULL UNIQUE CHECK (code ~ '^LD-[0-9]{4}-[0-9]{4,}$') DEFAULT (
|
|
'LD-' || to_char(now() AT TIME ZONE 'Asia/Bangkok', 'YYYY') || '-'
|
|
|| lpad(nextval('contract_codes')::text, 4, '0')
|
|
),
|
|
client_id BIGINT NOT NULL REFERENCES clients (id),
|
|
title TEXT NOT NULL CHECK (title <> ''),
|
|
kind TEXT NOT NULL,
|
|
-- the other side: a venue, a label, an employer, a buyer
|
|
counterparty TEXT NOT NULL DEFAULT '',
|
|
status TEXT NOT NULL DEFAULT 'draft',
|
|
lawyer_id BIGINT REFERENCES lawyers (id),
|
|
signed_on DATE,
|
|
starts_on DATE,
|
|
ends_on DATE,
|
|
-- the day by which either side must give notice, if it renews by itself
|
|
notice_by DATE,
|
|
value_thb BIGINT,
|
|
notes TEXT NOT NULL DEFAULT '',
|
|
created_by TEXT NOT NULL DEFAULT '',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX contracts_client ON contracts (client_id);
|
|
|
|
-- The contract files are documents of the client, so they are encrypted
|
|
-- in the store like every other document.
|
|
ALTER TABLE documents ADD COLUMN client_id BIGINT REFERENCES clients (id);
|
|
ALTER TABLE documents DROP CONSTRAINT documents_check;
|
|
ALTER TABLE documents ADD CONSTRAINT documents_one_owner
|
|
CHECK (num_nonnulls(person_id, company_id, client_id) = 1);
|
|
CREATE INDEX documents_client ON documents (client_id) WHERE client_id IS NOT NULL;
|
|
|
|
-- A document's file never changes, whoever owns it: the checksum stays the
|
|
-- file's.
|
|
CREATE FUNCTION documents_file_unchanged() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF NEW.sha256 <> OLD.sha256 OR NEW.size_bytes <> OLD.size_bytes
|
|
OR NEW.content_type <> OLD.content_type THEN
|
|
RAISE EXCEPTION 'a document''s file never changes: upload a new one';
|
|
END IF;
|
|
RETURN NEW;
|
|
END
|
|
$$;
|
|
CREATE TRIGGER documents_file_no_change
|
|
BEFORE UPDATE ON documents
|
|
FOR EACH ROW EXECUTE FUNCTION documents_file_unchanged();
|
|
|
|
CREATE TABLE contract_versions (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
contract_id BIGINT NOT NULL REFERENCES contracts (id),
|
|
version INTEGER NOT NULL CHECK (version > 0),
|
|
-- what this version is: "first draft", "the label's changes", "signed"
|
|
note TEXT NOT NULL CHECK (note <> ''),
|
|
-- the text, when it is kept in the app (paragraphs, "# " headings, "- "
|
|
-- items, as in the agreements), and its SHA-256 as UTF-8
|
|
body TEXT NOT NULL DEFAULT '',
|
|
body_sha256 TEXT CHECK (body_sha256 ~ '^[0-9a-f]{64}$'),
|
|
-- the file: a draft as PDF, the scan of the signed paper. ON DELETE
|
|
-- is refused, so the file stays as long as the version.
|
|
document_id BIGINT REFERENCES documents (id),
|
|
file_sha256 TEXT CHECK (file_sha256 ~ '^[0-9a-f]{64}$'),
|
|
added_by TEXT NOT NULL,
|
|
added_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE (contract_id, version),
|
|
CHECK (body <> '' OR document_id IS NOT NULL),
|
|
CHECK ((body = '') = (body_sha256 IS NULL)),
|
|
CHECK ((document_id IS NULL) = (file_sha256 IS NULL))
|
|
);
|
|
|
|
CREATE FUNCTION contract_versions_unchanged() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION 'a contract version never changes: add a new version';
|
|
END
|
|
$$;
|
|
CREATE TRIGGER contract_versions_no_change
|
|
BEFORE UPDATE OR DELETE ON contract_versions
|
|
FOR EACH ROW EXECUTE FUNCTION contract_versions_unchanged();
|
|
|
|
CREATE TABLE paper_copies (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
-- LD-2026-0007-P1, written on the paper or on its label
|
|
code TEXT NOT NULL UNIQUE,
|
|
contract_id BIGINT NOT NULL REFERENCES contracts (id),
|
|
-- the version the paper is, when known
|
|
version_id BIGINT REFERENCES contract_versions (id),
|
|
-- "Signed original", "Certified copy" ...
|
|
what TEXT NOT NULL CHECK (what <> ''),
|
|
-- where it lies now, as anyone in the office finds it again:
|
|
-- "Office, cabinet A, drawer 2, folder Poy Records"
|
|
location TEXT NOT NULL CHECK (location <> ''),
|
|
state TEXT NOT NULL DEFAULT 'filed'
|
|
CHECK (state IN ('filed', 'out', 'handed-over', 'destroyed')),
|
|
-- who has it while it is out, or whom it was handed to
|
|
holder TEXT NOT NULL DEFAULT '',
|
|
created_by TEXT NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
moved_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX paper_copies_contract ON paper_copies (contract_id);
|
|
|
|
-- A paper's id, contract, version and description never change; where it
|
|
-- lies does, and every move is in the journal.
|
|
CREATE FUNCTION paper_copies_fixed() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF TG_OP = 'DELETE' THEN
|
|
RAISE EXCEPTION 'a paper copy stays on record: mark it handed over or destroyed';
|
|
END IF;
|
|
IF NEW.code <> OLD.code OR NEW.contract_id <> OLD.contract_id
|
|
OR NEW.version_id IS DISTINCT FROM OLD.version_id OR NEW.what <> OLD.what THEN
|
|
RAISE EXCEPTION 'a paper copy''s id, contract, version and description never change';
|
|
END IF;
|
|
RETURN NEW;
|
|
END
|
|
$$;
|
|
CREATE TRIGGER paper_copies_no_change
|
|
BEFORE UPDATE OR DELETE ON paper_copies
|
|
FOR EACH ROW EXECUTE FUNCTION paper_copies_fixed();
|
|
|
|
CREATE TABLE journal (
|
|
-- the place in the chain, from 1 without gaps
|
|
seq BIGINT PRIMARY KEY CHECK (seq > 0),
|
|
at TIMESTAMPTZ NOT NULL,
|
|
actor TEXT NOT NULL,
|
|
client_id BIGINT REFERENCES clients (id),
|
|
contract_id BIGINT REFERENCES contracts (id),
|
|
event TEXT NOT NULL CHECK (event <> ''),
|
|
-- in words, with the fingerprints of what was added
|
|
detail TEXT NOT NULL,
|
|
-- the hash of entry seq - 1 (64 zeros for the first) and of this entry
|
|
prev_hash TEXT NOT NULL CHECK (prev_hash ~ '^[0-9a-f]{64}$'),
|
|
hash TEXT NOT NULL UNIQUE CHECK (hash ~ '^[0-9a-f]{64}$')
|
|
);
|
|
CREATE INDEX journal_client ON journal (client_id) WHERE client_id IS NOT NULL;
|
|
CREATE INDEX journal_contract ON journal (contract_id) WHERE contract_id IS NOT NULL;
|
|
|
|
CREATE FUNCTION journal_append_only() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION 'the journal is append-only';
|
|
END
|
|
$$;
|
|
CREATE TRIGGER journal_no_change
|
|
BEFORE UPDATE OR DELETE ON journal
|
|
FOR EACH ROW EXECUTE FUNCTION journal_append_only();
|
|
CREATE TRIGGER journal_no_truncate
|
|
BEFORE TRUNCATE ON journal
|
|
FOR EACH STATEMENT EXECUTE FUNCTION journal_append_only();
|
|
|
|
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'
|
|
)
|
|
);
|