166 lines
6.3 KiB
Go
166 lines
6.3 KiB
Go
package domain
|
|
|
|
import (
|
|
"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"`
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// PracticeParentsPtr returns nil when there are no parents (JSON null); otherwise a pointer to the slice.
|
|
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"`
|
|
}
|