feat: add assessment, privacy and support flows

This commit is contained in:
TakuyaYagam1 committed 2026-10-08 08:08:25 +03:00
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
}
@@ -0,0 +1,133 @@
package anonymousprofile
import (
"bytes"
"context"
"encoding/json"
"errors"
"strings"
"testing"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
type privacyCohortReaderFunc func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error)
func (reader privacyCohortReaderFunc) ListVisibleCandidateViewSources(ctx context.Context, organizationID uuid.UUID) ([]domain.CandidateViewSource, error) {
return reader(ctx, organizationID)
}
func TestListVisibleReturnsOrganizationScopedAliasesAndSuppressesSmallCohort(t *testing.T) {
t.Parallel()
organizationID := uuid.New()
candidateIDs := make([]uuid.UUID, 5)
sources := make([]domain.CandidateViewSource, 0, len(candidateIDs))
fields, err := domain.NewCardFieldSet([]domain.CardField{domain.CardFieldStack})
if err != nil {
t.Fatalf("NewCardFieldSet() error = %v", err)
}
for index := range candidateIDs {
candidateIDs[index] = uuid.New()
profile, err := domain.EmptyCandidateProfile(candidateIDs[index])
if err != nil {
t.Fatalf("EmptyCandidateProfile() error = %v", err)
}
source, err := domain.NewCandidateViewSource(
candidateIDs[index], fields, domain.CandidateViewProfileFromContent(profile.Content()), nil, nil, nil,
)
if err != nil {
t.Fatalf("NewCandidateViewSource() error = %v", err)
}
sources = append(sources, source)
}
key, err := domain.NewCandidateViewPseudonymKey(bytes.Repeat([]byte("purpose-specific-key"), 2))
if err != nil {
t.Fatalf("NewCandidateViewPseudonymKey() error = %v", err)
}
service := NewService(privacyCohortReaderFunc(func(_ context.Context, got uuid.UUID) ([]domain.CandidateViewSource, error) {
if got != organizationID {
t.Fatalf("reader organization = %s, want %s", got, organizationID)
}
return sources, nil
}), key, func() time.Time { return time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC) })
views, err := service.ListVisible(context.Background(), organizationID)
if err != nil {
t.Fatalf("ListVisible() error = %v", err)
}
if len(views) != 5 {
t.Fatalf("ListVisible() returned %d views, want five", len(views))
}
aliases := make([]string, 0, len(views))
for _, view := range views {
aliases = append(aliases, view.Pseudonym)
}
seenAliases := make(map[string]struct{}, len(aliases))
for _, alias := range aliases {
if _, duplicate := seenAliases[alias]; duplicate {
t.Fatalf("pseudonyms are not unique within the organization: %v", aliases)
}
seenAliases[alias] = struct{}{}
}
encoded, err := json.Marshal(views)
if err != nil {
t.Fatalf("Marshal() error = %v", err)
}
for _, candidateID := range candidateIDs {
if bytes.Contains(encoded, []byte(candidateID.String())) {
t.Fatalf("organization-facing projection contains source ID %s", candidateID)
}
}
otherOrganization := uuid.New()
otherService := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) {
return sources, nil
}), key, func() time.Time { return time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC) })
otherViews, err := otherService.ListVisible(context.Background(), otherOrganization)
if err != nil {
t.Fatalf("ListVisible(other organization) error = %v", err)
}
if otherViews[0].Pseudonym == views[0].Pseudonym {
t.Fatal("candidate pseudonym was reused across organizations")
}
smallService := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) {
return sources[:4], nil
}), key, time.Now)
small, err := smallService.ListVisible(context.Background(), organizationID)
if err != nil {
t.Fatalf("ListVisible(small cohort) error = %v", err)
}
if len(small) != 0 {
t.Fatalf("small cohort returned %d views, want suppression", len(small))
}
}
func TestListVisibleRejectsInvalidScopeAndSourceFailures(t *testing.T) {
t.Parallel()
key, err := domain.NewCandidateViewPseudonymKey(bytes.Repeat([]byte("purpose-specific-key"), 2))
if err != nil {
t.Fatalf("NewCandidateViewPseudonymKey() error = %v", err)
}
service := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) {
return nil, errors.New("storage unavailable")
}), key, time.Now)
if _, err := service.ListVisible(context.Background(), uuid.Nil); !errors.Is(err, ErrInvalidOrganization) {
t.Fatalf("ListVisible(empty organization) error = %v, want ErrInvalidOrganization", err)
}
if _, err := service.ListVisible(context.Background(), uuid.New()); err == nil || !strings.Contains(err.Error(), "read visible candidate sources") {
t.Fatalf("ListVisible(storage error) = %v, want wrapped read error", err)
}
invalidSourceService := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) {
return []domain.CandidateViewSource{{}}, nil
}), key, time.Now)
if _, err := invalidSourceService.ListVisible(context.Background(), uuid.New()); !errors.Is(err, domain.ErrInvalidAnonymousCandidateView) {
t.Fatalf("ListVisible(invalid source) error = %v, want invalid projection", err)
}
}