feat(assessment): assign the demonstration category and keep result history
This commit is contained in:
1 parent
b3c6dc870b
commit
8277133712
61 files changed
+4338
-268
No files matched your search
@@ -0,0 +1,161 @@
|
||||
package assessment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// CategoryAttestation describes one stored attestation of a synthetic
|
||||
// demonstration category. It holds no candidate data.
|
||||
type CategoryAttestation struct {
|
||||
ReviewID uuid.UUID
|
||||
OpeningID uuid.UUID
|
||||
Category domain.AssessmentCategory
|
||||
ProductPolicyVersion int32
|
||||
ReviewedBy domain.AssessmentOperator
|
||||
// Created is false when an identical attestation already existed.
|
||||
Created bool
|
||||
}
|
||||
|
||||
// CategoryAttestor lets an operator attest the exact current opening of a
|
||||
// category, its published manifest, and the product policy now in force as a
|
||||
// source for synthetic demonstration categories. The attestation row is the
|
||||
// audit record: it is immutable and keeps the operator handle and the database
|
||||
// time. It grants nothing by itself; assignment also needs the runtime gate.
|
||||
type CategoryAttestor struct {
|
||||
transactor outbound.Transactor
|
||||
}
|
||||
|
||||
func NewCategoryAttestor(transactor outbound.Transactor) (*CategoryAttestor, error) {
|
||||
if transactor == nil {
|
||||
return nil, errors.New("assessment.NewCategoryAttestor - transactor must be set")
|
||||
}
|
||||
return &CategoryAttestor{transactor: transactor}, nil
|
||||
}
|
||||
|
||||
// Attest resolves the same current selection that a new attempt would pin and
|
||||
// stores the attestation in one transaction. The policy must be the complete
|
||||
// explicit demo_basic demonstration policy. Repeating the call for the same
|
||||
// opening, manifest, and policy returns the stored attestation unchanged.
|
||||
func (attestor *CategoryAttestor) Attest(
|
||||
ctx context.Context,
|
||||
specialization domain.SpecializationCode,
|
||||
grade domain.GradeCode,
|
||||
language *domain.TaskLanguageCode,
|
||||
actor string,
|
||||
) (CategoryAttestation, error) {
|
||||
const location = "CategoryAttestor.Attest"
|
||||
operator, err := domain.ParseAssessmentOperator(actor)
|
||||
if err != nil || !specialization.IsKnown() || !grade.IsKnown() || language != nil && !language.IsKnown() {
|
||||
return CategoryAttestation{}, fmt.Errorf("%s - validate request: %w", location, domain.ErrInvalidAssessmentCategory)
|
||||
}
|
||||
reviewID, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return CategoryAttestation{}, fmt.Errorf("%s - generate review ID: %w", location, err)
|
||||
}
|
||||
var result CategoryAttestation
|
||||
err = attestor.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
||||
selection, err := unit.AssessmentBank().ResolveCurrentSelection(ctx, specialization, grade, language)
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve current selection: %w", err)
|
||||
}
|
||||
policy, err := unit.Policies().GetByID(ctx, selection.ProductPolicyVersionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read product policy: %w", err)
|
||||
}
|
||||
if err := domain.ValidateSyntheticCategoryPolicy(policy); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err = storeCategoryAttestation(ctx, unit, selection, policy, operator, reviewID)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return CategoryAttestation{}, fmt.Errorf("%s - transaction: %w", location, err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// storeCategoryAttestation returns the stored attestation of the exact opening,
|
||||
// manifest, and policy, or creates it.
|
||||
func storeCategoryAttestation(
|
||||
ctx context.Context,
|
||||
unit outbound.UnitOfWork,
|
||||
selection outbound.AssessmentBankSelection,
|
||||
policy domain.PolicyVersion,
|
||||
operator domain.AssessmentOperator,
|
||||
reviewID uuid.UUID,
|
||||
) (CategoryAttestation, error) {
|
||||
repository := unit.AssessmentAttempts()
|
||||
category := selection.Opening.Category
|
||||
stored, err := readStoredCategoryAttestation(ctx, repository, selection, policy)
|
||||
if err == nil {
|
||||
return stored, nil
|
||||
}
|
||||
if !errors.Is(err, errCategoryAttestationMissing) {
|
||||
return CategoryAttestation{}, err
|
||||
}
|
||||
at, err := unit.AssessmentBank().DatabaseTime(ctx)
|
||||
if err != nil {
|
||||
return CategoryAttestation{}, fmt.Errorf("read database time: %w", err)
|
||||
}
|
||||
review, err := domain.NewAssessmentSyntheticCategoryReview(domain.AssessmentSyntheticCategoryReviewSnapshot{
|
||||
ID: reviewID, OpeningID: selection.Opening.ID, Category: category,
|
||||
Source: domain.AssessmentCategorySourceSyntheticDemo, OpeningManifestHash: selection.ManifestSHA,
|
||||
ProductPolicyVersionID: policy.ID(), ProductPolicyVersion: policy.Version(),
|
||||
ReviewedBy: operator, ReviewedAt: at.UTC().Truncate(time.Microsecond),
|
||||
})
|
||||
if err != nil {
|
||||
return CategoryAttestation{}, fmt.Errorf("build attestation: %w", err)
|
||||
}
|
||||
created, err := repository.CreateSyntheticCategoryReview(ctx, review)
|
||||
if err != nil {
|
||||
return CategoryAttestation{}, fmt.Errorf("store attestation: %w", err)
|
||||
}
|
||||
if !created {
|
||||
// A concurrent operator stored the same exact source first.
|
||||
stored, err := readStoredCategoryAttestation(ctx, repository, selection, policy)
|
||||
if errors.Is(err, errCategoryAttestationMissing) {
|
||||
return CategoryAttestation{}, fmt.Errorf("attestation vanished after a conflict: %w", err)
|
||||
}
|
||||
return stored, err
|
||||
}
|
||||
return CategoryAttestation{
|
||||
ReviewID: reviewID, OpeningID: selection.Opening.ID, Category: category,
|
||||
ProductPolicyVersion: policy.Version(), ReviewedBy: operator, Created: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var errCategoryAttestationMissing = errors.New("category attestation is missing")
|
||||
|
||||
// readStoredCategoryAttestation reads the attestation of the exact opening,
|
||||
// manifest, and policy, or reports errCategoryAttestationMissing.
|
||||
func readStoredCategoryAttestation(
|
||||
ctx context.Context,
|
||||
repository outbound.AssessmentAttemptRepository,
|
||||
selection outbound.AssessmentBankSelection,
|
||||
policy domain.PolicyVersion,
|
||||
) (CategoryAttestation, error) {
|
||||
category := selection.Opening.Category
|
||||
existing, found, err := repository.GetSyntheticCategoryReview(
|
||||
ctx, selection.Opening.ID, category, selection.ManifestSHA,
|
||||
policy.ID(), policy.Version(), selection.Opening.PolicyVersionID,
|
||||
)
|
||||
if err != nil {
|
||||
return CategoryAttestation{}, fmt.Errorf("read existing attestation: %w", err)
|
||||
}
|
||||
if !found {
|
||||
return CategoryAttestation{}, errCategoryAttestationMissing
|
||||
}
|
||||
snapshot := existing.Snapshot()
|
||||
return CategoryAttestation{
|
||||
ReviewID: snapshot.ID, OpeningID: snapshot.OpeningID, Category: category,
|
||||
ProductPolicyVersion: snapshot.ProductPolicyVersion, ReviewedBy: snapshot.ReviewedBy,
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user