558 lines
14 KiB
Go
558 lines
14 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: invitations.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const advanceInvitationDraft = `-- name: AdvanceInvitationDraft :one
|
|
UPDATE invitations
|
|
SET
|
|
need_id = $1,
|
|
need_requirements_version = $2,
|
|
response_period_days = $3,
|
|
policy_version_id = $4,
|
|
terms_version = $5,
|
|
version = $6,
|
|
updated_at = $7
|
|
WHERE
|
|
organization_id = $8
|
|
AND id = $9
|
|
AND state = 'draft'
|
|
AND version = $10
|
|
AND $6::bigint = $10::bigint + 1
|
|
RETURNING id
|
|
`
|
|
|
|
type AdvanceInvitationDraftParams struct {
|
|
NeedID pgtype.UUID
|
|
NeedRequirementsVersion pgtype.Int8
|
|
ResponsePeriodDays int16
|
|
PolicyVersionID pgtype.UUID
|
|
TermsVersion int64
|
|
NextVersion int64
|
|
UpdatedAt pgtype.Timestamptz
|
|
OrganizationID pgtype.UUID
|
|
InvitationID pgtype.UUID
|
|
ExpectedVersion int64
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) AdvanceInvitationDraft(ctx context.Context, arg AdvanceInvitationDraftParams) (pgtype.UUID, error) {
|
|
row := q.db.QueryRow(ctx, advanceInvitationDraft,
|
|
arg.NeedID,
|
|
arg.NeedRequirementsVersion,
|
|
arg.ResponsePeriodDays,
|
|
arg.PolicyVersionID,
|
|
arg.TermsVersion,
|
|
arg.NextVersion,
|
|
arg.UpdatedAt,
|
|
arg.OrganizationID,
|
|
arg.InvitationID,
|
|
arg.ExpectedVersion,
|
|
)
|
|
var id pgtype.UUID
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const getInvitationCreationResult = `-- name: GetInvitationCreationResult :one
|
|
SELECT snapshot::text AS snapshot
|
|
FROM invitation_creation_results
|
|
WHERE scope_hash = $1
|
|
`
|
|
|
|
func (q *Queries) GetInvitationCreationResult(ctx context.Context, scopeHash []byte) (string, error) {
|
|
row := q.db.QueryRow(ctx, getInvitationCreationResult, scopeHash)
|
|
var snapshot string
|
|
err := row.Scan(&snapshot)
|
|
return snapshot, err
|
|
}
|
|
|
|
const insertInvitation = `-- 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 (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10,
|
|
$11,
|
|
$12,
|
|
$13
|
|
)
|
|
`
|
|
|
|
type InsertInvitationParams struct {
|
|
ID pgtype.UUID
|
|
OrganizationID pgtype.UUID
|
|
CandidateID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
NeedRequirementsVersion pgtype.Int8
|
|
State string
|
|
Version int64
|
|
TermsVersion int64
|
|
ResponsePeriodDays int16
|
|
PolicyVersionID pgtype.UUID
|
|
CreatedBy pgtype.UUID
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) InsertInvitation(ctx context.Context, arg InsertInvitationParams) error {
|
|
_, err := q.db.Exec(ctx, insertInvitation,
|
|
arg.ID,
|
|
arg.OrganizationID,
|
|
arg.CandidateID,
|
|
arg.NeedID,
|
|
arg.NeedRequirementsVersion,
|
|
arg.State,
|
|
arg.Version,
|
|
arg.TermsVersion,
|
|
arg.ResponsePeriodDays,
|
|
arg.PolicyVersionID,
|
|
arg.CreatedBy,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertInvitationEvent = `-- name: InsertInvitationEvent :exec
|
|
INSERT INTO invitation_events (
|
|
id,
|
|
invitation_id,
|
|
organization_id,
|
|
version,
|
|
terms_version,
|
|
kind,
|
|
actor_id,
|
|
changed_fields,
|
|
occurred_at
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8::text [],
|
|
$9
|
|
)
|
|
`
|
|
|
|
type InsertInvitationEventParams struct {
|
|
ID pgtype.UUID
|
|
InvitationID pgtype.UUID
|
|
OrganizationID pgtype.UUID
|
|
Version int64
|
|
TermsVersion int64
|
|
Kind string
|
|
ActorID pgtype.UUID
|
|
ChangedFields []string
|
|
OccurredAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) InsertInvitationEvent(ctx context.Context, arg InsertInvitationEventParams) error {
|
|
_, err := q.db.Exec(ctx, insertInvitationEvent,
|
|
arg.ID,
|
|
arg.InvitationID,
|
|
arg.OrganizationID,
|
|
arg.Version,
|
|
arg.TermsVersion,
|
|
arg.Kind,
|
|
arg.ActorID,
|
|
arg.ChangedFields,
|
|
arg.OccurredAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertInvitationTermsVersion = `-- 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 (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4::jsonb,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10
|
|
)
|
|
`
|
|
|
|
type InsertInvitationTermsVersionParams struct {
|
|
InvitationID pgtype.UUID
|
|
OrganizationID pgtype.UUID
|
|
Version int64
|
|
Terms []byte
|
|
SalaryFrom int64
|
|
SalaryTo int64
|
|
SalaryCurrency string
|
|
SalaryPeriod string
|
|
SalaryTaxBasis string
|
|
CreatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) InsertInvitationTermsVersion(ctx context.Context, arg InsertInvitationTermsVersionParams) error {
|
|
_, err := q.db.Exec(ctx, insertInvitationTermsVersion,
|
|
arg.InvitationID,
|
|
arg.OrganizationID,
|
|
arg.Version,
|
|
arg.Terms,
|
|
arg.SalaryFrom,
|
|
arg.SalaryTo,
|
|
arg.SalaryCurrency,
|
|
arg.SalaryPeriod,
|
|
arg.SalaryTaxBasis,
|
|
arg.CreatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const invitationNeedVersionExists = `-- name: InvitationNeedVersionExists :one
|
|
SELECT EXISTS (
|
|
SELECT 1
|
|
FROM organization_need_versions
|
|
WHERE
|
|
organization_id = $1
|
|
AND need_id = $2
|
|
AND version = $3::bigint
|
|
AND snapshot ->> 'requirements_version' = $3::bigint::text
|
|
) AS found
|
|
`
|
|
|
|
type InvitationNeedVersionExistsParams struct {
|
|
OrganizationID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
RequirementsVersion int64
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) InvitationNeedVersionExists(ctx context.Context, arg InvitationNeedVersionExistsParams) (bool, error) {
|
|
row := q.db.QueryRow(ctx, invitationNeedVersionExists, arg.OrganizationID, arg.NeedID, arg.RequirementsVersion)
|
|
var found bool
|
|
err := row.Scan(&found)
|
|
return found, err
|
|
}
|
|
|
|
const listInvitations = `-- 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 = $1
|
|
AND (
|
|
$2::timestamptz IS NULL
|
|
OR (invitation.created_at, invitation.id) < (
|
|
$2::timestamptz,
|
|
$3::uuid
|
|
)
|
|
)
|
|
ORDER BY invitation.created_at DESC, invitation.id DESC
|
|
LIMIT $4
|
|
`
|
|
|
|
type ListInvitationsParams struct {
|
|
OrganizationID pgtype.UUID
|
|
AfterCreatedAt pgtype.Timestamptz
|
|
AfterInvitationID pgtype.UUID
|
|
RowLimit int32
|
|
}
|
|
|
|
type ListInvitationsRow 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
|
|
}
|
|
|
|
// One bounded newest-first page. Both the organization scope and the tuple
|
|
// cursor are applied before the limit.
|
|
func (q *Queries) ListInvitations(ctx context.Context, arg ListInvitationsParams) ([]ListInvitationsRow, error) {
|
|
rows, err := q.db.Query(ctx, listInvitations,
|
|
arg.OrganizationID,
|
|
arg.AfterCreatedAt,
|
|
arg.AfterInvitationID,
|
|
arg.RowLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListInvitationsRow
|
|
for rows.Next() {
|
|
var i ListInvitationsRow
|
|
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,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockInvitation = `-- name: LockInvitation :one
|
|
SELECT id
|
|
FROM invitations
|
|
WHERE
|
|
organization_id = $1
|
|
AND id = $2
|
|
FOR UPDATE
|
|
`
|
|
|
|
type LockInvitationParams struct {
|
|
OrganizationID pgtype.UUID
|
|
InvitationID pgtype.UUID
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) LockInvitation(ctx context.Context, arg LockInvitationParams) (pgtype.UUID, error) {
|
|
row := q.db.QueryRow(ctx, lockInvitation, arg.OrganizationID, arg.InvitationID)
|
|
var id pgtype.UUID
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const readInvitation = `-- 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 = $1
|
|
AND invitation.id = $2
|
|
`
|
|
|
|
type ReadInvitationParams struct {
|
|
OrganizationID pgtype.UUID
|
|
InvitationID pgtype.UUID
|
|
}
|
|
|
|
type ReadInvitationRow 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 current invitation with its terms snapshot. Both the organization
|
|
// and the invitation scope the row, so another organization's identifier
|
|
// finds nothing.
|
|
func (q *Queries) ReadInvitation(ctx context.Context, arg ReadInvitationParams) (ReadInvitationRow, error) {
|
|
row := q.db.QueryRow(ctx, readInvitation, arg.OrganizationID, arg.InvitationID)
|
|
var i ReadInvitationRow
|
|
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
|
|
}
|
|
|
|
const readInvitationTermsVersion = `-- name: ReadInvitationTermsVersion :one
|
|
SELECT terms::text AS terms
|
|
FROM invitation_terms_versions
|
|
WHERE
|
|
organization_id = $1
|
|
AND invitation_id = $2
|
|
AND version = $3
|
|
`
|
|
|
|
type ReadInvitationTermsVersionParams struct {
|
|
OrganizationID pgtype.UUID
|
|
InvitationID pgtype.UUID
|
|
Version int64
|
|
}
|
|
|
|
// Read one historical terms version of one invitation of one organization.
|
|
func (q *Queries) ReadInvitationTermsVersion(ctx context.Context, arg ReadInvitationTermsVersionParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, readInvitationTermsVersion, arg.OrganizationID, arg.InvitationID, arg.Version)
|
|
var terms string
|
|
err := row.Scan(&terms)
|
|
return terms, err
|
|
}
|
|
|
|
const saveInvitationCreationResult = `-- name: SaveInvitationCreationResult :exec
|
|
INSERT INTO invitation_creation_results (scope_hash, snapshot)
|
|
VALUES ($1, $2::jsonb)
|
|
ON CONFLICT (scope_hash) DO UPDATE
|
|
SET snapshot = excluded.snapshot
|
|
`
|
|
|
|
type SaveInvitationCreationResultParams struct {
|
|
ScopeHash []byte
|
|
Snapshot []byte
|
|
}
|
|
|
|
func (q *Queries) SaveInvitationCreationResult(ctx context.Context, arg SaveInvitationCreationResultParams) error {
|
|
_, err := q.db.Exec(ctx, saveInvitationCreationResult, arg.ScopeHash, arg.Snapshot)
|
|
return err
|
|
}
|