chore: checkpoint assessment and inbox integration
This commit is contained in:
1 parent
88a31ba0d5
commit
5c52fd8b91
31 files changed
+4608
-181
No files matched your search
@@ -0,0 +1,675 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var ErrInvalidAssessmentCategory = errors.New("invalid assessment category")
|
||||
|
||||
type AssessmentCategoryDecisionStatus string
|
||||
|
||||
const (
|
||||
AssessmentCategoryDecisionNotEligible AssessmentCategoryDecisionStatus = "not_eligible"
|
||||
AssessmentCategoryDecisionNotConfirmed AssessmentCategoryDecisionStatus = "not_confirmed"
|
||||
AssessmentCategoryDecisionAssigned AssessmentCategoryDecisionStatus = "assigned"
|
||||
AssessmentCategoryDecisionUnchanged AssessmentCategoryDecisionStatus = "unchanged"
|
||||
AssessmentCategoryDecisionTrainingOnly AssessmentCategoryDecisionStatus = "training_only"
|
||||
)
|
||||
|
||||
const (
|
||||
AssessmentAttemptResultHistorySchemaV1 int32 = 1
|
||||
AssessmentAttemptResultHistorySchemaV2 int32 = 2
|
||||
)
|
||||
|
||||
type AssessmentCategoryDecisionMethod string
|
||||
|
||||
const AssessmentCategoryDecisionDemoBasicPassMark AssessmentCategoryDecisionMethod = "demo_basic_pass_mark"
|
||||
|
||||
type AssessmentCategoryCalibrationStatus string
|
||||
|
||||
const AssessmentCategoryCalibrationUncalibrated AssessmentCategoryCalibrationStatus = "uncalibrated"
|
||||
|
||||
type AssessmentCategorySource string
|
||||
|
||||
const AssessmentCategorySourceSyntheticDemo AssessmentCategorySource = "synthetic_demo"
|
||||
|
||||
// AssessmentSyntheticCategoryReviewSnapshot pins the reviewed synthetic bank
|
||||
// opening to the exact product policy under which demo categories may be assigned.
|
||||
type AssessmentSyntheticCategoryReviewSnapshot struct {
|
||||
ID uuid.UUID
|
||||
OpeningID uuid.UUID
|
||||
Category AssessmentCategory
|
||||
Source AssessmentCategorySource
|
||||
OpeningManifestHash [32]byte
|
||||
ProductPolicyVersionID PolicyVersionID
|
||||
ProductPolicyVersion int32
|
||||
ReviewedBy AssessmentOperator
|
||||
ReviewedAt time.Time
|
||||
}
|
||||
|
||||
// AssessmentSyntheticCategoryReview is an immutable review attestation used
|
||||
// only when the separate synthetic-category config gate is enabled.
|
||||
type AssessmentSyntheticCategoryReview struct {
|
||||
snapshot AssessmentSyntheticCategoryReviewSnapshot
|
||||
}
|
||||
|
||||
func NewAssessmentSyntheticCategoryReview(
|
||||
snapshot AssessmentSyntheticCategoryReviewSnapshot,
|
||||
) (AssessmentSyntheticCategoryReview, error) {
|
||||
const location = "domain.NewAssessmentSyntheticCategoryReview"
|
||||
review, err := RestoreAssessmentSyntheticCategoryReview(snapshot)
|
||||
if err != nil {
|
||||
return AssessmentSyntheticCategoryReview{}, fmt.Errorf("%s - validate review: %w", location, err)
|
||||
}
|
||||
return review, nil
|
||||
}
|
||||
|
||||
func RestoreAssessmentSyntheticCategoryReview(
|
||||
snapshot AssessmentSyntheticCategoryReviewSnapshot,
|
||||
) (AssessmentSyntheticCategoryReview, error) {
|
||||
const location = "domain.RestoreAssessmentSyntheticCategoryReview"
|
||||
operator, operatorErr := ParseAssessmentOperator(string(snapshot.ReviewedBy))
|
||||
category, categoryErr := ParseAssessmentCategory(string(snapshot.Category))
|
||||
if snapshot.ID == uuid.Nil || snapshot.OpeningID == uuid.Nil || snapshot.OpeningManifestHash == [32]byte{} ||
|
||||
snapshot.ProductPolicyVersionID.IsZero() || snapshot.ProductPolicyVersion <= 0 || snapshot.ReviewedAt.IsZero() ||
|
||||
snapshot.Source != AssessmentCategorySourceSyntheticDemo ||
|
||||
operatorErr != nil || operator != snapshot.ReviewedBy || categoryErr != nil || category != snapshot.Category {
|
||||
return AssessmentSyntheticCategoryReview{}, fmt.Errorf("%s - review identity or pins are invalid: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
snapshot.ReviewedAt = canonicalAssessmentTime(snapshot.ReviewedAt)
|
||||
return AssessmentSyntheticCategoryReview{snapshot: snapshot}, nil
|
||||
}
|
||||
|
||||
func (review AssessmentSyntheticCategoryReview) IsZero() bool { return review.snapshot.ID == uuid.Nil }
|
||||
|
||||
func (review AssessmentSyntheticCategoryReview) Snapshot() AssessmentSyntheticCategoryReviewSnapshot {
|
||||
return review.snapshot
|
||||
}
|
||||
|
||||
// CandidateAssessmentCategorySnapshot is the private current-state and audit
|
||||
// record. It contains candidate and source identifiers and must not be returned
|
||||
// directly to an employer-facing API.
|
||||
type CandidateAssessmentCategorySnapshot struct {
|
||||
CandidateID uuid.UUID
|
||||
Specialization SpecializationCode
|
||||
Grade GradeCode
|
||||
SourceAttemptID AssessmentAttemptID
|
||||
SourceReviewID uuid.UUID
|
||||
ProductPolicyVersionID PolicyVersionID
|
||||
ProductPolicyVersion int32
|
||||
OpeningID uuid.UUID
|
||||
OpeningManifestHash [32]byte
|
||||
Source AssessmentCategorySource
|
||||
CalibrationStatus AssessmentCategoryCalibrationStatus
|
||||
AssignedAt time.Time
|
||||
}
|
||||
|
||||
// CandidateAssessmentCategory is the current confirmed category for one
|
||||
// candidate specialization.
|
||||
type CandidateAssessmentCategory struct {
|
||||
snapshot CandidateAssessmentCategorySnapshot
|
||||
}
|
||||
|
||||
func RestoreCandidateAssessmentCategory(
|
||||
snapshot CandidateAssessmentCategorySnapshot,
|
||||
) (CandidateAssessmentCategory, error) {
|
||||
const location = "domain.RestoreCandidateAssessmentCategory"
|
||||
if snapshot.CandidateID == uuid.Nil || !snapshot.Specialization.IsKnown() || !snapshot.Grade.IsKnown() ||
|
||||
snapshot.SourceAttemptID.IsZero() || snapshot.SourceReviewID == uuid.Nil ||
|
||||
snapshot.ProductPolicyVersionID.IsZero() || snapshot.ProductPolicyVersion <= 0 ||
|
||||
snapshot.OpeningID == uuid.Nil || snapshot.OpeningManifestHash == [32]byte{} ||
|
||||
snapshot.Source != AssessmentCategorySourceSyntheticDemo ||
|
||||
snapshot.CalibrationStatus != AssessmentCategoryCalibrationUncalibrated || snapshot.AssignedAt.IsZero() {
|
||||
return CandidateAssessmentCategory{}, fmt.Errorf("%s - current category or source is invalid: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
snapshot.AssignedAt = canonicalAssessmentTime(snapshot.AssignedAt)
|
||||
return CandidateAssessmentCategory{snapshot: snapshot}, nil
|
||||
}
|
||||
|
||||
func (category CandidateAssessmentCategory) IsZero() bool {
|
||||
return category.snapshot.CandidateID == uuid.Nil
|
||||
}
|
||||
|
||||
func (category CandidateAssessmentCategory) Snapshot() CandidateAssessmentCategorySnapshot {
|
||||
return category.snapshot
|
||||
}
|
||||
|
||||
// AssessmentCategoryDecisionSnapshot is the allowlisted category extension
|
||||
// for one immutable result. It contains no private answer or item data.
|
||||
type AssessmentCategoryDecisionSnapshot struct {
|
||||
AttemptID AssessmentAttemptID
|
||||
BankCategory AssessmentCategory
|
||||
Specialization *SpecializationCode
|
||||
AttemptedGrade *GradeCode
|
||||
PreviousGrade *GradeCode
|
||||
CurrentGrade *GradeCode
|
||||
Status AssessmentCategoryDecisionStatus
|
||||
Method AssessmentCategoryDecisionMethod
|
||||
ProductPolicyVersionID PolicyVersionID
|
||||
ProductPolicyVersion int32
|
||||
GradingPolicyVersionID PolicyVersionID
|
||||
OpeningID uuid.UUID
|
||||
OpeningManifestHash [32]byte
|
||||
ReviewID uuid.UUID
|
||||
Source AssessmentCategorySource
|
||||
CalibrationStatus AssessmentCategoryCalibrationStatus
|
||||
DecidedAt time.Time
|
||||
}
|
||||
|
||||
// AssessmentCategoryDecision is the immutable category outcome associated
|
||||
// with a completed result. Current category changes only for a higher passing
|
||||
// grade from a reviewed synthetic source.
|
||||
type AssessmentCategoryDecision struct {
|
||||
snapshot AssessmentCategoryDecisionSnapshot
|
||||
current *CandidateAssessmentCategory
|
||||
}
|
||||
|
||||
type AssessmentCategoryDecisionInput struct {
|
||||
Attempt AssessmentAttempt
|
||||
Result AssessmentAttemptResultSnapshot
|
||||
Policy PolicyVersion
|
||||
Review *AssessmentSyntheticCategoryReview
|
||||
Current *CandidateAssessmentCategory
|
||||
SyntheticDemoCategoryAssignmentEnabled bool
|
||||
}
|
||||
|
||||
func DecideAssessmentCategory(
|
||||
input AssessmentCategoryDecisionInput,
|
||||
) (AssessmentCategoryDecision, error) {
|
||||
const location = "domain.DecideAssessmentCategory"
|
||||
result, err := RestoreAssessmentAttemptResultSnapshot(input.Result)
|
||||
if err != nil || !assessmentResultMatchesAttempt(input.Attempt, result) {
|
||||
return AssessmentCategoryDecision{}, fmt.Errorf("%s - result and attempt do not match: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
attempt := input.Attempt.Snapshot()
|
||||
if input.Current != nil && input.Current.snapshot.CandidateID != attempt.UserID {
|
||||
return AssessmentCategoryDecision{}, fmt.Errorf("%s - current category belongs to another candidate: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
if attempt.Kind == AssessmentAttemptKindDemoExpress {
|
||||
return restoreDecision(newTrainingOnlyCategoryDecision(attempt, result), input.Current)
|
||||
}
|
||||
if attempt.Kind != AssessmentAttemptKindRoot && attempt.Kind != AssessmentAttemptKindCorrective {
|
||||
return AssessmentCategoryDecision{}, fmt.Errorf("%s - attempt kind cannot produce a category: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
if attempt.Specialization == nil || attempt.TargetGrade == nil || !input.PolicyMatchesResult(result) {
|
||||
return AssessmentCategoryDecision{}, fmt.Errorf("%s - track or pinned policy is invalid: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
if input.Current != nil && !currentCategoryMatchesAttempt(input.Current, attempt) {
|
||||
return AssessmentCategoryDecision{}, fmt.Errorf("%s - current category is outside the attempt scope: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
return decideReviewedSyntheticCategory(input, attempt, result)
|
||||
}
|
||||
|
||||
func decideReviewedSyntheticCategory(
|
||||
input AssessmentCategoryDecisionInput,
|
||||
attempt AssessmentAttemptSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
) (AssessmentCategoryDecision, error) {
|
||||
decision := newRealCategoryDecision(attempt, result, input.Current)
|
||||
if !input.SyntheticDemoCategoryAssignmentEnabled || !eligibleSyntheticCategoryPolicy(input.Policy, result) ||
|
||||
!reviewMatchesCategoryAttempt(input.Review, attempt, result) {
|
||||
return restoreDecision(decision, input.Current)
|
||||
}
|
||||
decision.Status = AssessmentCategoryDecisionNotConfirmed
|
||||
decision.Method = AssessmentCategoryDecisionDemoBasicPassMark
|
||||
decision.ReviewID = input.Review.snapshot.ID
|
||||
decision.Source = AssessmentCategorySourceSyntheticDemo
|
||||
decision.CalibrationStatus = AssessmentCategoryCalibrationUncalibrated
|
||||
if result.Outcome != AssessmentAttemptOutcomeConfirmed {
|
||||
return restoreDecision(decision, input.Current)
|
||||
}
|
||||
if input.Current != nil && gradeRank(*attempt.TargetGrade) <= gradeRank(input.Current.snapshot.Grade) {
|
||||
decision.Status = AssessmentCategoryDecisionUnchanged
|
||||
return restoreDecision(decision, input.Current)
|
||||
}
|
||||
return assignSyntheticCategory(decision, attempt, result, input)
|
||||
}
|
||||
|
||||
func assignSyntheticCategory(
|
||||
decision AssessmentCategoryDecisionSnapshot,
|
||||
attempt AssessmentAttemptSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
input AssessmentCategoryDecisionInput,
|
||||
) (AssessmentCategoryDecision, error) {
|
||||
decision.Status = AssessmentCategoryDecisionAssigned
|
||||
decision.CurrentGrade = clonePointer(attempt.TargetGrade)
|
||||
current, err := RestoreCandidateAssessmentCategory(CandidateAssessmentCategorySnapshot{
|
||||
CandidateID: attempt.UserID, Specialization: *attempt.Specialization, Grade: *attempt.TargetGrade,
|
||||
SourceAttemptID: attempt.ID, SourceReviewID: input.Review.snapshot.ID,
|
||||
ProductPolicyVersionID: result.Pins.ProductPolicyVersionID, ProductPolicyVersion: result.Pins.ProductPolicyVersion,
|
||||
OpeningID: result.Pins.OpeningID, OpeningManifestHash: result.Pins.OpeningManifestHash,
|
||||
Source: AssessmentCategorySourceSyntheticDemo, CalibrationStatus: AssessmentCategoryCalibrationUncalibrated,
|
||||
AssignedAt: result.DecidedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return AssessmentCategoryDecision{}, fmt.Errorf("domain.assignSyntheticCategory - build current category: %w", err)
|
||||
}
|
||||
return restoreDecision(decision, ¤t)
|
||||
}
|
||||
|
||||
func (input AssessmentCategoryDecisionInput) PolicyMatchesResult(result AssessmentAttemptResultSnapshot) bool {
|
||||
return !input.Policy.IsZero() && input.Policy.Key() == ProductPolicyKey &&
|
||||
input.Policy.ID() == result.Pins.ProductPolicyVersionID &&
|
||||
input.Policy.Version() == result.Pins.ProductPolicyVersion
|
||||
}
|
||||
|
||||
func newTrainingOnlyCategoryDecision(
|
||||
attempt AssessmentAttemptSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
) AssessmentCategoryDecisionSnapshot {
|
||||
return AssessmentCategoryDecisionSnapshot{
|
||||
AttemptID: attempt.ID, Status: AssessmentCategoryDecisionTrainingOnly,
|
||||
ProductPolicyVersionID: result.Pins.ProductPolicyVersionID,
|
||||
ProductPolicyVersion: result.Pins.ProductPolicyVersion, DecidedAt: result.DecidedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func newRealCategoryDecision(
|
||||
attempt AssessmentAttemptSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
current *CandidateAssessmentCategory,
|
||||
) AssessmentCategoryDecisionSnapshot {
|
||||
decision := AssessmentCategoryDecisionSnapshot{
|
||||
AttemptID: attempt.ID, BankCategory: attempt.BankCategory,
|
||||
Specialization: clonePointer(attempt.Specialization), AttemptedGrade: clonePointer(attempt.TargetGrade),
|
||||
Status: AssessmentCategoryDecisionNotEligible,
|
||||
ProductPolicyVersionID: result.Pins.ProductPolicyVersionID,
|
||||
ProductPolicyVersion: result.Pins.ProductPolicyVersion,
|
||||
GradingPolicyVersionID: result.Pins.GradingPolicyVersionID,
|
||||
OpeningID: result.Pins.OpeningID, OpeningManifestHash: result.Pins.OpeningManifestHash,
|
||||
DecidedAt: result.DecidedAt,
|
||||
}
|
||||
if current != nil {
|
||||
previous := current.snapshot.Grade
|
||||
decision.PreviousGrade = &previous
|
||||
decision.CurrentGrade = &previous
|
||||
}
|
||||
return decision
|
||||
}
|
||||
|
||||
func eligibleSyntheticCategoryPolicy(policy PolicyVersion, result AssessmentAttemptResultSnapshot) bool {
|
||||
product := policy.Policy()
|
||||
return policy.Kind() == PolicyKindDemo && policy.Key() == ProductPolicyKey &&
|
||||
sameAssessmentPolicy(product.Assessment, DemoProductPolicyV2().Assessment) &&
|
||||
result.Attempt.Mode == AssessmentAttemptModeDemoBasic && result.Rationale.Rule == assessmentResultRuleBasicPassMark &&
|
||||
int32(product.Assessment.Attempt.BasicPassMark) == result.Rationale.PassMarkBasisPoints &&
|
||||
(result.Outcome == AssessmentAttemptOutcomeConfirmed || result.Outcome == AssessmentAttemptOutcomeNotConfirmed)
|
||||
}
|
||||
|
||||
func sameAssessmentPolicy(left, right AssessmentPolicy) bool {
|
||||
return sameAssessmentPolicyRules(left, right) &&
|
||||
sameOptionalAssessmentAttemptPolicy(left.Attempt, right.Attempt) &&
|
||||
sameOptionalAssessmentTrainingPolicy(left.Training, right.Training)
|
||||
}
|
||||
|
||||
func sameAssessmentPolicyRules(left, right AssessmentPolicy) bool {
|
||||
return left.Calibration == right.Calibration && left.SameGradeRetryAfter == right.SameGradeRetryAfter &&
|
||||
left.MaxAttemptsPerWindow == right.MaxAttemptsPerWindow && left.AttemptWindow == right.AttemptWindow &&
|
||||
left.PromotionMinInterval == right.PromotionMinInterval && left.ConfirmationThreshold == right.ConfirmationThreshold &&
|
||||
left.HighStrengthThreshold == right.HighStrengthThreshold && left.PracticeRequired == right.PracticeRequired &&
|
||||
left.StopRule == right.StopRule
|
||||
}
|
||||
|
||||
func sameOptionalAssessmentAttemptPolicy(left, right *AssessmentAttemptPolicy) bool {
|
||||
if left == nil || right == nil {
|
||||
return left == right
|
||||
}
|
||||
return *left == *right
|
||||
}
|
||||
|
||||
func sameOptionalAssessmentTrainingPolicy(left, right *AssessmentTrainingPolicy) bool {
|
||||
if left == nil || right == nil {
|
||||
return left == right
|
||||
}
|
||||
return *left == *right
|
||||
}
|
||||
|
||||
func reviewMatchesCategoryAttempt(
|
||||
review *AssessmentSyntheticCategoryReview,
|
||||
attempt AssessmentAttemptSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
) bool {
|
||||
if review == nil || review.IsZero() {
|
||||
return false
|
||||
}
|
||||
snapshot := review.snapshot
|
||||
return !snapshot.ReviewedAt.After(result.DecidedAt) &&
|
||||
snapshot.OpeningID == result.Pins.OpeningID && snapshot.Category == attempt.BankCategory &&
|
||||
snapshot.OpeningManifestHash == result.Pins.OpeningManifestHash &&
|
||||
snapshot.ProductPolicyVersionID == result.Pins.ProductPolicyVersionID &&
|
||||
snapshot.ProductPolicyVersion == result.Pins.ProductPolicyVersion
|
||||
}
|
||||
|
||||
func assessmentResultMatchesAttempt(attempt AssessmentAttempt, result AssessmentAttemptResultSnapshot) bool {
|
||||
if !assessmentAttemptCompletedMatchesResult(attempt, result) || !assessmentAttemptPublicMatchesResult(attempt, result) {
|
||||
return false
|
||||
}
|
||||
return assessmentAttemptVersionsMatchResult(attempt.Snapshot().Versions, result.Pins)
|
||||
}
|
||||
|
||||
func assessmentAttemptCompletedMatchesResult(attempt AssessmentAttempt, result AssessmentAttemptResultSnapshot) bool {
|
||||
return !attempt.IsZero() && attempt.State() == AssessmentAttemptStateCompleted &&
|
||||
result.Attempt.ID == attempt.ID() && result.Attempt.Kind == attempt.Kind() &&
|
||||
result.Attempt.Mode == attempt.Mode() && result.Outcome == attempt.snapshot.Outcome &&
|
||||
result.DecidedAt.Equal(attempt.snapshot.EndedAt)
|
||||
}
|
||||
|
||||
func assessmentAttemptPublicMatchesResult(attempt AssessmentAttempt, result AssessmentAttemptResultSnapshot) bool {
|
||||
public := attempt.PublicSnapshot()
|
||||
return public.PolicyVersion == result.Attempt.PolicyVersion && public.BankVersion == result.Attempt.BankVersion &&
|
||||
public.AlgorithmVersion == result.Attempt.AlgorithmVersion &&
|
||||
sameSpecialization(public.Specialization, result.Attempt.Specialization) &&
|
||||
sameGrade(public.TargetGrade, result.Attempt.TargetGrade)
|
||||
}
|
||||
|
||||
func assessmentAttemptVersionsMatchResult(versions AssessmentAttemptVersions, pins AssessmentAttemptResultPins) bool {
|
||||
return versions.ProductPolicyVersionID == pins.ProductPolicyVersionID && versions.ProductPolicyVersion == pins.ProductPolicyVersion &&
|
||||
versions.GradingPolicyVersionID == pins.GradingPolicyVersionID && versions.OpeningID == pins.OpeningID &&
|
||||
versions.OpeningManifestHash == pins.OpeningManifestHash && versions.TrainingBindingID == pins.TrainingBindingID &&
|
||||
versions.TrainingManifestHash == pins.TrainingManifestHash && versions.BankVersion == pins.BankVersion &&
|
||||
versions.AlgorithmVersion == pins.AlgorithmVersion
|
||||
}
|
||||
|
||||
func currentCategoryMatchesAttempt(current *CandidateAssessmentCategory, attempt AssessmentAttemptSnapshot) bool {
|
||||
return current != nil && !current.IsZero() && attempt.Specialization != nil &&
|
||||
current.snapshot.CandidateID == attempt.UserID && current.snapshot.Specialization == *attempt.Specialization
|
||||
}
|
||||
|
||||
func restoreDecision(
|
||||
snapshot AssessmentCategoryDecisionSnapshot,
|
||||
current *CandidateAssessmentCategory,
|
||||
) (AssessmentCategoryDecision, error) {
|
||||
const location = "domain.restoreDecision"
|
||||
if err := validateAssessmentCategoryDecision(snapshot); err != nil {
|
||||
return AssessmentCategoryDecision{}, fmt.Errorf("%s - decision is invalid: %w", location, err)
|
||||
}
|
||||
cloned := snapshot
|
||||
cloned.Specialization = clonePointer(snapshot.Specialization)
|
||||
cloned.AttemptedGrade = clonePointer(snapshot.AttemptedGrade)
|
||||
cloned.PreviousGrade = clonePointer(snapshot.PreviousGrade)
|
||||
cloned.CurrentGrade = clonePointer(snapshot.CurrentGrade)
|
||||
return AssessmentCategoryDecision{snapshot: cloned, current: current}, nil
|
||||
}
|
||||
|
||||
// RestoreAssessmentCategoryDecision validates a stored immutable decision.
|
||||
func RestoreAssessmentCategoryDecision(snapshot AssessmentCategoryDecisionSnapshot) (AssessmentCategoryDecision, error) {
|
||||
return restoreDecision(snapshot, nil)
|
||||
}
|
||||
|
||||
func validateAssessmentCategoryDecision(snapshot AssessmentCategoryDecisionSnapshot) error {
|
||||
if snapshot.AttemptID.IsZero() || snapshot.DecidedAt.IsZero() {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
if snapshot.Status == AssessmentCategoryDecisionTrainingOnly {
|
||||
return validateTrainingOnlyCategoryDecision(snapshot)
|
||||
}
|
||||
category, categoryErr := ParseAssessmentCategory(string(snapshot.BankCategory))
|
||||
if categoryErr != nil || category != snapshot.BankCategory {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
if !validRealCategoryDecisionTrack(snapshot) || !validRealCategoryDecisionPins(snapshot) || !validRealCategoryDecisionGrades(snapshot) {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
return validateCategoryDecisionStatus(snapshot)
|
||||
}
|
||||
|
||||
func validateTrainingOnlyCategoryDecision(snapshot AssessmentCategoryDecisionSnapshot) error {
|
||||
if snapshot.Specialization != nil || snapshot.AttemptedGrade != nil || snapshot.PreviousGrade != nil || snapshot.CurrentGrade != nil ||
|
||||
snapshot.Method != "" || snapshot.ReviewID != uuid.Nil || snapshot.Source != "" || snapshot.CalibrationStatus != "" {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validRealCategoryDecisionPins(snapshot AssessmentCategoryDecisionSnapshot) bool {
|
||||
return !snapshot.ProductPolicyVersionID.IsZero() && snapshot.ProductPolicyVersion > 0 &&
|
||||
!snapshot.GradingPolicyVersionID.IsZero() && snapshot.OpeningID != uuid.Nil && snapshot.OpeningManifestHash != [32]byte{}
|
||||
}
|
||||
|
||||
func validRealCategoryDecisionGrades(snapshot AssessmentCategoryDecisionSnapshot) bool {
|
||||
return (snapshot.PreviousGrade == nil || snapshot.PreviousGrade.IsKnown()) &&
|
||||
(snapshot.CurrentGrade == nil || snapshot.CurrentGrade.IsKnown())
|
||||
}
|
||||
|
||||
func validateCategoryDecisionStatus(snapshot AssessmentCategoryDecisionSnapshot) error {
|
||||
switch snapshot.Status {
|
||||
case AssessmentCategoryDecisionNotEligible:
|
||||
return validateNotEligibleCategoryDecision(snapshot)
|
||||
case AssessmentCategoryDecisionNotConfirmed:
|
||||
return validateNotConfirmedCategoryDecision(snapshot)
|
||||
case AssessmentCategoryDecisionAssigned:
|
||||
return validateAssignedCategoryDecision(snapshot)
|
||||
case AssessmentCategoryDecisionUnchanged:
|
||||
return validateUnchangedCategoryDecision(snapshot)
|
||||
case AssessmentCategoryDecisionTrainingOnly:
|
||||
return ErrInvalidAssessmentCategory
|
||||
default:
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
}
|
||||
|
||||
func validateNotEligibleCategoryDecision(snapshot AssessmentCategoryDecisionSnapshot) error {
|
||||
if snapshot.ReviewID != uuid.Nil || snapshot.Method != "" || snapshot.Source != "" ||
|
||||
snapshot.CalibrationStatus != "" || !sameGrade(snapshot.CurrentGrade, snapshot.PreviousGrade) {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateNotConfirmedCategoryDecision(snapshot AssessmentCategoryDecisionSnapshot) error {
|
||||
if !hasSyntheticReviewDecisionPins(snapshot) || !sameGrade(snapshot.CurrentGrade, snapshot.PreviousGrade) {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAssignedCategoryDecision(snapshot AssessmentCategoryDecisionSnapshot) error {
|
||||
if !hasSyntheticReviewDecisionPins(snapshot) || snapshot.CurrentGrade == nil ||
|
||||
*snapshot.CurrentGrade != *snapshot.AttemptedGrade || snapshot.PreviousGrade != nil &&
|
||||
gradeRank(*snapshot.CurrentGrade) <= gradeRank(*snapshot.PreviousGrade) {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateUnchangedCategoryDecision(snapshot AssessmentCategoryDecisionSnapshot) error {
|
||||
if !hasSyntheticReviewDecisionPins(snapshot) || snapshot.PreviousGrade == nil || snapshot.CurrentGrade == nil ||
|
||||
*snapshot.CurrentGrade != *snapshot.PreviousGrade ||
|
||||
gradeRank(*snapshot.AttemptedGrade) > gradeRank(*snapshot.PreviousGrade) {
|
||||
return ErrInvalidAssessmentCategory
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func hasSyntheticReviewDecisionPins(snapshot AssessmentCategoryDecisionSnapshot) bool {
|
||||
return snapshot.ReviewID != uuid.Nil && snapshot.Method == AssessmentCategoryDecisionDemoBasicPassMark &&
|
||||
snapshot.Source == AssessmentCategorySourceSyntheticDemo &&
|
||||
snapshot.CalibrationStatus == AssessmentCategoryCalibrationUncalibrated
|
||||
}
|
||||
|
||||
func validRealCategoryDecisionTrack(snapshot AssessmentCategoryDecisionSnapshot) bool {
|
||||
return snapshot.Specialization != nil && snapshot.Specialization.IsKnown() &&
|
||||
snapshot.AttemptedGrade != nil && snapshot.AttemptedGrade.IsKnown()
|
||||
}
|
||||
|
||||
func sameGrade(left, right *GradeCode) bool {
|
||||
if left == nil || right == nil {
|
||||
return left == nil && right == nil
|
||||
}
|
||||
return *left == *right
|
||||
}
|
||||
|
||||
func sameSpecialization(left, right *SpecializationCode) bool {
|
||||
if left == nil || right == nil {
|
||||
return left == nil && right == nil
|
||||
}
|
||||
return *left == *right
|
||||
}
|
||||
|
||||
func gradeRank(grade GradeCode) int {
|
||||
switch grade {
|
||||
case GradeIntern:
|
||||
return 0
|
||||
case GradeJunior:
|
||||
return 1
|
||||
case GradeMiddle:
|
||||
return 2
|
||||
case GradeSenior:
|
||||
return 3
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
func (decision AssessmentCategoryDecision) Snapshot() AssessmentCategoryDecisionSnapshot {
|
||||
snapshot := decision.snapshot
|
||||
snapshot.Specialization = clonePointer(decision.snapshot.Specialization)
|
||||
snapshot.AttemptedGrade = clonePointer(decision.snapshot.AttemptedGrade)
|
||||
snapshot.PreviousGrade = clonePointer(decision.snapshot.PreviousGrade)
|
||||
snapshot.CurrentGrade = clonePointer(decision.snapshot.CurrentGrade)
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func (decision AssessmentCategoryDecision) ShouldUpdateCurrent() bool {
|
||||
return decision.snapshot.Status == AssessmentCategoryDecisionAssigned && decision.current != nil
|
||||
}
|
||||
|
||||
func (decision AssessmentCategoryDecision) CurrentCategory() (CandidateAssessmentCategory, bool) {
|
||||
if decision.current != nil {
|
||||
return *decision.current, true
|
||||
}
|
||||
return CandidateAssessmentCategory{}, false
|
||||
}
|
||||
|
||||
// AssessmentAttemptResultHistoryEntry is a read-side projection of the
|
||||
// immutable result store. V1 entries have no category decision and therefore
|
||||
// carry no inferred current grade. V2 entries capture the category decision
|
||||
// and current grade as they existed when that result was finalized.
|
||||
type AssessmentAttemptResultHistoryEntry struct {
|
||||
SchemaVersion int32
|
||||
Result AssessmentAttemptResultSnapshot
|
||||
CategoryDecision *AssessmentCategoryDecisionSnapshot
|
||||
CurrentConfirmedGrade *GradeCode
|
||||
}
|
||||
|
||||
func NewLegacyAssessmentAttemptResultHistoryEntry(
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
) (AssessmentAttemptResultHistoryEntry, error) {
|
||||
const location = "domain.NewLegacyAssessmentAttemptResultHistoryEntry"
|
||||
validated, err := RestoreAssessmentAttemptResultSnapshot(result)
|
||||
if err != nil {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - validate legacy result: %w", location, err)
|
||||
}
|
||||
return AssessmentAttemptResultHistoryEntry{
|
||||
SchemaVersion: AssessmentAttemptResultHistorySchemaV1,
|
||||
Result: validated,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewCategoryAssessmentAttemptResultHistoryEntry(
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
decision AssessmentCategoryDecisionSnapshot,
|
||||
) (AssessmentAttemptResultHistoryEntry, error) {
|
||||
const location = "domain.NewCategoryAssessmentAttemptResultHistoryEntry"
|
||||
validated, err := RestoreAssessmentAttemptResultSnapshot(result)
|
||||
if err != nil {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - validate result: %w", location, err)
|
||||
}
|
||||
if validated.Attempt.Kind == AssessmentAttemptKindDemoExpress {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - training result cannot carry category data: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
categoryDecision, err := RestoreAssessmentCategoryDecision(decision)
|
||||
if err != nil {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - validate category decision: %w", location, err)
|
||||
}
|
||||
if !categoryDecisionMatchesResult(categoryDecision.snapshot, validated) {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - decision does not match result: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
entry := AssessmentAttemptResultHistoryEntry{
|
||||
SchemaVersion: AssessmentAttemptResultHistorySchemaV2,
|
||||
Result: validated,
|
||||
CategoryDecision: pointer(categoryDecision.Snapshot()),
|
||||
CurrentConfirmedGrade: clonePointer(categoryDecision.snapshot.CurrentGrade),
|
||||
}
|
||||
return RestoreAssessmentAttemptResultHistoryEntry(entry)
|
||||
}
|
||||
|
||||
func RestoreAssessmentAttemptResultHistoryEntry(
|
||||
entry AssessmentAttemptResultHistoryEntry,
|
||||
) (AssessmentAttemptResultHistoryEntry, error) {
|
||||
const location = "domain.RestoreAssessmentAttemptResultHistoryEntry"
|
||||
result, err := RestoreAssessmentAttemptResultSnapshot(entry.Result)
|
||||
if err != nil {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - restore result: %w", location, err)
|
||||
}
|
||||
entry.Result = result
|
||||
switch entry.SchemaVersion {
|
||||
case AssessmentAttemptResultHistorySchemaV1:
|
||||
if entry.CategoryDecision != nil || entry.CurrentConfirmedGrade != nil {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - legacy result must not contain category data: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
return entry, nil
|
||||
case AssessmentAttemptResultHistorySchemaV2:
|
||||
if entry.CategoryDecision == nil || entry.CurrentConfirmedGrade != nil && !entry.CurrentConfirmedGrade.IsKnown() {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - category result is incomplete: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
decision, err := RestoreAssessmentCategoryDecision(*entry.CategoryDecision)
|
||||
if err != nil {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - restore category decision: %w", location, err)
|
||||
}
|
||||
snapshot := decision.Snapshot()
|
||||
if !categoryDecisionMatchesResult(snapshot, result) || !sameGrade(entry.CurrentConfirmedGrade, snapshot.CurrentGrade) {
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - category result pins or current grade do not match: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
entry.CategoryDecision = pointer(snapshot)
|
||||
entry.CurrentConfirmedGrade = clonePointer(entry.CurrentConfirmedGrade)
|
||||
return entry, nil
|
||||
default:
|
||||
return AssessmentAttemptResultHistoryEntry{}, fmt.Errorf("%s - unsupported result history version: %w", location, ErrInvalidAssessmentCategory)
|
||||
}
|
||||
}
|
||||
|
||||
func categoryDecisionMatchesResult(
|
||||
decision AssessmentCategoryDecisionSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
) bool {
|
||||
return validCategoryResultDecisionStatus(decision.Status) &&
|
||||
decision.AttemptID == result.Attempt.ID && decision.DecidedAt.Equal(result.DecidedAt) &&
|
||||
categoryDecisionPinsMatchResult(decision, result) && categoryDecisionTrackMatchesResult(decision, result)
|
||||
}
|
||||
|
||||
func validCategoryResultDecisionStatus(status AssessmentCategoryDecisionStatus) bool {
|
||||
switch status {
|
||||
case AssessmentCategoryDecisionNotEligible, AssessmentCategoryDecisionNotConfirmed,
|
||||
AssessmentCategoryDecisionAssigned, AssessmentCategoryDecisionUnchanged:
|
||||
return true
|
||||
case AssessmentCategoryDecisionTrainingOnly:
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func categoryDecisionPinsMatchResult(
|
||||
decision AssessmentCategoryDecisionSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
) bool {
|
||||
return decision.ProductPolicyVersionID == result.Pins.ProductPolicyVersionID &&
|
||||
decision.ProductPolicyVersion == result.Pins.ProductPolicyVersion &&
|
||||
decision.GradingPolicyVersionID == result.Pins.GradingPolicyVersionID &&
|
||||
decision.OpeningID == result.Pins.OpeningID && decision.OpeningManifestHash == result.Pins.OpeningManifestHash
|
||||
}
|
||||
|
||||
func categoryDecisionTrackMatchesResult(
|
||||
decision AssessmentCategoryDecisionSnapshot,
|
||||
result AssessmentAttemptResultSnapshot,
|
||||
) bool {
|
||||
return decision.Specialization != nil && sameSpecialization(decision.Specialization, result.Attempt.Specialization) &&
|
||||
sameGrade(decision.AttemptedGrade, result.Attempt.TargetGrade) &&
|
||||
(result.Attempt.Kind == AssessmentAttemptKindRoot || result.Attempt.Kind == AssessmentAttemptKindCorrective) &&
|
||||
result.Attempt.Mode == AssessmentAttemptModeDemoBasic
|
||||
}
|
||||
Reference in new issue
Block a user