Use initial assessment description as normalized level.

Made-with: Cursor
This commit is contained in:
Yared Yemane 2026-04-29 08:12:09 -07:00
parent 60290e5c34
commit eba2b87ed6

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -551,13 +550,25 @@ type listQuestionSetsRes struct {
TotalCount int64 `json:"total_count"` TotalCount int64 `json:"total_count"`
} }
var initialAssessmentTitlePattern = regexp.MustCompile(`^Level Test (A1|A2|B1|B2) \([^)]+\)$`) var validInitialAssessmentLevels = map[string]struct{}{
"A1": {},
"A2": {},
"B1": {},
"B2": {},
}
func validateInitialAssessmentTitle(title string) error { func normalizeInitialAssessmentLevel(description *string) (string, error) {
if !initialAssessmentTitlePattern.MatchString(strings.TrimSpace(title)) { if description == nil {
return fmt.Errorf("title must match format: Level Test <A1|A2|B1|B2> (<description>)") return "", fmt.Errorf("description is required and must be one of: A1, A2, B1, B2")
} }
return nil level := strings.ToUpper(strings.TrimSpace(*description))
if level == "" {
return "", fmt.Errorf("description is required and must be one of: A1, A2, B1, B2")
}
if _, ok := validInitialAssessmentLevels[level]; !ok {
return "", fmt.Errorf("description must be one of: A1, A2, B1, B2")
}
return level, nil
} }
func isSequenceGatedPractice(set domain.QuestionSet) bool { func isSequenceGatedPractice(set domain.QuestionSet) bool {
@ -616,12 +627,14 @@ func (h *Handler) CreateQuestionSet(c *fiber.Ctx) error {
}) })
} }
if strings.EqualFold(req.SetType, string(domain.QuestionSetTypeInitialAssessment)) { if strings.EqualFold(req.SetType, string(domain.QuestionSetTypeInitialAssessment)) {
if err := validateInitialAssessmentTitle(req.Title); err != nil { normalizedLevel, err := normalizeInitialAssessmentLevel(req.Description)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid initial assessment title", Message: "Invalid initial assessment level",
Error: err.Error(), Error: err.Error(),
}) })
} }
req.Description = &normalizedLevel
if req.PassingScore == nil { if req.PassingScore == nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid initial assessment set", Message: "Invalid initial assessment set",
@ -929,16 +942,18 @@ func (h *Handler) UpdateQuestionSet(c *fiber.Ctx) error {
} }
if strings.EqualFold(existingSet.SetType, string(domain.QuestionSetTypeInitialAssessment)) { if strings.EqualFold(existingSet.SetType, string(domain.QuestionSetTypeInitialAssessment)) {
effectiveTitle := existingSet.Title effectiveDescription := existingSet.Description
if req.Title != nil { if req.Description != nil {
effectiveTitle = *req.Title effectiveDescription = req.Description
} }
if err := validateInitialAssessmentTitle(effectiveTitle); err != nil { normalizedLevel, err := normalizeInitialAssessmentLevel(effectiveDescription)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Message: "Invalid initial assessment title", Message: "Invalid initial assessment level",
Error: err.Error(), Error: err.Error(),
}) })
} }
req.Description = &normalizedLevel
effectivePassingScore := existingSet.PassingScore effectivePassingScore := existingSet.PassingScore
if req.PassingScore != nil { if req.PassingScore != nil {