package outbound import ( "context" "errors" "time" "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") ) // 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 } // 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) // 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) }