RezinaLMS-BackEnd/internal/web_server/handlers/exam_prep_progress_helper.go
Yared Yemane 36594e03d6 feat: enforce sequential access gating for exam prep content
Mirror LMS precedence locking for students across catalog courses, units, modules, lessons, and practices, including handler enforcement on practice consumption routes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-20 03:03:40 -07:00

162 lines
4.4 KiB
Go

package handlers
import (
"context"
"Yimaru-Backend/internal/domain"
"github.com/gofiber/fiber/v2"
)
func (h *Handler) enforceExamPrepPracticeSequenceForStudent(c *fiber.Ctx, questionSetID int64) error {
role, _ := c.Locals("role").(domain.Role)
if !role.UsesLMSSequentialGating() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
okAccess, reason, err := h.lmsProgressSvc.CanAccessExamPrepPracticeByQuestionSet(c.Context(), userID, questionSetID)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate practice access")
}
if !okAccess {
code := fiber.StatusForbidden
if reason == "Practice not found" {
code = fiber.StatusNotFound
}
return fiber.NewError(code, reason)
}
return nil
}
func practiceSequenceErrorResponse(c *fiber.Ctx, err error) error {
status := fiber.StatusForbidden
msg := "Practice is locked"
if ferr, ok := err.(*fiber.Error); ok {
status = ferr.Code
msg = ferr.Message
}
return c.Status(status).JSON(domain.ErrorResponse{
Message: msg,
Error: err.Error(),
})
}
func (h *Handler) applyExamPrepAccessCatalogCourses(ctx context.Context, c *fiber.Ctx, items []domain.ExamPrepCatalogCourse) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
for i := range items {
if err := h.lmsProgressSvc.ApplyExamPrepAccessCatalogCourse(ctx, role, userID, &items[i]); err != nil {
return err
}
}
return nil
}
func (h *Handler) applyExamPrepAccessCatalogCourse(ctx context.Context, c *fiber.Ctx, item *domain.ExamPrepCatalogCourse) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
return h.lmsProgressSvc.ApplyExamPrepAccessCatalogCourse(ctx, role, userID, item)
}
func (h *Handler) applyExamPrepAccessUnits(ctx context.Context, c *fiber.Ctx, items []domain.ExamPrepUnit) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
for i := range items {
if err := h.lmsProgressSvc.ApplyExamPrepAccessUnit(ctx, role, userID, &items[i]); err != nil {
return err
}
}
return nil
}
func (h *Handler) applyExamPrepAccessUnit(ctx context.Context, c *fiber.Ctx, item *domain.ExamPrepUnit) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
return h.lmsProgressSvc.ApplyExamPrepAccessUnit(ctx, role, userID, item)
}
func (h *Handler) applyExamPrepAccessModules(ctx context.Context, c *fiber.Ctx, items []domain.ExamPrepModule) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
for i := range items {
if err := h.lmsProgressSvc.ApplyExamPrepAccessModule(ctx, role, userID, &items[i]); err != nil {
return err
}
}
return nil
}
func (h *Handler) applyExamPrepAccessModule(ctx context.Context, c *fiber.Ctx, item *domain.ExamPrepModule) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
return h.lmsProgressSvc.ApplyExamPrepAccessModule(ctx, role, userID, item)
}
func (h *Handler) applyExamPrepAccessLessons(ctx context.Context, c *fiber.Ctx, items []domain.ExamPrepLesson) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
for i := range items {
if err := h.lmsProgressSvc.ApplyExamPrepAccessLesson(ctx, role, userID, &items[i]); err != nil {
return err
}
}
return nil
}
func (h *Handler) applyExamPrepAccessLesson(ctx context.Context, c *fiber.Ctx, item *domain.ExamPrepLesson) error {
role, _ := c.Locals("role").(domain.Role)
if !role.IsCustomerLearnerRole() {
return nil
}
userID, ok := c.Locals("user_id").(int64)
if !ok || userID == 0 {
return nil
}
return h.lmsProgressSvc.ApplyExamPrepAccessLesson(ctx, role, userID, item)
}