fix(assessment): return confirmed categories with their basis and label demonstration categories

This commit is contained in:
shira committed 2026-10-09 21:24:24 +03:00
1 parent 623d41b969
commit d4903a3796
77 files changed
+1441 -288

No files matched your search

+67 -16
View File
@@ -2,9 +2,11 @@
package profile
import (
"cmp"
"context"
"errors"
"fmt"
"slices"
"time"
"github.com/google/uuid"
@@ -21,6 +23,16 @@ var (
ErrEmailNotVerified = errors.New("candidate profile requires a confirmed email")
)
// View is the profile as the candidate reads it: the saved document and the
// categories that assessment results confirmed. The categories are derived by
// the server, are read-only, and are never part of the saved document.
type View struct {
Profile domain.CandidateProfile
// ConfirmedCategories holds at most one category per specialization, in
// catalog order.
ConfirmedCategories []domain.CandidateAssessmentCategory
}
// Service is stateless between calls. Profile data, its history, and onboarding
// movement are written through one transaction.
type Service struct {
@@ -40,27 +52,28 @@ func NewService(transactor outbound.Transactor, clock func() time.Time) *Service
return &Service{transactor: transactor, clock: clock}
}
// GetMyProfile reads only the profile owned by userID. Version zero means the
// GetMyProfile reads only the profile owned by userID, with the categories
// that the candidate's assessment results confirmed. Version zero means the
// candidate has not saved a profile yet.
func (service *Service) GetMyProfile(ctx context.Context, userID uuid.UUID) (domain.CandidateProfile, error) {
func (service *Service) GetMyProfile(ctx context.Context, userID uuid.UUID) (View, error) {
const location = "Service.GetMyProfile"
if userID == uuid.Nil {
return domain.CandidateProfile{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateProfile)
return View{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateProfile)
}
var result domain.CandidateProfile
var result View
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireCandidate(ctx, unit, userID, location); err != nil {
return err
}
var err error
result, err = unit.CandidateProfiles().Get(ctx, userID)
profile, err := unit.CandidateProfiles().Get(ctx, userID)
if err != nil {
return fmt.Errorf("%s - read profile: %w", location, err)
}
return nil
result, err = readView(ctx, unit, userID, profile, location)
return err
})
if err != nil {
return domain.CandidateProfile{}, fmt.Errorf("%s - transaction: %w", location, err)
return View{}, fmt.Errorf("%s - transaction: %w", location, err)
}
return result, nil
}
@@ -73,16 +86,16 @@ func (service *Service) SaveMyProfile(
userID uuid.UUID,
expectedVersion int64,
input domain.CandidateProfileInput,
) (domain.CandidateProfile, error) {
) (View, error) {
const location = "Service.SaveMyProfile"
if userID == uuid.Nil {
return domain.CandidateProfile{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateProfile)
return View{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateProfile)
}
at := service.clock().UTC()
if at.IsZero() {
return domain.CandidateProfile{}, fmt.Errorf("%s - application clock returned zero: %w", location, domain.ErrInvalidCandidateProfile)
return View{}, fmt.Errorf("%s - application clock returned zero: %w", location, domain.ErrInvalidCandidateProfile)
}
var result domain.CandidateProfile
var result View
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := requireCandidate(ctx, unit, userID, location); err != nil {
return err
@@ -105,18 +118,56 @@ func (service *Service) SaveMyProfile(
if err := repository.AdvanceOnboarding(ctx, userID, at); err != nil {
return fmt.Errorf("%s - advance onboarding: %w", location, err)
}
result = updated
return nil
result, err = readView(ctx, unit, userID, updated, location)
return err
})
if err != nil {
if errors.Is(err, outbound.ErrCandidateProfileVersionConflict) || errors.Is(err, domain.ErrCandidateProfileVersionConflict) {
return domain.CandidateProfile{}, fmt.Errorf("%s - transaction: %w", location, domain.ErrCandidateProfileVersionConflict)
return View{}, fmt.Errorf("%s - transaction: %w", location, domain.ErrCandidateProfileVersionConflict)
}
return domain.CandidateProfile{}, fmt.Errorf("%s - transaction: %w", location, err)
return View{}, fmt.Errorf("%s - transaction: %w", location, err)
}
return result, nil
}
// readView attaches the confirmed categories of the candidate to a profile
// that was read or written in the same transaction.
func readView(
ctx context.Context,
unit outbound.UnitOfWork,
userID uuid.UUID,
profile domain.CandidateProfile,
location string,
) (View, error) {
categories, err := unit.AssessmentAttempts().ListCandidateCategories(ctx, userID)
if err != nil {
return View{}, fmt.Errorf("%s - read confirmed categories: %w", location, err)
}
return View{Profile: profile, ConfirmedCategories: inCatalogOrder(categories)}, nil
}
// inCatalogOrder sorts the categories by their place in the catalog. A
// category outside the catalog cannot exist (the storage constraint and the
// domain both reject it), so it sorts last instead of being dropped silently.
func inCatalogOrder(categories []domain.CandidateAssessmentCategory) []domain.CandidateAssessmentCategory {
position := make(map[domain.SpecializationCode]int, len(domain.SpecializationCatalog()))
for index, code := range domain.SpecializationCatalog() {
position[code] = index
}
sorted := slices.Clone(categories)
slices.SortStableFunc(sorted, func(left, right domain.CandidateAssessmentCategory) int {
return cmp.Compare(catalogPosition(position, left), catalogPosition(position, right))
})
return sorted
}
func catalogPosition(position map[domain.SpecializationCode]int, category domain.CandidateAssessmentCategory) int {
if index, known := position[category.Snapshot().Specialization]; known {
return index
}
return len(position)
}
func requireCandidate(ctx context.Context, unit outbound.UnitOfWork, userID uuid.UUID, location string) error {
user, err := unit.Users().GetByID(ctx, userID)
if err != nil {