217 lines
8.2 KiB
PL/PgSQL
217 lines
8.2 KiB
PL/PgSQL
-- +goose Up
|
|
-- The candidate's view and decline of a delivered invitation. The view moves
|
|
-- `delivered` to `viewed` and keeps the deadline. The decline moves
|
|
-- `delivered`, `viewed`, or the planned `counter` to the terminal `declined`
|
|
-- and stores the structured reason and the optional comment in their own
|
|
-- table, which only the candidate reads back. Neither creates a consent or a
|
|
-- contact grant. Each appends an audit event that names the version and the
|
|
-- actor and holds no reason or comment.
|
|
ALTER TABLE invitation_events
|
|
DROP CONSTRAINT invitation_events_kind_known;
|
|
|
|
ALTER TABLE invitation_events
|
|
ADD CONSTRAINT invitation_events_kind_known CHECK (
|
|
kind IN ('draft_created', 'draft_updated', 'delivered', 'viewed', 'declined')
|
|
);
|
|
|
|
ALTER TABLE invitation_events
|
|
ADD CONSTRAINT invitation_events_answer_changes_no_field CHECK (
|
|
kind NOT IN ('viewed', 'declined') OR cardinality(changed_fields) = 0
|
|
);
|
|
|
|
-- A viewed invitation records its view, a declined one its answer, and
|
|
-- neither precedes the delivery. A delivered invitation has neither yet.
|
|
ALTER TABLE invitations
|
|
ADD CONSTRAINT invitations_answer_matches_state CHECK (
|
|
(state <> 'delivered' OR (viewed_at IS NULL AND responded_at IS NULL))
|
|
AND (state <> 'viewed' OR (viewed_at IS NOT NULL AND responded_at IS NULL))
|
|
AND (state <> 'declined' OR responded_at IS NOT NULL)
|
|
AND (viewed_at IS NULL OR (delivered_at IS NOT NULL AND viewed_at >= delivered_at))
|
|
AND (
|
|
responded_at IS NULL
|
|
OR (delivered_at IS NOT NULL AND responded_at >= delivered_at)
|
|
)
|
|
);
|
|
|
|
-- The structured decline of one invitation. The organization never reads
|
|
-- this table: a reason such as `salary` would tell it that the hidden minimum
|
|
-- is above the range. The comment is the candidate's private text.
|
|
CREATE TABLE invitation_declines (
|
|
invitation_id uuid NOT NULL,
|
|
organization_id uuid NOT NULL,
|
|
reason text NOT NULL,
|
|
comment text,
|
|
declined_at timestamptz NOT NULL,
|
|
CONSTRAINT invitation_declines_pkey PRIMARY KEY (invitation_id),
|
|
CONSTRAINT invitation_declines_invitation_fkey FOREIGN KEY (
|
|
organization_id, invitation_id
|
|
) REFERENCES invitations (organization_id, id) ON DELETE RESTRICT,
|
|
CONSTRAINT invitation_declines_reason_known CHECK (
|
|
reason IN (
|
|
'salary', 'tasks', 'stack', 'work_format', 'package', 'company',
|
|
'not_looking', 'other_offer', 'other'
|
|
)
|
|
),
|
|
-- The contract bound of InvitationDeclineComment: 1 to 500 characters,
|
|
-- at least one of them not white space.
|
|
CONSTRAINT invitation_declines_comment_bounded CHECK (
|
|
comment IS NULL
|
|
OR (
|
|
char_length(comment) BETWEEN 1 AND 500
|
|
AND comment ~ '[^[:space:]]'
|
|
)
|
|
)
|
|
);
|
|
|
|
-- Outcome of an idempotent decline, kept beside the generic idempotency
|
|
-- record and removed with it after the 24-hour replay window. It holds no
|
|
-- copy of the response: a declined invitation and its decline never change,
|
|
-- so a replay rebuilds the same answer, and the comment is stored once. The
|
|
-- invitation is intentionally not a foreign key: the outcome of a missing
|
|
-- invitation names an identifier that does not exist.
|
|
CREATE TABLE invitation_decline_results (
|
|
scope_hash bytea NOT NULL,
|
|
invitation_id uuid NOT NULL,
|
|
outcome text NOT NULL,
|
|
CONSTRAINT invitation_decline_results_pkey PRIMARY KEY (scope_hash),
|
|
CONSTRAINT invitation_decline_results_idempotency_fkey FOREIGN KEY (
|
|
scope_hash
|
|
) REFERENCES idempotency_records (scope_hash) ON DELETE CASCADE,
|
|
CONSTRAINT invitation_decline_results_scope_hash_sha256 CHECK (
|
|
octet_length(scope_hash) = 32
|
|
),
|
|
CONSTRAINT invitation_decline_results_outcome CHECK (
|
|
outcome IN ('declined', 'not_found', 'state_conflict')
|
|
)
|
|
);
|
|
|
|
-- A view and an answer are never undone or moved, and a declined invitation
|
|
-- never leaves its terminal state.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION invitations_guard_answer() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF (OLD.viewed_at IS NOT NULL AND NEW.viewed_at IS DISTINCT FROM OLD.viewed_at)
|
|
OR (OLD.responded_at IS NOT NULL AND NEW.responded_at IS DISTINCT FROM OLD.responded_at)
|
|
OR (OLD.state = 'declined' AND NEW.state <> 'declined') THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'invitations_answer_immutable',
|
|
TABLE = 'invitations',
|
|
MESSAGE = 'a view, an answer, and a decline are never undone';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER invitations_guard_answer
|
|
BEFORE UPDATE ON invitations
|
|
FOR EACH ROW EXECUTE FUNCTION invitations_guard_answer();
|
|
|
|
-- A decline row describes the declined invitation it belongs to, answered at
|
|
-- the moment of the decline.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION invitation_declines_match_invitation() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1
|
|
FROM invitations
|
|
WHERE
|
|
invitations.organization_id = NEW.organization_id
|
|
AND invitations.id = NEW.invitation_id
|
|
AND invitations.state = 'declined'
|
|
AND invitations.responded_at = NEW.declined_at
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'invitation_declines_match_invitation',
|
|
TABLE = 'invitation_declines',
|
|
MESSAGE = 'a decline belongs to a declined invitation';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER invitation_declines_match_invitation
|
|
BEFORE INSERT ON invitation_declines
|
|
FOR EACH ROW EXECUTE FUNCTION invitation_declines_match_invitation();
|
|
|
|
CREATE TRIGGER invitation_declines_immutable
|
|
BEFORE UPDATE OR DELETE ON invitation_declines
|
|
FOR EACH ROW EXECUTE FUNCTION invitations_refuse_history_change();
|
|
|
|
-- Every declined invitation has its decline by the end of the transaction
|
|
-- that declined it.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION invitations_require_decline() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1
|
|
FROM invitation_declines
|
|
WHERE
|
|
invitation_declines.organization_id = NEW.organization_id
|
|
AND invitation_declines.invitation_id = NEW.id
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'invitations_decline_recorded',
|
|
TABLE = 'invitations',
|
|
MESSAGE = 'a declined invitation records its decline';
|
|
END IF;
|
|
RETURN NULL;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE CONSTRAINT TRIGGER invitations_require_decline
|
|
AFTER UPDATE ON invitations
|
|
DEFERRABLE INITIALLY DEFERRED
|
|
FOR EACH ROW
|
|
WHEN (new.state = 'declined' AND old.state <> 'declined')
|
|
EXECUTE FUNCTION invitations_require_decline();
|
|
|
|
-- +goose Down
|
|
-- A rollback must not silently remove a view, a decline, or a stored outcome.
|
|
-- +goose StatementBegin
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM invitation_events WHERE kind IN ('viewed', 'declined'))
|
|
OR EXISTS (SELECT 1 FROM invitations WHERE viewed_at IS NOT NULL OR state = 'declined')
|
|
OR EXISTS (SELECT 1 FROM invitation_declines)
|
|
OR EXISTS (SELECT 1 FROM invitation_decline_results) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'invitation_answers_retained',
|
|
TABLE = 'invitation_events',
|
|
MESSAGE = 'viewed and declined invitations must be resolved before rollback';
|
|
END IF;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
DROP TRIGGER invitations_require_decline ON invitations;
|
|
DROP TRIGGER invitations_guard_answer ON invitations;
|
|
DROP TABLE invitation_decline_results;
|
|
DROP TABLE invitation_declines;
|
|
DROP FUNCTION invitations_require_decline();
|
|
DROP FUNCTION invitation_declines_match_invitation();
|
|
DROP FUNCTION invitations_guard_answer();
|
|
|
|
ALTER TABLE invitations
|
|
DROP CONSTRAINT invitations_answer_matches_state;
|
|
|
|
ALTER TABLE invitation_events
|
|
DROP CONSTRAINT invitation_events_answer_changes_no_field;
|
|
|
|
ALTER TABLE invitation_events
|
|
DROP CONSTRAINT invitation_events_kind_known;
|
|
|
|
ALTER TABLE invitation_events
|
|
ADD CONSTRAINT invitation_events_kind_known CHECK (
|
|
kind IN ('draft_created', 'draft_updated', 'delivered')
|
|
);
|