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"
"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 <A1|A2|B1|B2> (<description>)")
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 {