feat(evidence): project skill evidence from confirmed assessments

This commit is contained in:
shira committed 2026-10-09 11:25:19 +03:00
1 parent 4761508f13
commit c2a8d17195
94 files changed
+10077 -151

No files matched your search

@@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"time"
"github.com/google/uuid"
@@ -114,23 +115,90 @@ func (service *Service) getOrganizationCardInTransaction(
if err != nil {
return domain.AnonymousCandidateViewDTO{}, err
}
decision, err := service.quota(ctx, unit, organization, candidateID)
err = service.reserveCardView(ctx, unit, repository, organization, candidateID, location, func(at time.Time) (domain.CandidateCardAccess, error) {
return cardViewAccessEvent(candidateID, organizationID, actorID, view, at)
})
if err != nil {
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - reserve card view: %w", location, err)
}
if !decision.Allowed {
return domain.AnonymousCandidateViewDTO{}, &RateLimitedError{RetryAt: decision.RetryAt.UTC()}
}
event, err := cardViewAccessEvent(candidateID, organizationID, actorID, view, service.clock())
if err != nil {
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - build access audit: %w", location, err)
}
if err := repository.RecordCandidateCardAccess(ctx, event); err != nil {
return domain.AnonymousCandidateViewDTO{}, fmt.Errorf("%s - record access audit: %w", location, err)
return domain.AnonymousCandidateViewDTO{}, err
}
return view, nil
}
// reserveCardView consumes the card-view quota and appends the access audit
// that auditFor builds. Both commit with the visibility checks that the
// caller ran in the same transaction, so a failed read leaves neither behind.
func (service *Service) reserveCardView(
ctx context.Context,
unit outbound.UnitOfWork,
repository outbound.PrivacyViewRepository,
organization domain.Organization,
candidateID uuid.UUID,
location string,
auditFor func(at time.Time) (domain.CandidateCardAccess, error),
) error {
decision, err := service.quota(ctx, unit, organization, candidateID)
if err != nil {
return fmt.Errorf("%s - reserve card view: %w", location, err)
}
if !decision.Allowed {
return &RateLimitedError{RetryAt: decision.RetryAt.UTC()}
}
event, err := auditFor(service.clock())
if err != nil {
return fmt.Errorf("%s - build access audit: %w", location, err)
}
if err := repository.RecordCandidateCardAccess(ctx, event); err != nil {
return fmt.Errorf("%s - record access audit: %w", location, err)
}
return nil
}
// AuthorizeCategoriesSection runs the checks of an organization card read
// inside the caller's transaction and reports which candidate, and which
// categories (specialization and grade), the card shows. It exists for the sections
// of a card that explain its categories, such as the permitted evidence: they
// must not disclose more than the categories of the current card do.
//
// The checks are those of GetOrganizationCard: the active membership of the
// actor, the effective C2 publication, the hiding of this organization, and
// the minimum cohort, all under the same locks. The card must also show at
// least one category after the cohort rules, otherwise the answer is
// ErrCandidateCardNotFound, the same as for an unknown handle. A successful
// call consumes the card-view quota and records the access with the
// categories field, so the candidate sees the read in the access history. The
// caller reads the section in the same transaction, after this returns.
func (service *Service) AuthorizeCategoriesSection(
ctx context.Context,
unit outbound.UnitOfWork,
actorID, organizationID uuid.UUID,
handle string,
) (uuid.UUID, []domain.CandidateViewCategory, error) {
const location = "candidateview.Service.AuthorizeCategoriesSection"
if actorID == uuid.Nil || organizationID == uuid.Nil {
return uuid.Nil, nil, ErrForbidden
}
if !domain.IsCandidateViewPseudonym(handle) {
return uuid.Nil, nil, ErrInvalidCandidateHandle
}
organization, repository, candidateID, view, err := service.currentOrganizationCard(ctx, unit, actorID, organizationID, handle, location)
if err != nil {
return uuid.Nil, nil, err
}
if len(view.Categories) == 0 {
return uuid.Nil, nil, ErrCandidateCardNotFound
}
err = service.reserveCardView(ctx, unit, repository, organization, candidateID, location, func(at time.Time) (domain.CandidateCardAccess, error) {
return domain.NewCandidateCardAccess(domain.CandidateCardAccessInput{
CandidateID: candidateID, OrganizationID: organizationID, ActorID: actorID,
Kind: domain.CandidateCardAccessCardViewed, ProjectionFields: []domain.CardField{domain.CardFieldCategories}, OccurredAt: at,
})
})
if err != nil {
return uuid.Nil, nil, err
}
return candidateID, slices.Clone(view.Categories), nil
}
func (service *Service) currentOrganizationCard(
ctx context.Context,
unit outbound.UnitOfWork,
@@ -216,9 +284,13 @@ func projectedViewForHandle(
// PreviewResult contains only the candidate's own current publication state
// and the strict anonymous-card projection.
type PreviewResult struct {
Published bool
Fields domain.CardFieldSet
Card *domain.AnonymousCandidateViewDTO
Published bool
Fields domain.CardFieldSet
Card *domain.AnonymousCandidateViewDTO
// Evidence is what an organization would read with the categories of the
// card: for each category, the one fact that supports its confirmed grade.
// It is empty when the card shows no category.
Evidence []domain.EvidenceOrganizationFact
GeneratedAt time.Time
}
@@ -266,7 +338,13 @@ func (service *Service) GetMyCardPreview(ctx context.Context, actorID uuid.UUID)
if err != nil {
return fmt.Errorf("%s - build private preview: %w", location, err)
}
result = PreviewResult{Published: true, Fields: source.PublishedFields(), Card: &card, GeneratedAt: generatedAt}
var facts []domain.EvidenceOrganizationFact
if len(card.Categories) > 0 {
if facts, err = unit.Evidence().ListPreviewFacts(ctx, actorID, card.Categories); err != nil {
return fmt.Errorf("%s - read preview evidence: %w", location, err)
}
}
result = PreviewResult{Published: true, Fields: source.PublishedFields(), Card: &card, Evidence: facts, GeneratedAt: generatedAt}
return nil
})
if err != nil {