725 lines
26 KiB
Go
725 lines
26 KiB
Go
package domain
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/text/unicode/norm"
|
|
)
|
|
|
|
var ErrInvalidAnonymousCandidateView = errors.New("invalid anonymous candidate view")
|
|
|
|
const (
|
|
minimumAnonymousCandidateCohort = 5
|
|
maximumCandidateViewGeneralization = 5
|
|
maximumCandidateViewSpecializations = 9
|
|
maximumCandidateViewWorkFormats = 3
|
|
candidateViewPseudonymPrefix = "cv1_"
|
|
minimumCandidateViewPseudonymKeyLen = 32
|
|
)
|
|
|
|
const candidateViewPseudonymPurpose = "lincodex/anonymous-candidate-view/v1\x00"
|
|
const candidatePreviewPseudonymPurpose = "lincodex/anonymous-candidate-preview/v1\x00"
|
|
|
|
// CandidateViewPseudonymKey is a dedicated secret for organization-scoped
|
|
// candidate aliases. It must not be reused for another HMAC purpose.
|
|
type CandidateViewPseudonymKey struct {
|
|
secret []byte
|
|
}
|
|
|
|
func NewCandidateViewPseudonymKey(secret []byte) (CandidateViewPseudonymKey, error) {
|
|
if len(secret) < minimumCandidateViewPseudonymKeyLen {
|
|
return CandidateViewPseudonymKey{}, ErrInvalidAnonymousCandidateView
|
|
}
|
|
return CandidateViewPseudonymKey{secret: slices.Clone(secret)}, nil
|
|
}
|
|
|
|
func (CandidateViewPseudonymKey) String() string { return "[candidate view pseudonym key]" }
|
|
func (CandidateViewPseudonymKey) GoString() string { return "[candidate view pseudonym key]" }
|
|
func (CandidateViewPseudonymKey) LogValue() slog.Value {
|
|
return slog.StringValue("[candidate view pseudonym key]")
|
|
}
|
|
|
|
// ForOrganization derives a stable pseudonym for one candidate in one
|
|
// organization. Fixed-width UUID bytes and a purpose label make the HMAC
|
|
// input unambiguous and separate it from other keyed identifiers.
|
|
func (key CandidateViewPseudonymKey) ForOrganization(organizationID, candidateID uuid.UUID) (string, error) {
|
|
if len(key.secret) < minimumCandidateViewPseudonymKeyLen || organizationID == uuid.Nil || candidateID == uuid.Nil {
|
|
return "", ErrInvalidAnonymousCandidateView
|
|
}
|
|
mac := hmac.New(sha256.New, key.secret)
|
|
_, _ = mac.Write([]byte(candidateViewPseudonymPurpose))
|
|
_, _ = mac.Write(organizationID[:])
|
|
_, _ = mac.Write(candidateID[:])
|
|
return candidateViewPseudonymPrefix + base64.RawURLEncoding.EncodeToString(mac.Sum(nil)), nil
|
|
}
|
|
|
|
// ForCandidatePreview derives a private preview alias. Its HMAC purpose is
|
|
// separate from organization aliases so a preview never reveals a linkable
|
|
// organization identity.
|
|
func (key CandidateViewPseudonymKey) ForCandidatePreview(candidateID uuid.UUID) (string, error) {
|
|
if len(key.secret) < minimumCandidateViewPseudonymKeyLen || candidateID == uuid.Nil {
|
|
return "", ErrInvalidAnonymousCandidateView
|
|
}
|
|
mac := hmac.New(sha256.New, key.secret)
|
|
_, _ = mac.Write([]byte(candidatePreviewPseudonymPurpose))
|
|
_, _ = mac.Write(candidateID[:])
|
|
return candidateViewPseudonymPrefix + base64.RawURLEncoding.EncodeToString(mac.Sum(nil)), nil
|
|
}
|
|
|
|
// CandidateViewSkillCode is a closed technology taxonomy. Raw profile tags
|
|
// are never copied to an organization-facing view.
|
|
type CandidateViewSkillCode string
|
|
|
|
const (
|
|
CandidateViewSkillGo CandidateViewSkillCode = "go"
|
|
CandidateViewSkillPostgreSQL CandidateViewSkillCode = "postgresql"
|
|
CandidateViewSkillKafka CandidateViewSkillCode = "kafka"
|
|
CandidateViewSkillDocker CandidateViewSkillCode = "docker"
|
|
)
|
|
|
|
var candidateViewSkillCodes = []CandidateViewSkillCode{
|
|
CandidateViewSkillDocker,
|
|
CandidateViewSkillGo,
|
|
CandidateViewSkillKafka,
|
|
CandidateViewSkillPostgreSQL,
|
|
}
|
|
|
|
func (code CandidateViewSkillCode) IsKnown() bool {
|
|
return slices.Contains(candidateViewSkillCodes, code)
|
|
}
|
|
|
|
// CandidateViewSkillCodes returns the closed skill taxonomy in canonical
|
|
// order.
|
|
func CandidateViewSkillCodes() []CandidateViewSkillCode {
|
|
return slices.Clone(candidateViewSkillCodes)
|
|
}
|
|
|
|
// CanonicalCandidateViewSkill maps a confirmed profile tag only when its
|
|
// normalized value exactly matches a supported taxonomy entry.
|
|
func CanonicalCandidateViewSkill(raw string) (CandidateViewSkillCode, bool) {
|
|
normalized := strings.ToLower(strings.TrimSpace(norm.NFKC.String(raw)))
|
|
switch normalized {
|
|
case string(CandidateViewSkillGo):
|
|
return CandidateViewSkillGo, true
|
|
case string(CandidateViewSkillPostgreSQL):
|
|
return CandidateViewSkillPostgreSQL, true
|
|
case string(CandidateViewSkillKafka):
|
|
return CandidateViewSkillKafka, true
|
|
case string(CandidateViewSkillDocker):
|
|
return CandidateViewSkillDocker, true
|
|
default:
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
// CandidateExperienceBand is the only work-tenure precision available in an
|
|
// anonymous candidate view. The first four values are the normal buckets;
|
|
// the last two are emitted only when a rare combination must be generalized.
|
|
type CandidateExperienceBand string
|
|
|
|
const (
|
|
CandidateExperienceBandZeroToOne CandidateExperienceBand = "0_1"
|
|
CandidateExperienceBandOneToThree CandidateExperienceBand = "1_3"
|
|
CandidateExperienceBandThreeToSix CandidateExperienceBand = "3_6"
|
|
CandidateExperienceBandSixPlus CandidateExperienceBand = "6_plus"
|
|
CandidateExperienceBandZeroToThree CandidateExperienceBand = "0_3"
|
|
CandidateExperienceBandThreePlus CandidateExperienceBand = "3_plus"
|
|
)
|
|
|
|
func (band CandidateExperienceBand) IsKnown() bool {
|
|
switch band {
|
|
case CandidateExperienceBandZeroToOne, CandidateExperienceBandOneToThree,
|
|
CandidateExperienceBandThreeToSix, CandidateExperienceBandSixPlus,
|
|
CandidateExperienceBandZeroToThree, CandidateExperienceBandThreePlus:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// CandidateViewConfirmationStatus says whether the current authoritative
|
|
// result source confirms a specialization. It carries no attempt history.
|
|
type CandidateViewConfirmationStatus string
|
|
|
|
const (
|
|
CandidateViewConfirmationConfirmed CandidateViewConfirmationStatus = "confirmed"
|
|
CandidateViewConfirmationUnconfirmed CandidateViewConfirmationStatus = "unconfirmed"
|
|
)
|
|
|
|
// CandidateViewConfirmationStrength is a coarse, non-numeric description of
|
|
// an authoritative confirmation. It is absent when no category is confirmed.
|
|
type CandidateViewConfirmationStrength string
|
|
|
|
const (
|
|
CandidateViewStrengthStandard CandidateViewConfirmationStrength = "standard"
|
|
CandidateViewStrengthStrong CandidateViewConfirmationStrength = "strong"
|
|
)
|
|
|
|
func (strength CandidateViewConfirmationStrength) IsKnown() bool {
|
|
return strength == CandidateViewStrengthStandard || strength == CandidateViewStrengthStrong
|
|
}
|
|
|
|
// CandidateViewConfirmedCategoryInput is accepted only from an authoritative
|
|
// assessment-result source. It intentionally has no field for attempts,
|
|
// failures, scores, or the candidate's self-assessment.
|
|
type CandidateViewConfirmedCategoryInput struct {
|
|
Specialization SpecializationCode
|
|
Grade GradeCode
|
|
Strength CandidateViewConfirmationStrength
|
|
// Basis says what the grade rests on. It is required: a category without
|
|
// a basis would read as a real confirmation.
|
|
Basis AssessmentCategorySource
|
|
}
|
|
|
|
type candidateViewStackClaim struct {
|
|
value string
|
|
status ProfileClaimStatus
|
|
}
|
|
|
|
type candidateViewRoleClaim struct {
|
|
value ProfileRole
|
|
status ProfileClaimStatus
|
|
}
|
|
|
|
type candidateViewExperienceClaim struct {
|
|
startedOn string
|
|
endedOn string
|
|
status ProfileClaimStatus
|
|
}
|
|
|
|
// CandidateViewProfile is the private, minimal input needed for projection.
|
|
// It carries no name, contact, employer, resume quote, summary, salary, or
|
|
// free-text location or soft skill.
|
|
type CandidateViewProfile struct {
|
|
stack []candidateViewStackClaim
|
|
roles []candidateViewRoleClaim
|
|
experience []candidateViewExperienceClaim
|
|
}
|
|
|
|
func (CandidateViewProfile) String() string { return "[candidate view profile]" }
|
|
func (CandidateViewProfile) GoString() string { return "[candidate view profile]" }
|
|
func (CandidateViewProfile) LogValue() slog.Value {
|
|
return slog.StringValue("[candidate view profile]")
|
|
}
|
|
|
|
// CandidateViewProfileFromContent removes private profile values that are
|
|
// not needed to build the anonymous organization-facing view.
|
|
func CandidateViewProfileFromContent(content CandidateProfileContent) CandidateViewProfile {
|
|
profile := CandidateViewProfile{}
|
|
for _, claim := range content.Stack() {
|
|
profile.stack = append(profile.stack, candidateViewStackClaim{value: claim.Value(), status: claim.Status()})
|
|
}
|
|
for _, claim := range content.Roles() {
|
|
profile.roles = append(profile.roles, candidateViewRoleClaim{value: claim.Value(), status: claim.Status()})
|
|
}
|
|
for _, claim := range content.Experience() {
|
|
value := claim.Value()
|
|
profile.experience = append(profile.experience, candidateViewExperienceClaim{
|
|
startedOn: value.StartedOn, endedOn: value.EndedOn, status: claim.Status(),
|
|
})
|
|
}
|
|
return profile
|
|
}
|
|
|
|
// CandidateViewCategory is the category, its coarse strength, and its basis,
|
|
// shown only when categories were explicitly included in the publication.
|
|
// The field order matches CandidateViewConfirmedCategoryInput so that the two
|
|
// convert into each other.
|
|
type CandidateViewCategory struct {
|
|
Specialization SpecializationCode `json:"specialization"`
|
|
Grade GradeCode `json:"grade"`
|
|
Strength CandidateViewConfirmationStrength `json:"strength"`
|
|
Basis AssessmentCategorySource `json:"basis"`
|
|
}
|
|
|
|
// CandidateViewSelfAssessment is a candidate-declared specialization and
|
|
// grade, separate from the confirmed categories collection.
|
|
type CandidateViewSelfAssessment struct {
|
|
Specialization SpecializationCode `json:"specialization"`
|
|
ConfirmationStatus CandidateViewConfirmationStatus `json:"confirmation_status"`
|
|
DeclaredGrade *GradeCode `json:"declared_grade,omitempty"`
|
|
}
|
|
|
|
// CandidateViewSalaryCompatibility is a typed comparison result. The numeric
|
|
// salary bounds and the candidate's minimum are never part of a view.
|
|
type CandidateViewSalaryCompatibility string
|
|
|
|
const (
|
|
CandidateViewSalaryCompatible CandidateViewSalaryCompatibility = "compatible"
|
|
CandidateViewSalaryNotCompatible CandidateViewSalaryCompatibility = "not_compatible"
|
|
)
|
|
|
|
func (compatibility CandidateViewSalaryCompatibility) IsKnown() bool {
|
|
return compatibility == CandidateViewSalaryCompatible || compatibility == CandidateViewSalaryNotCompatible
|
|
}
|
|
|
|
// AnonymousCandidateViewDTO is the complete closed organization-facing data
|
|
// transfer object. It contains no account ID, raw profile fields, or history.
|
|
type AnonymousCandidateViewDTO struct {
|
|
Pseudonym string `json:"pseudonym"`
|
|
Skills []CandidateViewSkillCode `json:"skills"`
|
|
Categories []CandidateViewCategory `json:"categories"`
|
|
SelfAssessment []CandidateViewSelfAssessment `json:"self_assessment"`
|
|
ExperienceBand *CandidateExperienceBand `json:"experience_band"`
|
|
WorkFormats []QuestionnaireWorkFormat `json:"work_formats"`
|
|
SalaryCompatibility *CandidateViewSalaryCompatibility `json:"salary_compatibility"`
|
|
}
|
|
|
|
func (AnonymousCandidateViewDTO) String() string { return "[anonymous candidate view]" }
|
|
func (AnonymousCandidateViewDTO) GoString() string { return "[anonymous candidate view]" }
|
|
func (AnonymousCandidateViewDTO) LogValue() slog.Value {
|
|
return slog.StringValue("[anonymous candidate view]")
|
|
}
|
|
|
|
// CandidateViewInput combines explicitly published profile data with typed
|
|
// results from authoritative sources. Profile content stays private and is
|
|
// projected through a closed allowlist.
|
|
type CandidateViewInput struct {
|
|
Pseudonym string
|
|
PublishedFields CardFieldSet
|
|
Profile CandidateViewProfile
|
|
AsOf time.Time
|
|
ConfirmedCategories []CandidateViewConfirmedCategoryInput
|
|
WorkFormats []QuestionnaireWorkFormat
|
|
SalaryCompatibility *CandidateViewSalaryCompatibility
|
|
}
|
|
|
|
func (CandidateViewInput) String() string { return "[candidate view input]" }
|
|
func (CandidateViewInput) GoString() string { return "[candidate view input]" }
|
|
func (CandidateViewInput) LogValue() slog.Value {
|
|
return slog.StringValue("[candidate view input]")
|
|
}
|
|
|
|
// AnonymousCandidateView is a validated, organization-scoped anonymous
|
|
// projection. The internal generalization level is never exposed.
|
|
type AnonymousCandidateView struct {
|
|
dto AnonymousCandidateViewDTO
|
|
generalizationLevel int
|
|
}
|
|
|
|
// NewAnonymousCandidateView builds a strictly allowlisted projection from
|
|
// currently published fields and typed, authoritative inputs.
|
|
func NewAnonymousCandidateView(input CandidateViewInput) (AnonymousCandidateView, error) {
|
|
const location = "domain.NewAnonymousCandidateView"
|
|
if !validCandidateViewPseudonym(input.Pseudonym) {
|
|
return AnonymousCandidateView{}, fmt.Errorf("%s - pseudonym is invalid: %w", location, ErrInvalidAnonymousCandidateView)
|
|
}
|
|
if err := validateCandidateViewConfirmations(input.ConfirmedCategories); err != nil {
|
|
return AnonymousCandidateView{}, fmt.Errorf("%s - confirmations are invalid: %w", location, err)
|
|
}
|
|
view := AnonymousCandidateView{dto: AnonymousCandidateViewDTO{Pseudonym: input.Pseudonym}}
|
|
if input.PublishedFields.Contains(CardFieldStack) {
|
|
view.dto.Skills = candidateViewSkills(input.Profile)
|
|
}
|
|
if input.PublishedFields.Contains(CardFieldExperience) {
|
|
band, err := candidateViewExperienceBand(input.Profile.experience, input.AsOf)
|
|
if err != nil {
|
|
return AnonymousCandidateView{}, fmt.Errorf("%s - project experience: %w", location, err)
|
|
}
|
|
view.dto.ExperienceBand = band
|
|
}
|
|
if input.PublishedFields.Contains(CardFieldCategories) {
|
|
view.dto.Categories = candidateViewCategories(input.ConfirmedCategories)
|
|
}
|
|
if input.PublishedFields.Contains(CardFieldSelfAssessment) {
|
|
view.dto.SelfAssessment = candidateViewSelfAssessments(input.Profile.roles, input.ConfirmedCategories)
|
|
}
|
|
if input.PublishedFields.Contains(CardFieldWorkFormats) {
|
|
formats, err := canonicalCandidateViewWorkFormats(input.WorkFormats)
|
|
if err != nil {
|
|
return AnonymousCandidateView{}, fmt.Errorf("%s - work formats are invalid: %w", location, err)
|
|
}
|
|
view.dto.WorkFormats = formats
|
|
}
|
|
if input.PublishedFields.Contains(CardFieldSalaryCompatibility) && input.SalaryCompatibility != nil {
|
|
if !input.SalaryCompatibility.IsKnown() {
|
|
return AnonymousCandidateView{}, fmt.Errorf("%s - salary compatibility is invalid: %w", location, ErrInvalidAnonymousCandidateView)
|
|
}
|
|
value := *input.SalaryCompatibility
|
|
view.dto.SalaryCompatibility = &value
|
|
}
|
|
return view, nil
|
|
}
|
|
|
|
// DTO returns a detached copy of the allowlisted organization-facing value.
|
|
func (view AnonymousCandidateView) DTO() AnonymousCandidateViewDTO {
|
|
return cloneAnonymousCandidateViewDTO(view.dto)
|
|
}
|
|
|
|
func cloneAnonymousCandidateViewDTO(dto AnonymousCandidateViewDTO) AnonymousCandidateViewDTO {
|
|
dto.Skills = slices.Clone(dto.Skills)
|
|
dto.Categories = slices.Clone(dto.Categories)
|
|
dto.SelfAssessment = slices.Clone(dto.SelfAssessment)
|
|
for index := range dto.SelfAssessment {
|
|
dto.SelfAssessment[index].DeclaredGrade = clonePointer(dto.SelfAssessment[index].DeclaredGrade)
|
|
}
|
|
dto.ExperienceBand = clonePointer(dto.ExperienceBand)
|
|
dto.WorkFormats = slices.Clone(dto.WorkFormats)
|
|
dto.SalaryCompatibility = clonePointer(dto.SalaryCompatibility)
|
|
return dto
|
|
}
|
|
|
|
func validCandidateViewPseudonym(pseudonym string) bool {
|
|
if !strings.HasPrefix(pseudonym, candidateViewPseudonymPrefix) {
|
|
return false
|
|
}
|
|
encoded := strings.TrimPrefix(pseudonym, candidateViewPseudonymPrefix)
|
|
decoded, err := base64.RawURLEncoding.DecodeString(encoded)
|
|
return err == nil && len(decoded) == sha256.Size && base64.RawURLEncoding.EncodeToString(decoded) == encoded
|
|
}
|
|
|
|
// IsCandidateViewPseudonym reports whether value is a canonical opaque view
|
|
// alias. It does not establish that the alias is visible to any organization.
|
|
func IsCandidateViewPseudonym(value string) bool {
|
|
return validCandidateViewPseudonym(value)
|
|
}
|
|
|
|
func validateCandidateViewConfirmations(confirmations []CandidateViewConfirmedCategoryInput) error {
|
|
seen := make(map[SpecializationCode]struct{}, len(confirmations))
|
|
for _, confirmation := range confirmations {
|
|
if !confirmation.Specialization.IsKnown() || !confirmation.Grade.IsKnown() || !confirmation.Strength.IsKnown() ||
|
|
!confirmation.Basis.IsKnown() {
|
|
return ErrInvalidAnonymousCandidateView
|
|
}
|
|
if _, duplicate := seen[confirmation.Specialization]; duplicate {
|
|
return ErrInvalidAnonymousCandidateView
|
|
}
|
|
seen[confirmation.Specialization] = struct{}{}
|
|
}
|
|
if len(confirmations) > maximumCandidateViewSpecializations {
|
|
return ErrInvalidAnonymousCandidateView
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func candidateViewSkills(profile CandidateViewProfile) []CandidateViewSkillCode {
|
|
seen := make(map[CandidateViewSkillCode]struct{})
|
|
for _, claim := range profile.stack {
|
|
if claim.status != ProfileClaimConfirmed {
|
|
continue
|
|
}
|
|
code, ok := CanonicalCandidateViewSkill(claim.value)
|
|
if ok {
|
|
seen[code] = struct{}{}
|
|
}
|
|
}
|
|
if len(seen) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]CandidateViewSkillCode, 0, len(seen))
|
|
for code := range seen {
|
|
result = append(result, code)
|
|
}
|
|
slices.Sort(result)
|
|
return result
|
|
}
|
|
|
|
func candidateViewExperienceBand(claims []candidateViewExperienceClaim, asOf time.Time) (*CandidateExperienceBand, error) {
|
|
periods := make([]candidateViewMonthRange, 0, len(claims))
|
|
for _, claim := range claims {
|
|
if claim.status != ProfileClaimConfirmed {
|
|
continue
|
|
}
|
|
period, err := candidateViewExperiencePeriod(claim, asOf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
periods = append(periods, period)
|
|
}
|
|
if len(periods) == 0 {
|
|
return nil, nil
|
|
}
|
|
slices.SortFunc(periods, func(left, right candidateViewMonthRange) int {
|
|
if left.start < right.start {
|
|
return -1
|
|
}
|
|
if left.start > right.start {
|
|
return 1
|
|
}
|
|
if left.end < right.end {
|
|
return -1
|
|
}
|
|
if left.end > right.end {
|
|
return 1
|
|
}
|
|
return 0
|
|
})
|
|
months := candidateViewUnionMonthCount(periods)
|
|
if months < 0 {
|
|
return nil, ErrInvalidAnonymousCandidateView
|
|
}
|
|
band := candidateViewBandForMonths(months)
|
|
return &band, nil
|
|
}
|
|
|
|
func candidateViewExperiencePeriod(experience candidateViewExperienceClaim, asOf time.Time) (candidateViewMonthRange, error) {
|
|
start, valid := candidateViewMonthIndex(experience.startedOn)
|
|
if !valid {
|
|
return candidateViewMonthRange{}, ErrInvalidAnonymousCandidateView
|
|
}
|
|
var end int64
|
|
switch {
|
|
case experience.endedOn != "":
|
|
end, valid = candidateViewMonthIndex(experience.endedOn)
|
|
case !asOf.IsZero():
|
|
end, valid = candidateViewMonthIndex(asOf.UTC().Format("2006-01"))
|
|
default:
|
|
return candidateViewMonthRange{}, ErrInvalidAnonymousCandidateView
|
|
}
|
|
if !valid || end < start {
|
|
return candidateViewMonthRange{}, ErrInvalidAnonymousCandidateView
|
|
}
|
|
return candidateViewMonthRange{start: start, end: end}, nil
|
|
}
|
|
|
|
type candidateViewMonthRange struct {
|
|
start int64
|
|
end int64
|
|
}
|
|
|
|
func candidateViewMonthIndex(value string) (int64, bool) {
|
|
if len(value) != 7 || value[4] != '-' {
|
|
return 0, false
|
|
}
|
|
var year, month int64
|
|
if _, err := fmt.Sscanf(value, "%4d-%2d", &year, &month); err != nil || year < 1 || month < 1 || month > 12 {
|
|
return 0, false
|
|
}
|
|
return year*12 + month - 1, true
|
|
}
|
|
|
|
func candidateViewUnionMonthCount(periods []candidateViewMonthRange) int64 {
|
|
start, end := periods[0].start, periods[0].end
|
|
var months int64
|
|
for _, period := range periods[1:] {
|
|
if period.start <= end+1 {
|
|
if period.end > end {
|
|
end = period.end
|
|
}
|
|
continue
|
|
}
|
|
months += end - start + 1
|
|
start, end = period.start, period.end
|
|
}
|
|
return months + end - start + 1
|
|
}
|
|
|
|
func candidateViewBandForMonths(months int64) CandidateExperienceBand {
|
|
switch {
|
|
case months < 12:
|
|
return CandidateExperienceBandZeroToOne
|
|
case months < 36:
|
|
return CandidateExperienceBandOneToThree
|
|
case months < 72:
|
|
return CandidateExperienceBandThreeToSix
|
|
default:
|
|
return CandidateExperienceBandSixPlus
|
|
}
|
|
}
|
|
|
|
func candidateViewCategories(confirmations []CandidateViewConfirmedCategoryInput) []CandidateViewCategory {
|
|
if len(confirmations) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]CandidateViewCategory, 0, len(confirmations))
|
|
for _, confirmation := range confirmations {
|
|
result = append(result, CandidateViewCategory(confirmation))
|
|
}
|
|
slices.SortFunc(result, func(left, right CandidateViewCategory) int {
|
|
return strings.Compare(string(left.Specialization), string(right.Specialization))
|
|
})
|
|
return result
|
|
}
|
|
|
|
func candidateViewSelfAssessments(
|
|
roles []candidateViewRoleClaim,
|
|
confirmations []CandidateViewConfirmedCategoryInput,
|
|
) []CandidateViewSelfAssessment {
|
|
confirmationBySpecialization := make(map[SpecializationCode]struct{}, len(confirmations))
|
|
for _, confirmation := range confirmations {
|
|
confirmationBySpecialization[confirmation.Specialization] = struct{}{}
|
|
}
|
|
result := make([]CandidateViewSelfAssessment, 0, len(roles))
|
|
seen := make(map[SpecializationCode]struct{}, len(roles))
|
|
for _, claim := range roles {
|
|
if claim.status != ProfileClaimConfirmed {
|
|
continue
|
|
}
|
|
role := claim.value
|
|
if _, duplicate := seen[role.Specialization]; duplicate {
|
|
return nil
|
|
}
|
|
seen[role.Specialization] = struct{}{}
|
|
status := CandidateViewConfirmationUnconfirmed
|
|
if _, confirmed := confirmationBySpecialization[role.Specialization]; confirmed {
|
|
status = CandidateViewConfirmationConfirmed
|
|
}
|
|
item := CandidateViewSelfAssessment{Specialization: role.Specialization, ConfirmationStatus: status}
|
|
if role.SelfAssessedGrade != "" {
|
|
grade := role.SelfAssessedGrade
|
|
item.DeclaredGrade = &grade
|
|
}
|
|
result = append(result, item)
|
|
}
|
|
if len(result) == 0 {
|
|
return nil
|
|
}
|
|
slices.SortFunc(result, func(left, right CandidateViewSelfAssessment) int {
|
|
return strings.Compare(string(left.Specialization), string(right.Specialization))
|
|
})
|
|
return result
|
|
}
|
|
|
|
func canonicalCandidateViewWorkFormats(formats []QuestionnaireWorkFormat) ([]QuestionnaireWorkFormat, error) {
|
|
if len(formats) > maximumCandidateViewWorkFormats {
|
|
return nil, ErrInvalidAnonymousCandidateView
|
|
}
|
|
if len(formats) == 0 {
|
|
return nil, nil
|
|
}
|
|
result := slices.Clone(formats)
|
|
for _, format := range result {
|
|
if !format.IsKnown() {
|
|
return nil, ErrInvalidAnonymousCandidateView
|
|
}
|
|
}
|
|
slices.Sort(result)
|
|
if len(slices.Compact(slices.Clone(result))) != len(result) {
|
|
return nil, ErrInvalidAnonymousCandidateView
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// GeneralizeAnonymousCandidateCohort suppresses or generalizes the complete
|
|
// visible quasi-identifier combination until every displayed combination has
|
|
// at least five distinct pseudonyms. The caller must pass only candidates
|
|
// whose C2, publication, and organization visibility checks succeeded.
|
|
func GeneralizeAnonymousCandidateCohort(views []AnonymousCandidateView) ([]AnonymousCandidateView, error) {
|
|
result := make([]AnonymousCandidateView, len(views))
|
|
seenPseudonyms := make(map[string]struct{}, len(views))
|
|
for index, view := range views {
|
|
if !validCandidateViewPseudonym(view.dto.Pseudonym) {
|
|
return nil, ErrInvalidAnonymousCandidateView
|
|
}
|
|
if _, duplicate := seenPseudonyms[view.dto.Pseudonym]; duplicate {
|
|
return nil, ErrInvalidAnonymousCandidateView
|
|
}
|
|
seenPseudonyms[view.dto.Pseudonym] = struct{}{}
|
|
result[index] = AnonymousCandidateView{
|
|
dto: cloneAnonymousCandidateViewDTO(view.dto), generalizationLevel: view.generalizationLevel,
|
|
}
|
|
}
|
|
for pass := 0; pass <= maximumCandidateViewGeneralization; pass++ {
|
|
groups := candidateViewCohortGroups(result)
|
|
changed := false
|
|
for _, indexes := range groups {
|
|
if len(indexes) >= minimumAnonymousCandidateCohort {
|
|
continue
|
|
}
|
|
for _, index := range indexes {
|
|
if result[index].generalizationLevel >= maximumCandidateViewGeneralization {
|
|
continue
|
|
}
|
|
result[index].generalizationLevel++
|
|
applyCandidateViewGeneralization(&result[index])
|
|
changed = true
|
|
}
|
|
}
|
|
if !changed {
|
|
break
|
|
}
|
|
}
|
|
return suppressSmallCandidateViewGroups(result), nil
|
|
}
|
|
|
|
func suppressSmallCandidateViewGroups(views []AnonymousCandidateView) []AnonymousCandidateView {
|
|
groups := candidateViewCohortGroups(views)
|
|
keep := make([]bool, len(views))
|
|
for _, indexes := range groups {
|
|
if len(indexes) < minimumAnonymousCandidateCohort {
|
|
continue
|
|
}
|
|
for _, index := range indexes {
|
|
keep[index] = true
|
|
}
|
|
}
|
|
result := make([]AnonymousCandidateView, 0, len(views))
|
|
for index, view := range views {
|
|
if keep[index] {
|
|
result = append(result, view)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func candidateViewCohortGroups(views []AnonymousCandidateView) map[string][]int {
|
|
groups := make(map[string][]int)
|
|
for index, view := range views {
|
|
encoded, _ := json.Marshal(candidateViewCohortFeatures(view.dto))
|
|
groups[string(encoded)] = append(groups[string(encoded)], index)
|
|
}
|
|
return groups
|
|
}
|
|
|
|
type anonymousCandidateViewFeatures struct {
|
|
Skills []CandidateViewSkillCode `json:"skills"`
|
|
Categories []CandidateViewCategory `json:"categories"`
|
|
SelfAssessment []CandidateViewSelfAssessment `json:"self_assessment"`
|
|
ExperienceBand *CandidateExperienceBand `json:"experience_band"`
|
|
WorkFormats []QuestionnaireWorkFormat `json:"work_formats"`
|
|
SalaryCompatibility *CandidateViewSalaryCompatibility `json:"salary_compatibility"`
|
|
}
|
|
|
|
func candidateViewCohortFeatures(dto AnonymousCandidateViewDTO) anonymousCandidateViewFeatures {
|
|
return anonymousCandidateViewFeatures{
|
|
Skills: dto.Skills, Categories: dto.Categories, SelfAssessment: dto.SelfAssessment,
|
|
ExperienceBand: dto.ExperienceBand, WorkFormats: dto.WorkFormats,
|
|
SalaryCompatibility: dto.SalaryCompatibility,
|
|
}
|
|
}
|
|
|
|
func applyCandidateViewGeneralization(view *AnonymousCandidateView) {
|
|
switch view.generalizationLevel {
|
|
case 1:
|
|
view.dto.Skills = nil
|
|
case 2:
|
|
view.dto.WorkFormats = nil
|
|
case 3:
|
|
view.dto.SalaryCompatibility = nil
|
|
view.dto.ExperienceBand = generalizedCandidateExperienceBand(view.dto.ExperienceBand)
|
|
case 4:
|
|
view.dto.Categories = nil
|
|
view.dto.SelfAssessment = nil
|
|
case 5:
|
|
view.dto.ExperienceBand = nil
|
|
}
|
|
}
|
|
|
|
func generalizedCandidateExperienceBand(band *CandidateExperienceBand) *CandidateExperienceBand {
|
|
if band == nil {
|
|
return nil
|
|
}
|
|
var generalized CandidateExperienceBand
|
|
switch *band {
|
|
case CandidateExperienceBandZeroToOne, CandidateExperienceBandOneToThree:
|
|
generalized = CandidateExperienceBandZeroToThree
|
|
case CandidateExperienceBandThreeToSix, CandidateExperienceBandSixPlus:
|
|
generalized = CandidateExperienceBandThreePlus
|
|
case CandidateExperienceBandZeroToThree, CandidateExperienceBandThreePlus:
|
|
generalized = *band
|
|
default:
|
|
return clonePointer(band)
|
|
}
|
|
return &generalized
|
|
}
|