RezinaLMS-BackEnd/internal/domain/practice.go
Yared Yemane fb528e9e77 Scope practice completion to the specific parent link in Learn English and Exam Prep.
Add migration 000109, require parent context on complete, and update progress queries and LMS rollups.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-22 15:37:20 +03:00

217 lines
8.0 KiB
Go

package domain
import (
"fmt"
"strings"
"time"
)
// ParentKind identifies which hierarchy entity a practice is linked to.
type ParentKind string
const (
ParentKindCourse ParentKind = "COURSE"
ParentKindModule ParentKind = "MODULE"
ParentKindLesson ParentKind = "LESSON"
)
// ParsePracticeParentKind maps API/path input to a supported parent kind.
func ParsePracticeParentKind(raw string) (ParentKind, bool) {
switch strings.TrimSpace(strings.ToUpper(raw)) {
case string(ParentKindCourse):
return ParentKindCourse, true
case string(ParentKindModule):
return ParentKindModule, true
case string(ParentKindLesson):
return ParentKindLesson, true
default:
return "", false
}
}
// PracticeParent links a practice to a course, module, or lesson.
type PracticeParent struct {
Kind ParentKind `json:"parent_kind"`
ID int64 `json:"parent_id"`
}
const (
PracticeContentDomainLMS = "LMS"
PracticeContentDomainExamPrep = "EXAM_PREP"
)
// PracticeCompletionScope identifies which hierarchy attachment a learner completed.
type PracticeCompletionScope struct {
ContentDomain string `json:"content_domain"`
ParentKind string `json:"parent_kind"`
ParentID int64 `json:"parent_id"`
}
// CompletePracticeInput is the learner completion payload for a specific parent link.
type CompletePracticeInput struct {
ParentKind string `json:"parent_kind"`
ParentID int64 `json:"parent_id"`
}
// PracticePublishStatus controls learner visibility for a practice shell (independent of question_set.status).
type PracticePublishStatus string
const (
PracticePublishDraft PracticePublishStatus = "DRAFT"
PracticePublishPublished PracticePublishStatus = "PUBLISHED"
)
// ParsePracticePublishStatusInput maps API input. Empty or unknown values default to PUBLISHED for backward compatibility.
func ParsePracticePublishStatusInput(raw string) PracticePublishStatus {
switch strings.TrimSpace(strings.ToUpper(raw)) {
case string(PracticePublishDraft):
return PracticePublishDraft
case string(PracticePublishPublished):
return PracticePublishPublished
default:
return PracticePublishPublished
}
}
// PracticePublishStatusFromDB maps persisted values into the domain type.
func PracticePublishStatusFromDB(raw string) PracticePublishStatus {
return ParsePracticePublishStatusInput(raw)
}
// ResolveLMSPracticeCompletionScope picks the parent link being completed in Learn English.
func ResolveLMSPracticeCompletionScope(parents []PracticeParent, input CompletePracticeInput) (PracticeCompletionScope, error) {
parents = DedupePracticeParents(parents)
if len(parents) == 0 {
return PracticeCompletionScope{}, fmt.Errorf("practice is not linked to any content")
}
kindRaw := strings.TrimSpace(strings.ToUpper(input.ParentKind))
parentID := input.ParentID
if kindRaw == "" || parentID <= 0 {
if len(parents) == 1 {
return PracticeCompletionScope{
ContentDomain: PracticeContentDomainLMS,
ParentKind: string(parents[0].Kind),
ParentID: parents[0].ID,
}, nil
}
return PracticeCompletionScope{}, fmt.Errorf("parent_kind and parent_id are required when the practice is linked in multiple places")
}
kind, ok := ParsePracticeParentKind(kindRaw)
if !ok {
return PracticeCompletionScope{}, fmt.Errorf("invalid parent_kind")
}
for _, parent := range parents {
if parent.Kind == kind && parent.ID == parentID {
return PracticeCompletionScope{
ContentDomain: PracticeContentDomainLMS,
ParentKind: string(kind),
ParentID: parentID,
}, nil
}
}
return PracticeCompletionScope{}, fmt.Errorf("practice is not linked to the specified content location")
}
func PracticeParentsPtr(parents []PracticeParent) *[]PracticeParent {
if len(parents) == 0 {
return nil
}
return &parents
}
// PracticeParentsDeref returns an empty slice when parents is nil.
func PracticeParentsDeref(parents *[]PracticeParent) []PracticeParent {
if parents == nil {
return nil
}
return *parents
}
// Practice is question-set content (story, persona, tips) linked to zero or more courses, modules, or lessons.
type Practice struct {
ID int64 `json:"id"`
Parents *[]PracticeParent `json:"parents"`
Title string `json:"title"`
StoryDescription *string `json:"story_description,omitempty"`
StoryImage *string `json:"story_image,omitempty"`
PersonaID *int64 `json:"persona_id,omitempty"` // lms_personas.id when set
QuestionSetID int64 `json:"question_set_id"`
PublishStatus PracticePublishStatus `json:"publish_status"`
AuthoringProfile PracticeAuthoringProfile `json:"authoring_profile"`
QuickTips *string `json:"quick_tips,omitempty"`
Access *PracticeAccess `json:"access,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
type PracticeAccess struct {
IsAccessible bool `json:"is_accessible"`
Reason string `json:"reason,omitempty"`
}
// VisibleToLearners is true when the practice shell should appear in subscribed learner catalogs and progression.
func (p Practice) VisibleToLearners() bool {
return p.PublishStatus == PracticePublishPublished
}
type CreatePracticeInput struct {
// Parents optionally attaches the practice to hierarchy entities. Omit or null to create unlinked.
Parents *[]PracticeParent `json:"parents" validate:"omitempty,dive"`
// ParentKind and ParentID are accepted for backward compatibility when parents is omitted.
ParentKind ParentKind `json:"parent_kind,omitempty" validate:"omitempty,oneof=COURSE MODULE LESSON"`
ParentID int64 `json:"parent_id,omitempty" validate:"omitempty,gt=0"`
Title *string `json:"title,omitempty"`
StoryDescription *string `json:"story_description,omitempty"`
StoryImage *string `json:"story_image,omitempty"`
PersonaID *int64 `json:"persona_id,omitempty"`
QuestionSetID int64 `json:"question_set_id" validate:"required,gt=0"`
QuickTips *string `json:"quick_tips,omitempty"`
// Omit or empty for backward compatibility defaults to PUBLISHED; set DRAFT to save hidden from learners until published.
PublishStatus string `json:"publish_status,omitempty" validate:"omitempty,oneof=DRAFT draft PUBLISHED published"`
}
// ResolvedParents returns deduplicated parents from parents or the legacy single-parent fields.
func (in CreatePracticeInput) ResolvedParents() []PracticeParent {
if in.Parents != nil {
return DedupePracticeParents(*in.Parents)
}
if in.ParentKind != "" && in.ParentID > 0 {
return []PracticeParent{{Kind: in.ParentKind, ID: in.ParentID}}
}
return nil
}
// UpdatePracticeParentsInput replaces all parent links on an existing practice.
type UpdatePracticeParentsInput struct {
Parents []PracticeParent `json:"parents" validate:"required,dive"`
}
// DedupePracticeParents removes invalid and duplicate parent entries.
func DedupePracticeParents(parents []PracticeParent) []PracticeParent {
seen := make(map[PracticeParent]struct{}, len(parents))
out := make([]PracticeParent, 0, len(parents))
for _, p := range parents {
kind, ok := ParsePracticeParentKind(string(p.Kind))
if !ok || p.ID <= 0 {
continue
}
p.Kind = kind
if _, exists := seen[p]; exists {
continue
}
seen[p] = struct{}{}
out = append(out, p)
}
return out
}
type UpdatePracticeInput struct {
Title *string `json:"title,omitempty"`
StoryDescription *string `json:"story_description,omitempty"`
StoryImage *string `json:"story_image,omitempty"`
PersonaID *int64 `json:"persona_id,omitempty"`
QuestionSetID *int64 `json:"question_set_id,omitempty"`
QuickTips *string `json:"quick_tips,omitempty"`
PublishStatus *string `json:"publish_status,omitempty" validate:"omitempty,oneof=DRAFT draft PUBLISHED published"`
AuthoringProfile *string `json:"authoring_profile,omitempty" validate:"omitempty,oneof=STANDALONE IELTS_SHARED_STIMULUS"`
}