-- 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' ) );