Keep draft practices visible to admin staff and allow multiple Learn English practices per parent.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-13 12:03:05 +03:00
parent edcdaa5bfb
commit 3304d8818f
22 changed files with 94 additions and 70 deletions

View File

@ -0,0 +1,8 @@
DELETE FROM lms_practice_parents a
USING lms_practice_parents b
WHERE a.id > b.id
AND a.parent_kind = b.parent_kind
AND a.parent_id = b.parent_id;
ALTER TABLE lms_practice_parents
ADD CONSTRAINT lms_practice_parents_parent_kind_parent_id_key UNIQUE (parent_kind, parent_id);

View File

@ -0,0 +1,4 @@
-- Learn English: allow multiple practices per course, module, or lesson
-- (matches exam-prep 000094). Duplicate links on the same practice remain unique.
ALTER TABLE lms_practice_parents
DROP CONSTRAINT IF EXISTS lms_practice_parents_parent_kind_parent_id_key;

View File

@ -88,7 +88,10 @@ SELECT
SELECT 1
FROM exam_prep.lesson_practices p
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
WHERE p.publish_status = 'PUBLISHED'
WHERE (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
AND (
(
pp.parent_kind = 'CATALOG_COURSE'

View File

@ -69,7 +69,10 @@ SELECT
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
WHERE pp.parent_kind = 'LESSON'
AND pp.parent_id = l.id
AND p.publish_status = 'PUBLISHED'
AND (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice,
l.created_at,
l.updated_at

View File

@ -65,7 +65,10 @@ WITH module_counts AS (
LEFT JOIN exam_prep.practice_parents pp ON pp.parent_kind = 'LESSON'
AND pp.parent_id = l.id
LEFT JOIN exam_prep.lesson_practices p ON p.id = pp.practice_id
AND p.publish_status = 'PUBLISHED'
AND (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
GROUP BY m.id
)
SELECT

View File

@ -77,7 +77,10 @@ WITH unit_counts AS (
SELECT COUNT(DISTINCT p.id)::BIGINT
FROM exam_prep.lesson_practices p
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
WHERE p.publish_status = 'PUBLISHED'
WHERE (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
AND (
(
pp.parent_kind = 'UNIT'

View File

@ -94,14 +94,20 @@ SELECT
WHERE
lpp.parent_kind = 'COURSE'
AND lpp.parent_id = c.id
AND p.publish_status = 'PUBLISHED') AS practice_count,
AND (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)) AS practice_count,
EXISTS (
SELECT 1
FROM lms_practice_parents lpp
INNER JOIN lms_practices p ON p.id = lpp.practice_id
WHERE lpp.parent_kind = 'COURSE'
AND lpp.parent_id = c.id
AND p.publish_status = 'PUBLISHED'
AND (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice
FROM
courses c

View File

@ -63,7 +63,10 @@ SELECT
INNER JOIN lms_practices p ON p.id = lpp.practice_id
WHERE lpp.parent_kind = 'LESSON'
AND lpp.parent_id = l.id
AND p.publish_status = 'PUBLISHED'
AND (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice
FROM
lessons l

View File

@ -75,7 +75,10 @@ SELECT
INNER JOIN lms_practices p ON p.id = lpp.practice_id
WHERE lpp.parent_kind = 'MODULE'
AND lpp.parent_id = m.id
AND p.publish_status = 'PUBLISHED'
AND (
sqlc.arg('published_only')::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice
FROM
modules m

View File

@ -207,7 +207,10 @@ SELECT
SELECT 1
FROM exam_prep.lesson_practices p
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
WHERE p.publish_status = 'PUBLISHED'
WHERE (
$3::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
AND (
(
pp.parent_kind = 'CATALOG_COURSE'

View File

@ -208,7 +208,10 @@ SELECT
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
WHERE pp.parent_kind = 'LESSON'
AND pp.parent_id = l.id
AND p.publish_status = 'PUBLISHED'
AND (
$4::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice,
l.created_at,
l.updated_at

View File

@ -204,7 +204,10 @@ WITH module_counts AS (
LEFT JOIN exam_prep.practice_parents pp ON pp.parent_kind = 'LESSON'
AND pp.parent_id = l.id
LEFT JOIN exam_prep.lesson_practices p ON p.id = pp.practice_id
AND p.publish_status = 'PUBLISHED'
AND (
$4::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
GROUP BY m.id
)
SELECT

View File

@ -213,7 +213,10 @@ WITH unit_counts AS (
SELECT COUNT(DISTINCT p.id)::BIGINT
FROM exam_prep.lesson_practices p
INNER JOIN exam_prep.practice_parents pp ON pp.practice_id = p.id
WHERE p.publish_status = 'PUBLISHED'
WHERE (
$4::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
AND (
(
pp.parent_kind = 'UNIT'

View File

@ -197,14 +197,20 @@ SELECT
WHERE
lpp.parent_kind = 'COURSE'
AND lpp.parent_id = c.id
AND p.publish_status = 'PUBLISHED') AS practice_count,
AND (
$4::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)) AS practice_count,
EXISTS (
SELECT 1
FROM lms_practice_parents lpp
INNER JOIN lms_practices p ON p.id = lpp.practice_id
WHERE lpp.parent_kind = 'COURSE'
AND lpp.parent_id = c.id
AND p.publish_status = 'PUBLISHED'
AND (
$4::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice
FROM
courses c

View File

@ -183,7 +183,10 @@ SELECT
INNER JOIN lms_practices p ON p.id = lpp.practice_id
WHERE lpp.parent_kind = 'LESSON'
AND lpp.parent_id = l.id
AND p.publish_status = 'PUBLISHED'
AND (
$4::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice
FROM
lessons l

View File

@ -183,7 +183,10 @@ SELECT
INNER JOIN lms_practices p ON p.id = lpp.practice_id
WHERE lpp.parent_kind = 'MODULE'
AND lpp.parent_id = m.id
AND p.publish_status = 'PUBLISHED'
AND (
$5::boolean = FALSE
OR p.publish_status = 'PUBLISHED'::TEXT
)
) AS has_practice
FROM
modules m

View File

@ -429,9 +429,6 @@ func (s *Service) CreateExamPrepPracticeFromInput(ctx context.Context, input dom
}
func (s *Service) listExamPrepPractices(ctx context.Context, parent domain.ExamPrepPracticeParent, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if limit <= 0 {
limit = 20
}
if limit > 200 {
limit = 200
}
@ -505,9 +502,6 @@ func (s *Service) ListExamPrepPracticesByUnit(ctx context.Context, unitID int64,
}
func (s *Service) ListAllExamPrepPractices(ctx context.Context, publishedOnly, unlinkedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
if limit <= 0 {
limit = 20
}
if limit > 200 {
limit = 200
}

View File

@ -3,7 +3,6 @@ package practices
import (
"Yimaru-Backend/internal/domain"
"Yimaru-Backend/internal/ports"
"Yimaru-Backend/internal/repository"
"Yimaru-Backend/internal/services/courses"
"Yimaru-Backend/internal/services/lessons"
"Yimaru-Backend/internal/services/modules"
@ -111,25 +110,6 @@ func (s *Service) validateParentList(ctx context.Context, parents []domain.Pract
return nil
}
func (s *Service) ensureParentsAvailable(ctx context.Context, parents []domain.PracticeParent, excludePracticeID int64) error {
for _, parent := range parents {
var count int64
var err error
if excludePracticeID > 0 {
count, err = s.practices.CountLmsPracticesByParentExcludingPractice(ctx, parent.Kind, parent.ID, excludePracticeID)
} else {
count, err = s.practices.CountLmsPracticesByParent(ctx, parent.Kind, parent.ID)
}
if err != nil {
return err
}
if count >= 1 {
return ErrPracticeParentLimitExceeded
}
}
return nil
}
func (s *Service) Create(ctx context.Context, in domain.CreatePracticeInput) (domain.Practice, error) {
if err := s.validateQuestionSet(ctx, in.QuestionSetID); err != nil {
return domain.Practice{}, err
@ -144,15 +124,8 @@ func (s *Service) Create(ctx context.Context, in domain.CreatePracticeInput) (do
if err := s.validateParentList(ctx, parents); err != nil {
return domain.Practice{}, err
}
if err := s.ensureParentsAvailable(ctx, parents, 0); err != nil {
return domain.Practice{}, err
}
}
p, err := s.practices.CreateLmsPractice(ctx, in, parents)
if err != nil && repository.IsUniqueViolation(err) {
return domain.Practice{}, ErrPracticeParentLimitExceeded
}
return p, err
return s.practices.CreateLmsPractice(ctx, in, parents)
}
func (s *Service) TryGetByQuestionSetID(ctx context.Context, questionSetID int64) (domain.Practice, bool, error) {
@ -255,14 +228,8 @@ func (s *Service) UpdateParents(ctx context.Context, id int64, in domain.UpdateP
if err := s.validateParentList(ctx, parents); err != nil {
return domain.Practice{}, err
}
if err := s.ensureParentsAvailable(ctx, parents, id); err != nil {
return domain.Practice{}, err
}
}
if err := s.practices.ReplaceLmsPracticeParents(ctx, id, parents); err != nil {
if repository.IsUniqueViolation(err) {
return domain.Practice{}, ErrPracticeParentLimitExceeded
}
return domain.Practice{}, err
}
return s.practices.GetLmsPracticeByID(ctx, id)

View File

@ -63,7 +63,7 @@ func (h *Handler) ListAllExamPrepPractices(c *fiber.Ctx) error {
Error: err.Error(),
})
}
publishedOnly := !h.canManageExamPrepPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
unlinkedOnly := false
if h.canManageExamPrepPractices(c) {
unlinkedOnly, _ = strconv.ParseBool(c.Query("unlinked_only", "false"))
@ -146,7 +146,7 @@ func (h *Handler) ListExamPrepPracticesByLesson(c *fiber.Ctx) error {
Error: err.Error(),
})
}
publishedOnly := !h.canManageExamPrepPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
items, total, err := h.examPrepSvc.ListExamPrepPracticesByLesson(c.Context(), lessonID, publishedOnly, limit, offset)
if err != nil {
if resp := h.listExamPrepPracticesResponse(c, nil, 0, int(limit), int(offset), err); resp != nil {
@ -494,7 +494,7 @@ func (h *Handler) ListExamPrepPracticesByCatalogCourse(c *fiber.Ctx) error {
Error: err.Error(),
})
}
publishedOnly := !h.canManageExamPrepPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
items, total, err := h.examPrepSvc.ListExamPrepPracticesByCatalogCourse(c.Context(), catalogCourseID, publishedOnly, limit, offset)
if err != nil {
if resp := h.listExamPrepPracticesResponse(c, nil, 0, int(limit), int(offset), err); resp != nil {
@ -555,7 +555,7 @@ func (h *Handler) ListExamPrepPracticesByUnit(c *fiber.Ctx) error {
Error: err.Error(),
})
}
publishedOnly := !h.canManageExamPrepPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
items, total, err := h.examPrepSvc.ListExamPrepPracticesByUnit(c.Context(), unitID, publishedOnly, limit, offset)
if err != nil {
if resp := h.listExamPrepPracticesResponse(c, nil, 0, int(limit), int(offset), err); resp != nil {

View File

@ -80,8 +80,7 @@ func (h *Handler) CreatePractice(c *fiber.Ctx) error {
func (h *Handler) ListPractices(c *fiber.Ctx) error {
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
unlinkedOnly := false
if h.canManageLMSPractices(c) {
unlinkedOnly, _ = strconv.ParseBool(c.Query("unlinked_only", "false"))
@ -117,8 +116,7 @@ func (h *Handler) ListPracticesByCourse(c *fiber.Ctx) error {
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
items, total, err := h.practiceSvc.ListByCourse(c.Context(), courseID, publishedOnly, int32(limit), int32(offset))
if err != nil {
if errors.Is(err, courses.ErrCourseNotFound) {
@ -153,8 +151,7 @@ func (h *Handler) ListPracticesByModule(c *fiber.Ctx) error {
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
items, total, err := h.practiceSvc.ListByModule(c.Context(), moduleID, publishedOnly, int32(limit), int32(offset))
if err != nil {
if errors.Is(err, modules.ErrModuleNotFound) {
@ -189,8 +186,7 @@ func (h *Handler) ListPracticesByLesson(c *fiber.Ctx) error {
}
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
items, total, err := h.practiceSvc.ListByLesson(c.Context(), lessonID, publishedOnly, int32(limit), int32(offset))
if err != nil {
if errors.Is(err, lessons.ErrLessonNotFound) {

View File

@ -11,6 +11,14 @@ func (h *Handler) canManageLMSPractices(c *fiber.Ctx) bool {
return h.rbacSvc.HasPermission(rn, "practices.create") || h.rbacSvc.HasPermission(rn, "practices.update")
}
// hideUnpublishedPractices is true only for customer learner roles.
// Admin-panel staff (including custom RBAC roles with list permission) must see DRAFT shells
// on parent practice pages; gating create/update is handled separately by RequirePermission.
func (h *Handler) hideUnpublishedPractices(c *fiber.Ctx) bool {
role, _ := c.Locals("role").(domain.Role)
return role.IsCustomerLearnerRole()
}
func (h *Handler) canManageLessons(c *fiber.Ctx) bool {
rn := string(c.Locals("role").(domain.Role))
return h.rbacSvc.HasPermission(rn, "lessons.create") || h.rbacSvc.HasPermission(rn, "lessons.update")

View File

@ -347,8 +347,7 @@ func (h *Handler) ListPracticesByQuestionTypeDefinition(c *fiber.Ctx) error {
limit, _ := strconv.Atoi(c.Query("limit", "20"))
offset, _ := strconv.Atoi(c.Query("offset", "0"))
role, _ := c.Locals("role").(domain.Role)
publishedOnly := role.IsCustomerLearnerRole() || !h.canManageLMSPractices(c)
publishedOnly := h.hideUnpublishedPractices(c)
summary, err := h.questionsSvc.ListPracticesByQuestionTypeDefinition(c.Context(), id, publishedOnly, int32(limit), int32(offset))
if err != nil {