Add migration 000109, require parent context on complete, and update progress queries and LMS rollups. Co-authored-by: Cursor <cursoragent@cursor.com>
179 lines
7.3 KiB
Go
179 lines
7.3 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ExamPrepParentKind identifies which exam-prep hierarchy entity a practice is linked to.
|
|
type ExamPrepParentKind string
|
|
|
|
const (
|
|
ExamPrepParentKindCatalogCourse ExamPrepParentKind = "CATALOG_COURSE"
|
|
ExamPrepParentKindUnit ExamPrepParentKind = "UNIT"
|
|
ExamPrepParentKindLesson ExamPrepParentKind = "LESSON"
|
|
)
|
|
|
|
// ParseExamPrepParentKind maps API/path input to a supported exam-prep parent kind.
|
|
func ParseExamPrepParentKind(raw string) (ExamPrepParentKind, bool) {
|
|
switch strings.TrimSpace(strings.ToUpper(raw)) {
|
|
case string(ExamPrepParentKindCatalogCourse):
|
|
return ExamPrepParentKindCatalogCourse, true
|
|
case string(ExamPrepParentKindUnit):
|
|
return ExamPrepParentKindUnit, true
|
|
case string(ExamPrepParentKindLesson):
|
|
return ExamPrepParentKindLesson, true
|
|
default:
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
// ExamPrepPracticeParent links a practice to a catalog course, unit, or lesson.
|
|
type ExamPrepPracticeParent struct {
|
|
Kind ExamPrepParentKind `json:"parent_kind"`
|
|
ID int64 `json:"parent_id"`
|
|
}
|
|
|
|
// ExamPrepPractice is question-set content tied to an exam-prep parent; uses shared question_sets / questions.
|
|
type ExamPrepPractice struct {
|
|
ID int64 `json:"id"`
|
|
Parents *[]ExamPrepPracticeParent `json:"parents,omitempty"`
|
|
LessonID *int64 `json:"lesson_id,omitempty"`
|
|
Title string `json:"title"`
|
|
StoryDescription *string `json:"story_description,omitempty"`
|
|
StoryImage *string `json:"story_image,omitempty"`
|
|
PersonaID *int64 `json:"persona_id,omitempty"`
|
|
QuestionSetID int64 `json:"question_set_id"`
|
|
PublishStatus PracticePublishStatus `json:"publish_status"`
|
|
AuthoringProfile PracticeAuthoringProfile `json:"authoring_profile"`
|
|
QuickTips *string `json:"quick_tips,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
|
}
|
|
|
|
// ExamPrepPracticeParentsDeref returns parents or nil when unset.
|
|
func ExamPrepPracticeParentsDeref(parents *[]ExamPrepPracticeParent) []ExamPrepPracticeParent {
|
|
if parents == nil {
|
|
return nil
|
|
}
|
|
return *parents
|
|
}
|
|
|
|
// ExamPrepPracticeParentsPtr returns a pointer to a copy of parents, or nil when empty.
|
|
func ExamPrepPracticeParentsPtr(parents []ExamPrepPracticeParent) *[]ExamPrepPracticeParent {
|
|
if len(parents) == 0 {
|
|
return nil
|
|
}
|
|
cp := append([]ExamPrepPracticeParent(nil), parents...)
|
|
return &cp
|
|
}
|
|
|
|
// ExamPrepPracticePrimaryParent returns the single parent when exactly one is set.
|
|
func ExamPrepPracticePrimaryParent(p ExamPrepPractice) (ExamPrepPracticeParent, bool) {
|
|
parents := ExamPrepPracticeParentsDeref(p.Parents)
|
|
if len(parents) != 1 {
|
|
return ExamPrepPracticeParent{}, false
|
|
}
|
|
return parents[0], true
|
|
}
|
|
|
|
// VisibleToLearners mirrors LMS practice visibility rules for subscribers.
|
|
func (p ExamPrepPractice) VisibleToLearners() bool {
|
|
return p.PublishStatus == PracticePublishPublished
|
|
}
|
|
|
|
// CreateExamPrepPracticeInput is the body for POST .../exam-prep/practices and nested parent routes.
|
|
type CreateExamPrepPracticeInput struct {
|
|
// Parents optionally attaches the practice to catalog course, unit, or lesson. Omit or null to create unlinked.
|
|
Parents *[]ExamPrepPracticeParent `json:"parents" validate:"omitempty,dive"`
|
|
// ParentKind and ParentID are accepted for backward compatibility when parents is omitted.
|
|
ParentKind ExamPrepParentKind `json:"parent_kind,omitempty" validate:"omitempty,oneof=CATALOG_COURSE UNIT 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"`
|
|
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 CreateExamPrepPracticeInput) ResolvedParents() []ExamPrepPracticeParent {
|
|
if in.Parents != nil {
|
|
return DedupeExamPrepPracticeParents(*in.Parents)
|
|
}
|
|
if in.ParentKind != "" && in.ParentID > 0 {
|
|
return []ExamPrepPracticeParent{{Kind: in.ParentKind, ID: in.ParentID}}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateExamPrepPracticeParentsInput replaces all parent links on an existing exam-prep practice.
|
|
type UpdateExamPrepPracticeParentsInput struct {
|
|
Parents []ExamPrepPracticeParent `json:"parents" validate:"required,dive"`
|
|
}
|
|
|
|
// ResolveExamPrepPracticeCompletionScope picks the parent link being completed in exam prep.
|
|
func ResolveExamPrepPracticeCompletionScope(parents []ExamPrepPracticeParent, input CompletePracticeInput) (PracticeCompletionScope, error) {
|
|
parents = DedupeExamPrepPracticeParents(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: PracticeContentDomainExamPrep,
|
|
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 := ParseExamPrepParentKind(kindRaw)
|
|
if !ok {
|
|
return PracticeCompletionScope{}, fmt.Errorf("invalid parent_kind")
|
|
}
|
|
for _, parent := range parents {
|
|
if parent.Kind == kind && parent.ID == parentID {
|
|
return PracticeCompletionScope{
|
|
ContentDomain: PracticeContentDomainExamPrep,
|
|
ParentKind: string(kind),
|
|
ParentID: parentID,
|
|
}, nil
|
|
}
|
|
}
|
|
return PracticeCompletionScope{}, fmt.Errorf("practice is not linked to the specified content location")
|
|
}
|
|
|
|
// DedupeExamPrepPracticeParents removes invalid and duplicate parent entries.
|
|
func DedupeExamPrepPracticeParents(parents []ExamPrepPracticeParent) []ExamPrepPracticeParent {
|
|
seen := make(map[ExamPrepPracticeParent]struct{}, len(parents))
|
|
out := make([]ExamPrepPracticeParent, 0, len(parents))
|
|
for _, p := range parents {
|
|
if p.Kind == "" || p.ID <= 0 {
|
|
continue
|
|
}
|
|
if _, ok := seen[p]; ok {
|
|
continue
|
|
}
|
|
seen[p] = struct{}{}
|
|
out = append(out, p)
|
|
}
|
|
return out
|
|
}
|
|
|
|
type UpdateExamPrepPracticeInput 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"`
|
|
}
|