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>
This commit is contained in:
parent
c742cb01c3
commit
36594e03d6
|
|
@ -129,3 +129,181 @@ WHERE
|
|||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND p.publish_status = 'PUBLISHED';
|
||||
|
||||
-- name: GetPreviousExamPrepCatalogCourse :one
|
||||
-- Immediate predecessor by sort_order within the same category (gaps in sort_order are allowed).
|
||||
SELECT
|
||||
cc2.id,
|
||||
cc2.name,
|
||||
cc2.description,
|
||||
cc2.thumbnail,
|
||||
cc2.sort_order,
|
||||
cc2.category,
|
||||
cc2.publish_status,
|
||||
cc2.access_tier,
|
||||
cc2.created_at,
|
||||
cc2.updated_at
|
||||
FROM
|
||||
exam_prep.catalog_courses AS cc1
|
||||
INNER JOIN exam_prep.catalog_courses AS cc2 ON cc2.category = cc1.category
|
||||
AND cc2.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
cc2.sort_order < cc1.sort_order
|
||||
OR (
|
||||
cc2.sort_order = cc1.sort_order
|
||||
AND cc2.id < cc1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
cc1.id = $1
|
||||
ORDER BY
|
||||
cc2.sort_order DESC,
|
||||
cc2.id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetPreviousExamPrepUnitInCatalogCourse :one
|
||||
SELECT
|
||||
u2.id,
|
||||
u2.catalog_course_id,
|
||||
u2.name,
|
||||
u2.description,
|
||||
u2.thumbnail,
|
||||
u2.sort_order,
|
||||
u2.publish_status,
|
||||
u2.access_tier,
|
||||
u2.created_at,
|
||||
u2.updated_at
|
||||
FROM
|
||||
exam_prep.units AS u1
|
||||
INNER JOIN exam_prep.units AS u2 ON u2.catalog_course_id = u1.catalog_course_id
|
||||
AND u2.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
u2.sort_order < u1.sort_order
|
||||
OR (
|
||||
u2.sort_order = u1.sort_order
|
||||
AND u2.id < u1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
u1.id = $1
|
||||
ORDER BY
|
||||
u2.sort_order DESC,
|
||||
u2.id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetPreviousExamPrepModuleInUnit :one
|
||||
SELECT
|
||||
m2.id,
|
||||
m2.unit_id,
|
||||
m2.name,
|
||||
m2.description,
|
||||
m2.thumbnail,
|
||||
m2.icon,
|
||||
m2.sort_order,
|
||||
m2.publish_status,
|
||||
m2.access_tier,
|
||||
m2.created_at,
|
||||
m2.updated_at
|
||||
FROM
|
||||
exam_prep.unit_modules AS m1
|
||||
INNER JOIN exam_prep.unit_modules AS m2 ON m2.unit_id = m1.unit_id
|
||||
AND m2.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
m2.sort_order < m1.sort_order
|
||||
OR (
|
||||
m2.sort_order = m1.sort_order
|
||||
AND m2.id < m1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
m1.id = $1
|
||||
ORDER BY
|
||||
m2.sort_order DESC,
|
||||
m2.id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetPreviousExamPrepLessonInModule :one
|
||||
SELECT
|
||||
l2.id,
|
||||
l2.unit_module_id,
|
||||
l2.title,
|
||||
l2.video_url,
|
||||
l2.thumbnail,
|
||||
l2.description,
|
||||
l2.sort_order,
|
||||
l2.publish_status,
|
||||
l2.access_tier,
|
||||
l2.created_at,
|
||||
l2.updated_at
|
||||
FROM
|
||||
exam_prep.unit_module_lessons AS l1
|
||||
INNER JOIN exam_prep.unit_module_lessons AS l2 ON l2.unit_module_id = l1.unit_module_id
|
||||
AND l2.publish_status = 'PUBLISHED'
|
||||
AND l1.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
l2.sort_order < l1.sort_order
|
||||
OR (
|
||||
l2.sort_order = l1.sort_order
|
||||
AND l2.id < l1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
l1.id = $1
|
||||
ORDER BY
|
||||
l2.sort_order DESC,
|
||||
l2.id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetFirstIncompletePreviousExamPrepPractice :one
|
||||
WITH target AS (
|
||||
SELECT
|
||||
lp.id,
|
||||
lp.unit_module_lesson_id,
|
||||
lp.created_at,
|
||||
lp.question_set_id
|
||||
FROM
|
||||
exam_prep.lesson_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.question_set_id = @question_set_id::BIGINT
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
),
|
||||
candidates AS (
|
||||
SELECT
|
||||
lp.id,
|
||||
lp.title,
|
||||
lp.question_set_id,
|
||||
lp.created_at
|
||||
FROM
|
||||
exam_prep.lesson_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN target t ON lp.unit_module_lesson_id = t.unit_module_lesson_id
|
||||
WHERE
|
||||
lp.publish_status = 'PUBLISHED'
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND (
|
||||
lp.created_at < t.created_at
|
||||
OR (
|
||||
lp.created_at = t.created_at
|
||||
AND lp.id < t.id
|
||||
)
|
||||
)
|
||||
)
|
||||
SELECT
|
||||
c.id,
|
||||
c.title,
|
||||
c.question_set_id
|
||||
FROM
|
||||
candidates c
|
||||
LEFT JOIN user_practice_progress upp ON upp.question_set_id = c.question_set_id
|
||||
AND upp.user_id = @user_id::BIGINT
|
||||
AND upp.completed_at IS NOT NULL
|
||||
WHERE
|
||||
upp.id IS NULL
|
||||
ORDER BY
|
||||
c.created_at ASC,
|
||||
c.id ASC
|
||||
LIMIT 1;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ package dbgen
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const CountPublishedExamPrepPracticesInCatalogCourse = `-- name: CountPublishedExamPrepPracticesInCatalogCourse :one
|
||||
|
|
@ -224,3 +226,332 @@ func (q *Queries) CountUserCompletedPublishedExamPrepPracticesInUnit(ctx context
|
|||
err := row.Scan(&n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
const GetFirstIncompletePreviousExamPrepPractice = `-- name: GetFirstIncompletePreviousExamPrepPractice :one
|
||||
WITH target AS (
|
||||
SELECT
|
||||
lp.id,
|
||||
lp.unit_module_lesson_id,
|
||||
lp.created_at,
|
||||
lp.question_set_id
|
||||
FROM
|
||||
exam_prep.lesson_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
WHERE
|
||||
lp.question_set_id = $2::BIGINT
|
||||
AND lp.publish_status = 'PUBLISHED'
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
),
|
||||
candidates AS (
|
||||
SELECT
|
||||
lp.id,
|
||||
lp.title,
|
||||
lp.question_set_id,
|
||||
lp.created_at
|
||||
FROM
|
||||
exam_prep.lesson_practices lp
|
||||
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
||||
INNER JOIN target t ON lp.unit_module_lesson_id = t.unit_module_lesson_id
|
||||
WHERE
|
||||
lp.publish_status = 'PUBLISHED'
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND qs.status = 'PUBLISHED'
|
||||
AND (
|
||||
lp.created_at < t.created_at
|
||||
OR (
|
||||
lp.created_at = t.created_at
|
||||
AND lp.id < t.id
|
||||
)
|
||||
)
|
||||
)
|
||||
SELECT
|
||||
c.id,
|
||||
c.title,
|
||||
c.question_set_id
|
||||
FROM
|
||||
candidates c
|
||||
LEFT JOIN user_practice_progress upp ON upp.question_set_id = c.question_set_id
|
||||
AND upp.user_id = $1::BIGINT
|
||||
AND upp.completed_at IS NOT NULL
|
||||
WHERE
|
||||
upp.id IS NULL
|
||||
ORDER BY
|
||||
c.created_at ASC,
|
||||
c.id ASC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetFirstIncompletePreviousExamPrepPracticeParams struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
QuestionSetID int64 `json:"question_set_id"`
|
||||
}
|
||||
|
||||
type GetFirstIncompletePreviousExamPrepPracticeRow struct {
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
QuestionSetID int64 `json:"question_set_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetFirstIncompletePreviousExamPrepPractice(ctx context.Context, arg GetFirstIncompletePreviousExamPrepPracticeParams) (GetFirstIncompletePreviousExamPrepPracticeRow, error) {
|
||||
row := q.db.QueryRow(ctx, GetFirstIncompletePreviousExamPrepPractice, arg.UserID, arg.QuestionSetID)
|
||||
var i GetFirstIncompletePreviousExamPrepPracticeRow
|
||||
err := row.Scan(&i.ID, &i.Title, &i.QuestionSetID)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const GetPreviousExamPrepCatalogCourse = `-- name: GetPreviousExamPrepCatalogCourse :one
|
||||
SELECT
|
||||
cc2.id,
|
||||
cc2.name,
|
||||
cc2.description,
|
||||
cc2.thumbnail,
|
||||
cc2.sort_order,
|
||||
cc2.category,
|
||||
cc2.publish_status,
|
||||
cc2.access_tier,
|
||||
cc2.created_at,
|
||||
cc2.updated_at
|
||||
FROM
|
||||
exam_prep.catalog_courses AS cc1
|
||||
INNER JOIN exam_prep.catalog_courses AS cc2 ON cc2.category = cc1.category
|
||||
AND cc2.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
cc2.sort_order < cc1.sort_order
|
||||
OR (
|
||||
cc2.sort_order = cc1.sort_order
|
||||
AND cc2.id < cc1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
cc1.id = $1
|
||||
ORDER BY
|
||||
cc2.sort_order DESC,
|
||||
cc2.id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetPreviousExamPrepCatalogCourseRow struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description pgtype.Text `json:"description"`
|
||||
Thumbnail pgtype.Text `json:"thumbnail"`
|
||||
SortOrder int32 `json:"sort_order"`
|
||||
Category string `json:"category"`
|
||||
PublishStatus string `json:"publish_status"`
|
||||
AccessTier string `json:"access_tier"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
// Immediate predecessor by sort_order within the same category (gaps in sort_order are allowed).
|
||||
func (q *Queries) GetPreviousExamPrepCatalogCourse(ctx context.Context, id int64) (GetPreviousExamPrepCatalogCourseRow, error) {
|
||||
row := q.db.QueryRow(ctx, GetPreviousExamPrepCatalogCourse, id)
|
||||
var i GetPreviousExamPrepCatalogCourseRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Thumbnail,
|
||||
&i.SortOrder,
|
||||
&i.Category,
|
||||
&i.PublishStatus,
|
||||
&i.AccessTier,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const GetPreviousExamPrepLessonInModule = `-- name: GetPreviousExamPrepLessonInModule :one
|
||||
SELECT
|
||||
l2.id,
|
||||
l2.unit_module_id,
|
||||
l2.title,
|
||||
l2.video_url,
|
||||
l2.thumbnail,
|
||||
l2.description,
|
||||
l2.sort_order,
|
||||
l2.publish_status,
|
||||
l2.access_tier,
|
||||
l2.created_at,
|
||||
l2.updated_at
|
||||
FROM
|
||||
exam_prep.unit_module_lessons AS l1
|
||||
INNER JOIN exam_prep.unit_module_lessons AS l2 ON l2.unit_module_id = l1.unit_module_id
|
||||
AND l2.publish_status = 'PUBLISHED'
|
||||
AND l1.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
l2.sort_order < l1.sort_order
|
||||
OR (
|
||||
l2.sort_order = l1.sort_order
|
||||
AND l2.id < l1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
l1.id = $1
|
||||
ORDER BY
|
||||
l2.sort_order DESC,
|
||||
l2.id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetPreviousExamPrepLessonInModuleRow struct {
|
||||
ID int64 `json:"id"`
|
||||
UnitModuleID int64 `json:"unit_module_id"`
|
||||
Title string `json:"title"`
|
||||
VideoUrl pgtype.Text `json:"video_url"`
|
||||
Thumbnail pgtype.Text `json:"thumbnail"`
|
||||
Description pgtype.Text `json:"description"`
|
||||
SortOrder int32 `json:"sort_order"`
|
||||
PublishStatus string `json:"publish_status"`
|
||||
AccessTier string `json:"access_tier"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetPreviousExamPrepLessonInModule(ctx context.Context, id int64) (GetPreviousExamPrepLessonInModuleRow, error) {
|
||||
row := q.db.QueryRow(ctx, GetPreviousExamPrepLessonInModule, id)
|
||||
var i GetPreviousExamPrepLessonInModuleRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.UnitModuleID,
|
||||
&i.Title,
|
||||
&i.VideoUrl,
|
||||
&i.Thumbnail,
|
||||
&i.Description,
|
||||
&i.SortOrder,
|
||||
&i.PublishStatus,
|
||||
&i.AccessTier,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const GetPreviousExamPrepModuleInUnit = `-- name: GetPreviousExamPrepModuleInUnit :one
|
||||
SELECT
|
||||
m2.id,
|
||||
m2.unit_id,
|
||||
m2.name,
|
||||
m2.description,
|
||||
m2.thumbnail,
|
||||
m2.icon,
|
||||
m2.sort_order,
|
||||
m2.publish_status,
|
||||
m2.access_tier,
|
||||
m2.created_at,
|
||||
m2.updated_at
|
||||
FROM
|
||||
exam_prep.unit_modules AS m1
|
||||
INNER JOIN exam_prep.unit_modules AS m2 ON m2.unit_id = m1.unit_id
|
||||
AND m2.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
m2.sort_order < m1.sort_order
|
||||
OR (
|
||||
m2.sort_order = m1.sort_order
|
||||
AND m2.id < m1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
m1.id = $1
|
||||
ORDER BY
|
||||
m2.sort_order DESC,
|
||||
m2.id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetPreviousExamPrepModuleInUnitRow struct {
|
||||
ID int64 `json:"id"`
|
||||
UnitID int64 `json:"unit_id"`
|
||||
Name string `json:"name"`
|
||||
Description pgtype.Text `json:"description"`
|
||||
Thumbnail pgtype.Text `json:"thumbnail"`
|
||||
Icon pgtype.Text `json:"icon"`
|
||||
SortOrder int32 `json:"sort_order"`
|
||||
PublishStatus string `json:"publish_status"`
|
||||
AccessTier string `json:"access_tier"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetPreviousExamPrepModuleInUnit(ctx context.Context, id int64) (GetPreviousExamPrepModuleInUnitRow, error) {
|
||||
row := q.db.QueryRow(ctx, GetPreviousExamPrepModuleInUnit, id)
|
||||
var i GetPreviousExamPrepModuleInUnitRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.UnitID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Thumbnail,
|
||||
&i.Icon,
|
||||
&i.SortOrder,
|
||||
&i.PublishStatus,
|
||||
&i.AccessTier,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const GetPreviousExamPrepUnitInCatalogCourse = `-- name: GetPreviousExamPrepUnitInCatalogCourse :one
|
||||
SELECT
|
||||
u2.id,
|
||||
u2.catalog_course_id,
|
||||
u2.name,
|
||||
u2.description,
|
||||
u2.thumbnail,
|
||||
u2.sort_order,
|
||||
u2.publish_status,
|
||||
u2.access_tier,
|
||||
u2.created_at,
|
||||
u2.updated_at
|
||||
FROM
|
||||
exam_prep.units AS u1
|
||||
INNER JOIN exam_prep.units AS u2 ON u2.catalog_course_id = u1.catalog_course_id
|
||||
AND u2.publish_status = 'PUBLISHED'
|
||||
AND (
|
||||
u2.sort_order < u1.sort_order
|
||||
OR (
|
||||
u2.sort_order = u1.sort_order
|
||||
AND u2.id < u1.id
|
||||
)
|
||||
)
|
||||
WHERE
|
||||
u1.id = $1
|
||||
ORDER BY
|
||||
u2.sort_order DESC,
|
||||
u2.id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetPreviousExamPrepUnitInCatalogCourseRow struct {
|
||||
ID int64 `json:"id"`
|
||||
CatalogCourseID int64 `json:"catalog_course_id"`
|
||||
Name string `json:"name"`
|
||||
Description pgtype.Text `json:"description"`
|
||||
Thumbnail pgtype.Text `json:"thumbnail"`
|
||||
SortOrder int32 `json:"sort_order"`
|
||||
PublishStatus string `json:"publish_status"`
|
||||
AccessTier string `json:"access_tier"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetPreviousExamPrepUnitInCatalogCourse(ctx context.Context, id int64) (GetPreviousExamPrepUnitInCatalogCourseRow, error) {
|
||||
row := q.db.QueryRow(ctx, GetPreviousExamPrepUnitInCatalogCourse, id)
|
||||
var i GetPreviousExamPrepUnitInCatalogCourseRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CatalogCourseID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Thumbnail,
|
||||
&i.SortOrder,
|
||||
&i.PublishStatus,
|
||||
&i.AccessTier,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -426,6 +426,22 @@ type QuestionTypeDefinition struct {
|
|||
ResponseSchema []byte `json:"response_schema"`
|
||||
}
|
||||
|
||||
type QuestionTypeDefinitionGroup struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description pgtype.Text `json:"description"`
|
||||
DisplayOrder int32 `json:"display_order"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
||||
}
|
||||
|
||||
type QuestionTypeDefinitionGroupMember struct {
|
||||
DefinitionID int64 `json:"definition_id"`
|
||||
GroupID int64 `json:"group_id"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type Rating struct {
|
||||
ID int64 `json:"id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
|
|
|
|||
59
internal/repository/exam_prep_access.go
Normal file
59
internal/repository/exam_prep_access.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
dbgen "Yimaru-Backend/gen/db"
|
||||
"Yimaru-Backend/internal/domain"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *Store) ExamPrepGetPreviousCatalogCourse(ctx context.Context, catalogCourseID int64) (int64, error) {
|
||||
row, err := s.queries.GetPreviousExamPrepCatalogCourse(ctx, catalogCourseID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return row.ID, nil
|
||||
}
|
||||
|
||||
func (s *Store) ExamPrepGetPreviousUnitInCatalogCourse(ctx context.Context, unitID int64) (int64, error) {
|
||||
row, err := s.queries.GetPreviousExamPrepUnitInCatalogCourse(ctx, unitID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return row.ID, nil
|
||||
}
|
||||
|
||||
func (s *Store) ExamPrepGetPreviousModuleInUnit(ctx context.Context, moduleID int64) (int64, error) {
|
||||
row, err := s.queries.GetPreviousExamPrepModuleInUnit(ctx, moduleID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return row.ID, nil
|
||||
}
|
||||
|
||||
func (s *Store) ExamPrepGetPreviousLessonInModule(ctx context.Context, lessonID int64) (int64, error) {
|
||||
row, err := s.queries.GetPreviousExamPrepLessonInModule(ctx, lessonID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return row.ID, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetFirstIncompletePreviousExamPrepPractice(ctx context.Context, userID, questionSetID int64) (*domain.PracticeAccessBlock, error) {
|
||||
row, err := s.queries.GetFirstIncompletePreviousExamPrepPractice(ctx, dbgen.GetFirstIncompletePreviousExamPrepPracticeParams{
|
||||
UserID: userID,
|
||||
QuestionSetID: questionSetID,
|
||||
})
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &domain.PracticeAccessBlock{
|
||||
QuestionSetID: row.QuestionSetID,
|
||||
Title: row.Title,
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -16,6 +16,12 @@ const (
|
|||
errPrevCourse = "Complete the previous course in this program first."
|
||||
errPrevModule = "Complete the previous module in this course first."
|
||||
errPrevLesson = "Complete the previous lesson in this module first."
|
||||
|
||||
errPrevExamPrepCatalogCourse = "Complete the previous catalog course before accessing this one."
|
||||
errPrevExamPrepUnit = "Complete the previous unit in this catalog course first."
|
||||
errPrevExamPrepModule = "Complete the previous module in this unit first."
|
||||
errPrevExamPrepLesson = "Complete the previous lesson in this module first."
|
||||
errPrevExamPrepPractice = "Complete the previous practice in this lesson first."
|
||||
)
|
||||
|
||||
// Service enforces sequential LMS access for learners and records lesson progress.
|
||||
|
|
@ -148,6 +154,178 @@ func (s *Service) CanAccessLesson(ctx context.Context, userID, lessonID int64) (
|
|||
return true, "", nil
|
||||
}
|
||||
|
||||
// CanAccessExamPrepCatalogCourse returns whether the user may use content under this catalog course.
|
||||
func (s *Service) CanAccessExamPrepCatalogCourse(ctx context.Context, userID, catalogCourseID int64) (ok bool, reason string, err error) {
|
||||
if _, err := s.store.GetExamPrepCatalogCourseByID(ctx, catalogCourseID); err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
prevID, err := s.store.ExamPrepGetPreviousCatalogCourse(ctx, catalogCourseID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return true, "", nil
|
||||
}
|
||||
return false, "", err
|
||||
}
|
||||
done, err := s.examPrepCatalogCourseIsComplete(ctx, userID, prevID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if !done {
|
||||
return false, errPrevExamPrepCatalogCourse, nil
|
||||
}
|
||||
return true, "", nil
|
||||
}
|
||||
|
||||
// CanAccessExamPrepUnit requires the parent catalog course chain and the previous unit to be completed.
|
||||
func (s *Service) CanAccessExamPrepUnit(ctx context.Context, userID, unitID int64) (ok bool, reason string, err error) {
|
||||
u, err := s.store.GetExamPrepUnitByID(ctx, unitID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
ok, reason, err = s.CanAccessExamPrepCatalogCourse(ctx, userID, u.CatalogCourseID)
|
||||
if err != nil || !ok {
|
||||
return ok, reason, err
|
||||
}
|
||||
prevID, err := s.store.ExamPrepGetPreviousUnitInCatalogCourse(ctx, unitID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return true, "", nil
|
||||
}
|
||||
return false, "", err
|
||||
}
|
||||
done, err := s.examPrepUnitIsComplete(ctx, userID, prevID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if !done {
|
||||
return false, errPrevExamPrepUnit, nil
|
||||
}
|
||||
return true, "", nil
|
||||
}
|
||||
|
||||
// CanAccessExamPrepModule requires the unit chain and the previous module in the unit to be completed.
|
||||
func (s *Service) CanAccessExamPrepModule(ctx context.Context, userID, moduleID int64) (ok bool, reason string, err error) {
|
||||
m, err := s.store.GetExamPrepUnitModuleByID(ctx, moduleID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
ok, reason, err = s.CanAccessExamPrepUnit(ctx, userID, m.UnitID)
|
||||
if err != nil || !ok {
|
||||
return ok, reason, err
|
||||
}
|
||||
prevID, err := s.store.ExamPrepGetPreviousModuleInUnit(ctx, moduleID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return true, "", nil
|
||||
}
|
||||
return false, "", err
|
||||
}
|
||||
done, err := s.examPrepModuleIsComplete(ctx, userID, prevID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if !done {
|
||||
return false, errPrevExamPrepModule, nil
|
||||
}
|
||||
return true, "", nil
|
||||
}
|
||||
|
||||
// CanAccessExamPrepLesson requires the module chain and the previous lesson's practices to be completed.
|
||||
func (s *Service) CanAccessExamPrepLesson(ctx context.Context, userID, lessonID int64) (ok bool, reason string, err error) {
|
||||
lesson, err := s.store.GetExamPrepUnitModuleLessonByID(ctx, lessonID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
ok, reason, err = s.CanAccessExamPrepModule(ctx, userID, lesson.UnitModuleID)
|
||||
if err != nil || !ok {
|
||||
return ok, reason, err
|
||||
}
|
||||
prevID, err := s.store.ExamPrepGetPreviousLessonInModule(ctx, lessonID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return true, "", nil
|
||||
}
|
||||
return false, "", err
|
||||
}
|
||||
done, err := s.examPrepLessonIsComplete(ctx, userID, prevID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if !done {
|
||||
return false, errPrevExamPrepLesson, nil
|
||||
}
|
||||
return true, "", nil
|
||||
}
|
||||
|
||||
// CanAccessExamPrepPracticeByQuestionSet enforces lesson chain and within-lesson practice order for exam-prep practices.
|
||||
// Returns ok=true with no error when the question set is not linked to exam_prep.lesson_practices.
|
||||
func (s *Service) CanAccessExamPrepPracticeByQuestionSet(ctx context.Context, userID, questionSetID int64) (ok bool, reason string, err error) {
|
||||
practice, found, err := s.store.TryGetExamPrepLessonPracticeByQuestionSetID(ctx, questionSetID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if !found {
|
||||
return true, "", nil
|
||||
}
|
||||
if !practice.VisibleToLearners() {
|
||||
return false, "Practice not found", nil
|
||||
}
|
||||
ok, reason, err = s.CanAccessExamPrepLesson(ctx, userID, practice.LessonID)
|
||||
if err != nil || !ok {
|
||||
return ok, reason, err
|
||||
}
|
||||
blockedBy, err := s.store.GetFirstIncompletePreviousExamPrepPractice(ctx, userID, questionSetID)
|
||||
if err != nil {
|
||||
return false, "", err
|
||||
}
|
||||
if blockedBy != nil {
|
||||
return false, errPrevExamPrepPractice, nil
|
||||
}
|
||||
return true, "", nil
|
||||
}
|
||||
|
||||
func (s *Service) examPrepCatalogCourseIsComplete(ctx context.Context, userID, catalogCourseID int64) (bool, error) {
|
||||
_, done, _, total, err := s.examPrepCatalogCourseProgress(ctx, userID, catalogCourseID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return examPrepSiblingComplete(done, total), nil
|
||||
}
|
||||
|
||||
func (s *Service) examPrepUnitIsComplete(ctx context.Context, userID, unitID int64) (bool, error) {
|
||||
_, done, _, total, err := s.examPrepUnitProgress(ctx, userID, unitID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return examPrepSiblingComplete(done, total), nil
|
||||
}
|
||||
|
||||
func (s *Service) examPrepModuleIsComplete(ctx context.Context, userID, moduleID int64) (bool, error) {
|
||||
_, done, _, total, err := s.examPrepModuleProgress(ctx, userID, moduleID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return examPrepSiblingComplete(done, total), nil
|
||||
}
|
||||
|
||||
func (s *Service) examPrepLessonIsComplete(ctx context.Context, userID, lessonID int64) (bool, error) {
|
||||
completed, total, err := s.store.ExamPrepUserPracticeProgressInLesson(ctx, userID, lessonID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if total == 0 {
|
||||
return true, nil
|
||||
}
|
||||
return lmsProgressComplete(completed, total), nil
|
||||
}
|
||||
|
||||
func examPrepSiblingComplete(done bool, total int32) bool {
|
||||
if total == 0 {
|
||||
return true
|
||||
}
|
||||
return done
|
||||
}
|
||||
|
||||
// ApplyAccessProgram sets p.Access for learner roles. Staff roles omit Access from JSON.
|
||||
// STUDENT: is_accessible reflects sequential prerequisites; OPEN_LEARNER: is_accessible and is_completed always true.
|
||||
func (s *Service) ApplyAccessProgram(ctx context.Context, role domain.Role, userID int64, p *domain.Program) error {
|
||||
|
|
@ -243,7 +421,14 @@ func (s *Service) ApplyExamPrepAccessCatalogCourse(ctx context.Context, role dom
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cc.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(true, "", done, comp, tot, fraction))
|
||||
ok, reason := true, ""
|
||||
if role.UsesLMSSequentialGating() {
|
||||
ok, reason, err = s.CanAccessExamPrepCatalogCourse(ctx, userID, cc.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
cc.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(ok, reason, done, comp, tot, fraction))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -257,7 +442,14 @@ func (s *Service) ApplyExamPrepAccessUnit(ctx context.Context, role domain.Role,
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(true, "", done, comp, tot, fraction))
|
||||
ok, reason := true, ""
|
||||
if role.UsesLMSSequentialGating() {
|
||||
ok, reason, err = s.CanAccessExamPrepUnit(ctx, userID, u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
u.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(ok, reason, done, comp, tot, fraction))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -271,7 +463,14 @@ func (s *Service) ApplyExamPrepAccessModule(ctx context.Context, role domain.Rol
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(true, "", done, comp, tot, fraction))
|
||||
ok, reason := true, ""
|
||||
if role.UsesLMSSequentialGating() {
|
||||
ok, reason, err = s.CanAccessExamPrepModule(ctx, userID, m.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
m.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(ok, reason, done, comp, tot, fraction))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -285,7 +484,14 @@ func (s *Service) ApplyExamPrepAccessLesson(ctx context.Context, role domain.Rol
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
les.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(true, "", done, comp, tot, fraction))
|
||||
ok, reason := true, ""
|
||||
if role.UsesLMSSequentialGating() {
|
||||
ok, reason, err = s.CanAccessExamPrepLesson(ctx, userID, les.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
les.Access = finalizeOpenLearnerAccess(role, buildLMSEntityAccessFromFraction(ok, reason, done, comp, tot, fraction))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -138,6 +138,9 @@ func (h *Handler) GetExamPrepPracticeByID(c *fiber.Ctx) error {
|
|||
if !p.VisibleToLearners() && !h.canManageExamPrepPractices(c) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found"})
|
||||
}
|
||||
if err := h.enforceExamPrepPracticeSequenceForStudent(c, p.QuestionSetID); err != nil {
|
||||
return practiceSequenceErrorResponse(c, err)
|
||||
}
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Practice retrieved successfully",
|
||||
Data: p,
|
||||
|
|
|
|||
|
|
@ -8,6 +8,42 @@ import (
|
|||
"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() {
|
||||
|
|
|
|||
|
|
@ -203,6 +203,9 @@ func (h *Handler) GetExamPrepPracticeFull(c *fiber.Ctx) error {
|
|||
if !practice.VisibleToLearners() && !h.canManageExamPrepPractices(c) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{Message: "Practice not found"})
|
||||
}
|
||||
if err := h.enforceExamPrepPracticeSequenceForStudent(c, practice.QuestionSetID); err != nil {
|
||||
return practiceSequenceErrorResponse(c, err)
|
||||
}
|
||||
|
||||
catalog, err := h.activeQuestionTypeDefinitionCatalog(c.Context())
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -797,30 +797,33 @@ func isSequenceGatedPractice(set domain.QuestionSet) bool {
|
|||
|
||||
func (h *Handler) enforcePracticeSequenceForStudent(c *fiber.Ctx, set domain.QuestionSet) error {
|
||||
role := c.Locals("role").(domain.Role)
|
||||
if !role.UsesLMSSequentialGating() || !isSequenceGatedPractice(set) {
|
||||
if !role.UsesLMSSequentialGating() {
|
||||
return nil
|
||||
}
|
||||
if !strings.EqualFold(set.Status, "PUBLISHED") {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Practice not found")
|
||||
}
|
||||
|
||||
userID := c.Locals("user_id").(int64)
|
||||
blockedBy, err := h.questionsSvc.GetFirstIncompletePreviousPractice(c.Context(), userID, set.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate practice access")
|
||||
}
|
||||
if blockedBy != nil {
|
||||
return fiber.NewError(
|
||||
fiber.StatusForbidden,
|
||||
fmt.Sprintf(
|
||||
"Complete practice '%s' (display_order=%d) before accessing this one",
|
||||
blockedBy.Title,
|
||||
blockedBy.DisplayOrder,
|
||||
),
|
||||
)
|
||||
|
||||
if isSequenceGatedPractice(set) {
|
||||
if !strings.EqualFold(set.Status, "PUBLISHED") {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Practice not found")
|
||||
}
|
||||
blockedBy, err := h.questionsSvc.GetFirstIncompletePreviousPractice(c.Context(), userID, set.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to validate practice access")
|
||||
}
|
||||
if blockedBy != nil {
|
||||
return fiber.NewError(
|
||||
fiber.StatusForbidden,
|
||||
fmt.Sprintf(
|
||||
"Complete practice '%s' (display_order=%d) before accessing this one",
|
||||
blockedBy.Title,
|
||||
blockedBy.DisplayOrder,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return h.enforceExamPrepPracticeSequenceForStudent(c, set.ID)
|
||||
}
|
||||
|
||||
// CreateQuestionSet godoc
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user