Preserve matching stimulus IDs, validate question-owned payloads independently, and expose additive effective payloads without overriding shared content. Co-authored-by: Cursor <cursoragent@cursor.com>
115 lines
3.7 KiB
Go
115 lines
3.7 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
PracticeKindLMS = "LMS"
|
|
PracticeKindExamPrep = "EXAM_PREP"
|
|
)
|
|
|
|
// PracticeAuthoringProfile controls how the admin UI authors practice content.
|
|
type PracticeAuthoringProfile string
|
|
|
|
const (
|
|
PracticeAuthoringProfileStandalone PracticeAuthoringProfile = "STANDALONE"
|
|
PracticeAuthoringProfileIELTSSharedStimulus PracticeAuthoringProfile = "IELTS_SHARED_STIMULUS"
|
|
)
|
|
|
|
// ParsePracticeAuthoringProfile maps API/DB values; unknown values default to STANDALONE.
|
|
func ParsePracticeAuthoringProfile(raw string) PracticeAuthoringProfile {
|
|
switch strings.ToUpper(strings.TrimSpace(raw)) {
|
|
case string(PracticeAuthoringProfileIELTSSharedStimulus):
|
|
return PracticeAuthoringProfileIELTSSharedStimulus
|
|
default:
|
|
return PracticeAuthoringProfileStandalone
|
|
}
|
|
}
|
|
|
|
// PracticeStimulusBlock is shared stimulus content scoped to one practice.
|
|
type PracticeStimulusBlock struct {
|
|
ID int64 `json:"id,omitempty"`
|
|
BlockKey string `json:"block_key"`
|
|
DisplayOrder int32 `json:"display_order"`
|
|
Stimulus []DynamicElementInstance `json:"stimulus"`
|
|
CreatedAt time.Time `json:"-"`
|
|
UpdatedAt *time.Time `json:"-"`
|
|
}
|
|
|
|
// FullUpdateStimulusBlockItem creates or updates a practice-level stimulus block.
|
|
type FullUpdateStimulusBlockItem struct {
|
|
BlockKey string `json:"block_key"`
|
|
DisplayOrder *int32 `json:"display_order,omitempty"`
|
|
Stimulus []DynamicElementInstance `json:"stimulus"`
|
|
}
|
|
|
|
var stimulusBlockKeyPattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$`)
|
|
|
|
// ValidateStimulusBlockKey reports whether key is a non-empty client identifier.
|
|
func ValidateStimulusBlockKey(key string) error {
|
|
k := strings.TrimSpace(key)
|
|
if k == "" {
|
|
return fmt.Errorf("block_key is required")
|
|
}
|
|
if !stimulusBlockKeyPattern.MatchString(k) {
|
|
return fmt.Errorf("block_key %q is invalid", key)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidatePracticeStimulusBlock validates one block payload from a full practice save.
|
|
func ValidatePracticeStimulusBlock(block FullUpdateStimulusBlockItem) error {
|
|
if err := ValidateStimulusBlockKey(block.BlockKey); err != nil {
|
|
return err
|
|
}
|
|
for i, el := range block.Stimulus {
|
|
kind := strings.TrimSpace(el.Kind)
|
|
if !IsValidStimulusComponentKind(kind) {
|
|
return fmt.Errorf("stimulus_blocks[%s].stimulus[%d]: invalid kind %q", block.BlockKey, i, el.Kind)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MergeEffectiveDynamicPayload combines block-level stimulus with question-level payload.
|
|
// Shared block elements are always additional stimulus: matching IDs are preserved, not overridden.
|
|
func MergeEffectiveDynamicPayload(blockStimulus []DynamicElementInstance, questionPayload *DynamicQuestionPayload) *DynamicQuestionPayload {
|
|
if questionPayload == nil && len(blockStimulus) == 0 {
|
|
return nil
|
|
}
|
|
|
|
out := &DynamicQuestionPayload{
|
|
Stimulus: make([]DynamicElementInstance, 0, len(blockStimulus)),
|
|
Response: nil,
|
|
}
|
|
out.Stimulus = append(out.Stimulus, blockStimulus...)
|
|
if questionPayload != nil {
|
|
out.Stimulus = append(out.Stimulus, questionPayload.Stimulus...)
|
|
out.Response = append([]DynamicElementInstance(nil), questionPayload.Response...)
|
|
}
|
|
|
|
if len(out.Stimulus) == 0 {
|
|
out.Stimulus = nil
|
|
}
|
|
if len(out.Response) == 0 {
|
|
out.Response = nil
|
|
}
|
|
if out.Stimulus == nil && out.Response == nil {
|
|
return nil
|
|
}
|
|
return out
|
|
}
|
|
|
|
// StimulusByBlockKey indexes blocks by block_key.
|
|
func StimulusByBlockKey(blocks []PracticeStimulusBlock) map[string][]DynamicElementInstance {
|
|
out := make(map[string][]DynamicElementInstance, len(blocks))
|
|
for _, block := range blocks {
|
|
out[block.BlockKey] = block.Stimulus
|
|
}
|
|
return out
|
|
}
|