Fix practice completion lookup for progress endpoint.

Prioritize resolving lms_practices.id before falling back to question_set.id to avoid false 404 responses caused by cross-table ID collisions.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-05-13 03:36:39 -07:00
parent c711df68b9
commit 86ab4e53d4

View File

@ -1547,29 +1547,25 @@ func (h *Handler) CompletePractice(c *fiber.Ctx) error {
}) })
} }
set, err := h.questionsSvc.GetQuestionSetByID(c.Context(), id) // Prefer LMS practice ID resolution to avoid accidental collisions with question_set IDs.
if err != nil { practice, practiceErr := h.practiceSvc.GetByID(c.Context(), id)
// Backward/UX compatibility: accept either question_set.id or lms_practices.id. var set domain.QuestionSet
practice, practiceErr := h.practiceSvc.GetByID(c.Context(), id) var setErr error
if practiceErr != nil { if practiceErr == nil {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ set, setErr = h.questionsSvc.GetQuestionSetByID(c.Context(), practice.QuestionSetID)
Message: "Practice not found", } else {
Error: err.Error(), // Backward compatibility: also accept question_set.id directly.
}) set, setErr = h.questionsSvc.GetQuestionSetByID(c.Context(), id)
}
set, err = h.questionsSvc.GetQuestionSetByID(c.Context(), practice.QuestionSetID)
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Practice not found",
Error: err.Error(),
})
}
} }
if !isSequenceGatedPractice(set) || !strings.EqualFold(set.Status, "PUBLISHED") { if setErr != nil {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Practice not found", Message: "Practice not found",
Error: setErr.Error(),
}) })
} }
if !strings.EqualFold(set.SetType, string(domain.QuestionSetTypePractice)) || !strings.EqualFold(set.Status, "PUBLISHED") {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found"})
}
if err := h.enforcePracticeSequenceForStudent(c, set); err != nil { if err := h.enforcePracticeSequenceForStudent(c, set); err != nil {
return c.Status(fiber.StatusForbidden).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusForbidden).JSON(domain.ErrorResponse{