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,336 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: invitation_delivery.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const deliverInvitation = `-- name: DeliverInvitation :one
|
||||
UPDATE invitations
|
||||
SET
|
||||
state = 'delivered',
|
||||
version = $1,
|
||||
delivered_at = $2,
|
||||
response_deadline_at = $3,
|
||||
updated_at = $2
|
||||
WHERE
|
||||
organization_id = $4
|
||||
AND id = $5
|
||||
AND state = 'draft'
|
||||
AND version = $6
|
||||
AND $1::bigint = $6::bigint + 1
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type DeliverInvitationParams struct {
|
||||
NextVersion int64
|
||||
DeliveredAt pgtype.Timestamptz
|
||||
ResponseDeadlineAt pgtype.Timestamptz
|
||||
OrganizationID pgtype.UUID
|
||||
InvitationID pgtype.UUID
|
||||
ExpectedVersion int64
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (q *Queries) DeliverInvitation(ctx context.Context, arg DeliverInvitationParams) (pgtype.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, deliverInvitation,
|
||||
arg.NextVersion,
|
||||
arg.DeliveredAt,
|
||||
arg.ResponseDeadlineAt,
|
||||
arg.OrganizationID,
|
||||
arg.InvitationID,
|
||||
arg.ExpectedVersion,
|
||||
)
|
||||
var id pgtype.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const getInvitationSendResult = `-- 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 = $1
|
||||
`
|
||||
|
||||
type GetInvitationSendResultRow struct {
|
||||
OrganizationID pgtype.UUID
|
||||
InvitationID pgtype.UUID
|
||||
Outcome string
|
||||
Snapshot string
|
||||
}
|
||||
|
||||
func (q *Queries) GetInvitationSendResult(ctx context.Context, scopeHash []byte) (GetInvitationSendResultRow, error) {
|
||||
row := q.db.QueryRow(ctx, getInvitationSendResult, scopeHash)
|
||||
var i GetInvitationSendResultRow
|
||||
err := row.Scan(
|
||||
&i.OrganizationID,
|
||||
&i.InvitationID,
|
||||
&i.Outcome,
|
||||
&i.Snapshot,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertInvitationSendResult = `-- name: InsertInvitationSendResult :exec
|
||||
INSERT INTO invitation_send_results (
|
||||
scope_hash,
|
||||
organization_id,
|
||||
invitation_id,
|
||||
outcome,
|
||||
snapshot
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5::jsonb
|
||||
)
|
||||
`
|
||||
|
||||
type InsertInvitationSendResultParams struct {
|
||||
ScopeHash []byte
|
||||
OrganizationID pgtype.UUID
|
||||
InvitationID pgtype.UUID
|
||||
Outcome string
|
||||
Snapshot []byte
|
||||
}
|
||||
|
||||
func (q *Queries) InsertInvitationSendResult(ctx context.Context, arg InsertInvitationSendResultParams) error {
|
||||
_, err := q.db.Exec(ctx, insertInvitationSendResult,
|
||||
arg.ScopeHash,
|
||||
arg.OrganizationID,
|
||||
arg.InvitationID,
|
||||
arg.Outcome,
|
||||
arg.Snapshot,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listCandidateInvitations = `-- 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 = $1
|
||||
AND invitation.delivered_at IS NOT NULL
|
||||
AND (
|
||||
$2::timestamptz IS NULL
|
||||
OR (invitation.delivered_at, invitation.id) < (
|
||||
$2::timestamptz,
|
||||
$3::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY invitation.delivered_at DESC, invitation.id DESC
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListCandidateInvitationsParams struct {
|
||||
CandidateID pgtype.UUID
|
||||
AfterDeliveredAt pgtype.Timestamptz
|
||||
AfterInvitationID pgtype.UUID
|
||||
RowLimit int32
|
||||
}
|
||||
|
||||
type ListCandidateInvitationsRow struct {
|
||||
ID pgtype.UUID
|
||||
OrganizationID pgtype.UUID
|
||||
CandidateID pgtype.UUID
|
||||
NeedID pgtype.UUID
|
||||
NeedRequirementsVersion pgtype.Int8
|
||||
State string
|
||||
ExpiryReason pgtype.Text
|
||||
Version int64
|
||||
TermsVersion int64
|
||||
ResponsePeriodDays int16
|
||||
PolicyVersionID pgtype.UUID
|
||||
ResponseDeadlineAt pgtype.Timestamptz
|
||||
SalaryRaiseUsed bool
|
||||
CreatedBy pgtype.UUID
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
DeliveredAt pgtype.Timestamptz
|
||||
ViewedAt pgtype.Timestamptz
|
||||
RespondedAt pgtype.Timestamptz
|
||||
Terms string
|
||||
OrganizationName string
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (q *Queries) ListCandidateInvitations(ctx context.Context, arg ListCandidateInvitationsParams) ([]ListCandidateInvitationsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listCandidateInvitations,
|
||||
arg.CandidateID,
|
||||
arg.AfterDeliveredAt,
|
||||
arg.AfterInvitationID,
|
||||
arg.RowLimit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListCandidateInvitationsRow
|
||||
for rows.Next() {
|
||||
var i ListCandidateInvitationsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.OrganizationID,
|
||||
&i.CandidateID,
|
||||
&i.NeedID,
|
||||
&i.NeedRequirementsVersion,
|
||||
&i.State,
|
||||
&i.ExpiryReason,
|
||||
&i.Version,
|
||||
&i.TermsVersion,
|
||||
&i.ResponsePeriodDays,
|
||||
&i.PolicyVersionID,
|
||||
&i.ResponseDeadlineAt,
|
||||
&i.SalaryRaiseUsed,
|
||||
&i.CreatedBy,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeliveredAt,
|
||||
&i.ViewedAt,
|
||||
&i.RespondedAt,
|
||||
&i.Terms,
|
||||
&i.OrganizationName,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const readCandidateInvitation = `-- 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 = $1
|
||||
AND invitation.id = $2
|
||||
AND invitation.delivered_at IS NOT NULL
|
||||
`
|
||||
|
||||
type ReadCandidateInvitationParams struct {
|
||||
CandidateID pgtype.UUID
|
||||
InvitationID pgtype.UUID
|
||||
}
|
||||
|
||||
type ReadCandidateInvitationRow struct {
|
||||
ID pgtype.UUID
|
||||
OrganizationID pgtype.UUID
|
||||
CandidateID pgtype.UUID
|
||||
NeedID pgtype.UUID
|
||||
NeedRequirementsVersion pgtype.Int8
|
||||
State string
|
||||
ExpiryReason pgtype.Text
|
||||
Version int64
|
||||
TermsVersion int64
|
||||
ResponsePeriodDays int16
|
||||
PolicyVersionID pgtype.UUID
|
||||
ResponseDeadlineAt pgtype.Timestamptz
|
||||
SalaryRaiseUsed bool
|
||||
CreatedBy pgtype.UUID
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
DeliveredAt pgtype.Timestamptz
|
||||
ViewedAt pgtype.Timestamptz
|
||||
RespondedAt pgtype.Timestamptz
|
||||
Terms string
|
||||
}
|
||||
|
||||
// Read one delivered invitation addressed to the candidate. A draft and an
|
||||
// invitation of another candidate find nothing.
|
||||
func (q *Queries) ReadCandidateInvitation(ctx context.Context, arg ReadCandidateInvitationParams) (ReadCandidateInvitationRow, error) {
|
||||
row := q.db.QueryRow(ctx, readCandidateInvitation, arg.CandidateID, arg.InvitationID)
|
||||
var i ReadCandidateInvitationRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrganizationID,
|
||||
&i.CandidateID,
|
||||
&i.NeedID,
|
||||
&i.NeedRequirementsVersion,
|
||||
&i.State,
|
||||
&i.ExpiryReason,
|
||||
&i.Version,
|
||||
&i.TermsVersion,
|
||||
&i.ResponsePeriodDays,
|
||||
&i.PolicyVersionID,
|
||||
&i.ResponseDeadlineAt,
|
||||
&i.SalaryRaiseUsed,
|
||||
&i.CreatedBy,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeliveredAt,
|
||||
&i.ViewedAt,
|
||||
&i.RespondedAt,
|
||||
&i.Terms,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
Reference in new issue
Block a user