feat(invitations): let candidates read, view and decline their invitations

This commit is contained in:
shira committed 2026-10-09 17:25:12 +03:00
1 parent c410636b7f
commit ac57c05aca
27 files changed
+3285 -21

No files matched your search

@@ -0,0 +1,322 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: invitation_answers.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const declineInvitation = `-- name: DeclineInvitation :one
UPDATE invitations
SET
state = 'declined',
version = $1,
responded_at = $2,
updated_at = $2
WHERE
organization_id = $3
AND id = $4
AND state IN ('delivered', 'viewed', 'counter')
AND version = $5
AND response_deadline_at > $2
AND $1::bigint = $5::bigint + 1
RETURNING id
`
type DeclineInvitationParams struct {
NextVersion int64
DeclinedAt pgtype.Timestamptz
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
ExpectedVersion int64
}
// The compare-and-swap of a decline: it applies only to an answerable
// invitation at the version the caller read and before its deadline.
func (q *Queries) DeclineInvitation(ctx context.Context, arg DeclineInvitationParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, declineInvitation,
arg.NextVersion,
arg.DeclinedAt,
arg.OrganizationID,
arg.InvitationID,
arg.ExpectedVersion,
)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const getInvitationDeclineResult = `-- name: GetInvitationDeclineResult :one
SELECT
invitation_id,
outcome
FROM invitation_decline_results
WHERE scope_hash = $1
`
type GetInvitationDeclineResultRow struct {
InvitationID pgtype.UUID
Outcome string
}
func (q *Queries) GetInvitationDeclineResult(ctx context.Context, scopeHash []byte) (GetInvitationDeclineResultRow, error) {
row := q.db.QueryRow(ctx, getInvitationDeclineResult, scopeHash)
var i GetInvitationDeclineResultRow
err := row.Scan(&i.InvitationID, &i.Outcome)
return i, err
}
const insertInvitationDecline = `-- name: InsertInvitationDecline :exec
INSERT INTO invitation_declines (
invitation_id,
organization_id,
reason,
comment,
declined_at
) VALUES (
$1,
$2,
$3,
$4,
$5
)
`
type InsertInvitationDeclineParams struct {
InvitationID pgtype.UUID
OrganizationID pgtype.UUID
Reason string
Comment pgtype.Text
DeclinedAt pgtype.Timestamptz
}
func (q *Queries) InsertInvitationDecline(ctx context.Context, arg InsertInvitationDeclineParams) error {
_, err := q.db.Exec(ctx, insertInvitationDecline,
arg.InvitationID,
arg.OrganizationID,
arg.Reason,
arg.Comment,
arg.DeclinedAt,
)
return err
}
const insertInvitationDeclineResult = `-- name: InsertInvitationDeclineResult :exec
INSERT INTO invitation_decline_results (
scope_hash,
invitation_id,
outcome
) VALUES (
$1,
$2,
$3
)
`
type InsertInvitationDeclineResultParams struct {
ScopeHash []byte
InvitationID pgtype.UUID
Outcome string
}
func (q *Queries) InsertInvitationDeclineResult(ctx context.Context, arg InsertInvitationDeclineResultParams) error {
_, err := q.db.Exec(ctx, insertInvitationDeclineResult, arg.ScopeHash, arg.InvitationID, arg.Outcome)
return err
}
const lockCandidateInvitation = `-- name: LockCandidateInvitation :one
SELECT id
FROM invitations
WHERE
candidate_id = $1
AND id = $2
AND delivered_at IS NOT NULL
FOR UPDATE
`
type LockCandidateInvitationParams struct {
CandidateID pgtype.UUID
InvitationID pgtype.UUID
}
// Lock one delivered invitation addressed to the candidate. Only the
// invitation row is locked, never the organization.
func (q *Queries) LockCandidateInvitation(ctx context.Context, arg LockCandidateInvitationParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, lockCandidateInvitation, arg.CandidateID, arg.InvitationID)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const readCandidateInvitationEntry = `-- 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 = $1
AND invitation.id = $2
AND invitation.delivered_at IS NOT NULL
`
type ReadCandidateInvitationEntryParams struct {
CandidateID pgtype.UUID
InvitationID pgtype.UUID
}
type ReadCandidateInvitationEntryRow 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
}
// 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.
func (q *Queries) ReadCandidateInvitationEntry(ctx context.Context, arg ReadCandidateInvitationEntryParams) (ReadCandidateInvitationEntryRow, error) {
row := q.db.QueryRow(ctx, readCandidateInvitationEntry, arg.CandidateID, arg.InvitationID)
var i ReadCandidateInvitationEntryRow
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,
&i.OrganizationName,
)
return i, err
}
const readInvitationDecline = `-- name: ReadInvitationDecline :one
SELECT
invitation_id,
organization_id,
reason,
comment,
declined_at
FROM invitation_declines
WHERE
organization_id = $1
AND invitation_id = $2
`
type ReadInvitationDeclineParams struct {
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
}
func (q *Queries) ReadInvitationDecline(ctx context.Context, arg ReadInvitationDeclineParams) (InvitationDecline, error) {
row := q.db.QueryRow(ctx, readInvitationDecline, arg.OrganizationID, arg.InvitationID)
var i InvitationDecline
err := row.Scan(
&i.InvitationID,
&i.OrganizationID,
&i.Reason,
&i.Comment,
&i.DeclinedAt,
)
return i, err
}
const recordInvitationView = `-- name: RecordInvitationView :one
UPDATE invitations
SET
state = 'viewed',
version = $1,
viewed_at = $2,
updated_at = $2
WHERE
organization_id = $3
AND id = $4
AND state = 'delivered'
AND version = $5
AND response_deadline_at > $2
AND $1::bigint = $5::bigint + 1
RETURNING id
`
type RecordInvitationViewParams struct {
NextVersion int64
ViewedAt pgtype.Timestamptz
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
ExpectedVersion int64
}
// 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.
func (q *Queries) RecordInvitationView(ctx context.Context, arg RecordInvitationViewParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, recordInvitationView,
arg.NextVersion,
arg.ViewedAt,
arg.OrganizationID,
arg.InvitationID,
arg.ExpectedVersion,
)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
@@ -597,6 +597,20 @@ type InvitationCreationResult struct {
Snapshot []byte
}
type InvitationDecline struct {
InvitationID pgtype.UUID
OrganizationID pgtype.UUID
Reason string
Comment pgtype.Text
DeclinedAt pgtype.Timestamptz
}
type InvitationDeclineResult struct {
ScopeHash []byte
InvitationID pgtype.UUID
Outcome string
}
type InvitationEvent struct {
ID pgtype.UUID
InvitationID pgtype.UUID
@@ -157,6 +157,9 @@ type Querier interface {
// The caller appends the matching outbox intent in the same transaction only
// when this insert returns a row.
CreateAccountDeletionRequest(ctx context.Context, arg CreateAccountDeletionRequestParams) (CreateAccountDeletionRequestRow, error)
// The compare-and-swap of a decline: it applies only to an answerable
// invitation at the version the caller read and before its deadline.
DeclineInvitation(ctx context.Context, arg DeclineInvitationParams) (pgtype.UUID, error)
// A newly reserved generic idempotency window can replace an expired typed
// outcome before the new immutable response is inserted.
DeleteAssessmentAttemptIdempotencyOutcome(ctx context.Context, scopeHash []byte) error
@@ -302,6 +305,7 @@ type Querier interface {
// Explains why ConsumeEmailToken matched no row.
GetEmailTokenState(ctx context.Context, arg GetEmailTokenStateParams) (GetEmailTokenStateRow, error)
GetInvitationCreationResult(ctx context.Context, scopeHash []byte) (string, error)
GetInvitationDeclineResult(ctx context.Context, scopeHash []byte) (GetInvitationDeclineResultRow, error)
GetInvitationSendResult(ctx context.Context, scopeHash []byte) (GetInvitationSendResultRow, error)
// The caller holds the attempt row lock, so the last mark is stable.
GetLastAssessmentOriginMark(ctx context.Context, attemptID pgtype.UUID) (AssessmentAttemptOriginMark, error)
@@ -477,6 +481,8 @@ type Querier interface {
// inserted in one caller-owned transaction. The deferred reference to the
// current terms version is checked when the transaction commits.
InsertInvitation(ctx context.Context, arg InsertInvitationParams) error
InsertInvitationDecline(ctx context.Context, arg InsertInvitationDeclineParams) error
InsertInvitationDeclineResult(ctx context.Context, arg InsertInvitationDeclineResultParams) error
InsertInvitationEvent(ctx context.Context, arg InsertInvitationEventParams) error
InsertInvitationSendResult(ctx context.Context, arg InsertInvitationSendResultParams) error
InsertInvitationTermsVersion(ctx context.Context, arg InsertInvitationTermsVersionParams) error
@@ -716,6 +722,9 @@ type Querier interface {
// Acquired after C3, candidate/specialization scope, and attempt row locks.
// This independent lock serializes current category upgrades for one scope.
LockCandidateAssessmentCategory(ctx context.Context, arg LockCandidateAssessmentCategoryParams) error
// Lock one delivered invitation addressed to the candidate. Only the
// invitation row is locked, never the organization.
LockCandidateInvitation(ctx context.Context, arg LockCandidateInvitationParams) (pgtype.UUID, error)
// Locks the candidate's published row only when it refers to a C2 record
// revoked by this transaction. The setting remains linked to that immutable
// record; ReadCandidatePublication computes the effective closed state and
@@ -846,6 +855,10 @@ type Querier interface {
// Read one delivered invitation addressed to the candidate. A draft and an
// invitation of another candidate find nothing.
ReadCandidateInvitation(ctx context.Context, arg ReadCandidateInvitationParams) (ReadCandidateInvitationRow, error)
// 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.
ReadCandidateInvitationEntry(ctx context.Context, arg ReadCandidateInvitationEntryParams) (ReadCandidateInvitationEntryRow, error)
// Queries of the candidate visibility settings: the publication of
// the card, the search status, the hidden organizations, and the history of
// these settings. Every statement is scoped by the candidate's user_id, and
@@ -888,6 +901,7 @@ type Querier interface {
// and the invitation scope the row, so another organization's identifier
// finds nothing.
ReadInvitation(ctx context.Context, arg ReadInvitationParams) (ReadInvitationRow, error)
ReadInvitationDecline(ctx context.Context, arg ReadInvitationDeclineParams) (InvitationDecline, error)
// Read one historical terms version of one invitation of one organization.
ReadInvitationTermsVersion(ctx context.Context, arg ReadInvitationTermsVersionParams) (string, error)
// Read one historical version without exposing another organization's record.
@@ -933,6 +947,10 @@ type Querier interface {
// the delivery is no longer pending or waiting, or another worker holds it.
// now is the application clock.
RecordDeliveryFailure(ctx context.Context, arg RecordDeliveryFailureParams) (string, error)
// 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.
RecordInvitationView(ctx context.Context, arg RecordInvitationViewParams) (pgtype.UUID, error)
// Records the latest settled outcome of a send under the reservation.
RecordMailQuotaOutcome(ctx context.Context, arg RecordMailQuotaOutcomeParams) (int64, error)
RefreshCompanyCardViewQuotaUsage(ctx context.Context, arg RefreshCompanyCardViewQuotaUsageParams) (int64, error)