diff --git a/db/migrations/000105_lms_multiple_practices_per_parent.down.sql b/db/migrations/000105_lms_multiple_practices_per_parent.down.sql new file mode 100644 index 00000000..444f2719 --- /dev/null +++ b/db/migrations/000105_lms_multiple_practices_per_parent.down.sql @@ -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); diff --git a/db/migrations/000105_lms_multiple_practices_per_parent.up.sql b/db/migrations/000105_lms_multiple_practices_per_parent.up.sql new file mode 100644 index 00000000..e4d63256 --- /dev/null +++ b/db/migrations/000105_lms_multiple_practices_per_parent.up.sql @@ -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; diff --git a/db/query/exam_prep_catalog_courses.sql b/db/query/exam_prep_catalog_courses.sql index 01a06fff..70fa1032 100644 --- a/db/query/exam_prep_catalog_courses.sql +++ b/db/query/exam_prep_catalog_courses.sql @@ -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' diff --git a/db/query/exam_prep_unit_module_lessons.sql b/db/query/exam_prep_unit_module_lessons.sql index cba0be5e..bfefede6 100644 --- a/db/query/exam_prep_unit_module_lessons.sql +++ b/db/query/exam_prep_unit_module_lessons.sql @@ -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 diff --git a/db/query/exam_prep_unit_modules.sql b/db/query/exam_prep_unit_modules.sql index fc1f8d87..0ddd3dbb 100644 --- a/db/query/exam_prep_unit_modules.sql +++ b/db/query/exam_prep_unit_modules.sql @@ -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 diff --git a/db/query/exam_prep_units.sql b/db/query/exam_prep_units.sql index 615a5bec..b71b2d69 100644 --- a/db/query/exam_prep_units.sql +++ b/db/query/exam_prep_units.sql @@ -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' diff --git a/db/query/lms_courses.sql b/db/query/lms_courses.sql index 412c40b3..cc8c35e4 100644 --- a/db/query/lms_courses.sql +++ b/db/query/lms_courses.sql @@ -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 diff --git a/db/query/lms_lessons.sql b/db/query/lms_lessons.sql index e039d862..6f477c93 100644 --- a/db/query/lms_lessons.sql +++ b/db/query/lms_lessons.sql @@ -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 diff --git a/db/query/lms_modules.sql b/db/query/lms_modules.sql index 3c3e23b8..2b6f8056 100644 --- a/db/query/lms_modules.sql +++ b/db/query/lms_modules.sql @@ -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 diff --git a/gen/db/exam_prep_catalog_courses.sql.go b/gen/db/exam_prep_catalog_courses.sql.go index 05c9c5e4..e8e26dad 100644 --- a/gen/db/exam_prep_catalog_courses.sql.go +++ b/gen/db/exam_prep_catalog_courses.sql.go @@ -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' diff --git a/gen/db/exam_prep_unit_module_lessons.sql.go b/gen/db/exam_prep_unit_module_lessons.sql.go index 05544a37..cacf9934 100644 --- a/gen/db/exam_prep_unit_module_lessons.sql.go +++ b/gen/db/exam_prep_unit_module_lessons.sql.go @@ -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 diff --git a/gen/db/exam_prep_unit_modules.sql.go b/gen/db/exam_prep_unit_modules.sql.go index e0f3caae..b0960f07 100644 --- a/gen/db/exam_prep_unit_modules.sql.go +++ b/gen/db/exam_prep_unit_modules.sql.go @@ -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 diff --git a/gen/db/exam_prep_units.sql.go b/gen/db/exam_prep_units.sql.go index 4d7eb777..2b13fd77 100644 --- a/gen/db/exam_prep_units.sql.go +++ b/gen/db/exam_prep_units.sql.go @@ -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' diff --git a/gen/db/lms_courses.sql.go b/gen/db/lms_courses.sql.go index 0e22c08d..82674b1e 100644 --- a/gen/db/lms_courses.sql.go +++ b/gen/db/lms_courses.sql.go @@ -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 diff --git a/gen/db/lms_lessons.sql.go b/gen/db/lms_lessons.sql.go index 44d15e2c..18c91314 100644 --- a/gen/db/lms_lessons.sql.go +++ b/gen/db/lms_lessons.sql.go @@ -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 diff --git a/gen/db/lms_modules.sql.go b/gen/db/lms_modules.sql.go index c7dc57d1..baf60a5a 100644 --- a/gen/db/lms_modules.sql.go +++ b/gen/db/lms_modules.sql.go @@ -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 diff --git a/internal/services/examprep/service.go b/internal/services/examprep/service.go index ce8bcbb0..0a82dd4d 100644 --- a/internal/services/examprep/service.go +++ b/internal/services/examprep/service.go @@ -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 } diff --git a/internal/services/practices/service.go b/internal/services/practices/service.go index ef6baa0e..7322e996 100644 --- a/internal/services/practices/service.go +++ b/internal/services/practices/service.go @@ -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) diff --git a/internal/web_server/handlers/exam_prep_practice_handler.go b/internal/web_server/handlers/exam_prep_practice_handler.go index 5b6ef76e..ff13f319 100644 --- a/internal/web_server/handlers/exam_prep_practice_handler.go +++ b/internal/web_server/handlers/exam_prep_practice_handler.go @@ -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 { diff --git a/internal/web_server/handlers/practice_handler.go b/internal/web_server/handlers/practice_handler.go index 59ebc8ae..b0c5def7 100644 --- a/internal/web_server/handlers/practice_handler.go +++ b/internal/web_server/handlers/practice_handler.go @@ -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) { diff --git a/internal/web_server/handlers/practice_publish_gate.go b/internal/web_server/handlers/practice_publish_gate.go index e17ebc88..eb3b9266 100644 --- a/internal/web_server/handlers/practice_publish_gate.go +++ b/internal/web_server/handlers/practice_publish_gate.go @@ -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") diff --git a/internal/web_server/handlers/question_type_builder.go b/internal/web_server/handlers/question_type_builder.go index 530f547e..5e058498 100644 --- a/internal/web_server/handlers/question_type_builder.go +++ b/internal/web_server/handlers/question_type_builder.go @@ -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 {