feat(invitations): let candidates read, view and decline their invitations
This commit is contained in:
1 parent
c410636b7f
commit
ac57c05aca
27 files changed
+3285
-21
No files matched your search
@@ -0,0 +1,216 @@
|
||||
-- +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')
|
||||
);
|
||||
@@ -0,0 +1,129 @@
|
||||
-- Read one delivered invitation addressed to the candidate with the current
|
||||
-- name of the organization that sent it. A draft and an invitation of
|
||||
-- another candidate find nothing.
|
||||
-- name: ReadCandidateInvitationEntry :one
|
||||
SELECT
|
||||
invitation.id,
|
||||
invitation.organization_id,
|
||||
invitation.candidate_id,
|
||||
invitation.need_id,
|
||||
invitation.need_requirements_version,
|
||||
invitation.state,
|
||||
invitation.expiry_reason,
|
||||
invitation.version,
|
||||
invitation.terms_version,
|
||||
invitation.response_period_days,
|
||||
invitation.policy_version_id,
|
||||
invitation.response_deadline_at,
|
||||
invitation.salary_raise_used,
|
||||
invitation.created_by,
|
||||
invitation.created_at,
|
||||
invitation.updated_at,
|
||||
invitation.delivered_at,
|
||||
invitation.viewed_at,
|
||||
invitation.responded_at,
|
||||
terms.terms::text AS terms,
|
||||
organization.name AS organization_name
|
||||
FROM invitations AS invitation
|
||||
INNER JOIN invitation_terms_versions AS terms
|
||||
ON terms.invitation_id = invitation.id
|
||||
AND terms.version = invitation.terms_version
|
||||
INNER JOIN organizations AS organization
|
||||
ON organization.id = invitation.organization_id
|
||||
WHERE
|
||||
invitation.candidate_id = sqlc.arg(candidate_id)
|
||||
AND invitation.id = sqlc.arg(invitation_id)
|
||||
AND invitation.delivered_at IS NOT NULL;
|
||||
|
||||
-- Lock one delivered invitation addressed to the candidate. Only the
|
||||
-- invitation row is locked, never the organization.
|
||||
-- name: LockCandidateInvitation :one
|
||||
SELECT id
|
||||
FROM invitations
|
||||
WHERE
|
||||
candidate_id = sqlc.arg(candidate_id)
|
||||
AND id = sqlc.arg(invitation_id)
|
||||
AND delivered_at IS NOT NULL
|
||||
FOR UPDATE;
|
||||
|
||||
-- The compare-and-swap of the first view: it applies only to a delivered
|
||||
-- invitation at the version the caller read and before its deadline, and it
|
||||
-- keeps the deadline.
|
||||
-- name: RecordInvitationView :one
|
||||
UPDATE invitations
|
||||
SET
|
||||
state = 'viewed',
|
||||
version = sqlc.arg(next_version),
|
||||
viewed_at = sqlc.arg(viewed_at),
|
||||
updated_at = sqlc.arg(viewed_at)
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND id = sqlc.arg(invitation_id)
|
||||
AND state = 'delivered'
|
||||
AND version = sqlc.arg(expected_version)
|
||||
AND response_deadline_at > sqlc.arg(viewed_at)
|
||||
AND sqlc.arg(next_version)::bigint = sqlc.arg(expected_version)::bigint + 1
|
||||
RETURNING id;
|
||||
|
||||
-- The compare-and-swap of a decline: it applies only to an answerable
|
||||
-- invitation at the version the caller read and before its deadline.
|
||||
-- name: DeclineInvitation :one
|
||||
UPDATE invitations
|
||||
SET
|
||||
state = 'declined',
|
||||
version = sqlc.arg(next_version),
|
||||
responded_at = sqlc.arg(declined_at),
|
||||
updated_at = sqlc.arg(declined_at)
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND id = sqlc.arg(invitation_id)
|
||||
AND state IN ('delivered', 'viewed', 'counter')
|
||||
AND version = sqlc.arg(expected_version)
|
||||
AND response_deadline_at > sqlc.arg(declined_at)
|
||||
AND sqlc.arg(next_version)::bigint = sqlc.arg(expected_version)::bigint + 1
|
||||
RETURNING id;
|
||||
|
||||
-- name: InsertInvitationDecline :exec
|
||||
INSERT INTO invitation_declines (
|
||||
invitation_id,
|
||||
organization_id,
|
||||
reason,
|
||||
comment,
|
||||
declined_at
|
||||
) VALUES (
|
||||
sqlc.arg(invitation_id),
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(reason),
|
||||
sqlc.narg(comment),
|
||||
sqlc.arg(declined_at)
|
||||
);
|
||||
|
||||
-- name: ReadInvitationDecline :one
|
||||
SELECT
|
||||
invitation_id,
|
||||
organization_id,
|
||||
reason,
|
||||
comment,
|
||||
declined_at
|
||||
FROM invitation_declines
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND invitation_id = sqlc.arg(invitation_id);
|
||||
|
||||
-- name: InsertInvitationDeclineResult :exec
|
||||
INSERT INTO invitation_decline_results (
|
||||
scope_hash,
|
||||
invitation_id,
|
||||
outcome
|
||||
) VALUES (
|
||||
sqlc.arg(scope_hash),
|
||||
sqlc.arg(invitation_id),
|
||||
sqlc.arg(outcome)
|
||||
);
|
||||
|
||||
-- name: GetInvitationDeclineResult :one
|
||||
SELECT
|
||||
invitation_id,
|
||||
outcome
|
||||
FROM invitation_decline_results
|
||||
WHERE scope_hash = sqlc.arg(scope_hash);
|
||||
Reference in new issue
Block a user