Skip to content

Commit

Permalink
allow duplicated req with last question to return next question
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-cronus committed Feb 8, 2024
1 parent 401dd0e commit 578178e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
6 changes: 3 additions & 3 deletions kyc/quiz/.testdata/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ logger:
users: &users
wintr/connectors/storage/v2:
runDDL: true
primaryURL: postgresql://root:pass@localhost:5432/ice
primaryURL: postgresql://root:pass@localhost:5433/eskimo
credentials:
user: root
password: pass
replicaURLs:
- postgresql://root:pass@localhost:5432/ice
- postgresql://root:pass@localhost:5433/eskimo

kyc/quiz:
<<: *users
Expand All @@ -24,4 +24,4 @@ kyc/quiz:
maxResetCount: 2
maxAttemptsAllowed: 3
availabilityWindowSeconds: 604800
globalStartDate: 2024-02-03T16:20:52.156534Z
globalStartDate: '2024-02-03T16:20:52.156534Z'
48 changes: 31 additions & 17 deletions kyc/quiz/quiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,29 +889,43 @@ func (r *repositoryImpl) ContinueQuizSession( //nolint:funlen,revive,gocognit //
_, err = r.CheckQuestionNumber(ctx, progress.Lang, progress.Questions, question, tx)
if err != nil {
return err
} else if uint8(len(progress.Answers)) != question-1 {
} else if int(question)-len(progress.Answers) < 0 || question-uint8(len(progress.Answers)) > 1 {
return errors.Wrap(ErrUnknownQuestionNumber, "please answer questions in order")
}
newAnswers, aErr := r.UserAddAnswer(ctx, userID, tx, answer)
if aErr != nil {
return aErr
}
correctNum, incorrectNum := calculateProgress(progress.CorrectAnswers, newAnswers)
quiz = &Quiz{
Progress: &Progress{
MaxQuestions: uint8(len(progress.Questions)),
CorrectAnswers: correctNum,
IncorrectAnswers: incorrectNum,
},
}
var answeredQuestionsCount int
if uint8(len(progress.Answers)) == question-1 {
newAnswers, aErr := r.UserAddAnswer(ctx, userID, tx, answer)
if aErr != nil {
return aErr
}
answeredQuestionsCount = len(newAnswers)
correctNum, incorrectNum := calculateProgress(progress.CorrectAnswers, newAnswers)
quiz = &Quiz{
Progress: &Progress{
MaxQuestions: uint8(len(progress.Questions)),
CorrectAnswers: correctNum,
IncorrectAnswers: incorrectNum,
},
}

if int(incorrectNum) > r.config.MaxWrongAnswersPerSession {
quiz.Result = FailureResult
if int(incorrectNum) > r.config.MaxWrongAnswersPerSession {
quiz.Result = FailureResult

return r.UserMarkSessionAsFinished(ctx, userID, now, tx, false, false)
return r.UserMarkSessionAsFinished(ctx, userID, now, tx, false, false)
}
} else {
answeredQuestionsCount = len(progress.Answers)
correctNum, incorrectNum := calculateProgress(progress.CorrectAnswers, progress.Answers)
quiz = &Quiz{
Progress: &Progress{
MaxQuestions: uint8(len(progress.Questions)),
CorrectAnswers: correctNum,
IncorrectAnswers: incorrectNum,
},
}
}

if len(newAnswers) != len(progress.CorrectAnswers) {
if answeredQuestionsCount != len(progress.CorrectAnswers) {
nextQuestion, nErr := r.LoadQuestionByID(ctx, tx, progress.Lang, progress.Questions[question])
if nErr != nil {
return nErr
Expand Down
17 changes: 16 additions & 1 deletion kyc/quiz/quiz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,24 @@ func testManagerSessionContinueWithCorrectAnswers(ctx context.Context, t *testin
require.Equal(t, uint8(1), session.Progress.CorrectAnswers)
require.Equal(t, uint8(0), session.Progress.IncorrectAnswers)

nextQNum := session.Progress.NextQuestion.Number
ans = helperSolveQuestion(t, session.Progress.NextQuestion.Text)
t.Logf("q: %v, ans: %d", session.Progress.NextQuestion.Text, ans)
session, err = r.ContinueQuizSession(ctx, "bogus", session.Progress.NextQuestion.Number, ans)
session, err = r.ContinueQuizSession(ctx, "bogus", nextQNum, ans)
require.NoError(t, err)
require.NotNil(t, session)
t.Logf("next q: %v, deadline %v", session.Progress.NextQuestion.Text, session.Progress.ExpiresAt)
require.Empty(t, session.Result)
require.NotNil(t, session.Progress)
require.NotNil(t, session.Progress.ExpiresAt)
require.Equal(t, uint8(3), session.Progress.MaxQuestions)
require.NotEmpty(t, session.Progress.NextQuestion.Text)
require.Equal(t, uint8(2), session.Progress.CorrectAnswers)
require.Equal(t, uint8(0), session.Progress.IncorrectAnswers)

ans = helperSolveQuestion(t, session.Progress.NextQuestion.Text)
t.Logf("q: %v, ans: %d", session.Progress.NextQuestion.Text, ans)
session, err = r.ContinueQuizSession(ctx, "bogus", nextQNum, ans)
require.NoError(t, err)
require.NotNil(t, session)
t.Logf("next q: %v, deadline %v", session.Progress.NextQuestion.Text, session.Progress.ExpiresAt)
Expand Down

0 comments on commit 578178e

Please sign in to comment.