390 lines
12 KiB
Go
390 lines
12 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"context"
|
|
|
|
"Rezina-Backend/internal/domain"
|
|
coursessvc "Rezina-Backend/internal/services/courses"
|
|
lessonsvc "Rezina-Backend/internal/services/lessons"
|
|
modulesvc "Rezina-Backend/internal/services/modules"
|
|
practicessvc "Rezina-Backend/internal/services/practices"
|
|
programssvc "Rezina-Backend/internal/services/programs"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (a *App) RequireLMSSubscriptionUnlessFree() fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
// Reads return catalog metadata (including effective_access_tier) for client-side lock UI.
|
|
// Subscription is enforced on consumption routes (complete, questions, video heartbeat).
|
|
if isReadOnlyHTTPMethod(c.Method()) {
|
|
return c.Next()
|
|
}
|
|
role, userID, err := subscriptionScopedUser(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if bypassSubscriptionForRole(role) {
|
|
return c.Next()
|
|
}
|
|
// Only STUDENT is subject to LMS subscription enforcement.
|
|
if role != domain.RoleStudent {
|
|
return c.Next()
|
|
}
|
|
if domain.CategorySubscriptionGateDisabled {
|
|
return c.Next()
|
|
}
|
|
|
|
tier, resolved, err := a.resolveLMSEffectiveAccessTier(c)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, programssvc.ErrProgramNotFound),
|
|
errors.Is(err, coursessvc.ErrCourseNotFound),
|
|
errors.Is(err, modulesvc.ErrModuleNotFound),
|
|
errors.Is(err, lessonsvc.ErrLessonNotFound),
|
|
errors.Is(err, practicessvc.ErrPracticeNotFound):
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
default:
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to verify content access")
|
|
}
|
|
}
|
|
if !resolved || !tier.RequiresSubscription() {
|
|
return c.Next()
|
|
}
|
|
|
|
active, err := a.subscriptionsSvc.HasActiveSubscriptionByCategory(c.Context(), userID, domain.SubscriptionCategoryEducation)
|
|
if err != nil {
|
|
a.mongoLoggerSvc.Error("category subscription check failed",
|
|
zap.Int64("userID", userID),
|
|
zap.String("category", string(domain.SubscriptionCategoryEducation)),
|
|
zap.String("path", c.Path()),
|
|
zap.Error(err),
|
|
)
|
|
return fiber.NewError(fiber.StatusInternalServerError, "Failed to verify subscription")
|
|
}
|
|
if !active {
|
|
return fiber.NewError(fiber.StatusForbidden, fmt.Sprintf("An active %s subscription is required", humanizeSubscriptionCategory(domain.SubscriptionCategoryEducation)))
|
|
}
|
|
return c.Next()
|
|
}
|
|
}
|
|
|
|
func (a *App) resolveLMSEffectiveAccessTier(c *fiber.Ctx) (domain.ContentAccessTier, bool, error) {
|
|
ctx := c.Context()
|
|
routePath := c.Route().Path
|
|
|
|
if strings.Contains(routePath, "/practices/:practiceId/questions") || strings.Contains(routePath, "/practices/:id") {
|
|
practiceID, ok, err := parseRouteInt64(c, "practiceId")
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if !ok {
|
|
practiceID, ok, err = parseRouteInt64(c, "id")
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if !ok {
|
|
goto unresolved
|
|
}
|
|
}
|
|
return a.lmsEffectiveTierForPractice(ctx, practiceID)
|
|
}
|
|
|
|
if lessonID, ok, err := parseRouteInt64(c, "lessonId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.lmsEffectiveTierForLesson(ctx, lessonID)
|
|
}
|
|
|
|
if moduleID, ok, err := parseRouteInt64(c, "moduleId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.lmsEffectiveTierForModule(ctx, moduleID)
|
|
}
|
|
|
|
if _, ok, err := parseRouteInt64(c, "courseId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return "", false, nil
|
|
}
|
|
|
|
switch {
|
|
case strings.Contains(routePath, "/lessons/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.lmsEffectiveTierForLesson(ctx, id)
|
|
}
|
|
case strings.Contains(routePath, "/modules/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.lmsEffectiveTierForModule(ctx, id)
|
|
}
|
|
case strings.Contains(routePath, "/courses/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.lmsEffectiveTierForCourse(ctx, id)
|
|
}
|
|
case strings.Contains(routePath, "/programs/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.lmsEffectiveTierForProgram(ctx, id)
|
|
}
|
|
}
|
|
|
|
unresolved:
|
|
return "", false, nil
|
|
}
|
|
|
|
func (a *App) lmsEffectiveTierForProgram(ctx context.Context, programID int64) (domain.ContentAccessTier, bool, error) {
|
|
program, err := a.programSvc.GetByID(ctx, programID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return program.AccessTier, true, nil
|
|
}
|
|
|
|
func (a *App) lmsEffectiveTierForCourse(ctx context.Context, courseID int64) (domain.ContentAccessTier, bool, error) {
|
|
course, err := a.courseSvc.GetByID(ctx, courseID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
program, err := a.programSvc.GetByID(ctx, course.ProgramID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return domain.EffectiveContentAccessTier(program.AccessTier, course.AccessTier), true, nil
|
|
}
|
|
|
|
func (a *App) lmsEffectiveTierForModule(ctx context.Context, moduleID int64) (domain.ContentAccessTier, bool, error) {
|
|
module, err := a.moduleSvc.GetByID(ctx, moduleID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
course, err := a.courseSvc.GetByID(ctx, module.CourseID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
program, err := a.programSvc.GetByID(ctx, course.ProgramID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return domain.EffectiveContentAccessTier(program.AccessTier, course.AccessTier, module.AccessTier), true, nil
|
|
}
|
|
|
|
func (a *App) lmsEffectiveTierForLesson(ctx context.Context, lessonID int64) (domain.ContentAccessTier, bool, error) {
|
|
lesson, err := a.lessonSvc.GetByID(ctx, lessonID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
module, err := a.moduleSvc.GetByID(ctx, lesson.ModuleID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
course, err := a.courseSvc.GetByID(ctx, module.CourseID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
program, err := a.programSvc.GetByID(ctx, course.ProgramID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return domain.EffectiveContentAccessTier(program.AccessTier, course.AccessTier, module.AccessTier, lesson.AccessTier), true, nil
|
|
}
|
|
|
|
func (a *App) resolveExamPrepEffectiveAccessTier(c *fiber.Ctx) (domain.ContentAccessTier, bool, error) {
|
|
ctx := c.Context()
|
|
routePath := c.Route().Path
|
|
|
|
if strings.Contains(routePath, "/videos/engagement/heartbeat") {
|
|
var input domain.VideoEngagementHeartbeatInput
|
|
if err := c.BodyParser(&input); err != nil || input.ContentID <= 0 {
|
|
return "", false, fiber.NewError(fiber.StatusBadRequest, "Invalid video content")
|
|
}
|
|
return a.examPrepEffectiveTierForLesson(ctx, input.ContentID)
|
|
}
|
|
|
|
if id, ok, err := parseRouteInt64(c, "courseId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForCatalogCourse(ctx, id)
|
|
}
|
|
if id, ok, err := parseRouteInt64(c, "chapterId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForUnit(ctx, id)
|
|
}
|
|
if id, ok, err := parseRouteInt64(c, "moduleId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForModule(ctx, id)
|
|
}
|
|
if id, ok, err := parseRouteInt64(c, "lessonId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForLesson(ctx, id)
|
|
}
|
|
|
|
if id, ok, err := parseRouteInt64(c, "practiceId"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForPractice(ctx, id)
|
|
}
|
|
|
|
switch {
|
|
case strings.Contains(routePath, "/courses/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForCatalogCourse(ctx, id)
|
|
}
|
|
case strings.Contains(routePath, "/chapters/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForUnit(ctx, id)
|
|
}
|
|
case strings.Contains(routePath, "/modules/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForModule(ctx, id)
|
|
}
|
|
case strings.Contains(routePath, "/lessons/:id"):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForLesson(ctx, id)
|
|
}
|
|
case (strings.Contains(routePath, "/practices/:id") || strings.Contains(routePath, "/quizzes/:id")):
|
|
if id, ok, err := parseRouteInt64(c, "id"); err != nil {
|
|
return "", false, err
|
|
} else if ok {
|
|
return a.examPrepEffectiveTierForPractice(ctx, id)
|
|
}
|
|
}
|
|
return "", false, nil
|
|
}
|
|
|
|
func (a *App) examPrepEffectiveTierForCatalogCourse(ctx context.Context, catalogCourseID int64) (domain.ContentAccessTier, bool, error) {
|
|
cc, err := a.examPrepSvc.GetCatalogCourseByID(ctx, catalogCourseID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return cc.AccessTier, true, nil
|
|
}
|
|
|
|
func (a *App) examPrepEffectiveTierForUnit(ctx context.Context, unitID int64) (domain.ContentAccessTier, bool, error) {
|
|
unit, err := a.examPrepSvc.GetUnitByID(ctx, unitID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
cc, err := a.examPrepSvc.GetCatalogCourseByID(ctx, unit.CatalogCourseID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return domain.EffectiveContentAccessTier(cc.AccessTier, unit.AccessTier), true, nil
|
|
}
|
|
|
|
func (a *App) examPrepEffectiveTierForModule(ctx context.Context, moduleID int64) (domain.ContentAccessTier, bool, error) {
|
|
module, err := a.examPrepSvc.GetModuleByID(ctx, moduleID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
unit, err := a.examPrepSvc.GetUnitByID(ctx, module.UnitID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
cc, err := a.examPrepSvc.GetCatalogCourseByID(ctx, unit.CatalogCourseID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return domain.EffectiveContentAccessTier(cc.AccessTier, unit.AccessTier, module.AccessTier), true, nil
|
|
}
|
|
|
|
func (a *App) examPrepEffectiveTierForLesson(ctx context.Context, lessonID int64) (domain.ContentAccessTier, bool, error) {
|
|
lesson, err := a.examPrepSvc.GetLessonByID(ctx, lessonID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
module, err := a.examPrepSvc.GetModuleByID(ctx, lesson.UnitModuleID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
unit, err := a.examPrepSvc.GetUnitByID(ctx, module.UnitID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
cc, err := a.examPrepSvc.GetCatalogCourseByID(ctx, unit.CatalogCourseID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return domain.EffectiveContentAccessTier(cc.AccessTier, unit.AccessTier, module.AccessTier, lesson.AccessTier), true, nil
|
|
}
|
|
|
|
func (a *App) examPrepEffectiveTierForPractice(ctx context.Context, practiceID int64) (domain.ContentAccessTier, bool, error) {
|
|
practice, err := a.examPrepSvc.GetExamPrepPracticeByID(ctx, practiceID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
parent, ok := domain.ExamPrepPracticePrimaryParent(practice)
|
|
if !ok {
|
|
return "", false, nil
|
|
}
|
|
switch parent.Kind {
|
|
case domain.ExamPrepParentKindCatalogCourse:
|
|
return a.examPrepEffectiveTierForCatalogCourse(ctx, parent.ID)
|
|
case domain.ExamPrepParentKindUnit:
|
|
return a.examPrepEffectiveTierForUnit(ctx, parent.ID)
|
|
case domain.ExamPrepParentKindLesson:
|
|
return a.examPrepEffectiveTierForLesson(ctx, parent.ID)
|
|
default:
|
|
return "", false, nil
|
|
}
|
|
}
|
|
|
|
func (a *App) lmsEffectiveTierForPractice(ctx context.Context, practiceID int64) (domain.ContentAccessTier, bool, error) {
|
|
practice, err := a.practiceSvc.GetByID(ctx, practiceID)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if len(domain.PracticeParentsDeref(practice.Parents)) == 0 {
|
|
return "", false, nil
|
|
}
|
|
okAny := false
|
|
var tier domain.ContentAccessTier
|
|
for _, parent := range domain.PracticeParentsDeref(practice.Parents) {
|
|
var (
|
|
parentTier domain.ContentAccessTier
|
|
ok bool
|
|
err error
|
|
)
|
|
switch parent.Kind {
|
|
case domain.ParentKindLesson:
|
|
parentTier, ok, err = a.lmsEffectiveTierForLesson(ctx, parent.ID)
|
|
case domain.ParentKindModule:
|
|
parentTier, ok, err = a.lmsEffectiveTierForModule(ctx, parent.ID)
|
|
case domain.ParentKindCourse:
|
|
parentTier, ok, err = a.lmsEffectiveTierForCourse(ctx, parent.ID)
|
|
default:
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
if !ok {
|
|
continue
|
|
}
|
|
okAny = true
|
|
tier = domain.EffectiveContentAccessTier(tier, parentTier)
|
|
}
|
|
return tier, okAny, nil
|
|
}
|