feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
+118
-24
@@ -1,6 +1,7 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -25,16 +26,19 @@ var (
|
||||
)
|
||||
|
||||
const (
|
||||
maxNeedTitleRunes = 200
|
||||
maxNeedSourceTextRunes = 10000
|
||||
maxNeedNameRunes = 100
|
||||
maxNeedTaskRunes = 300
|
||||
maxNeedQuoteRunes = 300
|
||||
maxNeedSkillItems = 30
|
||||
maxNeedTaskItems = 20
|
||||
maxNeedWorkFormats = 3
|
||||
MaxNeedSnapshotBytes = 262144
|
||||
redactedNeed = "[redacted need]"
|
||||
maxNeedTitleRunes = 200
|
||||
maxNeedSourceTextRunes = 10000
|
||||
maxNeedNameRunes = 100
|
||||
maxNeedTaskRunes = 300
|
||||
maxNeedQuoteRunes = 300
|
||||
maxNeedSkillItems = 30
|
||||
maxNeedTaskItems = 20
|
||||
maxNeedWorkFormats = 3
|
||||
MaxNeedRejectedSuggestions = 64
|
||||
MaxNeedSnapshotBytes = 262144
|
||||
MaxNeedJSONBTextHeadroom = 8192
|
||||
MaxNeedSnapshotStorageBytes = MaxNeedSnapshotBytes + MaxNeedJSONBTextHeadroom
|
||||
redactedNeed = "[redacted need]"
|
||||
)
|
||||
|
||||
// NeedValueStatus is the confirmation state of one need field.
|
||||
@@ -157,16 +161,39 @@ func EmptyNeedRequirements() NeedRequirements {
|
||||
// NeedSnapshot is the complete immutable business state of one need version.
|
||||
// Derived readiness and recommendations are computed separately.
|
||||
type NeedSnapshot struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
OrganizationID uuid.UUID `json:"organization_id"`
|
||||
VacancyID *uuid.UUID `json:"vacancy_id"`
|
||||
Title string `json:"title"`
|
||||
SourceText *string `json:"source_text"`
|
||||
Version int64 `json:"version"`
|
||||
RequirementsVersion int64 `json:"requirements_version"`
|
||||
Requirements NeedRequirements `json:"requirements"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
OrganizationID uuid.UUID `json:"organization_id"`
|
||||
VacancyID *uuid.UUID `json:"vacancy_id"`
|
||||
Title string `json:"title"`
|
||||
SourceText *string `json:"source_text"`
|
||||
Version int64 `json:"version"`
|
||||
RequirementsVersion int64 `json:"requirements_version"`
|
||||
Requirements NeedRequirements `json:"requirements"`
|
||||
RejectedSuggestions []NeedRejectedSuggestion `json:"rejected_suggestions"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// NeedRejectedSuggestionField groups extracted values for suppression after
|
||||
// an owner explicitly deletes them.
|
||||
type NeedRejectedSuggestionField string
|
||||
|
||||
const (
|
||||
NeedRejectedSpecialization NeedRejectedSuggestionField = "specialization"
|
||||
NeedRejectedGrade NeedRejectedSuggestionField = "grade"
|
||||
NeedRejectedSkills NeedRejectedSuggestionField = "skills"
|
||||
NeedRejectedTasks NeedRejectedSuggestionField = "tasks"
|
||||
NeedRejectedDomain NeedRejectedSuggestionField = "domain"
|
||||
NeedRejectedWorkFormats NeedRejectedSuggestionField = "work_formats"
|
||||
NeedRejectedLocation NeedRejectedSuggestionField = "location"
|
||||
NeedRejectedSalary NeedRejectedSuggestionField = "salary"
|
||||
)
|
||||
|
||||
// NeedRejectedSuggestion records an extracted proposal that an owner removed.
|
||||
// It is private need state and is never included in the public Need response.
|
||||
type NeedRejectedSuggestion struct {
|
||||
Field NeedRejectedSuggestionField `json:"field"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// Need is one immutable version of an organization's hiring need. Snapshot
|
||||
@@ -190,6 +217,9 @@ func RestoreNeed(snapshot NeedSnapshot) (Need, error) {
|
||||
const location = "domain.RestoreNeed"
|
||||
snapshot.CreatedAt = canonicalNeedTime(snapshot.CreatedAt)
|
||||
snapshot.UpdatedAt = canonicalNeedTime(snapshot.UpdatedAt)
|
||||
if snapshot.RejectedSuggestions == nil {
|
||||
snapshot.RejectedSuggestions = []NeedRejectedSuggestion{}
|
||||
}
|
||||
if err := validateNeedSnapshot(location, snapshot); err != nil {
|
||||
return Need{}, err
|
||||
}
|
||||
@@ -453,6 +483,19 @@ func confirmedNeedTasks(tasks []NeedTask) []string {
|
||||
}
|
||||
|
||||
func validateNeedSnapshot(location string, snapshot NeedSnapshot) error {
|
||||
if err := validateNeedSnapshotMetadata(location, snapshot); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateNeedRejectedSuggestions(location, snapshot.RejectedSuggestions); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateNeedRequirements(location, snapshot.Version, snapshot.Requirements); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateNeedSnapshotSize(location, snapshot)
|
||||
}
|
||||
|
||||
func validateNeedSnapshotMetadata(location string, snapshot NeedSnapshot) error {
|
||||
switch {
|
||||
case snapshot.ID == uuid.Nil || snapshot.OrganizationID == uuid.Nil:
|
||||
return invalidNeed(location, "need and organization IDs must be set")
|
||||
@@ -462,10 +505,60 @@ func validateNeedSnapshot(location string, snapshot NeedSnapshot) error {
|
||||
return invalidNeedField(location, "/title", "must be 1 to 200 characters without surrounding whitespace or line breaks")
|
||||
case snapshot.SourceText != nil && !validNeedLength(*snapshot.SourceText, maxNeedSourceTextRunes):
|
||||
return invalidNeedField(location, "/source_text", "must be 1 to 10000 characters")
|
||||
case snapshot.CreatedAt.IsZero() || snapshot.UpdatedAt.IsZero() || snapshot.UpdatedAt.Before(snapshot.CreatedAt):
|
||||
return invalidNeed(location, "timestamps must be set and ordered")
|
||||
case !validNeedTimestamp(snapshot.CreatedAt) || !validNeedTimestamp(snapshot.UpdatedAt) || snapshot.UpdatedAt.Before(snapshot.CreatedAt):
|
||||
return invalidNeed(location, "timestamps must be set, representable, and ordered")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateNeedSnapshotSize(location string, snapshot NeedSnapshot) error {
|
||||
encoded, err := json.Marshal(snapshot)
|
||||
if err != nil {
|
||||
return invalidNeed(location, "snapshot cannot be encoded")
|
||||
}
|
||||
if len(encoded) > MaxNeedSnapshotBytes {
|
||||
return invalidNeedField(location, "/rejected_suggestions", "combined need snapshot exceeds its storage bound")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validNeedTimestamp(value time.Time) bool {
|
||||
return !value.IsZero() && value.Year() >= 1 && value.Year() <= 9999
|
||||
}
|
||||
|
||||
func validateNeedRejectedSuggestions(location string, suggestions []NeedRejectedSuggestion) error {
|
||||
if suggestions == nil || len(suggestions) > MaxNeedRejectedSuggestions {
|
||||
return invalidNeedField(location, "/rejected_suggestions", "must be present and within its storage bound")
|
||||
}
|
||||
seen := make(map[string]struct{}, len(suggestions))
|
||||
for index, suggestion := range suggestions {
|
||||
pointer := "/rejected_suggestions/" + strconv.Itoa(index)
|
||||
if !knownNeedRejectedSuggestionField(suggestion.Field) || !validNeedLength(suggestion.Value, maxNeedTaskRunes) ||
|
||||
strings.TrimSpace(suggestion.Value) != suggestion.Value || strings.ToLower(suggestion.Value) != suggestion.Value {
|
||||
return invalidNeedField(location, pointer, "must have a normalized field and value")
|
||||
}
|
||||
for _, char := range suggestion.Value {
|
||||
if isNeedLineBreak(char) {
|
||||
return invalidNeedField(location, pointer+"/value", "must not contain line breaks")
|
||||
}
|
||||
}
|
||||
key := string(suggestion.Field) + "\x00" + suggestion.Value
|
||||
if _, exists := seen[key]; exists {
|
||||
return invalidNeedField(location, pointer, "duplicates a rejected suggestion")
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func knownNeedRejectedSuggestionField(field NeedRejectedSuggestionField) bool {
|
||||
switch field {
|
||||
case NeedRejectedSpecialization, NeedRejectedGrade, NeedRejectedSkills, NeedRejectedTasks,
|
||||
NeedRejectedDomain, NeedRejectedWorkFormats, NeedRejectedLocation, NeedRejectedSalary:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return validateNeedRequirements(location, snapshot.Version, snapshot.Requirements)
|
||||
}
|
||||
|
||||
func validateNeedRequirements(location string, version int64, requirements NeedRequirements) error {
|
||||
@@ -718,7 +811,7 @@ func validNeedText(value string, maxRunes int) bool {
|
||||
}
|
||||
|
||||
func validNeedLength(value string, maxRunes int) bool {
|
||||
if !utf8.ValidString(value) {
|
||||
if !utf8.ValidString(value) || strings.ContainsRune(value, '\x00') {
|
||||
return false
|
||||
}
|
||||
length := utf8.RuneCountInString(value)
|
||||
@@ -743,6 +836,7 @@ func cloneNeedSnapshot(snapshot NeedSnapshot) NeedSnapshot {
|
||||
cloned.VacancyID = cloneNeedPointer(snapshot.VacancyID)
|
||||
cloned.SourceText = cloneNeedPointer(snapshot.SourceText)
|
||||
cloned.Requirements = cloneNeedRequirements(snapshot.Requirements)
|
||||
cloned.RejectedSuggestions = slices.Clone(snapshot.RejectedSuggestions)
|
||||
return cloned
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user