feat: normalize legacy question responses to dynamic_payload shape
Synthesize dynamic_payload from flat legacy fields on outbound API responses so AUDIO, MCQ, and SHORT_ANSWER questions match DYNAMIC question structure. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
d5ef5ace51
commit
a442c1bd72
285
internal/domain/question_outbound_payload.go
Normal file
285
internal/domain/question_outbound_payload.go
Normal file
|
|
@ -0,0 +1,285 @@
|
||||||
|
package domain
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ResolveOutboundDynamicPayload returns the dynamic_payload clients should render.
|
||||||
|
// Stored dynamic_payload is returned as-is; legacy rows are synthesized from flat columns.
|
||||||
|
func ResolveOutboundDynamicPayload(q QuestionWithDetails, catalog []QuestionTypeDefinition) *DynamicQuestionPayload {
|
||||||
|
if hasDynamicPayloadContent(q.DynamicPayload) {
|
||||||
|
return q.DynamicPayload
|
||||||
|
}
|
||||||
|
if UsesDynamicQuestionPayload(q.QuestionType) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
def := ResolveQuestionTypeDefinitionForQuestion(q.QuestionTypeDefinitionID, q.QuestionType, catalog)
|
||||||
|
payload := buildLegacyDynamicPayload(q, def)
|
||||||
|
if !hasDynamicPayloadContent(payload) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasDynamicPayloadContent(payload *DynamicQuestionPayload) bool {
|
||||||
|
return payload != nil && (len(payload.Stimulus) > 0 || len(payload.Response) > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildLegacyDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
||||||
|
switch strings.ToUpper(strings.TrimSpace(q.QuestionType)) {
|
||||||
|
case "AUDIO":
|
||||||
|
return buildLegacyAudioDynamicPayload(q, def)
|
||||||
|
case "MCQ", "TRUE_FALSE":
|
||||||
|
return buildLegacyMCQDynamicPayload(q, def)
|
||||||
|
case "SHORT_ANSWER":
|
||||||
|
return buildLegacyShortAnswerDynamicPayload(q, def)
|
||||||
|
default:
|
||||||
|
return buildLegacyGenericDynamicPayload(q, def)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildLegacyAudioDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
||||||
|
var stimulus []DynamicElementInstance
|
||||||
|
|
||||||
|
questionTextID := schemaElementID(def.StimulusSchema, string(StimulusQuestionText), "question_text_1")
|
||||||
|
voicePromptID := schemaElementID(def.StimulusSchema, string(StimulusAudioPrompt), "voice_prompt_1")
|
||||||
|
sampleVoiceID := schemaElementIDBySuffix(def.StimulusSchema, string(StimulusAudioPrompt), "sample_answer_voice_prompt_1", voicePromptID)
|
||||||
|
audioResponseID := schemaElementID(def.ResponseSchema, string(ResponseAudioResponse), "audio_response_1")
|
||||||
|
|
||||||
|
if text := strings.TrimSpace(q.QuestionText); text != "" {
|
||||||
|
stimulus = append(stimulus, DynamicElementInstance{
|
||||||
|
ID: questionTextID,
|
||||||
|
Kind: string(StimulusQuestionText),
|
||||||
|
Value: text,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if q.VoicePrompt != nil {
|
||||||
|
if value := strings.TrimSpace(*q.VoicePrompt); value != "" {
|
||||||
|
stimulus = append(stimulus, DynamicElementInstance{
|
||||||
|
ID: voicePromptID,
|
||||||
|
Kind: string(StimulusAudioPrompt),
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if q.SampleAnswerVoicePrompt != nil {
|
||||||
|
if value := strings.TrimSpace(*q.SampleAnswerVoicePrompt); value != "" {
|
||||||
|
stimulus = append(stimulus, DynamicElementInstance{
|
||||||
|
ID: sampleVoiceID,
|
||||||
|
Kind: string(StimulusAudioPrompt),
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if q.ImageURL != nil {
|
||||||
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
||||||
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
||||||
|
stimulus = append(stimulus, DynamicElementInstance{
|
||||||
|
ID: imageID,
|
||||||
|
Kind: string(StimulusImage),
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var response []DynamicElementInstance
|
||||||
|
if value := legacyAudioResponseValue(q); value != "" {
|
||||||
|
response = append(response, DynamicElementInstance{
|
||||||
|
ID: audioResponseID,
|
||||||
|
Kind: string(ResponseAudioResponse),
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return &DynamicQuestionPayload{Stimulus: stimulus, Response: response}
|
||||||
|
}
|
||||||
|
|
||||||
|
func legacyAudioResponseValue(q QuestionWithDetails) string {
|
||||||
|
if q.SampleAnswerVoicePrompt != nil {
|
||||||
|
if value := strings.TrimSpace(*q.SampleAnswerVoicePrompt); value != "" {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if q.AudioAnswer != nil {
|
||||||
|
if value := strings.TrimSpace(q.AudioAnswer.CorrectAnswerText); value != "" {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildLegacyMCQDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
||||||
|
stimulus := legacyTextStimulus(q, def)
|
||||||
|
if q.ImageURL != nil {
|
||||||
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
||||||
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
||||||
|
stimulus = append(stimulus, DynamicElementInstance{
|
||||||
|
ID: imageID,
|
||||||
|
Kind: string(StimulusImage),
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var response []DynamicElementInstance
|
||||||
|
if len(q.Options) > 0 {
|
||||||
|
mcqID := schemaElementID(def.ResponseSchema, string(ResponseMultipleChoice), "multiple_choice_1")
|
||||||
|
options := make([]map[string]interface{}, 0, len(q.Options))
|
||||||
|
for _, opt := range q.Options {
|
||||||
|
options = append(options, map[string]interface{}{
|
||||||
|
"id": opt.ID,
|
||||||
|
"option_text": opt.OptionText,
|
||||||
|
"option_order": opt.OptionOrder,
|
||||||
|
"is_correct": opt.IsCorrect,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
response = append(response, DynamicElementInstance{
|
||||||
|
ID: mcqID,
|
||||||
|
Kind: string(ResponseMultipleChoice),
|
||||||
|
Value: options,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return &DynamicQuestionPayload{Stimulus: stimulus, Response: response}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildLegacyShortAnswerDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
||||||
|
stimulus := legacyTextStimulus(q, def)
|
||||||
|
if q.ImageURL != nil {
|
||||||
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
||||||
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
||||||
|
stimulus = append(stimulus, DynamicElementInstance{
|
||||||
|
ID: imageID,
|
||||||
|
Kind: string(StimulusImage),
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var response []DynamicElementInstance
|
||||||
|
if len(q.ShortAnswers) > 0 {
|
||||||
|
shortID := schemaElementID(def.ResponseSchema, string(ResponseShortAnswer), "short_answer_1")
|
||||||
|
answers := make([]map[string]interface{}, 0, len(q.ShortAnswers))
|
||||||
|
for _, sa := range q.ShortAnswers {
|
||||||
|
answers = append(answers, map[string]interface{}{
|
||||||
|
"id": sa.ID,
|
||||||
|
"acceptable_answer": sa.AcceptableAnswer,
|
||||||
|
"match_type": sa.MatchType,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
response = append(response, DynamicElementInstance{
|
||||||
|
ID: shortID,
|
||||||
|
Kind: string(ResponseShortAnswer),
|
||||||
|
Value: answers,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return &DynamicQuestionPayload{Stimulus: stimulus, Response: response}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildLegacyGenericDynamicPayload(q QuestionWithDetails, def QuestionTypeDefinition) *DynamicQuestionPayload {
|
||||||
|
stimulus := legacyTextStimulus(q, def)
|
||||||
|
if q.ImageURL != nil {
|
||||||
|
if value := strings.TrimSpace(*q.ImageURL); value != "" {
|
||||||
|
imageID := schemaElementID(def.StimulusSchema, string(StimulusImage), "image_1")
|
||||||
|
stimulus = append(stimulus, DynamicElementInstance{
|
||||||
|
ID: imageID,
|
||||||
|
Kind: string(StimulusImage),
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &DynamicQuestionPayload{Stimulus: stimulus}
|
||||||
|
}
|
||||||
|
|
||||||
|
func legacyTextStimulus(q QuestionWithDetails, def QuestionTypeDefinition) []DynamicElementInstance {
|
||||||
|
text := strings.TrimSpace(q.QuestionText)
|
||||||
|
if text == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
kind, fallbackID := legacyTextStimulusKindAndID(def)
|
||||||
|
return []DynamicElementInstance{{
|
||||||
|
ID: schemaElementID(def.StimulusSchema, kind, fallbackID),
|
||||||
|
Kind: kind,
|
||||||
|
Value: text,
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func legacyTextStimulusKindAndID(def QuestionTypeDefinition) (kind string, fallbackID string) {
|
||||||
|
for _, k := range []string{string(StimulusQuestionText), string(StimulusInstruction), string(StimulusTextPassage)} {
|
||||||
|
if id := schemaElementID(def.StimulusSchema, k, ""); id != "" {
|
||||||
|
return k, id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(StimulusQuestionText), "question_text_1"
|
||||||
|
}
|
||||||
|
|
||||||
|
func schemaElementID(schema []DynamicElementDefinition, kind string, fallback string) string {
|
||||||
|
kind = strings.TrimSpace(kind)
|
||||||
|
for _, el := range schema {
|
||||||
|
if strings.TrimSpace(el.Kind) == kind {
|
||||||
|
if id := strings.TrimSpace(el.ID); id != "" {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
func schemaElementIDBySuffix(schema []DynamicElementDefinition, kind, preferred, fallback string) string {
|
||||||
|
kind = strings.TrimSpace(kind)
|
||||||
|
for _, el := range schema {
|
||||||
|
if strings.TrimSpace(el.Kind) != kind {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
id := strings.TrimSpace(el.ID)
|
||||||
|
if id == preferred {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if id := schemaElementID(schema, kind, ""); id != "" && id != fallback {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
return preferred
|
||||||
|
}
|
||||||
|
|
||||||
|
// UsesOutboundDynamicShape reports whether API responses should omit legacy flat fields.
|
||||||
|
func UsesOutboundDynamicShape(payload *DynamicQuestionPayload) bool {
|
||||||
|
return hasDynamicPayloadContent(payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutboundQuestionContent is the normalized question body for API responses.
|
||||||
|
type OutboundQuestionContent struct {
|
||||||
|
DynamicPayload *DynamicQuestionPayload
|
||||||
|
QuestionText *string
|
||||||
|
VoicePrompt *string
|
||||||
|
SampleAnswerVoicePrompt *string
|
||||||
|
ImageURL *string
|
||||||
|
AudioCorrectAnswerText *string
|
||||||
|
Options []QuestionOption
|
||||||
|
ShortAnswers []QuestionShortAnswer
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveOutboundQuestionContent normalizes legacy rows to dynamic_payload for API responses.
|
||||||
|
func ResolveOutboundQuestionContent(q QuestionWithDetails, catalog []QuestionTypeDefinition) OutboundQuestionContent {
|
||||||
|
payload := ResolveOutboundDynamicPayload(q, catalog)
|
||||||
|
if !UsesOutboundDynamicShape(payload) {
|
||||||
|
var audioCorrect *string
|
||||||
|
if q.AudioAnswer != nil && strings.TrimSpace(q.AudioAnswer.CorrectAnswerText) != "" {
|
||||||
|
text := q.AudioAnswer.CorrectAnswerText
|
||||||
|
audioCorrect = &text
|
||||||
|
}
|
||||||
|
questionText := QuestionTextJSONField(q.QuestionType, q.QuestionText)
|
||||||
|
return OutboundQuestionContent{
|
||||||
|
DynamicPayload: payload,
|
||||||
|
QuestionText: questionText,
|
||||||
|
VoicePrompt: q.VoicePrompt,
|
||||||
|
SampleAnswerVoicePrompt: q.SampleAnswerVoicePrompt,
|
||||||
|
ImageURL: q.ImageURL,
|
||||||
|
AudioCorrectAnswerText: audioCorrect,
|
||||||
|
Options: q.Options,
|
||||||
|
ShortAnswers: q.ShortAnswers,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return OutboundQuestionContent{DynamicPayload: payload}
|
||||||
|
}
|
||||||
93
internal/domain/question_outbound_payload_test.go
Normal file
93
internal/domain/question_outbound_payload_test.go
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
package domain
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestResolveOutboundDynamicPayload_legacyAUDIO(t *testing.T) {
|
||||||
|
catalog := []QuestionTypeDefinition{
|
||||||
|
{
|
||||||
|
ID: 37,
|
||||||
|
Key: "audio_conversation_type",
|
||||||
|
StimulusSchema: []DynamicElementDefinition{
|
||||||
|
{ID: "question_text_1", Kind: "QUESTION_TEXT"},
|
||||||
|
{ID: "voice_prompt_1", Kind: "AUDIO_PROMPT"},
|
||||||
|
{ID: "sample_answer_voice_prompt_1", Kind: "AUDIO_PROMPT"},
|
||||||
|
},
|
||||||
|
ResponseSchema: []DynamicElementDefinition{
|
||||||
|
{ID: "audio_response_1", Kind: "AUDIO_RESPONSE"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
voice := "https://example.com/voice.mp3"
|
||||||
|
sample := "https://example.com/sample.mp3"
|
||||||
|
q := QuestionWithDetails{
|
||||||
|
Question: Question{
|
||||||
|
QuestionText: "How is the weather?",
|
||||||
|
QuestionType: "AUDIO",
|
||||||
|
QuestionTypeDefinitionID: int64Ptr(37),
|
||||||
|
VoicePrompt: &voice,
|
||||||
|
SampleAnswerVoicePrompt: &sample,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
got := ResolveOutboundDynamicPayload(q, catalog)
|
||||||
|
if got == nil {
|
||||||
|
t.Fatal("expected synthesized payload")
|
||||||
|
}
|
||||||
|
if len(got.Stimulus) != 3 {
|
||||||
|
t.Fatalf("expected 3 stimulus elements, got %d", len(got.Stimulus))
|
||||||
|
}
|
||||||
|
if got.Stimulus[0].Kind != "QUESTION_TEXT" || got.Stimulus[0].Value != "How is the weather?" {
|
||||||
|
t.Fatalf("unexpected question text stimulus: %+v", got.Stimulus[0])
|
||||||
|
}
|
||||||
|
if got.Stimulus[1].ID != "voice_prompt_1" || got.Stimulus[1].Value != voice {
|
||||||
|
t.Fatalf("unexpected voice prompt: %+v", got.Stimulus[1])
|
||||||
|
}
|
||||||
|
if got.Stimulus[2].ID != "sample_answer_voice_prompt_1" || got.Stimulus[2].Value != sample {
|
||||||
|
t.Fatalf("unexpected sample voice prompt: %+v", got.Stimulus[2])
|
||||||
|
}
|
||||||
|
if len(got.Response) != 1 || got.Response[0].Kind != "AUDIO_RESPONSE" || got.Response[0].Value != sample {
|
||||||
|
t.Fatalf("unexpected response: %+v", got.Response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveOutboundDynamicPayload_storedDynamicPreferred(t *testing.T) {
|
||||||
|
stored := &DynamicQuestionPayload{
|
||||||
|
Stimulus: []DynamicElementInstance{{ID: "question_text_1", Kind: "QUESTION_TEXT", Value: "Stored"}},
|
||||||
|
}
|
||||||
|
q := QuestionWithDetails{
|
||||||
|
Question: Question{
|
||||||
|
QuestionText: "Legacy text",
|
||||||
|
QuestionType: "DYNAMIC",
|
||||||
|
DynamicPayload: stored,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := ResolveOutboundDynamicPayload(q, nil)
|
||||||
|
if got != stored {
|
||||||
|
t.Fatal("expected stored payload to be returned unchanged")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveOutboundDynamicPayload_legacyMCQ(t *testing.T) {
|
||||||
|
q := QuestionWithDetails{
|
||||||
|
Question: Question{
|
||||||
|
QuestionText: "Pick one",
|
||||||
|
QuestionType: "MCQ",
|
||||||
|
},
|
||||||
|
Options: []QuestionOption{
|
||||||
|
{ID: 1, OptionText: "A", OptionOrder: 1, IsCorrect: true},
|
||||||
|
{ID: 2, OptionText: "B", OptionOrder: 2, IsCorrect: false},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := ResolveOutboundDynamicPayload(q, testQuestionTypeCatalog())
|
||||||
|
if got == nil {
|
||||||
|
t.Fatal("expected synthesized payload")
|
||||||
|
}
|
||||||
|
if len(got.Stimulus) != 1 || got.Stimulus[0].Kind != "QUESTION_TEXT" {
|
||||||
|
t.Fatalf("unexpected stimulus: %+v", got.Stimulus)
|
||||||
|
}
|
||||||
|
if len(got.Response) != 1 || got.Response[0].Kind != "MULTIPLE_CHOICE" {
|
||||||
|
t.Fatalf("unexpected response: %+v", got.Response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func int64Ptr(v int64) *int64 { return &v }
|
||||||
|
|
@ -34,12 +34,13 @@ func fullPracticeQuestionResponses(
|
||||||
res.AssociatedQuestionID = item.AssociatedQuestionID
|
res.AssociatedQuestionID = item.AssociatedQuestionID
|
||||||
res.StimulusBlockKey = item.StimulusBlockKey
|
res.StimulusBlockKey = item.StimulusBlockKey
|
||||||
res.PrerequisiteQuestionIDs = domain.PrerequisiteQuestionIDs(memberships, question.ID)
|
res.PrerequisiteQuestionIDs = domain.PrerequisiteQuestionIDs(memberships, question.ID)
|
||||||
|
payloadForMerge := res.DynamicPayload
|
||||||
if item.StimulusBlockKey != nil {
|
if item.StimulusBlockKey != nil {
|
||||||
if blockStimulus, ok := blockStimulusByKey[*item.StimulusBlockKey]; ok {
|
if blockStimulus, ok := blockStimulusByKey[*item.StimulusBlockKey]; ok {
|
||||||
res.EffectiveDynamicPayload = domain.MergeEffectiveDynamicPayload(blockStimulus, question.DynamicPayload)
|
res.EffectiveDynamicPayload = domain.MergeEffectiveDynamicPayload(blockStimulus, payloadForMerge)
|
||||||
}
|
}
|
||||||
} else if question.DynamicPayload != nil {
|
} else if payloadForMerge != nil {
|
||||||
res.EffectiveDynamicPayload = domain.MergeEffectiveDynamicPayload(nil, question.DynamicPayload)
|
res.EffectiveDynamicPayload = domain.MergeEffectiveDynamicPayload(nil, payloadForMerge)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out = append(out, res)
|
out = append(out, res)
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,10 @@ func (h *Handler) activeQuestionTypeDefinitionCatalog(ctx context.Context) ([]do
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildQuestionRes(q domain.QuestionWithDetails, catalog []domain.QuestionTypeDefinition) questionRes {
|
func buildQuestionRes(q domain.QuestionWithDetails, catalog []domain.QuestionTypeDefinition) questionRes {
|
||||||
options := make([]optionRes, 0, len(q.Options))
|
outbound := domain.ResolveOutboundQuestionContent(q, catalog)
|
||||||
for _, opt := range q.Options {
|
|
||||||
|
options := make([]optionRes, 0, len(outbound.Options))
|
||||||
|
for _, opt := range outbound.Options {
|
||||||
options = append(options, optionRes{
|
options = append(options, optionRes{
|
||||||
ID: opt.ID,
|
ID: opt.ID,
|
||||||
OptionText: opt.OptionText,
|
OptionText: opt.OptionText,
|
||||||
|
|
@ -101,8 +103,8 @@ func buildQuestionRes(q domain.QuestionWithDetails, catalog []domain.QuestionTyp
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
shortAnswers := make([]shortAnswerRes, 0, len(q.ShortAnswers))
|
shortAnswers := make([]shortAnswerRes, 0, len(outbound.ShortAnswers))
|
||||||
for _, sa := range q.ShortAnswers {
|
for _, sa := range outbound.ShortAnswers {
|
||||||
shortAnswers = append(shortAnswers, shortAnswerRes{
|
shortAnswers = append(shortAnswers, shortAnswerRes{
|
||||||
ID: sa.ID,
|
ID: sa.ID,
|
||||||
AcceptableAnswer: sa.AcceptableAnswer,
|
AcceptableAnswer: sa.AcceptableAnswer,
|
||||||
|
|
@ -110,34 +112,38 @@ func buildQuestionRes(q domain.QuestionWithDetails, catalog []domain.QuestionTyp
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var audioCorrectAnswerText *string
|
var optionsPtr []optionRes
|
||||||
if q.AudioAnswer != nil {
|
if len(options) > 0 {
|
||||||
audioCorrectAnswerText = &q.AudioAnswer.CorrectAnswerText
|
optionsPtr = options
|
||||||
|
}
|
||||||
|
var shortAnswersPtr []shortAnswerRes
|
||||||
|
if len(shortAnswers) > 0 {
|
||||||
|
shortAnswersPtr = shortAnswers
|
||||||
}
|
}
|
||||||
|
|
||||||
return questionRes{
|
return questionRes{
|
||||||
ID: q.ID,
|
ID: q.ID,
|
||||||
QuestionText: questionTextField(q.QuestionType, q.QuestionText),
|
QuestionText: outbound.QuestionText,
|
||||||
QuestionType: q.QuestionType,
|
QuestionType: q.QuestionType,
|
||||||
QuestionTypeDefinitionID: domain.ResolveEffectiveQuestionTypeDefinitionID(
|
QuestionTypeDefinitionID: domain.ResolveEffectiveQuestionTypeDefinitionID(
|
||||||
q.QuestionTypeDefinitionID,
|
q.QuestionTypeDefinitionID,
|
||||||
q.QuestionType,
|
q.QuestionType,
|
||||||
catalog,
|
catalog,
|
||||||
q.DynamicPayload,
|
outbound.DynamicPayload,
|
||||||
),
|
),
|
||||||
DynamicPayload: q.DynamicPayload,
|
DynamicPayload: outbound.DynamicPayload,
|
||||||
DifficultyLevel: q.DifficultyLevel,
|
DifficultyLevel: q.DifficultyLevel,
|
||||||
Points: q.Points,
|
Points: q.Points,
|
||||||
Explanation: q.Explanation,
|
Explanation: q.Explanation,
|
||||||
Tips: q.Tips,
|
Tips: q.Tips,
|
||||||
VoicePrompt: q.VoicePrompt,
|
VoicePrompt: outbound.VoicePrompt,
|
||||||
SampleAnswerVoicePrompt: q.SampleAnswerVoicePrompt,
|
SampleAnswerVoicePrompt: outbound.SampleAnswerVoicePrompt,
|
||||||
ImageURL: q.ImageURL,
|
ImageURL: outbound.ImageURL,
|
||||||
Status: q.Status,
|
Status: q.Status,
|
||||||
CreatedAt: q.CreatedAt.String(),
|
CreatedAt: q.CreatedAt.String(),
|
||||||
Options: options,
|
Options: optionsPtr,
|
||||||
ShortAnswers: shortAnswers,
|
ShortAnswers: shortAnswersPtr,
|
||||||
AudioCorrectAnswerText: audioCorrectAnswerText,
|
AudioCorrectAnswerText: outbound.AudioCorrectAnswerText,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1307,10 +1313,30 @@ type paginatedQuestionSetItemsRes struct {
|
||||||
Offset int32 `json:"offset"`
|
Offset int32 `json:"offset"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func questionSetItemsToRes(items []domain.QuestionSetItemWithQuestion) []questionSetItemRes {
|
func questionSetItemsToRes(items []domain.QuestionSetItemWithQuestion, catalog []domain.QuestionTypeDefinition) []questionSetItemRes {
|
||||||
memberships := domain.MembershipsFromQuestionSetItems(items)
|
memberships := domain.MembershipsFromQuestionSetItems(items)
|
||||||
out := make([]questionSetItemRes, 0, len(items))
|
out := make([]questionSetItemRes, 0, len(items))
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
|
q := domain.QuestionWithDetails{
|
||||||
|
Question: domain.Question{
|
||||||
|
QuestionText: item.QuestionText,
|
||||||
|
QuestionType: item.QuestionType,
|
||||||
|
DynamicPayload: item.DynamicPayload,
|
||||||
|
DifficultyLevel: item.DifficultyLevel,
|
||||||
|
Points: item.Points,
|
||||||
|
Explanation: item.Explanation,
|
||||||
|
Tips: item.Tips,
|
||||||
|
VoicePrompt: item.VoicePrompt,
|
||||||
|
SampleAnswerVoicePrompt: item.SampleAnswerVoicePrompt,
|
||||||
|
ImageURL: item.ImageURL,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if item.AudioCorrectAnswerText != nil && strings.TrimSpace(*item.AudioCorrectAnswerText) != "" {
|
||||||
|
text := strings.TrimSpace(*item.AudioCorrectAnswerText)
|
||||||
|
q.AudioAnswer = &domain.QuestionAudioAnswer{CorrectAnswerText: text}
|
||||||
|
}
|
||||||
|
outbound := domain.ResolveOutboundQuestionContent(q, catalog)
|
||||||
|
|
||||||
out = append(out, questionSetItemRes{
|
out = append(out, questionSetItemRes{
|
||||||
ID: item.ID,
|
ID: item.ID,
|
||||||
SetID: item.SetID,
|
SetID: item.SetID,
|
||||||
|
|
@ -1318,17 +1344,17 @@ func questionSetItemsToRes(items []domain.QuestionSetItemWithQuestion) []questio
|
||||||
DisplayOrder: item.DisplayOrder,
|
DisplayOrder: item.DisplayOrder,
|
||||||
AssociatedQuestionID: item.AssociatedQuestionID,
|
AssociatedQuestionID: item.AssociatedQuestionID,
|
||||||
PrerequisiteQuestionIDs: domain.PrerequisiteQuestionIDs(memberships, item.QuestionID),
|
PrerequisiteQuestionIDs: domain.PrerequisiteQuestionIDs(memberships, item.QuestionID),
|
||||||
QuestionText: questionTextField(item.QuestionType, item.QuestionText),
|
QuestionText: outbound.QuestionText,
|
||||||
QuestionType: item.QuestionType,
|
QuestionType: item.QuestionType,
|
||||||
DynamicPayload: item.DynamicPayload,
|
DynamicPayload: outbound.DynamicPayload,
|
||||||
DifficultyLevel: item.DifficultyLevel,
|
DifficultyLevel: item.DifficultyLevel,
|
||||||
Points: item.Points,
|
Points: item.Points,
|
||||||
Explanation: item.Explanation,
|
Explanation: item.Explanation,
|
||||||
Tips: item.Tips,
|
Tips: item.Tips,
|
||||||
VoicePrompt: item.VoicePrompt,
|
VoicePrompt: outbound.VoicePrompt,
|
||||||
SampleAnswerVoicePrompt: item.SampleAnswerVoicePrompt,
|
SampleAnswerVoicePrompt: outbound.SampleAnswerVoicePrompt,
|
||||||
ImageURL: item.ImageURL,
|
ImageURL: outbound.ImageURL,
|
||||||
AudioCorrectAnswerText: item.AudioCorrectAnswerText,
|
AudioCorrectAnswerText: outbound.AudioCorrectAnswerText,
|
||||||
QuestionStatus: item.QuestionStatus,
|
QuestionStatus: item.QuestionStatus,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -1621,7 +1647,15 @@ func (h *Handler) GetQuestionsByPractice(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
itemResponses := questionSetItemsToRes(items)
|
catalog, err := h.activeQuestionTypeDefinitionCatalog(c.Context())
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||||
|
Message: "Failed to load question type catalog",
|
||||||
|
Error: err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
itemResponses := questionSetItemsToRes(items, catalog)
|
||||||
|
|
||||||
return c.JSON(domain.Response{
|
return c.JSON(domain.Response{
|
||||||
Message: "Practice questions retrieved successfully",
|
Message: "Practice questions retrieved successfully",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user