124 lines
4.1 KiB
SQL
124 lines
4.1 KiB
SQL
-- 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);
|