|
| 1 | +package plexams |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + set "github.com/deckarep/golang-set/v2" |
| 10 | + "github.com/logrusorgru/aurora" |
| 11 | + "github.com/rs/zerolog/log" |
| 12 | + "github.com/theckman/yacspin" |
| 13 | +) |
| 14 | + |
| 15 | +// TODO: all planned_rooms okay? especially after moving an exam? check room -> slot -> ancode sameslot? |
| 16 | +func (p *Plexams) ValidateStudentRegs() error { |
| 17 | + ctx := context.Background() |
| 18 | + cfg := yacspin.Config{ |
| 19 | + Frequency: 100 * time.Millisecond, |
| 20 | + CharSet: yacspin.CharSets[69], |
| 21 | + Suffix: aurora.Sprintf(aurora.Cyan(" validating student regs")), |
| 22 | + SuffixAutoColon: true, |
| 23 | + StopCharacter: "✓", |
| 24 | + StopColors: []string{"fgGreen"}, |
| 25 | + StopFailMessage: "error", |
| 26 | + StopFailCharacter: "✗", |
| 27 | + StopFailColors: []string{"fgRed"}, |
| 28 | + } |
| 29 | + |
| 30 | + spinner, err := yacspin.New(cfg) |
| 31 | + if err != nil { |
| 32 | + log.Debug().Err(err).Msg("cannot create spinner") |
| 33 | + } |
| 34 | + err = spinner.Start() |
| 35 | + if err != nil { |
| 36 | + log.Debug().Err(err).Msg("cannot start spinner") |
| 37 | + } |
| 38 | + |
| 39 | + validationMessages := make([]string, 0) |
| 40 | + |
| 41 | + studentRegs, err := p.dbClient.StudentRegsPerStudentPlanned(ctx) |
| 42 | + if err != nil { |
| 43 | + log.Error().Err(err).Msg("cannot get student regs") |
| 44 | + } |
| 45 | + |
| 46 | + spinner.Message(aurora.Sprintf(aurora.Yellow(" validating only regs from one program per student"))) |
| 47 | + for _, studentReg := range studentRegs { |
| 48 | + programs := set.NewSet[string]() |
| 49 | + for _, reg := range studentReg.RegsWithProgram { |
| 50 | + programs.Add(reg.Program) |
| 51 | + } |
| 52 | + if programs.Cardinality() > 1 { |
| 53 | + var sb strings.Builder |
| 54 | + for _, reg := range studentReg.RegsWithProgram { |
| 55 | + zpaExam, err := p.dbClient.GetZpaExamByAncode(ctx, reg.Reg) |
| 56 | + if err != nil { |
| 57 | + log.Error().Err(err).Int("ancode", reg.Reg). |
| 58 | + Msg("cannot get zpa exam for student reg") |
| 59 | + continue |
| 60 | + } |
| 61 | + sb.WriteString(fmt.Sprintf("%s/%d: %s (%s)\n", reg.Program, zpaExam.AnCode, zpaExam.Module, zpaExam.MainExamer)) |
| 62 | + } |
| 63 | + |
| 64 | + validationMessages = append(validationMessages, aurora.Sprintf( |
| 65 | + aurora.Red("regs from more than one program for student %s (%s/%s): %v\n%s"), |
| 66 | + aurora.Magenta(studentReg.Name), |
| 67 | + aurora.Cyan(studentReg.Program), aurora.Cyan(studentReg.Mtknr), |
| 68 | + aurora.Yellow(programs.ToSlice()), |
| 69 | + aurora.Yellow(sb.String()), |
| 70 | + )) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if len(validationMessages) > 0 { |
| 75 | + spinner.StopFailMessage(aurora.Sprintf(aurora.Red("%d problems"), |
| 76 | + len(validationMessages))) |
| 77 | + err = spinner.StopFail() |
| 78 | + if err != nil { |
| 79 | + log.Debug().Err(err).Msg("cannot stop spinner") |
| 80 | + } |
| 81 | + for _, msg := range validationMessages { |
| 82 | + fmt.Printf("%s\n", msg) |
| 83 | + } |
| 84 | + |
| 85 | + } else { |
| 86 | + spinner.StopMessage(aurora.Sprintf(aurora.Green("%d student registrations are okay"), |
| 87 | + len(studentRegs))) |
| 88 | + err = spinner.Stop() |
| 89 | + if err != nil { |
| 90 | + log.Debug().Err(err).Msg("cannot stop spinner") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
0 commit comments