163 lines
4.5 KiB
Go
163 lines
4.5 KiB
Go
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.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)
|
|
}
|