Files
lincodex/backend/internal/port/outbound/assessment_bank.go
T

221 lines
11 KiB
Go

package outbound
import (
"context"
"crypto/sha256"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var (
// ErrAssessmentFamilyNotFound reports that no family version matches.
ErrAssessmentFamilyNotFound = errors.New("assessment family not found")
// ErrAssessmentFamilyConflict reports that the family key already has a
// version with that number, or the ID is taken. Nothing was stored and
// the surrounding transaction stays usable.
ErrAssessmentFamilyConflict = errors.New("assessment family version already exists")
// ErrAssessmentFamilyImmutable reports an attempt to change the content
// of a stored version or to remove one. The database refuses every such
// UPDATE, DELETE, and TRUNCATE; import a new version instead.
ErrAssessmentFamilyImmutable = errors.New("assessment family version is immutable")
// ErrAssessmentFamilyLifecycle reports a lifecycle step that is not
// allowed: not draft to published to retired, or from a state that
// changed in the meantime.
ErrAssessmentFamilyLifecycle = errors.New("assessment family lifecycle step is not allowed")
// ErrAssessmentFamilyNotPublishable reports a publication that the
// database refuses because the version has no approved review.
ErrAssessmentFamilyNotPublishable = errors.New("assessment family has no approved review")
// ErrAssessmentCoverageInsufficient reports an opening that the database
// refuses because the category has fewer published families than
// required.
ErrAssessmentCoverageInsufficient = errors.New("assessment category has insufficient coverage")
// ErrAssessmentGradingPolicyNotFound reports an opening that references
// a policy version that is not stored.
ErrAssessmentGradingPolicyNotFound = errors.New("assessment grading policy version not found")
// ErrAssessmentLockOutsideTransaction reports a LockCategory call on the
// standalone repository: an advisory lock that is scoped to a
// transaction would be released at once.
ErrAssessmentLockOutsideTransaction = errors.New("assessment category lock needs a transaction")
// ErrAssessmentCategoryNotOpen reports that the category has no opening.
ErrAssessmentCategoryNotOpen = errors.New("assessment category is not open")
// ErrAssessmentCatalogBindingImmutable reports an attempt to edit or remove
// an immutable reviewed catalog binding.
ErrAssessmentCatalogBindingImmutable = errors.New("assessment catalog binding is immutable")
// ErrAssessmentSelectionUnavailable reports that no current reviewed
// category exactly matches the requested public selection.
ErrAssessmentSelectionUnavailable = errors.New("assessment selection is unavailable")
// ErrAssessmentSelectionAmbiguous reports more than one current category
// matching the same public selection; the server never chooses arbitrarily.
ErrAssessmentSelectionAmbiguous = errors.New("assessment selection is ambiguous")
)
// MaxAssessmentFamilyPageSize is the largest page that ListFamilies returns.
const MaxAssessmentFamilyPageSize = 200
// AssessmentFamilyCursor is the position after which ListFamilies continues:
// the last key and version of the previous page. The zero value starts at the
// beginning.
type AssessmentFamilyCursor struct {
Key domain.AssessmentFamilyKey
Version int32
}
// AssessmentBankSelection is the exact reviewed category manifest used to
// build one real attempt. Families are the entries from the pinned manifest,
// not an inferred snapshot of current rows.
type AssessmentBankSelection struct {
Opening domain.AssessmentCategoryOpening
ProductPolicyVersionID domain.PolicyVersionID
ProductPolicyVersion int32
ManifestSHA [sha256.Size]byte
Families []domain.AssessmentFamily
}
// AssessmentTrainingBankSelection is an explicitly reviewed, non-qualification
// family set pinned to one product policy version.
type AssessmentTrainingBankSelection struct {
BindingID uuid.UUID
Category domain.AssessmentCategory
ProductPolicyVersionID domain.PolicyVersionID
ProductPolicyVersion int32
ManifestSHA [sha256.Size]byte
Families []domain.AssessmentFamily
}
// AssessmentAvailabilitySelection is one current, reviewed specialization,
// grade, and language binding with the family keys from its verified live
// published manifest. It exposes no task content.
type AssessmentAvailabilitySelection struct {
OpeningID uuid.UUID
Category domain.AssessmentCategory
ProductPolicyVersionID domain.PolicyVersionID
ProductPolicyVersion int32
Specialization domain.SpecializationCode
Grade domain.GradeCode
TaskLanguages []domain.TaskLanguageCode
FamilyKeys []domain.AssessmentFamilyKey
}
// AssessmentBankRepositories exposes the assessment bank. UnitOfWork binds it
// to one transaction; an adapter may also provide it outside a transaction,
// where every call is its own statement. A returned family passed
// domain.RestoreAssessmentFamily, including the content hash check. The
// repository has no method that edits the content of a stored version.
type AssessmentBankRepositories interface {
AssessmentBank() AssessmentBankRepository
}
// AssessmentBankRepository stores immutable family versions and category
// openings.
type AssessmentBankRepository interface {
// CreateFamily stores a draft and returns the stored version. It returns
// an error wrapping ErrAssessmentFamilyConflict for a taken ID or
// (key, version), without aborting a surrounding transaction.
CreateFamily(ctx context.Context, draft domain.AssessmentFamilyDraft) (domain.AssessmentFamily, error)
// GetFamilyVersion returns ErrAssessmentFamilyNotFound for an unknown
// version.
GetFamilyVersion(ctx context.Context, key domain.AssessmentFamilyKey, version int32) (domain.AssessmentFamily, error)
// GetLatestFamily returns the version with the highest number of the
// key, or ErrAssessmentFamilyNotFound.
GetLatestFamily(ctx context.Context, key domain.AssessmentFamilyKey) (domain.AssessmentFamily, error)
// ListFamilies returns up to limit versions of the category after the
// cursor, ordered by key and version. limit must be between 1 and
// MaxAssessmentFamilyPageSize.
ListFamilies(ctx context.Context, category domain.AssessmentCategory, after AssessmentFamilyCursor, limit int32) ([]domain.AssessmentFamily, error)
// GetPublishedFamily returns the one published version of the key, or
// ErrAssessmentFamilyNotFound when none is published.
GetPublishedFamily(ctx context.Context, key domain.AssessmentFamilyKey) (domain.AssessmentFamily, error)
// LockCategory takes a transaction-scoped advisory lock of the category.
// Openings, publications, and retirements of one category take it first,
// so the coverage that the database counts cannot miss a concurrent
// change. It is released when the transaction ends, so it works only
// inside a transaction: the repository of a UnitOfWork takes it, and the
// standalone repository returns ErrAssessmentLockOutsideTransaction.
LockCategory(ctx context.Context, category domain.AssessmentCategory) error
// CountPublishedFamilies counts the families of the category that have a
// published version.
CountPublishedFamilies(ctx context.Context, category domain.AssessmentCategory) (int32, error)
// MarkPublished moves a draft to published and records the operator. It
// returns an error wrapping
// ErrAssessmentFamilyLifecycle when the version is not a draft and
// ErrAssessmentFamilyNotPublishable when the database refuses it for
// lack of an approved review.
MarkPublished(ctx context.Context, id domain.AssessmentFamilyID, by domain.AssessmentOperator) (domain.AssessmentFamily, error)
// MarkRetired moves a published version to retired and records the
// operator, or returns an error
// wrapping ErrAssessmentFamilyLifecycle.
MarkRetired(ctx context.Context, id domain.AssessmentFamilyID, by domain.AssessmentOperator) (domain.AssessmentFamily, error)
// CreateCategoryOpening stores an opening and returns it with the
// coverage the database counted. It returns
// ErrAssessmentCoverageInsufficient or ErrAssessmentGradingPolicyNotFound
// when the database refuses the row. The database derives the required
// coverage from the policy document and counts the published families;
// the draft's own numbers are not stored.
CreateCategoryOpening(ctx context.Context, draft domain.AssessmentCategoryOpeningDraft) (domain.AssessmentCategoryOpening, error)
// BindCategoryCatalog attaches reviewed public metadata to an opening. The
// adapter pins it to the exact currently published family manifest.
BindCategoryCatalog(ctx context.Context, opening domain.AssessmentCategoryOpening, metadata domain.AssessmentCategoryCatalogMetadata) error
// ListCurrentSpecializationAvailability returns ready public specializations
// from one database snapshot. Legacy openings or bindings whose policy,
// coverage, or published-family manifest changed are omitted.
ListCurrentSpecializationAvailability(ctx context.Context) ([]domain.SpecializationAvailability, error)
// ListCurrentAssessmentAvailabilitySelections returns current, reviewed
// selection metadata pinned to the effective product policy at asOf. The
// implementation validates each live published-family manifest and does
// not take category locks or load task content.
ListCurrentAssessmentAvailabilitySelections(ctx context.Context, asOf time.Time) ([]AssessmentAvailabilitySelection, error)
// DatabaseTime returns the database clock. It stamps the effective time
// of a policy version, so the version in force now is judged against it
// and not against the application clock.
DatabaseTime(ctx context.Context) (time.Time, error)
// GetCurrentCategoryOpening returns the newest opening of the category,
// or ErrAssessmentCategoryNotOpen.
GetCurrentCategoryOpening(ctx context.Context, category domain.AssessmentCategory) (domain.AssessmentCategoryOpening, error)
// ResolveCurrentSelection finds exactly one current catalog binding for
// the public request, locks its category, and rechecks the opening, product
// policy, live manifest, and exact family versions under that lock.
ResolveCurrentSelection(
ctx context.Context,
specialization domain.SpecializationCode,
grade domain.GradeCode,
language *domain.TaskLanguageCode,
) (AssessmentBankSelection, error)
// ResolveCurrentTrainingSelection returns exactly one reviewed training
// binding whose policy pin and published family manifest remain current.
ResolveCurrentTrainingSelection(
ctx context.Context,
productPolicyID domain.PolicyVersionID,
productPolicyVersion int32,
) (AssessmentTrainingBankSelection, error)
}