Add associated_question_id on question set items so authors can group practice questions into ordered sections. Expose prerequisite_question_ids on read APIs for client-side gating, validate associations on save, and document admin panel integration. Co-authored-by: Cursor <cursoragent@cursor.com>
116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
var ErrInvalidQuestionAssociation = errors.New("invalid question association")
|
|
|
|
// QuestionSetMembership describes one question's placement and association within a set.
|
|
type QuestionSetMembership struct {
|
|
QuestionID int64
|
|
DisplayOrder int32
|
|
AssociatedQuestionID *int64
|
|
}
|
|
|
|
// ValidateQuestionSetAssociations checks association rules for all memberships in a set.
|
|
// associated_question_id must reference a section anchor (no association) in the same set
|
|
// with a lower display_order.
|
|
func ValidateQuestionSetAssociations(memberships []QuestionSetMembership) error {
|
|
byID := make(map[int64]QuestionSetMembership, len(memberships))
|
|
for _, m := range memberships {
|
|
byID[m.QuestionID] = m
|
|
}
|
|
|
|
for _, m := range memberships {
|
|
if m.AssociatedQuestionID == nil {
|
|
continue
|
|
}
|
|
assocID := *m.AssociatedQuestionID
|
|
if assocID == m.QuestionID {
|
|
return fmt.Errorf("question %d cannot be associated with itself", m.QuestionID)
|
|
}
|
|
assoc, ok := byID[assocID]
|
|
if !ok {
|
|
return fmt.Errorf("associated_question_id %d is not in the question set", assocID)
|
|
}
|
|
if assoc.AssociatedQuestionID != nil {
|
|
return fmt.Errorf("associated_question_id %d must reference a section anchor question", assocID)
|
|
}
|
|
if assoc.DisplayOrder >= m.DisplayOrder {
|
|
return fmt.Errorf("associated question %d must have a lower display_order than question %d", assocID, m.QuestionID)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PrerequisiteQuestionIDs returns question IDs that must be completed before accessing targetQuestionID.
|
|
func PrerequisiteQuestionIDs(memberships []QuestionSetMembership, targetQuestionID int64) []int64 {
|
|
byID := make(map[int64]QuestionSetMembership, len(memberships))
|
|
ordered := make([]QuestionSetMembership, 0, len(memberships))
|
|
for _, m := range memberships {
|
|
byID[m.QuestionID] = m
|
|
ordered = append(ordered, m)
|
|
}
|
|
target, ok := byID[targetQuestionID]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
seen := make(map[int64]struct{})
|
|
add := func(id int64) {
|
|
if id == targetQuestionID {
|
|
return
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
return
|
|
}
|
|
seen[id] = struct{}{}
|
|
}
|
|
|
|
if target.AssociatedQuestionID == nil {
|
|
for _, m := range ordered {
|
|
if m.DisplayOrder < target.DisplayOrder {
|
|
add(m.QuestionID)
|
|
}
|
|
}
|
|
} else {
|
|
anchorID := *target.AssociatedQuestionID
|
|
anchor := byID[anchorID]
|
|
for _, m := range ordered {
|
|
if m.DisplayOrder < anchor.DisplayOrder {
|
|
add(m.QuestionID)
|
|
}
|
|
}
|
|
for _, m := range ordered {
|
|
if m.QuestionID == anchorID || (m.AssociatedQuestionID != nil && *m.AssociatedQuestionID == anchorID) {
|
|
if m.DisplayOrder < target.DisplayOrder {
|
|
add(m.QuestionID)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
out := make([]int64, 0, len(seen))
|
|
for _, m := range ordered {
|
|
if _, ok := seen[m.QuestionID]; ok {
|
|
out = append(out, m.QuestionID)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// MembershipsFromQuestionSetItems converts set items to membership inputs for validation.
|
|
func MembershipsFromQuestionSetItems(items []QuestionSetItemWithQuestion) []QuestionSetMembership {
|
|
out := make([]QuestionSetMembership, len(items))
|
|
for i, item := range items {
|
|
out[i] = QuestionSetMembership{
|
|
QuestionID: item.QuestionID,
|
|
DisplayOrder: item.DisplayOrder,
|
|
AssociatedQuestionID: item.AssociatedQuestionID,
|
|
}
|
|
}
|
|
return out
|
|
}
|