Fix practice full load when question set has orphaned items.

Skip missing questions instead of failing the GET so admins can open and repair practices; map stray ErrNoRows to a clearer 404.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-06 12:59:36 +03:00
parent c2e7aea142
commit 78b60b8a52
2 changed files with 10 additions and 2 deletions

View File

@ -162,15 +162,20 @@ func (s *Service) loadQuestionsInSet(ctx context.Context, questionSetID int64) (
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
// Keep set membership aligned with questions that still exist. Orphaned
// question_set_items (deleted questions) must not fail the whole practice GET —
// admin editors need to open and repair those shells.
keptItems := make([]domain.QuestionSetItemWithQuestion, 0, len(items))
questions := make([]domain.QuestionWithDetails, 0, len(items)) questions := make([]domain.QuestionWithDetails, 0, len(items))
for _, item := range items { for _, item := range items {
q, ok := byID[item.QuestionID] q, ok := byID[item.QuestionID]
if !ok { if !ok {
return nil, nil, pgx.ErrNoRows continue
} }
keptItems = append(keptItems, item)
questions = append(questions, q) questions = append(questions, q)
} }
return items, questions, nil return keptItems, questions, nil
} }
func (s *Service) loadPracticeFullContent(ctx context.Context, questionSetID int64) (domain.QuestionSet, []domain.QuestionWithDetails, error) { func (s *Service) loadPracticeFullContent(ctx context.Context, questionSetID int64) (domain.QuestionSet, []domain.QuestionWithDetails, error) {

View File

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
) )
func fullPracticeQuestionResponses( func fullPracticeQuestionResponses(
@ -97,6 +98,8 @@ func mapPracticeFullError(err error, action string) (int, string) {
return fiber.StatusNotFound, "Question set not found" return fiber.StatusNotFound, "Question set not found"
case errors.Is(err, practicecontent.ErrQuestionNotFound): case errors.Is(err, practicecontent.ErrQuestionNotFound):
return fiber.StatusNotFound, "Question not found" return fiber.StatusNotFound, "Question not found"
case errors.Is(err, pgx.ErrNoRows):
return fiber.StatusNotFound, "Practice content not found"
case errors.Is(err, domain.ErrPersonaNotFound): case errors.Is(err, domain.ErrPersonaNotFound):
return fiber.StatusNotFound, "Persona not found" return fiber.StatusNotFound, "Persona not found"
case errors.Is(err, practicecontent.ErrInvalidQuestionItem): case errors.Is(err, practicecontent.ErrInvalidQuestionItem):