Until a contract is set, it is one open text for every side, as Alex asked: our team edits it in the app, each other party on its link. Edits are tracked changes in the author's color (crossed out, added, highlighted with a comment) until the other side accepts or rejects them. When no change or highlight is open and Lanna Desk and every party agree, the text becomes a contract version that never changes, and the draft locks. - One side edits at a time: Edit takes the draft for 45 seconds, which every save renews; Done editing, leaving the page or silence gives it up. The editor saves 0.7 s after the last keystroke; the other pages ask for news every 3 s, so each side sees edits, who edits, agreements and the lock without reloading. - A save may only touch the saver's own marks (reviews::check_save); accept, reject (or withdraw) and resolve are their own steps, journaled with the text concerned. A later change voids the agreements given. - Notices: the other side's edits (one a half hour of editing), settled changes, agreements, the lock. The bell in the top bar counts the unseen (every 15 s); Notices lists them and marks them seen. - The draft as a diff: the standing text against every open change taken. - Migration 0015: contract_drafts, draft_saves and draft_agreements (both kept unchanged by a trigger), notices, notice_reads; drops step 1's review_rounds (only the demo held any). The review links stay as each party's way in. - Property tests for the save rule and settling, the fuzz text target runs the parser, the save check and settling; integration tests play both sides to the lock. The demo's recording agreement has an open draft with the label's changes and a notice. - PLAN: phase 13 rewritten; step 3 is the PDF and signing.
125 lines
4.8 KiB
PL/PgSQL
125 lines
4.8 KiB
PL/PgSQL
-- The shared draft (phase 13, step 2): one draft a contract, open for edits
|
|
-- by our team and the other parties until all agree, then locked as a
|
|
-- contract version. It replaces the review rounds of step 1, which only
|
|
-- the demo ever held: the links stay, as each party's way in.
|
|
DROP TRIGGER review_rounds_no_change ON review_rounds;
|
|
DROP FUNCTION review_rounds_kept();
|
|
DROP TABLE review_rounds;
|
|
|
|
CREATE TABLE contract_drafts (
|
|
contract_id BIGINT PRIMARY KEY REFERENCES contracts (id),
|
|
-- the version the draft started from
|
|
base_version_id BIGINT NOT NULL REFERENCES contract_versions (id),
|
|
-- the text with its tracked changes: [[op, text, comment, side], ...]
|
|
pieces JSONB NOT NULL,
|
|
-- counts every save, accepted or rejected change and resolved highlight
|
|
revision INTEGER NOT NULL DEFAULT 1 CHECK (revision > 0),
|
|
state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'locked')),
|
|
-- who edits now: "us:<login>" or "party:<id>", until the lease ends
|
|
editor TEXT,
|
|
editor_name TEXT,
|
|
editor_until TIMESTAMPTZ,
|
|
opened_by TEXT NOT NULL,
|
|
opened_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
changed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
locked_at TIMESTAMPTZ,
|
|
locked_version_id BIGINT REFERENCES contract_versions (id),
|
|
CHECK ((editor IS NULL) = (editor_until IS NULL)),
|
|
CHECK ((state = 'locked') = (locked_at IS NOT NULL)),
|
|
CHECK ((state = 'locked') = (locked_version_id IS NOT NULL))
|
|
);
|
|
|
|
-- Every change to a draft, by whom: kept, never changed.
|
|
CREATE TABLE draft_saves (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
contract_id BIGINT NOT NULL REFERENCES contracts (id),
|
|
revision INTEGER NOT NULL,
|
|
-- "us" or "party:<id>"
|
|
side TEXT NOT NULL,
|
|
-- the staff login, or the name the party gave
|
|
actor TEXT NOT NULL,
|
|
kind TEXT NOT NULL CHECK (kind IN ('opened', 'edit', 'accept', 'reject', 'resolve')),
|
|
-- SHA-256 of the pieces after the change
|
|
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
|
at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE (contract_id, revision)
|
|
);
|
|
|
|
-- A side's agreement to the text at one revision; a later change voids it.
|
|
CREATE TABLE draft_agreements (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
contract_id BIGINT NOT NULL REFERENCES contracts (id),
|
|
revision INTEGER NOT NULL,
|
|
side TEXT NOT NULL,
|
|
actor TEXT NOT NULL,
|
|
-- SHA-256 of the text agreed to
|
|
text_sha256 TEXT NOT NULL CHECK (text_sha256 ~ '^[0-9a-f]{64}$'),
|
|
ip TEXT NOT NULL DEFAULT '',
|
|
user_agent TEXT NOT NULL DEFAULT '',
|
|
at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE (contract_id, revision, side)
|
|
);
|
|
|
|
CREATE FUNCTION drafts_kept() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION 'the history of a draft never changes';
|
|
END
|
|
$$;
|
|
CREATE TRIGGER draft_saves_no_change
|
|
BEFORE UPDATE OR DELETE ON draft_saves
|
|
FOR EACH ROW EXECUTE FUNCTION drafts_kept();
|
|
CREATE TRIGGER draft_agreements_no_change
|
|
BEFORE UPDATE OR DELETE ON draft_agreements
|
|
FOR EACH ROW EXECUTE FUNCTION drafts_kept();
|
|
|
|
-- A locked draft stays as it was locked.
|
|
CREATE FUNCTION contract_drafts_locked() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF TG_OP = 'DELETE' THEN
|
|
RAISE EXCEPTION 'a draft stays on record';
|
|
END IF;
|
|
IF OLD.state = 'locked' AND NEW.state = 'locked' AND NEW.pieces <> OLD.pieces THEN
|
|
RAISE EXCEPTION 'a locked draft never changes: open a new one';
|
|
END IF;
|
|
RETURN NEW;
|
|
END
|
|
$$;
|
|
CREATE TRIGGER contract_drafts_kept
|
|
BEFORE UPDATE OR DELETE ON contract_drafts
|
|
FOR EACH ROW EXECUTE FUNCTION contract_drafts_locked();
|
|
|
|
-- What the team should see: the other side's edits, agreements, locks.
|
|
CREATE TABLE notices (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
contract_id BIGINT REFERENCES contracts (id),
|
|
-- "edited", "accepted", "rejected", "agreed", "locked", "opened"
|
|
kind TEXT NOT NULL,
|
|
-- who did it, as shown: "Demo Records Co., Ltd. (Ploy Srisuk)"
|
|
actor TEXT NOT NULL,
|
|
-- the staff login that did it, so it is not news to them
|
|
by_login TEXT NOT NULL DEFAULT '',
|
|
text TEXT NOT NULL,
|
|
href TEXT NOT NULL
|
|
);
|
|
CREATE INDEX notices_contract ON notices (contract_id);
|
|
|
|
-- The newest notice each staff member has seen.
|
|
CREATE TABLE notice_reads (
|
|
login TEXT PRIMARY KEY,
|
|
seen_id BIGINT NOT NULL DEFAULT 0,
|
|
seen_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
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', 'review', 'draft'
|
|
)
|
|
);
|