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
@@ -0,0 +1,156 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var ErrInvalidCandidateCardAccess = errors.New("invalid candidate card access")
|
||||
|
||||
// CandidateCardAccessKind describes an employer read protected by the current
|
||||
// publication and grant policy.
|
||||
type CandidateCardAccessKind string
|
||||
|
||||
const (
|
||||
CandidateCardAccessCardViewed CandidateCardAccessKind = "card_viewed"
|
||||
CandidateCardAccessContactRead CandidateCardAccessKind = "contact_read"
|
||||
)
|
||||
|
||||
// CandidateCardAccessInput contains identifiers and field codes only. It
|
||||
// never stores the content that was read or a search query.
|
||||
type CandidateCardAccessInput struct {
|
||||
CandidateID uuid.UUID
|
||||
OrganizationID uuid.UUID
|
||||
ActorID uuid.UUID
|
||||
Kind CandidateCardAccessKind
|
||||
ProjectionFields []CardField
|
||||
ContactFields []ContactField
|
||||
OccurredAt time.Time
|
||||
}
|
||||
|
||||
// CandidateCardAccess is one immutable audit record for a permitted read.
|
||||
type CandidateCardAccess struct {
|
||||
id uuid.UUID
|
||||
candidateID uuid.UUID
|
||||
organizationID uuid.UUID
|
||||
actorID uuid.UUID
|
||||
kind CandidateCardAccessKind
|
||||
fieldCodes []string
|
||||
occurredAt time.Time
|
||||
}
|
||||
|
||||
// NewCandidateCardAccess builds an immutable event for one permitted read.
|
||||
func NewCandidateCardAccess(input CandidateCardAccessInput) (CandidateCardAccess, error) {
|
||||
const location = "domain.NewCandidateCardAccess"
|
||||
if input.CandidateID == uuid.Nil || input.OrganizationID == uuid.Nil || input.ActorID == uuid.Nil || input.OccurredAt.IsZero() {
|
||||
return CandidateCardAccess{}, fmt.Errorf("%s - identities and time are required: %w", location, ErrInvalidCandidateCardAccess)
|
||||
}
|
||||
input.OccurredAt = input.OccurredAt.UTC().Truncate(time.Microsecond)
|
||||
if err := validateCandidateCardAccessFields(input, true); err != nil {
|
||||
return CandidateCardAccess{}, fmt.Errorf("%s - validate field codes: %w", location, err)
|
||||
}
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return CandidateCardAccess{}, fmt.Errorf("%s - generate identifier: %w", location, err)
|
||||
}
|
||||
return CandidateCardAccess{
|
||||
id: id, candidateID: input.CandidateID, organizationID: input.OrganizationID,
|
||||
actorID: input.ActorID, kind: input.Kind, fieldCodes: candidateCardAccessFieldCodes(input),
|
||||
occurredAt: input.OccurredAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RestoreCandidateCardAccess validates an immutable audit record loaded from
|
||||
// storage.
|
||||
func RestoreCandidateCardAccess(id uuid.UUID, input CandidateCardAccessInput) (CandidateCardAccess, error) {
|
||||
const location = "domain.RestoreCandidateCardAccess"
|
||||
if id == uuid.Nil {
|
||||
return CandidateCardAccess{}, fmt.Errorf("%s - identifier is required: %w", location, ErrInvalidCandidateCardAccess)
|
||||
}
|
||||
if err := validateCandidateCardAccessFields(input, false); err != nil {
|
||||
return CandidateCardAccess{}, fmt.Errorf("%s - validate field codes: %w", location, err)
|
||||
}
|
||||
if input.CandidateID == uuid.Nil || input.OrganizationID == uuid.Nil || input.OccurredAt.IsZero() {
|
||||
return CandidateCardAccess{}, fmt.Errorf("%s - identities and time are required: %w", location, ErrInvalidCandidateCardAccess)
|
||||
}
|
||||
return CandidateCardAccess{
|
||||
id: id, candidateID: input.CandidateID, organizationID: input.OrganizationID,
|
||||
actorID: input.ActorID, kind: input.Kind, fieldCodes: candidateCardAccessFieldCodes(input),
|
||||
occurredAt: input.OccurredAt.UTC().Truncate(time.Microsecond),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func validateCandidateCardAccessFields(input CandidateCardAccessInput, requireActor bool) error {
|
||||
if requireActor && input.ActorID == uuid.Nil {
|
||||
return ErrInvalidCandidateCardAccess
|
||||
}
|
||||
switch input.Kind {
|
||||
case CandidateCardAccessCardViewed:
|
||||
if len(input.ContactFields) != 0 || len(input.ProjectionFields) > 6 {
|
||||
return ErrInvalidCandidateCardAccess
|
||||
}
|
||||
seen := make(map[CardField]struct{}, len(input.ProjectionFields))
|
||||
for _, field := range input.ProjectionFields {
|
||||
if !candidateCardProjectionField(field) {
|
||||
return ErrInvalidCandidateCardAccess
|
||||
}
|
||||
if _, exists := seen[field]; exists {
|
||||
return ErrInvalidCandidateCardAccess
|
||||
}
|
||||
seen[field] = struct{}{}
|
||||
}
|
||||
case CandidateCardAccessContactRead:
|
||||
if len(input.ProjectionFields) != 0 || len(input.ContactFields) == 0 {
|
||||
return ErrInvalidCandidateCardAccess
|
||||
}
|
||||
if _, err := NewContactFieldSet(input.ContactFields...); err != nil {
|
||||
return ErrInvalidCandidateCardAccess
|
||||
}
|
||||
default:
|
||||
return ErrInvalidCandidateCardAccess
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func candidateCardProjectionField(field CardField) bool {
|
||||
switch field {
|
||||
case CardFieldCategories, CardFieldStack, CardFieldExperience, CardFieldSelfAssessment,
|
||||
CardFieldWorkFormats, CardFieldSalaryCompatibility:
|
||||
return true
|
||||
case CardFieldSoftSkills, CardFieldLocation, CardFieldRelocation:
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func candidateCardAccessFieldCodes(input CandidateCardAccessInput) []string {
|
||||
codes := make([]string, 0, len(input.ProjectionFields)+len(input.ContactFields))
|
||||
if input.Kind == CandidateCardAccessCardViewed {
|
||||
for _, field := range input.ProjectionFields {
|
||||
codes = append(codes, string(field))
|
||||
}
|
||||
} else {
|
||||
for _, field := range input.ContactFields {
|
||||
codes = append(codes, string(field))
|
||||
}
|
||||
}
|
||||
slices.Sort(codes)
|
||||
return codes
|
||||
}
|
||||
|
||||
func (access CandidateCardAccess) ID() uuid.UUID { return access.id }
|
||||
func (access CandidateCardAccess) CandidateID() uuid.UUID { return access.candidateID }
|
||||
func (access CandidateCardAccess) OrganizationID() uuid.UUID { return access.organizationID }
|
||||
func (access CandidateCardAccess) ActorID() uuid.UUID { return access.actorID }
|
||||
func (access CandidateCardAccess) Kind() CandidateCardAccessKind { return access.kind }
|
||||
func (access CandidateCardAccess) FieldCodes() []string { return slices.Clone(access.fieldCodes) }
|
||||
func (access CandidateCardAccess) OccurredAt() time.Time { return access.occurredAt }
|
||||
|
||||
func (access CandidateCardAccess) IsZero() bool {
|
||||
return access.id == uuid.Nil || access.candidateID == uuid.Nil || access.organizationID == uuid.Nil
|
||||
}
|
||||
Reference in new issue
Block a user