feat(invitations): send invitations into the candidate inbox atomically
This commit is contained in:
1 parent
fc35e27af5
commit
1a067221f7
46 files changed
+3276
-113
No files matched your search
@@ -0,0 +1,117 @@
|
||||
-- +goose Up
|
||||
-- Sending an invitation. One transaction moves a draft to `delivered`, starts
|
||||
-- the response deadline, charges the organization quota, appends the audit
|
||||
-- event, and writes the outbox events of the notification. The send itself is
|
||||
-- never a stored state: the audit event of the step is `delivered`, and it
|
||||
-- changes none of the editable draft fields.
|
||||
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')
|
||||
);
|
||||
|
||||
ALTER TABLE invitation_events
|
||||
ADD CONSTRAINT invitation_events_delivery_changes_no_field CHECK (
|
||||
kind <> 'delivered' OR cardinality(changed_fields) = 0
|
||||
);
|
||||
|
||||
-- Invitation quota usage now names a stored invitation of the same
|
||||
-- organization, so a charge cannot exist without the invitation it paid for.
|
||||
-- Before sending existed nothing in the product wrote this table; a row that
|
||||
-- names no invitation is reported instead of being dropped.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM company_invitation_quota_usage AS usage
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM invitations
|
||||
WHERE
|
||||
invitations.organization_id = usage.organization_id
|
||||
AND invitations.id = usage.invitation_id
|
||||
)
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'foreign_key_violation',
|
||||
CONSTRAINT = 'company_invitation_quota_usage_invitation_fkey',
|
||||
TABLE = 'company_invitation_quota_usage',
|
||||
MESSAGE = 'invitation quota usage must name stored invitations';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
ALTER TABLE company_invitation_quota_usage
|
||||
ADD CONSTRAINT company_invitation_quota_usage_invitation_fkey
|
||||
FOREIGN KEY (organization_id, invitation_id)
|
||||
REFERENCES invitations (organization_id, id) ON DELETE RESTRICT;
|
||||
|
||||
-- Outcome of an idempotent send, kept beside the generic idempotency record
|
||||
-- and removed with it after the 24-hour replay window. A successful send
|
||||
-- keeps the original response; a missing invitation and a state conflict
|
||||
-- keep only the outcome, as the public contract stores them under the key.
|
||||
-- The invitation is intentionally not a foreign key: the outcome of a missing
|
||||
-- invitation names an identifier that does not exist.
|
||||
CREATE TABLE invitation_send_results (
|
||||
scope_hash bytea NOT NULL,
|
||||
organization_id uuid NOT NULL,
|
||||
invitation_id uuid NOT NULL,
|
||||
outcome text NOT NULL,
|
||||
snapshot jsonb,
|
||||
CONSTRAINT invitation_send_results_pkey PRIMARY KEY (scope_hash),
|
||||
CONSTRAINT invitation_send_results_idempotency_fkey FOREIGN KEY (
|
||||
scope_hash
|
||||
) REFERENCES idempotency_records (scope_hash) ON DELETE CASCADE,
|
||||
CONSTRAINT invitation_send_results_scope_hash_sha256 CHECK (
|
||||
octet_length(scope_hash) = 32
|
||||
),
|
||||
CONSTRAINT invitation_send_results_outcome CHECK (
|
||||
(
|
||||
outcome = 'sent'
|
||||
AND snapshot IS NOT NULL
|
||||
AND jsonb_typeof(snapshot) = 'object'
|
||||
AND octet_length(snapshot::text) <= 196608
|
||||
)
|
||||
OR (
|
||||
outcome IN ('not_found', 'state_conflict')
|
||||
AND snapshot IS NULL
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
-- A rollback must not silently remove delivery history or a stored outcome.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM invitation_events WHERE kind = 'delivered')
|
||||
OR EXISTS (SELECT 1 FROM invitation_send_results) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'invitation_deliveries_retained',
|
||||
TABLE = 'invitation_events',
|
||||
MESSAGE = 'sent invitations must be resolved before rollback';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
DROP TABLE invitation_send_results;
|
||||
|
||||
ALTER TABLE company_invitation_quota_usage
|
||||
DROP CONSTRAINT company_invitation_quota_usage_invitation_fkey;
|
||||
|
||||
ALTER TABLE invitation_events
|
||||
DROP CONSTRAINT invitation_events_delivery_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')
|
||||
);
|
||||
@@ -0,0 +1,123 @@
|
||||
-- The conditional update is the compare-and-swap of a send: it applies only to
|
||||
-- a draft whose version is still the one the caller read, records the first
|
||||
-- delivery with its deadline, and advances the version by one. No row is
|
||||
-- returned when it lost a race or the invitation is no longer a draft.
|
||||
-- name: DeliverInvitation :one
|
||||
UPDATE invitations
|
||||
SET
|
||||
state = 'delivered',
|
||||
version = sqlc.arg(next_version),
|
||||
delivered_at = sqlc.arg(delivered_at),
|
||||
response_deadline_at = sqlc.arg(response_deadline_at),
|
||||
updated_at = sqlc.arg(delivered_at)
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND id = sqlc.arg(invitation_id)
|
||||
AND state = 'draft'
|
||||
AND version = sqlc.arg(expected_version)
|
||||
AND sqlc.arg(next_version)::bigint = sqlc.arg(expected_version)::bigint + 1
|
||||
RETURNING id;
|
||||
|
||||
-- One bounded page of the candidate's inbox: every invitation delivered to
|
||||
-- the candidate, in any later state, newest first by delivery time. Drafts
|
||||
-- have no delivery and are never listed. The candidate scopes the rows, so
|
||||
-- no identifier of another candidate can appear.
|
||||
-- name: ListCandidateInvitations :many
|
||||
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.delivered_at IS NOT NULL
|
||||
AND (
|
||||
sqlc.narg(after_delivered_at)::timestamptz IS NULL
|
||||
OR (invitation.delivered_at, invitation.id) < (
|
||||
sqlc.narg(after_delivered_at)::timestamptz,
|
||||
sqlc.narg(after_invitation_id)::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY invitation.delivered_at DESC, invitation.id DESC
|
||||
LIMIT sqlc.arg(row_limit);
|
||||
|
||||
-- Read one delivered invitation addressed to the candidate. A draft and an
|
||||
-- invitation of another candidate find nothing.
|
||||
-- name: ReadCandidateInvitation :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
|
||||
FROM invitations AS invitation
|
||||
INNER JOIN invitation_terms_versions AS terms
|
||||
ON terms.invitation_id = invitation.id
|
||||
AND terms.version = invitation.terms_version
|
||||
WHERE
|
||||
invitation.candidate_id = sqlc.arg(candidate_id)
|
||||
AND invitation.id = sqlc.arg(invitation_id)
|
||||
AND invitation.delivered_at IS NOT NULL;
|
||||
|
||||
-- name: InsertInvitationSendResult :exec
|
||||
INSERT INTO invitation_send_results (
|
||||
scope_hash,
|
||||
organization_id,
|
||||
invitation_id,
|
||||
outcome,
|
||||
snapshot
|
||||
) VALUES (
|
||||
sqlc.arg(scope_hash),
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(invitation_id),
|
||||
sqlc.arg(outcome),
|
||||
sqlc.narg(snapshot)::jsonb
|
||||
);
|
||||
|
||||
-- name: GetInvitationSendResult :one
|
||||
SELECT
|
||||
organization_id,
|
||||
invitation_id,
|
||||
outcome,
|
||||
-- A refusal has no snapshot; the empty text stands for it.
|
||||
coalesce(snapshot::text, '')::text AS snapshot
|
||||
FROM invitation_send_results
|
||||
WHERE scope_hash = sqlc.arg(scope_hash);
|
||||
Reference in new issue
Block a user