fix: close expired test attempts at the attempt limit
This commit is contained in:
@@ -286,6 +286,9 @@ func (s *Service) Start(ctx context.Context, userID uint, slug, category string)
|
||||
|
||||
func (s *Service) startTest(ctx context.Context, userID uint, quiz *models.Quiz) (*Started, error) {
|
||||
var st *Started
|
||||
// limited refuses the start after the commit, so the expired attempts
|
||||
// closed on the way stay closed (an error would roll the closes back).
|
||||
var limited bool
|
||||
err := lagoon.Transaction(ctx, s.DB, func(ctx context.Context, tx *gorm.DB) error {
|
||||
tx = tx.WithContext(ctx)
|
||||
if err := tx.Exec(`SELECT pg_advisory_xact_lock(hashtext(?))`, AttemptLockKey(quiz.ID, userID)).Error; err != nil {
|
||||
@@ -327,7 +330,8 @@ func (s *Service) startTest(ctx context.Context, userID uint, quiz *models.Quiz)
|
||||
return nil
|
||||
}
|
||||
if left := AttemptsLeft(quiz, int(used)); left != nil && *left == 0 {
|
||||
return ErrAttemptLimit
|
||||
limited = true
|
||||
return nil
|
||||
}
|
||||
attempt := &models.Attempt{QuizID: quiz.ID, UserID: userID, StartedAt: now.UTC()}
|
||||
maxPoints, err := s.maxPoints(ctx, tx, attempt)
|
||||
@@ -344,6 +348,9 @@ func (s *Service) startTest(ctx context.Context, userID uint, quiz *models.Quiz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if limited {
|
||||
return nil, ErrAttemptLimit
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user