package handlers import ( "context" "Rezina-Backend/internal/domain" "github.com/gofiber/fiber/v2" ) func (h *Handler) enforceExamPrepPracticeSequenceForStudent(c *fiber.Ctx, questionSetID int64) error { role, _ := c.Locals("role").(domain.Role) // Exam-prep has no precedence locking; subscription/tier middleware is the access gate. if !role.UsesExamPrepSequentialGating() { 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.applyExamPrepAccessCatalogCourse(ctx, c, &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 } if err := h.lmsProgressSvc.ApplyExamPrepAccessCatalogCourse(ctx, role, userID, item); err != nil { return err } return h.applyCoursePurchaseAccess(ctx, c, "course", item.ID, item.Access) } 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.applyExamPrepAccessUnit(ctx, c, &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 } if err := h.lmsProgressSvc.ApplyExamPrepAccessUnit(ctx, role, userID, item); err != nil { return err } return h.applyCoursePurchaseAccess(ctx, c, "chapter", item.ID, item.Access) } 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.applyExamPrepAccessLesson(ctx, c, &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 } if err := h.lmsProgressSvc.ApplyExamPrepAccessLesson(ctx, role, userID, item); err != nil { return err } if err := h.applyCoursePurchaseAccess(ctx, c, "lesson", item.ID, item.Access); err != nil { return err } if item.Access != nil && !item.Access.IsAccessible { item.VideoURL = nil } return nil } func (h *Handler) applyCoursePurchaseAccess(ctx context.Context, c *fiber.Ctx, kind string, id int64, access *domain.LMSEntityAccess) error { role, _ := c.Locals("role").(domain.Role) if role.BypassesSubscription() || access == nil { return nil } resource, err := h.examPrepSvc.CourseContentAccess(ctx, kind, id) if err != nil { return err } if !resource.Published { access.IsAccessible = false access.Reason = "Content is not published" return nil } if resource.Tier == domain.ContentAccessFree { return nil } userID, _ := c.Locals("user_id").(int64) owned, err := h.examPrepSvc.HasCoursePurchase(ctx, userID, resource.CourseID) if err != nil { return err } if !owned { access.IsAccessible = false access.Reason = "Purchase this course to access premium content" } return nil }