Fix practice completion lookup for progress endpoint.

Accept either question-set IDs or LMS practice IDs and recognize LMS owner types so valid practice completions no longer return practice-not-found responses.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-05-12 02:57:05 -07:00
parent eae87b40b5
commit c711df68b9

View File

@ -731,7 +731,7 @@ func isSequenceGatedPractice(set domain.QuestionSet) bool {
return false return false
} }
ot := strings.ToUpper(strings.TrimSpace(*set.OwnerType)) ot := strings.ToUpper(strings.TrimSpace(*set.OwnerType))
return ot == "SUB_COURSE" || ot == "SUB_MODULE" return ot == "SUB_COURSE" || ot == "SUB_MODULE" || ot == "COURSE" || ot == "MODULE" || ot == "LESSON"
} }
func (h *Handler) enforcePracticeSequenceForStudent(c *fiber.Ctx, set domain.QuestionSet) error { func (h *Handler) enforcePracticeSequenceForStudent(c *fiber.Ctx, set domain.QuestionSet) error {
@ -1539,7 +1539,7 @@ func (h *Handler) CompletePractice(c *fiber.Ctx) error {
} }
userID := c.Locals("user_id").(int64) userID := c.Locals("user_id").(int64)
setID, err := strconv.ParseInt(c.Params("id"), 10, 64) id, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil { if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid practice ID", Message: "Invalid practice ID",
@ -1547,13 +1547,24 @@ func (h *Handler) CompletePractice(c *fiber.Ctx) error {
}) })
} }
set, err := h.questionsSvc.GetQuestionSetByID(c.Context(), setID) set, err := h.questionsSvc.GetQuestionSetByID(c.Context(), id)
if err != nil {
// Backward/UX compatibility: accept either question_set.id or lms_practices.id.
practice, practiceErr := h.practiceSvc.GetByID(c.Context(), id)
if practiceErr != nil {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Practice not found",
Error: err.Error(),
})
}
set, err = h.questionsSvc.GetQuestionSetByID(c.Context(), practice.QuestionSetID)
if err != nil { if err != 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: err.Error(),
}) })
} }
}
if !isSequenceGatedPractice(set) || !strings.EqualFold(set.Status, "PUBLISHED") { if !isSequenceGatedPractice(set) || !strings.EqualFold(set.Status, "PUBLISHED") {
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
Message: "Practice not found", Message: "Practice not found",