package v1 import ( "context" "errors" "fmt" "github.com/google/uuid" "lincodex/backend/internal/adapter/primary/http/api" "lincodex/backend/internal/adapter/primary/http/problem" assessmentusecase "lincodex/backend/internal/usecase/assessment" ) // AssessmentResultOperations reads the candidate's immutable result history. // An employer has no operation here; the current category reaches an employer // only through the anonymous card. type AssessmentResultOperations interface { ListMyResults(ctx context.Context, userID uuid.UUID, cursor string, limit int32) (assessmentusecase.AssessmentResultHistoryPage, error) } func (handler *Handler) ListMyAssessmentResults( ctx context.Context, params api.ListMyAssessmentResultsParams, ) (api.ListMyAssessmentResultsRes, error) { if handler.assessment == nil { return handler.UnimplementedHandler.ListMyAssessmentResults(ctx, params) } return handler.assessment.ListMyAssessmentResults(ctx, params) } // ListMyAssessmentResults serves the owner-only history. The operations of // AssessmentHandler provide it as an optional capability, like GetMyResult. func (handler *AssessmentHandler) ListMyAssessmentResults( ctx context.Context, params api.ListMyAssessmentResultsParams, ) (api.ListMyAssessmentResultsRes, error) { const location = "AssessmentHandler.ListMyAssessmentResults" principal, ok := PrincipalFromContext(ctx) if !ok { return (*api.ListMyAssessmentResultsUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil } operations, ok := handler.operations.(AssessmentResultOperations) if !ok { return nil, errors.New(location + " - result history operation is unavailable") } // The generated validator has already bounded limit to the contract range. page, err := operations.ListMyResults(ctx, principal.UserID, params.Cursor.Or(""), params.Limit.Or(0)) if err != nil { return assessmentResultsFailure(ctx, err) } items := make([]api.AssessmentRealResult, 0, len(page.Entries)) for _, entry := range page.Entries { item, err := assessmentRealResultResponse(assessmentusecase.AssessmentAttemptResultFromHistoryEntry(entry)) if err != nil { return nil, fmt.Errorf("%s - map stored result: %w", location, err) } items = append(items, item) } next := api.NilString{} if page.NextCursor == "" { next.SetToNull() } else { next.SetTo(page.NextCursor) } return &api.AssessmentResultHistoryPageHeaders{ CacheControl: api.CacheControlNoStoreNoStore, Response: api.AssessmentResultHistoryPage{Items: items, Page: api.PageInfo{HasMore: page.NextCursor != "", NextCursor: next}}, }, nil } func assessmentResultsFailure(ctx context.Context, err error) (api.ListMyAssessmentResultsRes, error) { switch { case errors.Is(err, assessmentusecase.ErrInvalidAssessmentResultHistoryRequest): return (*api.ListMyAssessmentResultsBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationQuery, "/cursor").Response), nil case errors.Is(err, assessmentusecase.ErrAttemptForbidden), errors.Is(err, assessmentusecase.ErrAttemptEmailNotVerified): return (*api.ListMyAssessmentResultsForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil default: return nil, err } }