Files
lincodex/backend/internal/usecase/assessment/category.go
T

178 lines
7.1 KiB
Go

package assessment
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var (
ErrAssessmentCategoryRepositoryRequired = errors.New("assessment category repository is required")
ErrAssessmentCategoryHistoryInvalid = errors.New("assessment result history is invalid")
)
// AssessmentCategoryDecisionSettings are explicit runtime gates. The zero
// value denies synthetic demo category assignment.
type AssessmentCategoryDecisionSettings struct {
SyntheticDemoAssignmentsEnabled bool
}
// AssessmentCategoryDecisionRepository is bound to the finalization
// transaction. Its category lock is acquired after the caller has obtained
// C3, candidate/specialization scope, and the attempt row lock, in that order.
// The caller persists the returned result and any current-category update in
// this same transaction; no separate category-history table is needed.
type AssessmentCategoryDecisionRepository interface {
LockCandidateCategory(ctx context.Context, candidateID uuid.UUID, specialization domain.SpecializationCode) error
GetCandidateCategory(
ctx context.Context,
candidateID uuid.UUID,
specialization domain.SpecializationCode,
) (domain.CandidateAssessmentCategory, bool, error)
// GetSyntheticCategoryReview takes the pins of one exact reviewed source.
// The repository must not substitute a current opening or a different
// manifest.
GetSyntheticCategoryReview(
ctx context.Context,
openingID uuid.UUID,
category domain.AssessmentCategory,
openingManifestHash [32]byte,
productPolicyID domain.PolicyVersionID,
productPolicyVersion int32,
gradingPolicyID domain.PolicyVersionID,
) (domain.AssessmentSyntheticCategoryReview, bool, error)
}
// AssessmentCategoryDecisionPlan contains the immutable v2 result projection
// and the current-state write, if the confirmed grade advances. The caller
// must save both atomically with the completed attempt transition.
type AssessmentCategoryDecisionPlan struct {
Result domain.AssessmentAttemptResultHistoryEntry
Decision domain.AssessmentCategoryDecisionSnapshot
CurrentCategory *domain.CandidateAssessmentCategory
ReplaceCurrent bool
}
// AssessmentCategoryDecisionService applies the explicit synthetic-category
// gate and the reviewed, pinned domain decision. It performs reads and locking
// only; the enclosing finalization transaction persists the plan atomically.
type AssessmentCategoryDecisionService struct {
settings AssessmentCategoryDecisionSettings
}
func NewAssessmentCategoryDecisionService(
settings AssessmentCategoryDecisionSettings,
) AssessmentCategoryDecisionService {
return AssessmentCategoryDecisionService{settings: settings}
}
// PrepareFinalizedResult reads the exact current category and optional
// synthetic review after the caller's C3, candidate/specialization, and
// attempt locks. V1 remains the stored shape for express training results;
// real root/corrective results get a v2 category projection even when the
// feature gate is off, so the contemporaneous current grade is explicit.
func (service AssessmentCategoryDecisionService) PrepareFinalizedResult(
ctx context.Context,
repository AssessmentCategoryDecisionRepository,
attempt domain.AssessmentAttempt,
result domain.AssessmentAttemptResultSnapshot,
policy domain.PolicyVersion,
) (AssessmentCategoryDecisionPlan, error) {
const location = "AssessmentCategoryDecisionService.PrepareFinalizedResult"
if repository == nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - require repository: %w", location, ErrAssessmentCategoryRepositoryRequired)
}
input := domain.AssessmentCategoryDecisionInput{
Attempt: attempt, Result: result, Policy: policy,
SyntheticDemoCategoryAssignmentEnabled: service.settings.SyntheticDemoAssignmentsEnabled,
}
preflight, err := domain.DecideAssessmentCategory(input)
if err != nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - validate finalized result: %w", location, err)
}
if attempt.Kind() == domain.AssessmentAttemptKindDemoExpress {
return prepareTrainingCategoryResult(location, result, preflight)
}
return service.prepareRealFinalizedResult(ctx, location, repository, attempt, result, input)
}
func prepareTrainingCategoryResult(
location string,
result domain.AssessmentAttemptResultSnapshot,
decision domain.AssessmentCategoryDecision,
) (AssessmentCategoryDecisionPlan, error) {
legacy, err := domain.NewLegacyAssessmentAttemptResultHistoryEntry(result)
if err != nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - build training result view: %w", location, err)
}
return AssessmentCategoryDecisionPlan{Result: legacy, Decision: decision.Snapshot()}, nil
}
func (service AssessmentCategoryDecisionService) prepareRealFinalizedResult(
ctx context.Context,
location string,
repository AssessmentCategoryDecisionRepository,
attempt domain.AssessmentAttempt,
result domain.AssessmentAttemptResultSnapshot,
input domain.AssessmentCategoryDecisionInput,
) (AssessmentCategoryDecisionPlan, error) {
track := attempt.Snapshot()
if track.Specialization == nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - finalized real attempt has no specialization: %w", location, domain.ErrInvalidAssessmentCategory)
}
if err := repository.LockCandidateCategory(ctx, track.UserID, *track.Specialization); err != nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - lock candidate category: %w", location, err)
}
current, found, err := repository.GetCandidateCategory(ctx, track.UserID, *track.Specialization)
if err != nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - read candidate category: %w", location, err)
}
var currentPointer *domain.CandidateAssessmentCategory
if found {
if current.IsZero() {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - repository returned an empty current category: %w", location, domain.ErrInvalidAssessmentCategory)
}
currentPointer = &current
}
if service.settings.SyntheticDemoAssignmentsEnabled {
review, reviewFound, err := repository.GetSyntheticCategoryReview(
ctx,
result.Pins.OpeningID,
track.BankCategory,
result.Pins.OpeningManifestHash,
result.Pins.ProductPolicyVersionID,
result.Pins.ProductPolicyVersion,
result.Pins.GradingPolicyVersionID,
)
if err != nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - read pinned synthetic review: %w", location, err)
}
if reviewFound {
input.Review = &review
}
}
input.Current = currentPointer
decision, err := domain.DecideAssessmentCategory(input)
if err != nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - decide category: %w", location, err)
}
decisionSnapshot := decision.Snapshot()
entry, err := domain.NewCategoryAssessmentAttemptResultHistoryEntry(result, decisionSnapshot)
if err != nil {
return AssessmentCategoryDecisionPlan{}, fmt.Errorf("%s - build immutable result v2 projection: %w", location, err)
}
currentValue, hasCurrent := decision.CurrentCategory()
plan := AssessmentCategoryDecisionPlan{
Result: entry, Decision: decisionSnapshot, ReplaceCurrent: decision.ShouldUpdateCurrent(),
}
if hasCurrent {
plan.CurrentCategory = &currentValue
}
return plan, nil
}