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
@@ -343,6 +343,56 @@ func (ProfileExperience) LogValue() slog.Value {
|
||||
}
|
||||
func (ProfileExperience) Format(state fmt.State, _ rune) { writeRedacted(state, redactedProfile) }
|
||||
|
||||
// ProfileExperiencePeriod is the exact month range used to acknowledge
|
||||
// overlapping work. Its dates are personal data, so formatting redacts them.
|
||||
type ProfileExperiencePeriod struct {
|
||||
StartedOn string `json:"started_on"`
|
||||
EndedOn string `json:"ended_on"`
|
||||
}
|
||||
|
||||
func (ProfileExperiencePeriod) String() string { return redactedProfile }
|
||||
func (ProfileExperiencePeriod) GoString() string { return redactedProfile }
|
||||
func (ProfileExperiencePeriod) LogValue() slog.Value {
|
||||
return slog.StringValue(redactedProfile)
|
||||
}
|
||||
func (ProfileExperiencePeriod) Format(state fmt.State, _ rune) { writeRedacted(state, redactedProfile) }
|
||||
|
||||
// ProfileParallelPeriodAcknowledgement records that the candidate reviewed
|
||||
// the exact dates of two overlapping experience entries at their indices.
|
||||
// Employer names and summaries are not part of the decision.
|
||||
type ProfileParallelPeriodAcknowledgement struct {
|
||||
FirstIndex int `json:"first_index"`
|
||||
SecondIndex int `json:"second_index"`
|
||||
FirstPeriod ProfileExperiencePeriod `json:"first_period"`
|
||||
SecondPeriod ProfileExperiencePeriod `json:"second_period"`
|
||||
}
|
||||
|
||||
func (ProfileParallelPeriodAcknowledgement) String() string { return redactedProfile }
|
||||
func (ProfileParallelPeriodAcknowledgement) GoString() string { return redactedProfile }
|
||||
func (ProfileParallelPeriodAcknowledgement) LogValue() slog.Value {
|
||||
return slog.StringValue(redactedProfile)
|
||||
}
|
||||
func (ProfileParallelPeriodAcknowledgement) Format(state fmt.State, _ rune) {
|
||||
writeRedacted(state, redactedProfile)
|
||||
}
|
||||
|
||||
// ProfileExperienceOverlapStatus reports whether the candidate acknowledged
|
||||
// an overlapping pair of periods in the current profile version.
|
||||
type ProfileExperienceOverlapStatus string
|
||||
|
||||
const (
|
||||
ProfileExperienceOverlapNeedsConfirmation ProfileExperienceOverlapStatus = "needs_confirmation"
|
||||
ProfileExperienceOverlapConfirmed ProfileExperienceOverlapStatus = "confirmed"
|
||||
)
|
||||
|
||||
// ProfileExperienceOverlap identifies two entries whose inclusive month
|
||||
// ranges overlap. Indices refer to one profile version's experience list.
|
||||
type ProfileExperienceOverlap struct {
|
||||
FirstIndex int
|
||||
SecondIndex int
|
||||
Status ProfileExperienceOverlapStatus
|
||||
}
|
||||
|
||||
// ProfileTaxBasis is the tax basis of a salary amount.
|
||||
type ProfileTaxBasis string
|
||||
|
||||
@@ -379,17 +429,20 @@ type ProfileClaimInput[V comparable] struct {
|
||||
// gave it. A nil single field is unknown and removes the stored value; a list
|
||||
// replaces the stored list. It holds personal data: never log it.
|
||||
type CandidateProfileInput struct {
|
||||
FullName *ProfileClaimInput[string]
|
||||
Email *ProfileClaimInput[string]
|
||||
Phone *ProfileClaimInput[string]
|
||||
Telegram *ProfileClaimInput[string]
|
||||
Stack []ProfileClaimInput[string]
|
||||
Roles []ProfileClaimInput[ProfileRole]
|
||||
Experience []ProfileClaimInput[ProfileExperience]
|
||||
SoftSkills []ProfileClaimInput[string]
|
||||
Location *ProfileClaimInput[string]
|
||||
Relocation *ProfileClaimInput[bool]
|
||||
SalaryMinimum *ProfileClaimInput[ProfileSalaryMinimum]
|
||||
FullName *ProfileClaimInput[string]
|
||||
Email *ProfileClaimInput[string]
|
||||
Phone *ProfileClaimInput[string]
|
||||
Telegram *ProfileClaimInput[string]
|
||||
Stack []ProfileClaimInput[string]
|
||||
Roles []ProfileClaimInput[ProfileRole]
|
||||
Experience []ProfileClaimInput[ProfileExperience]
|
||||
// ParallelPeriodAcknowledgements contains explicit acknowledgements for
|
||||
// the exact periods and indices in Experience.
|
||||
ParallelPeriodAcknowledgements []ProfileParallelPeriodAcknowledgement
|
||||
SoftSkills []ProfileClaimInput[string]
|
||||
Location *ProfileClaimInput[string]
|
||||
Relocation *ProfileClaimInput[bool]
|
||||
SalaryMinimum *ProfileClaimInput[ProfileSalaryMinimum]
|
||||
}
|
||||
|
||||
// CandidateProfileContent is the validated content of a profile: every claim
|
||||
@@ -398,17 +451,18 @@ type CandidateProfileInput struct {
|
||||
// and no reference to a file, the ML service, or an FSP account. It is
|
||||
// personal data: String, GoString, Format, and LogValue redact it.
|
||||
type CandidateProfileContent struct {
|
||||
fullName *ProfileClaim[string]
|
||||
email *ProfileClaim[string]
|
||||
phone *ProfileClaim[string]
|
||||
telegram *ProfileClaim[string]
|
||||
stack []ProfileClaim[string]
|
||||
roles []ProfileClaim[ProfileRole]
|
||||
experience []ProfileClaim[ProfileExperience]
|
||||
softSkills []ProfileClaim[string]
|
||||
location *ProfileClaim[string]
|
||||
relocation *ProfileClaim[bool]
|
||||
salaryMinimum *ProfileClaim[ProfileSalaryMinimum]
|
||||
fullName *ProfileClaim[string]
|
||||
email *ProfileClaim[string]
|
||||
phone *ProfileClaim[string]
|
||||
telegram *ProfileClaim[string]
|
||||
stack []ProfileClaim[string]
|
||||
roles []ProfileClaim[ProfileRole]
|
||||
experience []ProfileClaim[ProfileExperience]
|
||||
parallelPeriodAcknowledgements []ProfileParallelPeriodAcknowledgement
|
||||
softSkills []ProfileClaim[string]
|
||||
location *ProfileClaim[string]
|
||||
relocation *ProfileClaim[bool]
|
||||
salaryMinimum *ProfileClaim[ProfileSalaryMinimum]
|
||||
}
|
||||
|
||||
func optionalClaim[V comparable](claim *ProfileClaim[V]) (ProfileClaim[V], bool) {
|
||||
@@ -469,6 +523,35 @@ func (content CandidateProfileContent) Experience() []ProfileClaim[ProfileExperi
|
||||
return slices.Clone(content.experience)
|
||||
}
|
||||
|
||||
// ParallelPeriodAcknowledgements returns a copy of the stored date-pair
|
||||
// decisions for this profile version.
|
||||
func (content CandidateProfileContent) ParallelPeriodAcknowledgements() []ProfileParallelPeriodAcknowledgement {
|
||||
return slices.Clone(content.parallelPeriodAcknowledgements)
|
||||
}
|
||||
|
||||
// ExperienceOverlaps returns every overlapping pair in index order. A pair's
|
||||
// status is derived from an acknowledgement that still matches both periods.
|
||||
func (content CandidateProfileContent) ExperienceOverlaps() []ProfileExperienceOverlap {
|
||||
var overlaps []ProfileExperienceOverlap
|
||||
acknowledged := make(map[experiencePair]struct{}, len(content.parallelPeriodAcknowledgements))
|
||||
for _, acknowledgement := range content.parallelPeriodAcknowledgements {
|
||||
acknowledged[experiencePair{first: acknowledgement.FirstIndex, second: acknowledgement.SecondIndex}] = struct{}{}
|
||||
}
|
||||
for first := range content.experience {
|
||||
for second := first + 1; second < len(content.experience); second++ {
|
||||
if !experiencePeriodsOverlap(content.experience[first].value, content.experience[second].value) {
|
||||
continue
|
||||
}
|
||||
status := ProfileExperienceOverlapNeedsConfirmation
|
||||
if _, ok := acknowledged[experiencePair{first: first, second: second}]; ok {
|
||||
status = ProfileExperienceOverlapConfirmed
|
||||
}
|
||||
overlaps = append(overlaps, ProfileExperienceOverlap{FirstIndex: first, SecondIndex: second, Status: status})
|
||||
}
|
||||
}
|
||||
return overlaps
|
||||
}
|
||||
|
||||
// SoftSkills returns a copy of the soft skills in the order the candidate
|
||||
// gave them.
|
||||
func (content CandidateProfileContent) SoftSkills() []ProfileClaim[string] {
|
||||
@@ -488,6 +571,7 @@ func (content CandidateProfileContent) Equal(other CandidateProfileContent) bool
|
||||
slices.EqualFunc(content.stack, other.stack, claimsEqual) &&
|
||||
slices.EqualFunc(content.roles, other.roles, claimsEqual) &&
|
||||
slices.EqualFunc(content.experience, other.experience, claimsEqual) &&
|
||||
slices.Equal(content.parallelPeriodAcknowledgements, other.parallelPeriodAcknowledgements) &&
|
||||
slices.EqualFunc(content.softSkills, other.softSkills, claimsEqual)
|
||||
}
|
||||
|
||||
@@ -730,6 +814,166 @@ func newProfileRules(now time.Time) profileRules {
|
||||
}
|
||||
}
|
||||
|
||||
type experiencePair struct {
|
||||
first int
|
||||
second int
|
||||
}
|
||||
|
||||
func experiencePeriod(value ProfileExperience) ProfileExperiencePeriod {
|
||||
return ProfileExperiencePeriod{StartedOn: value.StartedOn, EndedOn: value.EndedOn}
|
||||
}
|
||||
|
||||
func experiencePeriodsOverlap(first, second ProfileExperience) bool {
|
||||
return (first.EndedOn == "" || second.StartedOn <= first.EndedOn) &&
|
||||
(second.EndedOn == "" || first.StartedOn <= second.EndedOn)
|
||||
}
|
||||
|
||||
func invalidParallelPeriodField(location string, index int, pointer, reason string) error {
|
||||
return invalidProfileField(location, fmt.Sprintf("/confirmed_parallel_periods/%d/%s", index, pointer), reason)
|
||||
}
|
||||
|
||||
func resolveParallelPeriodAcknowledgements(
|
||||
stored CandidateProfileContent,
|
||||
experiences []ProfileClaim[ProfileExperience],
|
||||
requested []ProfileParallelPeriodAcknowledgement,
|
||||
location string,
|
||||
) ([]ProfileParallelPeriodAcknowledgement, error) {
|
||||
const maxPairs = maxProfileExperienceItems * (maxProfileExperienceItems - 1) / 2
|
||||
if len(requested) > maxPairs {
|
||||
return nil, invalidProfileField(location, "/confirmed_parallel_periods", "must not contain more pairs than the experience list allows")
|
||||
}
|
||||
|
||||
acknowledgements := make(map[experiencePair]ProfileParallelPeriodAcknowledgement, len(requested)+len(stored.parallelPeriodAcknowledgements))
|
||||
requestedPairs := make(map[experiencePair]struct{}, len(requested))
|
||||
for index, acknowledgement := range requested {
|
||||
if err := addRequestedParallelPeriodAcknowledgement(
|
||||
acknowledgement, experiences, index, location, requestedPairs, acknowledgements,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
previousOverlaps := overlappingExperiencePairs(stored.experience)
|
||||
carryMatchingParallelPeriodAcknowledgements(stored.parallelPeriodAcknowledgements, experiences, acknowledgements)
|
||||
return resolveCurrentExperienceOverlaps(experiences, previousOverlaps, acknowledgements, location)
|
||||
}
|
||||
|
||||
func addRequestedParallelPeriodAcknowledgement(
|
||||
acknowledgement ProfileParallelPeriodAcknowledgement,
|
||||
experiences []ProfileClaim[ProfileExperience],
|
||||
index int,
|
||||
location string,
|
||||
requestedPairs map[experiencePair]struct{},
|
||||
acknowledgements map[experiencePair]ProfileParallelPeriodAcknowledgement,
|
||||
) error {
|
||||
pair := experiencePair{first: acknowledgement.FirstIndex, second: acknowledgement.SecondIndex}
|
||||
if pair.first < 0 || pair.first >= len(experiences) {
|
||||
return invalidParallelPeriodField(location, index, "first_index", "must refer to an experience entry")
|
||||
}
|
||||
if pair.second <= pair.first || pair.second >= len(experiences) {
|
||||
return invalidParallelPeriodField(location, index, "second_index", "must refer to a later experience entry")
|
||||
}
|
||||
if _, duplicate := requestedPairs[pair]; duplicate {
|
||||
return invalidParallelPeriodField(location, index, "second_index", "must not repeat a pair")
|
||||
}
|
||||
requestedPairs[pair] = struct{}{}
|
||||
first := experiences[pair.first].value
|
||||
second := experiences[pair.second].value
|
||||
firstPeriod := experiencePeriod(first)
|
||||
secondPeriod := experiencePeriod(second)
|
||||
if acknowledgement.FirstPeriod.StartedOn != firstPeriod.StartedOn {
|
||||
return invalidParallelPeriodField(location, index, "first_period/started_on", "must match the indexed experience period")
|
||||
}
|
||||
if acknowledgement.FirstPeriod.EndedOn != firstPeriod.EndedOn {
|
||||
return invalidParallelPeriodField(location, index, "first_period/ended_on", "must match the indexed experience period")
|
||||
}
|
||||
if acknowledgement.SecondPeriod.StartedOn != secondPeriod.StartedOn {
|
||||
return invalidParallelPeriodField(location, index, "second_period/started_on", "must match the indexed experience period")
|
||||
}
|
||||
if acknowledgement.SecondPeriod.EndedOn != secondPeriod.EndedOn {
|
||||
return invalidParallelPeriodField(location, index, "second_period/ended_on", "must match the indexed experience period")
|
||||
}
|
||||
if !experiencePeriodsOverlap(first, second) {
|
||||
return invalidParallelPeriodField(location, index, "second_index", "must identify an overlapping period")
|
||||
}
|
||||
acknowledgements[pair] = ProfileParallelPeriodAcknowledgement{
|
||||
FirstIndex: pair.first, SecondIndex: pair.second, FirstPeriod: firstPeriod, SecondPeriod: secondPeriod,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func overlappingExperiencePairs(experiences []ProfileClaim[ProfileExperience]) map[experiencePair]ProfileParallelPeriodAcknowledgement {
|
||||
previous := make(map[experiencePair]ProfileParallelPeriodAcknowledgement)
|
||||
for first := range experiences {
|
||||
for second := first + 1; second < len(experiences); second++ {
|
||||
firstValue := experiences[first].value
|
||||
secondValue := experiences[second].value
|
||||
if !experiencePeriodsOverlap(firstValue, secondValue) {
|
||||
continue
|
||||
}
|
||||
previous[experiencePair{first: first, second: second}] = ProfileParallelPeriodAcknowledgement{
|
||||
FirstIndex: first, SecondIndex: second,
|
||||
FirstPeriod: experiencePeriod(firstValue), SecondPeriod: experiencePeriod(secondValue),
|
||||
}
|
||||
}
|
||||
}
|
||||
return previous
|
||||
}
|
||||
|
||||
func carryMatchingParallelPeriodAcknowledgements(
|
||||
stored []ProfileParallelPeriodAcknowledgement,
|
||||
experiences []ProfileClaim[ProfileExperience],
|
||||
acknowledgements map[experiencePair]ProfileParallelPeriodAcknowledgement,
|
||||
) {
|
||||
for _, previous := range stored {
|
||||
pair := experiencePair{first: previous.FirstIndex, second: previous.SecondIndex}
|
||||
if pair.first < 0 || pair.second < 0 || pair.first >= len(experiences) || pair.second >= len(experiences) {
|
||||
continue
|
||||
}
|
||||
first := experiences[pair.first].value
|
||||
second := experiences[pair.second].value
|
||||
if experiencePeriod(first) != previous.FirstPeriod || experiencePeriod(second) != previous.SecondPeriod ||
|
||||
!experiencePeriodsOverlap(first, second) {
|
||||
continue
|
||||
}
|
||||
if _, explicitlyAcknowledged := acknowledgements[pair]; !explicitlyAcknowledged {
|
||||
acknowledgements[pair] = previous
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resolveCurrentExperienceOverlaps(
|
||||
experiences []ProfileClaim[ProfileExperience],
|
||||
previousOverlaps map[experiencePair]ProfileParallelPeriodAcknowledgement,
|
||||
acknowledgements map[experiencePair]ProfileParallelPeriodAcknowledgement,
|
||||
location string,
|
||||
) ([]ProfileParallelPeriodAcknowledgement, error) {
|
||||
resolved := make([]ProfileParallelPeriodAcknowledgement, 0, len(acknowledgements))
|
||||
for first := range experiences {
|
||||
for second := first + 1; second < len(experiences); second++ {
|
||||
firstValue := experiences[first].value
|
||||
secondValue := experiences[second].value
|
||||
if !experiencePeriodsOverlap(firstValue, secondValue) {
|
||||
continue
|
||||
}
|
||||
pair := experiencePair{first: first, second: second}
|
||||
if acknowledgement, ok := acknowledgements[pair]; ok {
|
||||
resolved = append(resolved, acknowledgement)
|
||||
continue
|
||||
}
|
||||
previous, existed := previousOverlaps[pair]
|
||||
if existed && previous.FirstPeriod == experiencePeriod(firstValue) && previous.SecondPeriod == experiencePeriod(secondValue) {
|
||||
continue
|
||||
}
|
||||
return nil, invalidStateDependentProfileField(location,
|
||||
fmt.Sprintf("/experience/%d/started_on", second),
|
||||
"overlaps another period and needs an explicit confirmation",
|
||||
)
|
||||
}
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
// ResolveCandidateProfileContent applies a request to the stored content and
|
||||
// returns the new content. It validates every value, resolves each decision
|
||||
// against the stored claim (see claimRules.resolve), and keeps a stored claim
|
||||
@@ -798,6 +1042,12 @@ func ResolveCandidateProfileContent(stored CandidateProfileContent, input Candid
|
||||
return CandidateProfileContent{}, err
|
||||
}
|
||||
}
|
||||
content.parallelPeriodAcknowledgements, err = resolveParallelPeriodAcknowledgements(
|
||||
stored, content.experience, input.ParallelPeriodAcknowledgements, location,
|
||||
)
|
||||
if err != nil {
|
||||
return CandidateProfileContent{}, fmt.Errorf("%s - resolve parallel period acknowledgements: %w", location, err)
|
||||
}
|
||||
return content, nil
|
||||
}
|
||||
|
||||
@@ -873,6 +1123,12 @@ func (profile CandidateProfile) Replace(input CandidateProfileInput, expectedVer
|
||||
if content.Equal(profile.content) {
|
||||
return profile, false, nil
|
||||
}
|
||||
if err := requireConfirmedExperienceOverlaps(content, location); err != nil {
|
||||
if expectedVersion != profile.version {
|
||||
return CandidateProfile{}, false, conflict
|
||||
}
|
||||
return CandidateProfile{}, false, err
|
||||
}
|
||||
if expectedVersion != profile.version {
|
||||
return CandidateProfile{}, false, conflict
|
||||
}
|
||||
@@ -888,6 +1144,18 @@ func (profile CandidateProfile) Replace(input CandidateProfileInput, expectedVer
|
||||
return updated, true, nil
|
||||
}
|
||||
|
||||
func requireConfirmedExperienceOverlaps(content CandidateProfileContent, location string) error {
|
||||
for _, overlap := range content.ExperienceOverlaps() {
|
||||
if overlap.Status == ProfileExperienceOverlapNeedsConfirmation {
|
||||
return invalidStateDependentProfileField(location,
|
||||
fmt.Sprintf("/experience/%d/started_on", overlap.SecondIndex),
|
||||
"overlaps another period and needs an explicit confirmation",
|
||||
)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (CandidateProfile) String() string { return redactedProfile }
|
||||
func (CandidateProfile) GoString() string { return redactedProfile }
|
||||
func (CandidateProfile) Format(state fmt.State, _ rune) {
|
||||
@@ -924,15 +1192,15 @@ const (
|
||||
// ConfirmedAt is a NULL column, and an empty Quote or ExtractionVersion is a
|
||||
// NULL one.
|
||||
type StoredProfileClaim struct {
|
||||
Kind string
|
||||
Ordinal int
|
||||
Value []byte
|
||||
Status string
|
||||
Origin string
|
||||
TrustLevel int
|
||||
Quote string
|
||||
ExtractionVersion string
|
||||
ConfirmedAt time.Time
|
||||
Kind string `json:"kind"`
|
||||
Ordinal int `json:"ordinal"`
|
||||
Value []byte `json:"value"`
|
||||
Status string `json:"status"`
|
||||
Origin string `json:"origin"`
|
||||
TrustLevel int `json:"trust_level"`
|
||||
Quote string `json:"quote"`
|
||||
ExtractionVersion string `json:"extraction_version"`
|
||||
ConfirmedAt time.Time `json:"confirmed_at"`
|
||||
}
|
||||
|
||||
// The JSON shapes of structured values. Their keys are the ones that the
|
||||
@@ -1053,6 +1321,16 @@ func (content CandidateProfileContent) StoredClaims() ([]StoredProfileClaim, err
|
||||
// any order; a list is ordered by Ordinal and must have no gap, and a single
|
||||
// field must appear once. The error never quotes a value.
|
||||
func RestoreCandidateProfileContent(rows []StoredProfileClaim) (CandidateProfileContent, error) {
|
||||
return RestoreCandidateProfileContentWithParallelPeriods(rows, nil)
|
||||
}
|
||||
|
||||
// RestoreCandidateProfileContentWithParallelPeriods also restores the
|
||||
// explicit date-pair decisions stored for this profile. Existing profiles may
|
||||
// contain unresolved overlaps, which are returned for the owner to resolve.
|
||||
func RestoreCandidateProfileContentWithParallelPeriods(
|
||||
rows []StoredProfileClaim,
|
||||
acknowledgements []ProfileParallelPeriodAcknowledgement,
|
||||
) (CandidateProfileContent, error) {
|
||||
const location = "domain.RestoreCandidateProfileContent"
|
||||
rules := newProfileRules(time.Time{})
|
||||
fail := func(row StoredProfileClaim, reason string) error {
|
||||
@@ -1070,39 +1348,78 @@ func RestoreCandidateProfileContent(rows []StoredProfileClaim) (CandidateProfile
|
||||
return CandidateProfileContent{}, fail(row, "has invalid provenance")
|
||||
}
|
||||
status := ProfileClaimStatus(row.Status)
|
||||
switch row.Kind {
|
||||
case profileKindFullName:
|
||||
err = restoreSingle(&content.fullName, rules.fullName, row, status, source)
|
||||
case profileKindEmail:
|
||||
err = restoreSingle(&content.email, rules.email, row, status, source)
|
||||
case profileKindPhone:
|
||||
err = restoreSingle(&content.phone, rules.phone, row, status, source)
|
||||
case profileKindTelegram:
|
||||
err = restoreSingle(&content.telegram, rules.telegram, row, status, source)
|
||||
case profileKindLocation:
|
||||
err = restoreSingle(&content.location, rules.location, row, status, source)
|
||||
case profileKindRelocation:
|
||||
err = restoreSingle(&content.relocation, rules.relocation, row, status, source)
|
||||
case profileKindSalaryMinimum:
|
||||
err = restoreSingle(&content.salaryMinimum, rules.salaryMinimum, row, status, source)
|
||||
case profileKindStack:
|
||||
err = restoreItem(&content.stack, rules.stack, row, status, source)
|
||||
case profileKindSoftSkill:
|
||||
err = restoreItem(&content.softSkills, rules.softSkills, row, status, source)
|
||||
case profileKindRole:
|
||||
err = restoreItem(&content.roles, rules.roles, row, status, source)
|
||||
case profileKindExperience:
|
||||
err = restoreItem(&content.experience, rules.experience, row, status, source)
|
||||
default:
|
||||
err = errors.New("kind is unknown")
|
||||
}
|
||||
err = restoreCandidateProfileClaim(&content, rules, row, status, source)
|
||||
if err != nil {
|
||||
return CandidateProfileContent{}, fail(row, err.Error())
|
||||
}
|
||||
}
|
||||
content.parallelPeriodAcknowledgements = slices.Clone(acknowledgements)
|
||||
if err := validateStoredParallelPeriodAcknowledgements(content.experience, content.parallelPeriodAcknowledgements); err != nil {
|
||||
return CandidateProfileContent{}, fmt.Errorf("%s - validate parallel period acknowledgements: %w", location, err)
|
||||
}
|
||||
return content, nil
|
||||
}
|
||||
|
||||
func restoreCandidateProfileClaim(
|
||||
content *CandidateProfileContent,
|
||||
rules profileRules,
|
||||
row StoredProfileClaim,
|
||||
status ProfileClaimStatus,
|
||||
source ProfileClaimSource,
|
||||
) error {
|
||||
switch row.Kind {
|
||||
case profileKindFullName:
|
||||
return restoreSingle(&content.fullName, rules.fullName, row, status, source)
|
||||
case profileKindEmail:
|
||||
return restoreSingle(&content.email, rules.email, row, status, source)
|
||||
case profileKindPhone:
|
||||
return restoreSingle(&content.phone, rules.phone, row, status, source)
|
||||
case profileKindTelegram:
|
||||
return restoreSingle(&content.telegram, rules.telegram, row, status, source)
|
||||
case profileKindLocation:
|
||||
return restoreSingle(&content.location, rules.location, row, status, source)
|
||||
case profileKindRelocation:
|
||||
return restoreSingle(&content.relocation, rules.relocation, row, status, source)
|
||||
case profileKindSalaryMinimum:
|
||||
return restoreSingle(&content.salaryMinimum, rules.salaryMinimum, row, status, source)
|
||||
case profileKindStack:
|
||||
return restoreItem(&content.stack, rules.stack, row, status, source)
|
||||
case profileKindSoftSkill:
|
||||
return restoreItem(&content.softSkills, rules.softSkills, row, status, source)
|
||||
case profileKindRole:
|
||||
return restoreItem(&content.roles, rules.roles, row, status, source)
|
||||
case profileKindExperience:
|
||||
return restoreItem(&content.experience, rules.experience, row, status, source)
|
||||
default:
|
||||
return errors.New("kind is unknown")
|
||||
}
|
||||
}
|
||||
|
||||
func validateStoredParallelPeriodAcknowledgements(
|
||||
experiences []ProfileClaim[ProfileExperience],
|
||||
acknowledgements []ProfileParallelPeriodAcknowledgement,
|
||||
) error {
|
||||
const location = "domain.validateStoredParallelPeriodAcknowledgements"
|
||||
seen := make(map[experiencePair]struct{}, len(acknowledgements))
|
||||
for _, acknowledgement := range acknowledgements {
|
||||
pair := experiencePair{first: acknowledgement.FirstIndex, second: acknowledgement.SecondIndex}
|
||||
if pair.first < 0 || pair.second <= pair.first || pair.second >= len(experiences) {
|
||||
return fmt.Errorf("%s - pair indices are invalid: %w", location, ErrInvalidCandidateProfile)
|
||||
}
|
||||
if _, duplicate := seen[pair]; duplicate {
|
||||
return fmt.Errorf("%s - pair is duplicated: %w", location, ErrInvalidCandidateProfile)
|
||||
}
|
||||
seen[pair] = struct{}{}
|
||||
first := experiences[pair.first].value
|
||||
second := experiences[pair.second].value
|
||||
if experiencePeriod(first) != acknowledgement.FirstPeriod ||
|
||||
experiencePeriod(second) != acknowledgement.SecondPeriod || !experiencePeriodsOverlap(first, second) {
|
||||
return fmt.Errorf("%s - pair does not match the stored experience periods: %w", location, ErrInvalidCandidateProfile)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// decodeStoredValue decodes the canonical JSON of a stored value into V.
|
||||
func decodeStoredValue[V comparable](row StoredProfileClaim) (V, error) {
|
||||
var value V
|
||||
@@ -1202,13 +1519,15 @@ func restoreItem[V comparable](
|
||||
return nil
|
||||
}
|
||||
|
||||
// StoredCandidateProfile carries the profile row and its claims.
|
||||
// StoredCandidateProfile carries the profile row, claims, and acknowledged
|
||||
// overlapping date pairs.
|
||||
type StoredCandidateProfile struct {
|
||||
UserID uuid.UUID
|
||||
Version int64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Claims []StoredProfileClaim
|
||||
UserID uuid.UUID
|
||||
Version int64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
Claims []StoredProfileClaim
|
||||
ParallelPeriodAcknowledgements []ProfileParallelPeriodAcknowledgement
|
||||
}
|
||||
|
||||
// RestoreCandidateProfile validates a stored profile with the rules of the
|
||||
@@ -1224,7 +1543,7 @@ func RestoreCandidateProfile(stored StoredCandidateProfile) (CandidateProfile, e
|
||||
case stored.CreatedAt.IsZero() || stored.UpdatedAt.Before(stored.CreatedAt):
|
||||
return CandidateProfile{}, fmt.Errorf("%s - times must be set and ordered: %w", location, ErrInvalidCandidateProfile)
|
||||
}
|
||||
content, err := RestoreCandidateProfileContent(stored.Claims)
|
||||
content, err := RestoreCandidateProfileContentWithParallelPeriods(stored.Claims, stored.ParallelPeriodAcknowledgements)
|
||||
if err != nil {
|
||||
return CandidateProfile{}, fmt.Errorf("%s - restore content: %w", location, err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user