-- 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:" or "party:", 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:" 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' ) );