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,121 @@
|
||||
// Package anonymousprofile builds organization-scoped, privacy-preserving
|
||||
// projections from the candidates that the storage adapter has already
|
||||
// filtered by effective publication and hidden-organization state.
|
||||
package anonymousprofile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
var ErrInvalidOrganization = errors.New("anonymous profile organization is invalid")
|
||||
|
||||
type Service struct {
|
||||
reader outbound.PrivacyCohortRepository
|
||||
key domain.CandidateViewPseudonymKey
|
||||
clock func() time.Time
|
||||
}
|
||||
|
||||
func NewService(reader outbound.PrivacyCohortRepository, key domain.CandidateViewPseudonymKey, clock func() time.Time) *Service {
|
||||
if reader == nil {
|
||||
panic("anonymousprofile.NewService - reader must not be nil")
|
||||
}
|
||||
if clock == nil {
|
||||
panic("anonymousprofile.NewService - clock must not be nil")
|
||||
}
|
||||
return &Service{reader: reader, key: key, clock: clock}
|
||||
}
|
||||
|
||||
// ListVisible returns only candidates whose complete generalized visible
|
||||
// cohort has at least five members. Active organization membership is checked
|
||||
// by the caller; the repository independently enforces publication and
|
||||
// organization hiding in one read statement.
|
||||
func (service *Service) ListVisible(ctx context.Context, organizationID uuid.UUID) ([]domain.AnonymousCandidateViewDTO, error) {
|
||||
const location = "anonymousprofile.Service.ListVisible"
|
||||
if organizationID == uuid.Nil {
|
||||
return nil, fmt.Errorf("%s - organization ID must be set: %w", location, ErrInvalidOrganization)
|
||||
}
|
||||
sources, err := service.reader.ListVisibleCandidateViewSources(ctx, organizationID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - read visible candidate sources: %w", location, err)
|
||||
}
|
||||
return service.ProjectSources(organizationID, sources)
|
||||
}
|
||||
|
||||
// ProjectSources builds a generalized view from a caller-provided current
|
||||
// source snapshot. A transactional reader can lock the source rows first and
|
||||
// reuse that exact snapshot without issuing a second, potentially newer query.
|
||||
func (service *Service) ProjectSources(
|
||||
organizationID uuid.UUID,
|
||||
sources []domain.CandidateViewSource,
|
||||
) ([]domain.AnonymousCandidateViewDTO, error) {
|
||||
const location = "anonymousprofile.Service.ProjectSources"
|
||||
if organizationID == uuid.Nil {
|
||||
return nil, fmt.Errorf("%s - organization ID must be set: %w", location, ErrInvalidOrganization)
|
||||
}
|
||||
asOf := service.clock().UTC()
|
||||
if asOf.IsZero() {
|
||||
return nil, fmt.Errorf("%s - clock returned zero: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
views := make([]domain.AnonymousCandidateView, 0, len(sources))
|
||||
seenCandidates := make(map[uuid.UUID]struct{}, len(sources))
|
||||
for _, source := range sources {
|
||||
if err := domain.ValidateCandidateViewSource(source); err != nil {
|
||||
return nil, fmt.Errorf("%s - source is invalid: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
candidateID := source.CandidateID()
|
||||
if _, duplicate := seenCandidates[candidateID]; duplicate {
|
||||
return nil, fmt.Errorf("%s - source list contains a duplicate candidate: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
seenCandidates[candidateID] = struct{}{}
|
||||
pseudonym, err := service.key.ForOrganization(organizationID, candidateID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - derive candidate alias: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
view, err := domain.NewAnonymousCandidateView(source.Input(pseudonym, asOf))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - build candidate projection: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
views = append(views, view)
|
||||
}
|
||||
var err error
|
||||
views, err = domain.GeneralizeAnonymousCandidateCohort(views)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s - generalize visible cohort: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
result := make([]domain.AnonymousCandidateViewDTO, 0, len(views))
|
||||
for _, view := range views {
|
||||
result = append(result, view.DTO())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// PreviewSource builds the candidate's private card preview with a purpose-
|
||||
// separated alias. It does not apply an organization cohort because no
|
||||
// organization context exists for this read.
|
||||
func (service *Service) PreviewSource(source domain.CandidateViewSource) (domain.AnonymousCandidateViewDTO, error) {
|
||||
const location = "anonymousprofile.Service.PreviewSource"
|
||||
if err := domain.ValidateCandidateViewSource(source); err != nil {
|
||||
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - source is invalid: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
asOf := service.clock().UTC()
|
||||
if asOf.IsZero() {
|
||||
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - clock returned zero: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
pseudonym, err := service.key.ForCandidatePreview(source.CandidateID())
|
||||
if err != nil {
|
||||
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - derive preview alias: %w", location, domain.ErrInvalidAnonymousCandidateView)
|
||||
}
|
||||
view, err := domain.NewAnonymousCandidateView(source.Input(pseudonym, asOf))
|
||||
if err != nil {
|
||||
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - build candidate preview: %w", location, err)
|
||||
}
|
||||
return view.DTO(), nil
|
||||
}
|
||||
Reference in new issue
Block a user