feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
@@ -94,6 +95,419 @@ func (repository assessmentBankRepository) ListCurrentSpecializationAvailability
|
||||
return buildAssessmentCatalogAvailability(aggregates), nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) ResolveCurrentSelection(
|
||||
ctx context.Context,
|
||||
specialization domain.SpecializationCode,
|
||||
grade domain.GradeCode,
|
||||
language *domain.TaskLanguageCode,
|
||||
) (outbound.AssessmentBankSelection, error) {
|
||||
const location = "assessmentBankRepository.ResolveCurrentSelection"
|
||||
if !repository.transactional {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - current selection needs a transaction: %w", location, outbound.ErrAssessmentLockOutsideTransaction)
|
||||
}
|
||||
initial, err := repository.currentSelectionRows(ctx, specialization, grade, language)
|
||||
if err != nil {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - read candidate selection: %w", location, err)
|
||||
}
|
||||
if len(initial) == 0 {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - no current reviewed selection: %w", location, outbound.ErrAssessmentSelectionUnavailable)
|
||||
}
|
||||
if len(initial) > 1 {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - multiple current reviewed selections: %w", location, outbound.ErrAssessmentSelectionAmbiguous)
|
||||
}
|
||||
category := domain.AssessmentCategory(initial[0].Category)
|
||||
if err := repository.LockCategory(ctx, category); err != nil {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - lock selected category: %w", location, err)
|
||||
}
|
||||
current, err := repository.currentSelectionRows(ctx, specialization, grade, language)
|
||||
if err != nil {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - recheck candidate selection: %w", location, err)
|
||||
}
|
||||
if len(current) == 0 || len(current) > 1 || current[0].Category != string(category) ||
|
||||
current[0].OpeningID.Bytes != initial[0].OpeningID.Bytes {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - selection changed while locking: %w", location, outbound.ErrAssessmentSelectionUnavailable)
|
||||
}
|
||||
return repository.currentSelectionFromRow(ctx, current[0])
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) currentSelectionRows(
|
||||
ctx context.Context,
|
||||
specialization domain.SpecializationCode,
|
||||
grade domain.GradeCode,
|
||||
language *domain.TaskLanguageCode,
|
||||
) ([]sqlc.ResolveCurrentAssessmentSelectionRow, error) {
|
||||
var selectedLanguage pgtype.Text
|
||||
if language != nil {
|
||||
selectedLanguage = pgText(string(*language))
|
||||
}
|
||||
rows, err := repository.queries.ResolveCurrentAssessmentSelection(ctx, sqlc.ResolveCurrentAssessmentSelectionParams{
|
||||
Specialization: string(specialization), Grade: string(grade), TaskLanguage: selectedLanguage,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, mapAssessmentError(err)
|
||||
}
|
||||
current := make([]sqlc.ResolveCurrentAssessmentSelectionRow, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
metadata, available, err := currentAssessmentSelectionMetadata(row)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("validate current catalog binding: %w", err)
|
||||
}
|
||||
if !available || metadata.Specialization() != specialization || metadata.Grade() != grade ||
|
||||
!assessmentCatalogLanguageMatches(metadata.TaskLanguages(), language) {
|
||||
continue
|
||||
}
|
||||
current = append(current, row)
|
||||
}
|
||||
return current, nil
|
||||
}
|
||||
|
||||
func currentAssessmentSelectionMetadata(
|
||||
row sqlc.ResolveCurrentAssessmentSelectionRow,
|
||||
) (domain.AssessmentCategoryCatalogMetadata, bool, error) {
|
||||
return currentAssessmentCatalogMetadata(sqlc.ListCurrentSpecializationAvailabilityRow{
|
||||
OpeningID: row.OpeningID, Category: row.Category, PolicyVersionID: row.PolicyVersionID,
|
||||
RequiredFamilies: row.RequiredFamilies, PublishedCount: row.PublishedCount,
|
||||
EffectivePolicyID: row.EffectivePolicyID, Specialization: row.Specialization,
|
||||
Grade: row.Grade, TaskLanguages: row.TaskLanguages,
|
||||
PublishedManifestSha256: row.PublishedManifestSha256, PublishedManifest: row.PublishedManifest,
|
||||
})
|
||||
}
|
||||
|
||||
func assessmentCatalogLanguageMatches(
|
||||
available []domain.TaskLanguageCode,
|
||||
requested *domain.TaskLanguageCode,
|
||||
) bool {
|
||||
if requested == nil {
|
||||
return len(available) == 0
|
||||
}
|
||||
return slices.Contains(available, *requested)
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) currentSelectionFromRow(
|
||||
ctx context.Context,
|
||||
row sqlc.ResolveCurrentAssessmentSelectionRow,
|
||||
) (outbound.AssessmentBankSelection, error) {
|
||||
const location = "assessmentBankRepository.currentSelectionFromRow"
|
||||
if !row.EffectivePolicyID.Valid || !row.EffectivePolicyVersion.Valid || row.EffectivePolicyVersion.Int32 < 1 {
|
||||
return outbound.AssessmentBankSelection{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
opening, err := repository.selectionOpening(ctx, row)
|
||||
if err != nil {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - validate current opening: %w", location, err)
|
||||
}
|
||||
entries, manifestSHA, err := currentSelectionManifest(row)
|
||||
if err != nil {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - validate current manifest: %w", location, err)
|
||||
}
|
||||
families, err := repository.resolveCurrentSelectionFamilies(ctx, opening.Category, entries)
|
||||
if err != nil {
|
||||
return outbound.AssessmentBankSelection{}, fmt.Errorf("%s - resolve pinned families: %w", location, err)
|
||||
}
|
||||
return outbound.AssessmentBankSelection{
|
||||
Opening: opening, ProductPolicyVersionID: domain.PolicyVersionID(row.EffectivePolicyID.Bytes),
|
||||
ProductPolicyVersion: row.EffectivePolicyVersion.Int32, ManifestSHA: manifestSHA, Families: families,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) selectionOpening(
|
||||
ctx context.Context,
|
||||
row sqlc.ResolveCurrentAssessmentSelectionRow,
|
||||
) (domain.AssessmentCategoryOpening, error) {
|
||||
opening, err := repository.GetCurrentCategoryOpening(ctx, domain.AssessmentCategory(row.Category))
|
||||
if err != nil {
|
||||
return domain.AssessmentCategoryOpening{}, err
|
||||
}
|
||||
if opening.ID != row.OpeningID.Bytes || opening.PolicyVersionID != domain.PolicyVersionID(row.PolicyVersionID.Bytes) ||
|
||||
opening.RequiredFamilies != row.RequiredFamilies || row.PublishedCount < row.RequiredFamilies {
|
||||
return domain.AssessmentCategoryOpening{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
return opening, nil
|
||||
}
|
||||
|
||||
func currentSelectionManifest(
|
||||
row sqlc.ResolveCurrentAssessmentSelectionRow,
|
||||
) ([]domain.AssessmentCategoryManifestEntry, [sha256.Size]byte, error) {
|
||||
entries, err := decodeAssessmentCategoryManifest(row.PublishedManifest)
|
||||
if err != nil {
|
||||
return nil, [sha256.Size]byte{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
fingerprint, err := assessmentCategoryManifestFingerprint(entries)
|
||||
if err != nil || len(entries) != int(row.PublishedCount) || !slices.Equal(fingerprint[:], row.PublishedManifestSha256) {
|
||||
return nil, [sha256.Size]byte{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
return entries, fingerprint, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) resolveCurrentSelectionFamilies(
|
||||
ctx context.Context,
|
||||
category domain.AssessmentCategory,
|
||||
entries []domain.AssessmentCategoryManifestEntry,
|
||||
) ([]domain.AssessmentFamily, error) {
|
||||
families := make([]domain.AssessmentFamily, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
family, err := repository.GetFamilyVersion(ctx, entry.FamilyKey, entry.Version)
|
||||
if err != nil {
|
||||
return nil, mapPinnedFamilyLookupError(err)
|
||||
}
|
||||
hash := family.ContentHash()
|
||||
if family.Category() != category || family.Lifecycle() != domain.AssessmentLifecyclePublished ||
|
||||
family.ReviewStatus() != domain.AssessmentReviewApproved || !slices.Equal(hash[:], entry.ContentSHA256) {
|
||||
return nil, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
families = append(families, family)
|
||||
}
|
||||
return families, nil
|
||||
}
|
||||
|
||||
func mapPinnedFamilyLookupError(err error) error {
|
||||
if errors.Is(err, outbound.ErrAssessmentFamilyNotFound) {
|
||||
return outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) ResolveCurrentTrainingSelection(
|
||||
ctx context.Context,
|
||||
productPolicyID domain.PolicyVersionID,
|
||||
productPolicyVersion int32,
|
||||
) (outbound.AssessmentTrainingBankSelection, error) {
|
||||
const location = "assessmentBankRepository.ResolveCurrentTrainingSelection"
|
||||
if !repository.transactional {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - training selection needs a transaction: %w", location, outbound.ErrAssessmentLockOutsideTransaction)
|
||||
}
|
||||
initial, err := repository.queries.ListCurrentAssessmentTrainingBindings(ctx)
|
||||
if err != nil {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - read current bindings: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
locked, err := repository.lockCurrentTrainingCategories(ctx, initial, productPolicyID, productPolicyVersion)
|
||||
if err != nil {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - lock training categories: %w", location, err)
|
||||
}
|
||||
current, err := repository.queries.ListCurrentAssessmentTrainingBindings(ctx)
|
||||
if err != nil {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - recheck current bindings: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
selections, err := repository.currentTrainingSelections(ctx, current, locked, productPolicyID, productPolicyVersion)
|
||||
if err != nil {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - validate current bindings: %w", location, err)
|
||||
}
|
||||
if len(selections) == 0 {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - no current reviewed training binding: %w", location, outbound.ErrAssessmentSelectionUnavailable)
|
||||
}
|
||||
if len(selections) > 1 {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - multiple current reviewed training bindings: %w", location, outbound.ErrAssessmentSelectionAmbiguous)
|
||||
}
|
||||
return selections[0], nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) lockCurrentTrainingCategories(
|
||||
ctx context.Context,
|
||||
rows []sqlc.ListCurrentAssessmentTrainingBindingsRow,
|
||||
productPolicyID domain.PolicyVersionID,
|
||||
productPolicyVersion int32,
|
||||
) (map[domain.AssessmentCategory]struct{}, error) {
|
||||
locked := make(map[domain.AssessmentCategory]struct{})
|
||||
for _, row := range rows {
|
||||
if !matchesProductPolicy(row.ProductPolicyVersionID, row.ProductPolicyVersion, productPolicyID, productPolicyVersion) {
|
||||
continue
|
||||
}
|
||||
category := domain.AssessmentCategory(row.Category)
|
||||
if _, exists := locked[category]; exists {
|
||||
continue
|
||||
}
|
||||
if err := repository.LockCategory(ctx, category); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
locked[category] = struct{}{}
|
||||
}
|
||||
return locked, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) currentTrainingSelections(
|
||||
ctx context.Context,
|
||||
rows []sqlc.ListCurrentAssessmentTrainingBindingsRow,
|
||||
locked map[domain.AssessmentCategory]struct{},
|
||||
productPolicyID domain.PolicyVersionID,
|
||||
productPolicyVersion int32,
|
||||
) ([]outbound.AssessmentTrainingBankSelection, error) {
|
||||
selections := make([]outbound.AssessmentTrainingBankSelection, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
if !matchesProductPolicy(row.ProductPolicyVersionID, row.ProductPolicyVersion, productPolicyID, productPolicyVersion) {
|
||||
continue
|
||||
}
|
||||
category := domain.AssessmentCategory(row.Category)
|
||||
if _, ok := locked[category]; !ok {
|
||||
return nil, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
selection, err := repository.currentTrainingSelectionFromRow(ctx, row)
|
||||
if errors.Is(err, outbound.ErrAssessmentSelectionUnavailable) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
selections = append(selections, selection)
|
||||
}
|
||||
return selections, nil
|
||||
}
|
||||
|
||||
func matchesProductPolicy(
|
||||
rowID pgtype.UUID,
|
||||
rowVersion int32,
|
||||
policyID domain.PolicyVersionID,
|
||||
policyVersion int32,
|
||||
) bool {
|
||||
return rowID.Bytes == uuid.UUID(policyID) && rowVersion == policyVersion
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) currentTrainingSelectionFromRow(
|
||||
ctx context.Context,
|
||||
row sqlc.ListCurrentAssessmentTrainingBindingsRow,
|
||||
) (outbound.AssessmentTrainingBankSelection, error) {
|
||||
const location = "assessmentBankRepository.currentTrainingSelectionFromRow"
|
||||
category := domain.AssessmentCategory(row.Category)
|
||||
entries, manifestSHA, err := repository.validateCurrentTrainingManifest(ctx, category, row)
|
||||
if err != nil {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - validate current manifest: %w", location, err)
|
||||
}
|
||||
members, err := repository.queries.ListAssessmentTrainingBindingFamilies(ctx, pgUUID(row.ID.Bytes))
|
||||
if err != nil {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - read pinned training families: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
families, err := repository.resolveCurrentTrainingFamilies(ctx, category, entries, members)
|
||||
if err != nil {
|
||||
return outbound.AssessmentTrainingBankSelection{}, fmt.Errorf("%s - resolve pinned families: %w", location, err)
|
||||
}
|
||||
return outbound.AssessmentTrainingBankSelection{
|
||||
BindingID: row.ID.Bytes, Category: category,
|
||||
ProductPolicyVersionID: domain.PolicyVersionID(row.ProductPolicyVersionID.Bytes),
|
||||
ProductPolicyVersion: row.ProductPolicyVersion, ManifestSHA: manifestSHA, Families: families,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) validateCurrentTrainingManifest(
|
||||
ctx context.Context,
|
||||
category domain.AssessmentCategory,
|
||||
row sqlc.ListCurrentAssessmentTrainingBindingsRow,
|
||||
) ([]domain.AssessmentCategoryManifestEntry, [sha256.Size]byte, error) {
|
||||
entries, err := decodeAssessmentCategoryManifest(string(row.FamilyManifest))
|
||||
if err != nil || len(entries) != int(row.ItemCount) {
|
||||
return nil, [sha256.Size]byte{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
manifestSHA, err := assessmentCategoryManifestFingerprint(entries)
|
||||
if err != nil || !slices.Equal(manifestSHA[:], row.ManifestSha256) {
|
||||
return nil, [sha256.Size]byte{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
liveEntries, err := repository.currentPublishedManifestEntries(ctx, category)
|
||||
if err != nil {
|
||||
return nil, [sha256.Size]byte{}, err
|
||||
}
|
||||
liveHash, err := assessmentCategoryManifestFingerprint(liveEntries)
|
||||
if err != nil || !assessmentManifestEntriesEqual(entries, liveEntries) || !slices.Equal(liveHash[:], row.ManifestSha256) {
|
||||
return nil, [sha256.Size]byte{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
return entries, manifestSHA, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) currentPublishedManifestEntries(
|
||||
ctx context.Context,
|
||||
category domain.AssessmentCategory,
|
||||
) ([]domain.AssessmentCategoryManifestEntry, error) {
|
||||
rows, err := repository.queries.ReadPublishedAssessmentCategoryManifest(ctx, string(category))
|
||||
if err != nil {
|
||||
return nil, mapAssessmentError(err)
|
||||
}
|
||||
entries := make([]domain.AssessmentCategoryManifestEntry, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
key, parseErr := domain.ParseAssessmentFamilyKey(row.FamilyKey)
|
||||
if parseErr != nil || row.Version < 1 || len(row.ContentSha256) != sha256.Size {
|
||||
return nil, domain.ErrInvalidAssessmentFamily
|
||||
}
|
||||
entries = append(entries, domain.AssessmentCategoryManifestEntry{
|
||||
FamilyKey: key, Version: row.Version, ContentSHA256: slices.Clone(row.ContentSha256),
|
||||
})
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) resolveCurrentTrainingFamilies(
|
||||
ctx context.Context,
|
||||
category domain.AssessmentCategory,
|
||||
entries []domain.AssessmentCategoryManifestEntry,
|
||||
members []sqlc.ListAssessmentTrainingBindingFamiliesRow,
|
||||
) ([]domain.AssessmentFamily, error) {
|
||||
if len(members) != len(entries) {
|
||||
return nil, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
sort.Slice(entries, func(i, j int) bool {
|
||||
if entries[i].FamilyKey == entries[j].FamilyKey {
|
||||
return entries[i].Version < entries[j].Version
|
||||
}
|
||||
return entries[i].FamilyKey < entries[j].FamilyKey
|
||||
})
|
||||
families := make([]domain.AssessmentFamily, 0, len(entries))
|
||||
for index, entry := range entries {
|
||||
family, err := repository.resolveTrainingFamily(ctx, category, entry, members[index], index)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
families = append(families, family)
|
||||
}
|
||||
return families, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) resolveTrainingFamily(
|
||||
ctx context.Context,
|
||||
category domain.AssessmentCategory,
|
||||
entry domain.AssessmentCategoryManifestEntry,
|
||||
member sqlc.ListAssessmentTrainingBindingFamiliesRow,
|
||||
index int,
|
||||
) (domain.AssessmentFamily, error) {
|
||||
if member.Ordinal < 1 || int64(member.Ordinal) != int64(index)+1 || member.FamilyKey != string(entry.FamilyKey) ||
|
||||
member.FamilyVersion != entry.Version || !slices.Equal(member.ContentSha256, entry.ContentSHA256) {
|
||||
return domain.AssessmentFamily{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
family, err := repository.GetFamilyVersion(ctx, entry.FamilyKey, entry.Version)
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, mapPinnedFamilyLookupError(err)
|
||||
}
|
||||
hash := family.ContentHash()
|
||||
if family.ID().UUID() != member.FamilyID.Bytes || family.Category() != category ||
|
||||
family.Lifecycle() != domain.AssessmentLifecyclePublished || family.ReviewStatus() != domain.AssessmentReviewApproved ||
|
||||
!slices.Equal(hash[:], entry.ContentSHA256) {
|
||||
return domain.AssessmentFamily{}, outbound.ErrAssessmentSelectionUnavailable
|
||||
}
|
||||
return family, nil
|
||||
}
|
||||
|
||||
func assessmentManifestEntriesEqual(
|
||||
left []domain.AssessmentCategoryManifestEntry,
|
||||
right []domain.AssessmentCategoryManifestEntry,
|
||||
) bool {
|
||||
if len(left) != len(right) {
|
||||
return false
|
||||
}
|
||||
canonical := func(entries []domain.AssessmentCategoryManifestEntry) []domain.AssessmentCategoryManifestEntry {
|
||||
result := slices.Clone(entries)
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
if result[i].FamilyKey == result[j].FamilyKey {
|
||||
return result[i].Version < result[j].Version
|
||||
}
|
||||
return result[i].FamilyKey < result[j].FamilyKey
|
||||
})
|
||||
return result
|
||||
}
|
||||
orderedLeft, orderedRight := canonical(left), canonical(right)
|
||||
for index := range orderedLeft {
|
||||
if orderedLeft[index].FamilyKey != orderedRight[index].FamilyKey ||
|
||||
orderedLeft[index].Version != orderedRight[index].Version ||
|
||||
!slices.Equal(orderedLeft[index].ContentSHA256, orderedRight[index].ContentSHA256) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func currentAssessmentCatalogMetadata(
|
||||
row sqlc.ListCurrentSpecializationAvailabilityRow,
|
||||
) (domain.AssessmentCategoryCatalogMetadata, bool, error) {
|
||||
|
||||
Reference in new issue
Block a user