feat(profile): persist candidate edits and enforce company access
This commit is contained in:
1 parent
3ab6bd91a3
commit
73c7caa225
67 files changed
+5286
-229
No files matched your search
@@ -11,6 +11,31 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const advanceCandidateOnboardingAfterProfile = `-- name: AdvanceCandidateOnboardingAfterProfile :exec
|
||||
UPDATE users
|
||||
SET
|
||||
onboarding_step = 'candidate_questionnaire',
|
||||
version = users.version + 1,
|
||||
updated_at = GREATEST(users.updated_at, $1::timestamptz)
|
||||
WHERE
|
||||
users.id = $2
|
||||
AND users.scenario = 'candidate'
|
||||
AND users.email_verified_at IS NOT NULL
|
||||
AND users.onboarding_step = 'candidate_profile'
|
||||
`
|
||||
|
||||
type AdvanceCandidateOnboardingAfterProfileParams struct {
|
||||
At pgtype.Timestamptz
|
||||
UserID pgtype.UUID
|
||||
}
|
||||
|
||||
// Saving a profile advances onboarding only from that exact step. A retry or
|
||||
// a save after a later step is already stored never regresses onboarding.
|
||||
func (q *Queries) AdvanceCandidateOnboardingAfterProfile(ctx context.Context, arg AdvanceCandidateOnboardingAfterProfileParams) error {
|
||||
_, err := q.db.Exec(ctx, advanceCandidateOnboardingAfterProfile, arg.At, arg.UserID)
|
||||
return err
|
||||
}
|
||||
|
||||
const advanceCandidateProfileVersion = `-- name: AdvanceCandidateProfileVersion :one
|
||||
UPDATE candidate_profiles
|
||||
SET
|
||||
@@ -47,6 +72,39 @@ func (q *Queries) AdvanceCandidateProfileVersion(ctx context.Context, arg Advanc
|
||||
return i, err
|
||||
}
|
||||
|
||||
const appendCandidateProfileHistory = `-- name: AppendCandidateProfileHistory :exec
|
||||
INSERT INTO candidate_profile_history (
|
||||
user_id,
|
||||
version,
|
||||
changed_at,
|
||||
snapshot
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4::jsonb
|
||||
)
|
||||
`
|
||||
|
||||
type AppendCandidateProfileHistoryParams struct {
|
||||
UserID pgtype.UUID
|
||||
Version int64
|
||||
ChangedAt pgtype.Timestamptz
|
||||
Snapshot []byte
|
||||
}
|
||||
|
||||
// Every changed version appends exactly one private snapshot. The unique
|
||||
// key prevents a second snapshot for the same profile version.
|
||||
func (q *Queries) AppendCandidateProfileHistory(ctx context.Context, arg AppendCandidateProfileHistoryParams) error {
|
||||
_, err := q.db.Exec(ctx, appendCandidateProfileHistory,
|
||||
arg.UserID,
|
||||
arg.Version,
|
||||
arg.ChangedAt,
|
||||
arg.Snapshot,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteCandidateProfileClaims = `-- name: DeleteCandidateProfileClaims :exec
|
||||
DELETE FROM candidate_profile_claims
|
||||
WHERE user_id = $1
|
||||
@@ -59,6 +117,18 @@ func (q *Queries) DeleteCandidateProfileClaims(ctx context.Context, userID pgtyp
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteCandidateProfileOverlapConfirmations = `-- name: DeleteCandidateProfileOverlapConfirmations :exec
|
||||
DELETE FROM candidate_profile_overlap_confirmations
|
||||
WHERE user_id = $1
|
||||
`
|
||||
|
||||
// Replaces the current explicit decisions with those in the new profile.
|
||||
// The versioned profile row is advanced first in this same transaction.
|
||||
func (q *Queries) DeleteCandidateProfileOverlapConfirmations(ctx context.Context, userID pgtype.UUID) error {
|
||||
_, err := q.db.Exec(ctx, deleteCandidateProfileOverlapConfirmations, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertCandidateProfile = `-- name: InsertCandidateProfile :one
|
||||
|
||||
INSERT INTO candidate_profiles (
|
||||
@@ -157,6 +227,49 @@ func (q *Queries) InsertCandidateProfileClaim(ctx context.Context, arg InsertCan
|
||||
return err
|
||||
}
|
||||
|
||||
const insertCandidateProfileOverlapConfirmation = `-- name: InsertCandidateProfileOverlapConfirmation :exec
|
||||
INSERT INTO candidate_profile_overlap_confirmations (
|
||||
user_id,
|
||||
first_index,
|
||||
second_index,
|
||||
first_started_on,
|
||||
first_ended_on,
|
||||
second_started_on,
|
||||
second_ended_on
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7
|
||||
)
|
||||
`
|
||||
|
||||
type InsertCandidateProfileOverlapConfirmationParams struct {
|
||||
UserID pgtype.UUID
|
||||
FirstIndex int16
|
||||
SecondIndex int16
|
||||
FirstStartedOn string
|
||||
FirstEndedOn pgtype.Text
|
||||
SecondStartedOn string
|
||||
SecondEndedOn pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) InsertCandidateProfileOverlapConfirmation(ctx context.Context, arg InsertCandidateProfileOverlapConfirmationParams) error {
|
||||
_, err := q.db.Exec(ctx, insertCandidateProfileOverlapConfirmation,
|
||||
arg.UserID,
|
||||
arg.FirstIndex,
|
||||
arg.SecondIndex,
|
||||
arg.FirstStartedOn,
|
||||
arg.FirstEndedOn,
|
||||
arg.SecondStartedOn,
|
||||
arg.SecondEndedOn,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const readCandidateProfile = `-- name: ReadCandidateProfile :many
|
||||
SELECT
|
||||
candidate_profiles.user_id,
|
||||
@@ -171,7 +284,28 @@ SELECT
|
||||
candidate_profile_claims.trust_level,
|
||||
candidate_profile_claims.quote,
|
||||
candidate_profile_claims.extraction_version,
|
||||
candidate_profile_claims.confirmed_at
|
||||
candidate_profile_claims.confirmed_at,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT jsonb_agg(
|
||||
jsonb_build_object(
|
||||
'first_index', confirmations.first_index,
|
||||
'second_index', confirmations.second_index,
|
||||
'first_period', jsonb_build_object(
|
||||
'started_on', confirmations.first_started_on,
|
||||
'ended_on', confirmations.first_ended_on
|
||||
),
|
||||
'second_period', jsonb_build_object(
|
||||
'started_on', confirmations.second_started_on,
|
||||
'ended_on', confirmations.second_ended_on
|
||||
)
|
||||
) ORDER BY confirmations.first_index, confirmations.second_index
|
||||
)::text
|
||||
FROM candidate_profile_overlap_confirmations AS confirmations
|
||||
WHERE confirmations.user_id = candidate_profiles.user_id
|
||||
),
|
||||
'[]'::text
|
||||
)::text AS parallel_period_acknowledgements
|
||||
FROM candidate_profiles
|
||||
LEFT JOIN candidate_profile_claims
|
||||
ON candidate_profile_claims.user_id = candidate_profiles.user_id
|
||||
@@ -180,19 +314,20 @@ ORDER BY candidate_profile_claims.kind, candidate_profile_claims.ordinal
|
||||
`
|
||||
|
||||
type ReadCandidateProfileRow struct {
|
||||
UserID pgtype.UUID
|
||||
Version int64
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
Kind pgtype.Text
|
||||
Ordinal pgtype.Int2
|
||||
Value []byte
|
||||
Status pgtype.Text
|
||||
Origin pgtype.Text
|
||||
TrustLevel pgtype.Int2
|
||||
Quote pgtype.Text
|
||||
ExtractionVersion pgtype.Text
|
||||
ConfirmedAt pgtype.Timestamptz
|
||||
UserID pgtype.UUID
|
||||
Version int64
|
||||
CreatedAt pgtype.Timestamptz
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
Kind pgtype.Text
|
||||
Ordinal pgtype.Int2
|
||||
Value []byte
|
||||
Status pgtype.Text
|
||||
Origin pgtype.Text
|
||||
TrustLevel pgtype.Int2
|
||||
Quote pgtype.Text
|
||||
ExtractionVersion pgtype.Text
|
||||
ConfirmedAt pgtype.Timestamptz
|
||||
ParallelPeriodAcknowledgements string
|
||||
}
|
||||
|
||||
// Reads the profile row of an account together with every claim in one
|
||||
@@ -225,6 +360,7 @@ func (q *Queries) ReadCandidateProfile(ctx context.Context, userID pgtype.UUID)
|
||||
&i.Quote,
|
||||
&i.ExtractionVersion,
|
||||
&i.ConfirmedAt,
|
||||
&i.ParallelPeriodAcknowledgements,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -94,6 +94,23 @@ type CandidateProfileClaim struct {
|
||||
ConfirmedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type CandidateProfileHistory struct {
|
||||
UserID pgtype.UUID
|
||||
Version int64
|
||||
ChangedAt pgtype.Timestamptz
|
||||
Snapshot []byte
|
||||
}
|
||||
|
||||
type CandidateProfileOverlapConfirmation struct {
|
||||
UserID pgtype.UUID
|
||||
FirstIndex int16
|
||||
SecondIndex int16
|
||||
FirstStartedOn string
|
||||
FirstEndedOn pgtype.Text
|
||||
SecondStartedOn string
|
||||
SecondEndedOn pgtype.Text
|
||||
}
|
||||
|
||||
type CandidatePublication struct {
|
||||
UserID pgtype.UUID
|
||||
State string
|
||||
|
||||
@@ -14,6 +14,9 @@ type Querier interface {
|
||||
// Adds recipients to the day and the month row. The caller holds their
|
||||
// locks from LockMailQuotaRows and has checked both limits.
|
||||
AddMailQuotaUsage(ctx context.Context, arg AddMailQuotaUsageParams) (int64, error)
|
||||
// Saving a profile advances onboarding only from that exact step. A retry or
|
||||
// a save after a later step is already stored never regresses onboarding.
|
||||
AdvanceCandidateOnboardingAfterProfile(ctx context.Context, arg AdvanceCandidateOnboardingAfterProfileParams) error
|
||||
// Advances the version over the one the caller read, in the same statement.
|
||||
// The row lock it takes serializes concurrent saves of one profile. The update
|
||||
// time never moves back: an application clock behind the stored time (clock
|
||||
@@ -28,6 +31,9 @@ type Querier interface {
|
||||
// ends every earlier token, and deletes the registration C1 of the earlier
|
||||
// registrant like UpdateUserRegistration does.
|
||||
AdvanceUserRegistration(ctx context.Context, arg AdvanceUserRegistrationParams) (AdvanceUserRegistrationRow, error)
|
||||
// Every changed version appends exactly one private snapshot. The unique
|
||||
// key prevents a second snapshot for the same profile version.
|
||||
AppendCandidateProfileHistory(ctx context.Context, arg AppendCandidateProfileHistoryParams) error
|
||||
// Authorizes one retry of a terminal unknown delivery. The update and append-
|
||||
// only decision share one statement and lock the current row, so a second or
|
||||
// concurrent operator decision cannot schedule another send for the same
|
||||
@@ -118,6 +124,9 @@ type Querier interface {
|
||||
// Removes the claims that a save replaces. The adapter calls it after the
|
||||
// version advanced and before it inserts the new set, in one transaction.
|
||||
DeleteCandidateProfileClaims(ctx context.Context, userID pgtype.UUID) error
|
||||
// Replaces the current explicit decisions with those in the new profile.
|
||||
// The versioned profile row is advanced first in this same transaction.
|
||||
DeleteCandidateProfileOverlapConfirmations(ctx context.Context, userID pgtype.UUID) error
|
||||
DeleteMailRequest(ctx context.Context, id pgtype.UUID) (int64, error)
|
||||
DeleteOrganizationRepresentatives(ctx context.Context, organizationID pgtype.UUID) error
|
||||
// Deletes one batch of accounts that stayed unverified since before cutoff,
|
||||
@@ -286,6 +295,7 @@ type Querier interface {
|
||||
// statement never takes a trust level from the caller, because every claim
|
||||
// has level 0.
|
||||
InsertCandidateProfileClaim(ctx context.Context, arg InsertCandidateProfileClaimParams) error
|
||||
InsertCandidateProfileOverlapConfirmation(ctx context.Context, arg InsertCandidateProfileOverlapConfirmationParams) error
|
||||
// Stores the first publication at version 1. No row returns when the account
|
||||
// already has a row, so a first publication that lost a race with another
|
||||
// one is answered as a version conflict. The foreign key refuses a record
|
||||
|
||||
Reference in new issue
Block a user