90 lines
3.5 KiB
Go
90 lines
3.5 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var ErrInvalidCandidateViewSource = errors.New("invalid candidate view source")
|
|
|
|
// CandidateViewSource is a private read-model source for one currently
|
|
// published candidate. Its identifier and source values must never be logged
|
|
// or serialized as an organization-facing response.
|
|
type CandidateViewSource struct {
|
|
candidateID uuid.UUID
|
|
publishedFields CardFieldSet
|
|
profile CandidateViewProfile
|
|
confirmedCategories []CandidateViewConfirmedCategoryInput
|
|
workFormats []QuestionnaireWorkFormat
|
|
salaryCompatibility *CandidateViewSalaryCompatibility
|
|
}
|
|
|
|
func NewCandidateViewSource(
|
|
candidateID uuid.UUID,
|
|
publishedFields CardFieldSet,
|
|
profile CandidateViewProfile,
|
|
confirmedCategories []CandidateViewConfirmedCategoryInput,
|
|
workFormats []QuestionnaireWorkFormat,
|
|
salaryCompatibility *CandidateViewSalaryCompatibility,
|
|
) (CandidateViewSource, error) {
|
|
if candidateID == uuid.Nil || publishedFields.IsEmpty() {
|
|
return CandidateViewSource{}, ErrInvalidCandidateViewSource
|
|
}
|
|
if len(workFormats) > maximumCandidateViewWorkFormats || len(confirmedCategories) > maximumCandidateViewSpecializations {
|
|
return CandidateViewSource{}, ErrInvalidCandidateViewSource
|
|
}
|
|
return CandidateViewSource{
|
|
candidateID: candidateID, publishedFields: publishedFields,
|
|
profile: cloneCandidateViewProfile(profile),
|
|
confirmedCategories: slices.Clone(confirmedCategories), workFormats: slices.Clone(workFormats),
|
|
salaryCompatibility: clonePointer(salaryCompatibility),
|
|
}, nil
|
|
}
|
|
|
|
func (CandidateViewSource) String() string { return "[private candidate view source]" }
|
|
func (CandidateViewSource) GoString() string { return "[private candidate view source]" }
|
|
func (CandidateViewSource) LogValue() slog.Value {
|
|
return slog.StringValue("[private candidate view source]")
|
|
}
|
|
|
|
// CandidateID is an internal join key for deriving the organization-scoped
|
|
// pseudonym. It is not part of the anonymous DTO.
|
|
func (source CandidateViewSource) CandidateID() uuid.UUID { return source.candidateID }
|
|
|
|
// PublishedFields returns the selected allowlist for the private projection.
|
|
func (source CandidateViewSource) PublishedFields() CardFieldSet { return source.publishedFields }
|
|
|
|
// Input returns detached, typed projection data without the candidate ID.
|
|
func (source CandidateViewSource) Input(pseudonym string, asOf time.Time) CandidateViewInput {
|
|
return CandidateViewInput{
|
|
Pseudonym: pseudonym, PublishedFields: source.publishedFields,
|
|
Profile: cloneCandidateViewProfile(source.profile), AsOf: asOf,
|
|
ConfirmedCategories: slices.Clone(source.confirmedCategories),
|
|
WorkFormats: slices.Clone(source.workFormats),
|
|
SalaryCompatibility: clonePointer(source.salaryCompatibility),
|
|
}
|
|
}
|
|
|
|
func cloneCandidateViewProfile(profile CandidateViewProfile) CandidateViewProfile {
|
|
return CandidateViewProfile{
|
|
stack: slices.Clone(profile.stack), roles: slices.Clone(profile.roles),
|
|
experience: slices.Clone(profile.experience),
|
|
projects: slices.Clone(profile.projects),
|
|
}
|
|
}
|
|
|
|
func ValidateCandidateViewSource(source CandidateViewSource) error {
|
|
if source.candidateID == uuid.Nil || source.publishedFields.IsEmpty() {
|
|
return fmt.Errorf("candidate view source identity or publication is invalid: %w", ErrInvalidCandidateViewSource)
|
|
}
|
|
if source.salaryCompatibility != nil && !source.salaryCompatibility.IsKnown() {
|
|
return fmt.Errorf("candidate view source compatibility is invalid: %w", ErrInvalidCandidateViewSource)
|
|
}
|
|
return nil
|
|
}
|