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:
parent
c2e7aea142
commit
78b60b8a52
|
|
@ -162,15 +162,20 @@ func (s *Service) loadQuestionsInSet(ctx context.Context, questionSetID int64) (
|
|||
if err != nil {
|
||||
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))
|
||||
for _, item := range items {
|
||||
q, ok := byID[item.QuestionID]
|
||||
if !ok {
|
||||
return nil, nil, pgx.ErrNoRows
|
||||
continue
|
||||
}
|
||||
keptItems = append(keptItems, item)
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func fullPracticeQuestionResponses(
|
||||
|
|
@ -97,6 +98,8 @@ func mapPracticeFullError(err error, action string) (int, string) {
|
|||
return fiber.StatusNotFound, "Question set not found"
|
||||
case errors.Is(err, practicecontent.ErrQuestionNotFound):
|
||||
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):
|
||||
return fiber.StatusNotFound, "Persona not found"
|
||||
case errors.Is(err, practicecontent.ErrInvalidQuestionItem):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user