quiz resetc

This commit is contained in:
Krispire Admin
2026-10-15 20:59:53 +05:30
parent 4b38047c26
commit b1f78b1655
8 changed files with 70 additions and 7 deletions
+10
View File
@@ -269,6 +269,16 @@ func (s *QuizService) ParticipantResult(ctx context.Context, quizID, userID stri
return s.quizzes.Result(ctx, quizID, userID, true)
}
// ResetParticipant wipes one participant's attempt; the quiz becomes pending
// for them. Works on unpublished quizzes too — staff may tidy results up
// while a quiz is offline.
func (s *QuizService) ResetParticipant(ctx context.Context, quizID, userID string) error {
if _, err := s.quizzes.Get(ctx, quizID, false); err != nil {
return err
}
return s.quizzes.ResetAttempt(ctx, quizID, userID)
}
// ── Taking ────────────────────────────────────────────────────────────────
// published loads a quiz that users may see.
+18
View File
@@ -278,6 +278,24 @@ func TestQuiz_TakingFlow(t *testing.T) {
t.Errorf("responses = %+v, %v", stats, err)
}
// Staff can reset one participant: their attempt and answers go, the quiz
// is pending for them again, and they can retake it from the start.
if err := svc.ResetParticipant(ctx, quiz.ID, other); !errors.Is(err, repository.ErrQuizNotStarted) {
t.Errorf("resetting someone with no attempt: %v, want ErrQuizNotStarted", err)
}
if err := svc.ResetParticipant(ctx, quiz.ID, user); err != nil {
t.Fatal(err)
}
if card, _ := svc.Card(ctx, user, quiz.ID); card.My.State != model.QuizNotStarted {
t.Errorf("state after reset = %s, want not_started", card.My.State)
}
if parts, _ := svc.Participants(ctx, quiz.ID); len(parts) != 0 {
t.Errorf("participants after reset = %d, want 0", len(parts))
}
if again, err := svc.Start(ctx, user, quiz.ID); err != nil || again.Answered != 0 || again.Question == nil {
t.Errorf("retaking after reset: %+v, %v", again, err)
}
// Taken, so questions are frozen — but the title can still change.
if _, err := svc.Update(ctx, quiz.ID, QuizDraft{Title: "x", Questions: []repository.QuestionInput{q("p", 0, "a", "b")}}); !errors.Is(err, ErrQuizLocked) {
t.Errorf("editing questions after attempts: %v, want ErrQuizLocked", err)