225 lines
6.9 KiB
SQL
225 lines
6.9 KiB
SQL
-- The invitation row, its first terms version, and its first audit event are
|
|
-- inserted in one caller-owned transaction. The deferred reference to the
|
|
-- current terms version is checked when the transaction commits.
|
|
-- name: InsertInvitation :exec
|
|
INSERT INTO invitations (
|
|
id,
|
|
organization_id,
|
|
candidate_id,
|
|
need_id,
|
|
need_requirements_version,
|
|
state,
|
|
version,
|
|
terms_version,
|
|
response_period_days,
|
|
policy_version_id,
|
|
created_by,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(organization_id),
|
|
sqlc.arg(candidate_id),
|
|
sqlc.narg(need_id),
|
|
sqlc.narg(need_requirements_version),
|
|
sqlc.arg(state),
|
|
sqlc.arg(version),
|
|
sqlc.arg(terms_version),
|
|
sqlc.arg(response_period_days),
|
|
sqlc.arg(policy_version_id),
|
|
sqlc.arg(created_by),
|
|
sqlc.arg(created_at),
|
|
sqlc.arg(updated_at)
|
|
);
|
|
|
|
-- name: InsertInvitationTermsVersion :exec
|
|
INSERT INTO invitation_terms_versions (
|
|
invitation_id,
|
|
organization_id,
|
|
version,
|
|
terms,
|
|
salary_from,
|
|
salary_to,
|
|
salary_currency,
|
|
salary_period,
|
|
salary_tax_basis,
|
|
created_at
|
|
) VALUES (
|
|
sqlc.arg(invitation_id),
|
|
sqlc.arg(organization_id),
|
|
sqlc.arg(version),
|
|
sqlc.arg(terms)::jsonb,
|
|
sqlc.arg(salary_from),
|
|
sqlc.arg(salary_to),
|
|
sqlc.arg(salary_currency),
|
|
sqlc.arg(salary_period),
|
|
sqlc.arg(salary_tax_basis),
|
|
sqlc.arg(created_at)
|
|
);
|
|
|
|
-- name: InsertInvitationEvent :exec
|
|
INSERT INTO invitation_events (
|
|
id,
|
|
invitation_id,
|
|
organization_id,
|
|
version,
|
|
terms_version,
|
|
kind,
|
|
actor_id,
|
|
changed_fields,
|
|
occurred_at
|
|
) VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(invitation_id),
|
|
sqlc.arg(organization_id),
|
|
sqlc.arg(version),
|
|
sqlc.arg(terms_version),
|
|
sqlc.arg(kind),
|
|
sqlc.arg(actor_id),
|
|
sqlc.arg(changed_fields)::text [],
|
|
sqlc.arg(occurred_at)
|
|
);
|
|
|
|
-- Read one current invitation with its terms snapshot. Both the organization
|
|
-- and the invitation scope the row, so another organization's identifier
|
|
-- finds nothing.
|
|
-- name: ReadInvitation :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.organization_id = sqlc.arg(organization_id)
|
|
AND invitation.id = sqlc.arg(invitation_id);
|
|
|
|
-- Takes the row lock of one invitation. It is a statement of its own, ahead of
|
|
-- ReadInvitation, because a locking read that joins the terms table is
|
|
-- re-checked against the new row version after a wait and then pairs the
|
|
-- updated terms_version with the terms row it fetched before the wait, which
|
|
-- finds nothing. Once the lock is held, the following read sees the committed
|
|
-- state and no writer can change it.
|
|
-- name: LockInvitation :one
|
|
SELECT id
|
|
FROM invitations
|
|
WHERE
|
|
organization_id = sqlc.arg(organization_id)
|
|
AND id = sqlc.arg(invitation_id)
|
|
FOR UPDATE;
|
|
|
|
-- Read one historical terms version of one invitation of one organization.
|
|
-- name: ReadInvitationTermsVersion :one
|
|
SELECT terms::text AS terms
|
|
FROM invitation_terms_versions
|
|
WHERE
|
|
organization_id = sqlc.arg(organization_id)
|
|
AND invitation_id = sqlc.arg(invitation_id)
|
|
AND version = sqlc.arg(version);
|
|
|
|
-- One bounded newest-first page. Both the organization scope and the tuple
|
|
-- cursor are applied before the limit.
|
|
-- name: ListInvitations :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
|
|
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.organization_id = sqlc.arg(organization_id)
|
|
AND (
|
|
sqlc.narg(after_created_at)::timestamptz IS NULL
|
|
OR (invitation.created_at, invitation.id) < (
|
|
sqlc.narg(after_created_at)::timestamptz,
|
|
sqlc.narg(after_invitation_id)::uuid
|
|
)
|
|
)
|
|
ORDER BY invitation.created_at DESC, invitation.id DESC
|
|
LIMIT sqlc.arg(row_limit);
|
|
|
|
-- The conditional update is the compare-and-swap of a draft: it applies only
|
|
-- to a draft whose version is still the one the caller read, and advances the
|
|
-- version by one. No row is returned when it lost a race or the invitation
|
|
-- left the draft state.
|
|
-- name: AdvanceInvitationDraft :one
|
|
UPDATE invitations
|
|
SET
|
|
need_id = sqlc.narg(need_id),
|
|
need_requirements_version = sqlc.narg(need_requirements_version),
|
|
response_period_days = sqlc.arg(response_period_days),
|
|
policy_version_id = sqlc.arg(policy_version_id),
|
|
terms_version = sqlc.arg(terms_version),
|
|
version = sqlc.arg(next_version),
|
|
updated_at = sqlc.arg(updated_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;
|
|
|
|
-- A requirements version that a need of the organization has had is the
|
|
-- version number of a stored snapshot whose own requirements version is that
|
|
-- number, so a later edit that only renamed the need is not accepted.
|
|
-- name: InvitationNeedVersionExists :one
|
|
SELECT EXISTS (
|
|
SELECT 1
|
|
FROM organization_need_versions
|
|
WHERE
|
|
organization_id = sqlc.arg(organization_id)
|
|
AND need_id = sqlc.arg(need_id)
|
|
AND version = sqlc.arg(requirements_version)::bigint
|
|
AND snapshot ->> 'requirements_version' = sqlc.arg(requirements_version)::bigint::text
|
|
) AS found;
|
|
|
|
-- name: SaveInvitationCreationResult :exec
|
|
INSERT INTO invitation_creation_results (scope_hash, snapshot)
|
|
VALUES (sqlc.arg(scope_hash), sqlc.arg(snapshot)::jsonb)
|
|
ON CONFLICT (scope_hash) DO UPDATE
|
|
SET snapshot = excluded.snapshot;
|
|
|
|
-- name: GetInvitationCreationResult :one
|
|
SELECT snapshot::text AS snapshot
|
|
FROM invitation_creation_results
|
|
WHERE scope_hash = sqlc.arg(scope_hash);
|