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,28 +1547,24 @@ 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 {
// Backward/UX compatibility: accept either question_set.id or lms_practices.id.
practice, practiceErr := h.practiceSvc.GetByID(c.Context(), id) practice, practiceErr := h.practiceSvc.GetByID(c.Context(), id)
if practiceErr != nil { var set domain.QuestionSet
var setErr error
if practiceErr == nil {
set, setErr = h.questionsSvc.GetQuestionSetByID(c.Context(), practice.QuestionSetID)
} else {
// Backward compatibility: also accept question_set.id directly.
set, setErr = h.questionsSvc.GetQuestionSetByID(c.Context(), id)
}
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: err.Error(), Error: setErr.Error(),
}) })
} }
set, err = h.questionsSvc.GetQuestionSetByID(c.Context(), practice.QuestionSetID) if !strings.EqualFold(set.SetType, string(domain.QuestionSetTypePractice)) || !strings.EqualFold(set.Status, "PUBLISHED") {
if err != nil { return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found"})
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Practice not found",
Error: err.Error(),
})
}
}
if !isSequenceGatedPractice(set) || !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 {