Files
visa/migrations/0014_reviews.sql
T
grabowski a4155548b8
CI / secret scan (push) Successful in 5s
CI / fmt, clippy, tests (push) Successful in 49s
CI / release (push) Skipped
The other side reviews a draft (phase 13, step 1)
A lawyer sends a contract version to one of its parties with a link. On
it the other side marks the text up in an editor that keeps every change
visible, as tracked changes do: deleting our text crosses it out in red,
typing is marked as added in green, and the highlighter marks a passage
in yellow with a comment. Restore and Undo take marks back. They send it
back once a round; each later version comes on the same link.

- Migration 0014: review_links (token kept as a SHA-256, one open link a
  party) and review_rounds (the reply written once: their pieces, text,
  SHA-256, name, message; a trigger refuses any other change and every
  delete). Journal entries "sent for review", "review returned" (with the
  fingerprint of their text) and "review link closed"; audit entity
  "review".
- The reply is a list of pieces (same, ins, del, mark). The server checks
  that the pieces without the added ones are exactly the version sent, so
  nothing changes unseen (reviews::check).
- Our review page: their message, the changes numbered with the words
  around them and a box each, the highlights with comments, a git-style
  diff (line numbers, - and + lines, the changed words marked, three lines
  of context per hunk) and their markup. "Take the ticked changes" opens
  the contract page with the version form filled with our text plus those
  changes.
- static/review-editor.js: edits work on the pieces and redraw, so our
  text cannot vanish; keyboard compositions and edits the browser will not
  cancel are compared with the text and taken as edits.
- Property tests (any markup is accepted and taken back exactly; markup
  that changes our text unseen is refused), the fuzz text target runs
  reviews::check, integration tests, and the demo has the label's review
  of the recording agreement.
- PLAN: phase 13.
2026-11-23 15:31:43 +07:00

91 lines
3.5 KiB
PL/PgSQL

-- The other side reviews a draft (phase 13). A lawyer sends a version with
-- a text to one of the contract's parties: a link for that party (one open
-- at a time) and a round for the version. The other side marks the text up
-- (crossed out, added, highlighted) and sends it back once per round.
CREATE TABLE review_links (
id BIGSERIAL PRIMARY KEY,
contract_id BIGINT NOT NULL REFERENCES contracts (id),
party_id BIGINT NOT NULL REFERENCES contract_parties (id),
-- SHA-256 of the token in the link, as for the client links
token_hash BYTEA NOT NULL UNIQUE,
csrf TEXT NOT NULL,
created_by TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ NOT NULL,
first_used_at TIMESTAMPTZ,
last_used_at TIMESTAMPTZ,
closed_at TIMESTAMPTZ,
closed_by TEXT,
CHECK ((closed_at IS NULL) = (closed_by IS NULL))
);
CREATE INDEX review_links_contract ON review_links (contract_id);
CREATE UNIQUE INDEX review_links_one_open ON review_links (party_id) WHERE closed_at IS NULL;
CREATE TABLE review_rounds (
id BIGSERIAL PRIMARY KEY,
link_id BIGINT NOT NULL REFERENCES review_links (id),
version_id BIGINT NOT NULL REFERENCES contract_versions (id),
-- what we wrote to them with this version
message TEXT NOT NULL DEFAULT '',
sent_by TEXT NOT NULL,
sent_at TIMESTAMPTZ NOT NULL DEFAULT now(),
-- their reply, written once
replied_at TIMESTAMPTZ,
reply_name TEXT,
reply_message TEXT,
-- the pieces: [["same"|"ins"|"del"|"mark", text, comment], ...]
reply_pieces JSONB,
-- their text: the pieces without the crossed-out ones
reply_body TEXT,
reply_sha256 TEXT CHECK (reply_sha256 ~ '^[0-9a-f]{64}$'),
reply_ip TEXT,
reply_user_agent TEXT,
CHECK (
(replied_at IS NULL) = (reply_name IS NULL)
AND (replied_at IS NULL) = (reply_pieces IS NULL)
AND (replied_at IS NULL) = (reply_body IS NULL)
AND (replied_at IS NULL) = (reply_sha256 IS NULL)
)
);
CREATE INDEX review_rounds_link ON review_rounds (link_id);
-- A round never changes, but for its reply, written once.
CREATE FUNCTION review_rounds_kept() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
RAISE EXCEPTION 'a review round stays on record';
END IF;
IF OLD.replied_at IS NOT NULL
OR NEW.link_id <> OLD.link_id OR NEW.version_id <> OLD.version_id
OR NEW.message <> OLD.message OR NEW.sent_by <> OLD.sent_by
OR NEW.sent_at <> OLD.sent_at THEN
RAISE EXCEPTION 'a review round never changes; its reply is written once';
END IF;
RETURN NEW;
END
$$;
CREATE TRIGGER review_rounds_no_change
BEFORE UPDATE OR DELETE ON review_rounds
FOR EACH ROW EXECUTE FUNCTION review_rounds_kept();
CREATE FUNCTION review_links_kept() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'a review link stays on record: close it';
END
$$;
CREATE TRIGGER review_links_no_delete
BEFORE DELETE ON review_links
FOR EACH ROW EXECUTE FUNCTION review_links_kept();
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'
)
);