diff --git a/internal/web_server/handlers/questions.go b/internal/web_server/handlers/questions.go index 629154b..6574a54 100644 --- a/internal/web_server/handlers/questions.go +++ b/internal/web_server/handlers/questions.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "fmt" - "regexp" "strconv" "strings" @@ -551,13 +550,25 @@ type listQuestionSetsRes struct { 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 { - if !initialAssessmentTitlePattern.MatchString(strings.TrimSpace(title)) { - return fmt.Errorf("title must match format: Level Test ()") +func normalizeInitialAssessmentLevel(description *string) (string, error) { + if description == nil { + 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 { @@ -616,12 +627,14 @@ func (h *Handler) CreateQuestionSet(c *fiber.Ctx) error { }) } 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{ - Message: "Invalid initial assessment title", + Message: "Invalid initial assessment level", Error: err.Error(), }) } + req.Description = &normalizedLevel if req.PassingScore == nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ 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)) { - effectiveTitle := existingSet.Title - if req.Title != nil { - effectiveTitle = *req.Title + effectiveDescription := existingSet.Description + if req.Description != nil { + effectiveDescription = req.Description } - if err := validateInitialAssessmentTitle(effectiveTitle); err != nil { + normalizedLevel, err := normalizeInitialAssessmentLevel(effectiveDescription) + if err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ - Message: "Invalid initial assessment title", + Message: "Invalid initial assessment level", Error: err.Error(), }) } + req.Description = &normalizedLevel effectivePassingScore := existingSet.PassingScore if req.PassingScore != nil {