Add migration 000109, require parent context on complete, and update progress queries and LMS rollups. Co-authored-by: Cursor <cursoragent@cursor.com>
479 lines
14 KiB
SQL
479 lines
14 KiB
SQL
-- name: CountPublishedExamPrepPracticesInLesson :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
WHERE
|
|
pp.parent_kind = 'LESSON'
|
|
AND pp.parent_id = $1
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountUserCompletedPublishedExamPrepPracticesInLesson :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
INNER JOIN user_practice_progress upp ON upp.question_set_id = p.question_set_id
|
|
AND upp.user_id = $2
|
|
AND upp.content_domain = 'EXAM_PREP'
|
|
AND upp.parent_kind = pp.parent_kind
|
|
AND upp.parent_id = pp.parent_id
|
|
AND upp.completed_at IS NOT NULL
|
|
WHERE
|
|
pp.parent_kind = 'LESSON'
|
|
AND pp.parent_id = $1
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountPublishedDirectExamPrepPracticesInUnit :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
WHERE
|
|
pp.parent_kind = 'UNIT'
|
|
AND pp.parent_id = $1
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountUserCompletedPublishedDirectExamPrepPracticesInUnit :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
INNER JOIN user_practice_progress upp ON upp.question_set_id = p.question_set_id
|
|
AND upp.user_id = $2
|
|
AND upp.content_domain = 'EXAM_PREP'
|
|
AND upp.parent_kind = pp.parent_kind
|
|
AND upp.parent_id = pp.parent_id
|
|
AND upp.completed_at IS NOT NULL
|
|
WHERE
|
|
pp.parent_kind = 'UNIT'
|
|
AND pp.parent_id = $1
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountPublishedDirectExamPrepPracticesInCatalogCourse :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
WHERE
|
|
pp.parent_kind = 'CATALOG_COURSE'
|
|
AND pp.parent_id = $1
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountUserCompletedPublishedDirectExamPrepPracticesInCatalogCourse :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
INNER JOIN user_practice_progress upp ON upp.question_set_id = p.question_set_id
|
|
AND upp.user_id = $2
|
|
AND upp.content_domain = 'EXAM_PREP'
|
|
AND upp.parent_kind = pp.parent_kind
|
|
AND upp.parent_id = pp.parent_id
|
|
AND upp.completed_at IS NOT NULL
|
|
WHERE
|
|
pp.parent_kind = 'CATALOG_COURSE'
|
|
AND pp.parent_id = $1
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountPublishedExamPrepPracticesInModule :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN exam_prep.unit_module_lessons l ON pp.parent_kind = 'LESSON'
|
|
AND l.id = pp.parent_id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
WHERE
|
|
l.unit_module_id = $1
|
|
AND l.publish_status = 'PUBLISHED'
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountUserCompletedPublishedExamPrepPracticesInModule :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN exam_prep.unit_module_lessons l ON pp.parent_kind = 'LESSON'
|
|
AND l.id = pp.parent_id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
INNER JOIN user_practice_progress upp ON upp.question_set_id = p.question_set_id
|
|
AND upp.user_id = $2
|
|
AND upp.content_domain = 'EXAM_PREP'
|
|
AND upp.parent_kind = pp.parent_kind
|
|
AND upp.parent_id = pp.parent_id
|
|
AND upp.completed_at IS NOT NULL
|
|
WHERE
|
|
l.unit_module_id = $1
|
|
AND l.publish_status = 'PUBLISHED'
|
|
AND qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED';
|
|
|
|
-- name: CountPublishedExamPrepPracticesInUnit :one
|
|
SELECT
|
|
count(DISTINCT p.id)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
LEFT JOIN exam_prep.unit_module_lessons l ON pp.parent_kind = 'LESSON'
|
|
AND l.id = pp.parent_id
|
|
LEFT JOIN exam_prep.unit_modules m ON l.unit_module_id = m.id
|
|
WHERE
|
|
qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED'
|
|
AND (
|
|
(
|
|
pp.parent_kind = 'UNIT'
|
|
AND pp.parent_id = $1
|
|
)
|
|
OR (
|
|
pp.parent_kind = 'LESSON'
|
|
AND m.unit_id = $1
|
|
AND l.publish_status = 'PUBLISHED'
|
|
AND m.publish_status = 'PUBLISHED'
|
|
)
|
|
);
|
|
|
|
-- name: CountUserCompletedPublishedExamPrepPracticesInUnit :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
INNER JOIN user_practice_progress upp ON upp.question_set_id = p.question_set_id
|
|
AND upp.user_id = $2
|
|
AND upp.content_domain = 'EXAM_PREP'
|
|
AND upp.parent_kind = pp.parent_kind
|
|
AND upp.parent_id = pp.parent_id
|
|
AND upp.completed_at IS NOT NULL
|
|
LEFT JOIN exam_prep.unit_module_lessons l ON pp.parent_kind = 'LESSON'
|
|
AND l.id = pp.parent_id
|
|
LEFT JOIN exam_prep.unit_modules m ON l.unit_module_id = m.id
|
|
WHERE
|
|
qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED'
|
|
AND (
|
|
(
|
|
pp.parent_kind = 'UNIT'
|
|
AND pp.parent_id = $1
|
|
)
|
|
OR (
|
|
pp.parent_kind = 'LESSON'
|
|
AND m.unit_id = $1
|
|
AND l.publish_status = 'PUBLISHED'
|
|
AND m.publish_status = 'PUBLISHED'
|
|
)
|
|
);
|
|
|
|
-- name: CountPublishedExamPrepPracticesInCatalogCourse :one
|
|
SELECT
|
|
count(DISTINCT p.id)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
WHERE
|
|
qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED'
|
|
AND (
|
|
(
|
|
pp.parent_kind = 'CATALOG_COURSE'
|
|
AND pp.parent_id = $1
|
|
)
|
|
OR (
|
|
pp.parent_kind = 'UNIT'
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM exam_prep.units u
|
|
WHERE u.id = pp.parent_id
|
|
AND u.catalog_course_id = $1
|
|
AND u.publish_status = 'PUBLISHED'
|
|
)
|
|
)
|
|
OR (
|
|
pp.parent_kind = 'LESSON'
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM exam_prep.unit_module_lessons l
|
|
INNER JOIN exam_prep.unit_modules m ON m.id = l.unit_module_id
|
|
INNER JOIN exam_prep.units u ON u.id = m.unit_id
|
|
WHERE l.id = pp.parent_id
|
|
AND u.catalog_course_id = $1
|
|
AND l.publish_status = 'PUBLISHED'
|
|
AND m.publish_status = 'PUBLISHED'
|
|
AND u.publish_status = 'PUBLISHED'
|
|
)
|
|
)
|
|
);
|
|
|
|
-- name: CountUserCompletedPublishedExamPrepPracticesInCatalogCourse :one
|
|
SELECT
|
|
count(*)::int AS n
|
|
FROM
|
|
exam_prep.lesson_practices p
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
|
|
INNER JOIN question_sets qs ON qs.id = p.question_set_id
|
|
INNER JOIN user_practice_progress upp ON upp.question_set_id = p.question_set_id
|
|
AND upp.user_id = $2
|
|
AND upp.content_domain = 'EXAM_PREP'
|
|
AND upp.parent_kind = pp.parent_kind
|
|
AND upp.parent_id = pp.parent_id
|
|
AND upp.completed_at IS NOT NULL
|
|
WHERE
|
|
qs.set_type = 'PRACTICE'
|
|
AND qs.status = 'PUBLISHED'
|
|
AND p.publish_status = 'PUBLISHED'
|
|
AND (
|
|
(
|
|
pp.parent_kind = 'CATALOG_COURSE'
|
|
AND pp.parent_id = $1
|
|
)
|
|
OR (
|
|
pp.parent_kind = 'UNIT'
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM exam_prep.units u
|
|
WHERE u.id = pp.parent_id
|
|
AND u.catalog_course_id = $1
|
|
AND u.publish_status = 'PUBLISHED'
|
|
)
|
|
)
|
|
OR (
|
|
pp.parent_kind = 'LESSON'
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM exam_prep.unit_module_lessons l
|
|
INNER JOIN exam_prep.unit_modules m ON m.id = l.unit_module_id
|
|
INNER JOIN exam_prep.units u ON u.id = m.unit_id
|
|
WHERE l.id = pp.parent_id
|
|
AND u.catalog_course_id = $1
|
|
AND l.publish_status = 'PUBLISHED'
|
|
AND m.publish_status = 'PUBLISHED'
|
|
AND u.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,
|
|
pp.parent_kind,
|
|
pp.parent_id,
|
|
lp.created_at,
|
|
lp.question_set_id
|
|
FROM
|
|
exam_prep.lesson_practices lp
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = lp.id
|
|
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
|
WHERE
|
|
lp.question_set_id = @question_set_id::BIGINT
|
|
AND pp.parent_kind = @parent_kind::VARCHAR
|
|
AND pp.parent_id = @parent_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,
|
|
pp.parent_kind,
|
|
pp.parent_id
|
|
FROM
|
|
exam_prep.lesson_practices lp
|
|
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = lp.id
|
|
INNER JOIN question_sets qs ON qs.id = lp.question_set_id
|
|
INNER JOIN target t ON pp.parent_kind = t.parent_kind
|
|
AND pp.parent_id = t.parent_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.content_domain = 'EXAM_PREP'
|
|
AND upp.parent_kind = c.parent_kind
|
|
AND upp.parent_id = c.parent_id
|
|
AND upp.completed_at IS NOT NULL
|
|
WHERE
|
|
upp.id IS NULL
|
|
ORDER BY
|
|
c.created_at ASC,
|
|
c.id ASC
|
|
LIMIT 1;
|