Skip to content

Commit

Permalink
a bit of a code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-ares committed Jan 11, 2024
1 parent a0444de commit ecb3e45
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 41 deletions.
6 changes: 6 additions & 0 deletions application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ kyc/social:
5: https://www.facebook.com/reel/1463272597773681
6: https://www.facebook.com/reel/1463272597773681
allow-long-live-tokens: true
kyc/quiz:
wintr/connectors/storage/v2: *db
maxSessionDurationSeconds: 600
maxQuestionsPerSession: 3
maxWrongAnswersPerSession: 1
sessionCoolDownSeconds: 3600
auth/email-link:
wintr/connectors/storage/v2: *db
fromEmailAddress: [email protected]
Expand Down
11 changes: 2 additions & 9 deletions kyc/quiz/.testdata/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ users: &users
- postgresql://root:pass@localhost:5432/ice

kyc/quiz:
<<: *users
maxSessionDurationSeconds: 600
maxQuestionsPerSession: 3
maxWrongAnswersPerSession: 1
sessionCoolDownSeconds: 3600
wintr/connectors/storage/v2:
runDDL: true
primaryURL: postgresql://root:pass@localhost:5432/ice
credentials:
user: root
password: pass
replicaURLs:
- postgresql://root:pass@localhost:5432/ice
sessionCoolDownSeconds: 3600
6 changes: 3 additions & 3 deletions kyc/quiz/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

create table if not exists questions
(
id bigint not null generated always as identity,
id bigint not null,
correct_option smallint not null,
options text[] not null,
language text not null,
Expand All @@ -11,7 +11,7 @@ create table if not exists questions
primary key (language, id)
);

create table if not exists failed_quizz_sessions
create table if not exists failed_quiz_sessions
(
started_at timestamp not null,
ended_at timestamp not null,
Expand All @@ -23,7 +23,7 @@ create table if not exists failed_quizz_sessions
primary key (user_id, started_at)
);

create table if not exists quizz_sessions
create table if not exists quiz_sessions
(
started_at timestamp not null,
ended_at timestamp,
Expand Down
8 changes: 0 additions & 8 deletions kyc/quiz/internal/.testdata/application.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions kyc/quiz/internal/contract.go

This file was deleted.

3 changes: 0 additions & 3 deletions kyc/quiz/internal/quiz.go

This file was deleted.

18 changes: 9 additions & 9 deletions kyc/quiz/quiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (r *repositoryImpl) CheckUserFailedSession(ctx context.Context, userID User
}

const stmt = `
select max(ended_at) as ended_at from failed_quizz_sessions where user_id = $1 having max(ended_at) > $2
select max(ended_at) as ended_at from failed_quiz_sessions where user_id = $1 having max(ended_at) > $2
`

term := now.
Expand Down Expand Up @@ -151,7 +151,7 @@ func (r *repositoryImpl) CheckUserActiveSession(ctx context.Context, userID User
Finished bool `db:"finished"`
FinishedSuccessfully bool `db:"ended_successfully"`
}
const stmt = `select started_at, ended_at is not null as finished, ended_successfully from quizz_sessions where user_id = $1`
const stmt = `select started_at, ended_at is not null as finished, ended_successfully from quiz_sessions where user_id = $1`

data, err := storage.Get[userSession](ctx, tx, stmt, userID)
if err != nil {
Expand Down Expand Up @@ -220,8 +220,8 @@ func (*repositoryImpl) CreateSessionEntry( //nolint:revive //.
tx storage.QueryExecer,
) error {
const stmt = `
insert into quizz_sessions (user_id, language, questions, started_at, answers) values ($1, $2, $3, $4, '{}'::smallint[])
on conflict on constraint quizz_sessions_pkey do update
insert into quiz_sessions (user_id, language, questions, started_at, answers) values ($1, $2, $3, $4, '{}'::smallint[])
on conflict on constraint quiz_sessions_pkey do update
set
started_at = excluded.started_at,
questions = excluded.questions,
Expand Down Expand Up @@ -335,7 +335,7 @@ select
array_agg(questions.correct_option order by q.nr) as correct_answers,
ended_successfully
from
quizz_sessions session,
quiz_sessions session,
questions
inner join unnest(session.questions) with ordinality AS q(id, nr)
on questions.id = q.id
Expand Down Expand Up @@ -399,7 +399,7 @@ func (*repositoryImpl) CheckQuestionNumber(ctx context.Context, questions []uint

func (*repositoryImpl) UserAddAnswer(ctx context.Context, userID UserID, tx storage.QueryExecer, answer uint8) ([]uint8, error) {
const stmt = `
update quizz_sessions
update quiz_sessions
set
answers = array_append(answers, $2)
where
Expand Down Expand Up @@ -438,15 +438,15 @@ func (r *repositoryImpl) UserMarkSessionAsFinished(
) error {
const stmt = `
with result as (
update quizz_sessions
update quiz_sessions
set
ended_at = $3,
ended_successfully = $2
where
user_id = $1
returning *
)
insert into failed_quizz_sessions (started_at, ended_at, questions, answers, language, user_id, skipped)
insert into failed_quiz_sessions (started_at, ended_at, questions, answers, language, user_id, skipped)
select
result.started_at,
result.ended_at,
Expand Down Expand Up @@ -572,7 +572,7 @@ func (r *repositoryImpl) ContinueQuizSession( //nolint:funlen,revive //.
func (r *repositoryImpl) ResetQuizSession(ctx context.Context, userID UserID) error {
// $1: user_id.
const stmt = `
delete from quizz_sessions
delete from quiz_sessions
where
user_id = $1
`
Expand Down
12 changes: 6 additions & 6 deletions kyc/quiz/quiz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func helperSolveQuestion(t *testing.T, text string) uint8 {
func helperForceFinishSession(t *testing.T, r *repositoryImpl, userID UserID, result bool) {
t.Helper()

_, err := storage.Exec(context.TODO(), r.DB, "update quizz_sessions set ended_at = now(), ended_successfully = $2 where user_id = $1", userID, result)
_, err := storage.Exec(context.TODO(), r.DB, "update quiz_sessions set ended_at = now(), ended_successfully = $2 where user_id = $1", userID, result)
require.NoError(t, err)
}

func helperForceResetSessionStartedAt(t *testing.T, r *repositoryImpl, userID UserID) {
t.Helper()

_, err := storage.Exec(context.TODO(), r.DB, "update quizz_sessions set ended_at = NULL, started_at = to_timestamp(42) where user_id = $1", userID)
_, err := storage.Exec(context.TODO(), r.DB, "update quiz_sessions set ended_at = NULL, started_at = to_timestamp(42) where user_id = $1", userID)
require.NoError(t, err)
}

Expand All @@ -66,7 +66,7 @@ func helperSessionReset(t *testing.T, r *repositoryImpl, userID UserID, full boo
require.NoError(t, err)

if full {
_, err = storage.Exec(context.TODO(), r.DB, "delete from failed_quizz_sessions where user_id = $1", userID)
_, err = storage.Exec(context.TODO(), r.DB, "delete from failed_quiz_sessions where user_id = $1", userID)
require.NoError(t, err)
}
}
Expand Down Expand Up @@ -167,8 +167,8 @@ func testManagerSessionStart(ctx context.Context, t *testing.T, r *repositoryImp
session, err := r.StartQuizSession(ctx, "bogus", "en")
require.NoError(t, err)

for i := uint(0); i < uint(session.Progress.MaxQuestions); i++ {
session, err = r.ContinueQuizSession(ctx, "bogus", i+uint(1), 0)
for i := uint8(0); i < uint8(session.Progress.MaxQuestions); i++ {
session, err = r.ContinueQuizSession(ctx, "bogus", i+uint8(1), 0)
require.NoError(t, err)
require.NotNil(t, session)
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func testManagerSessionContinueErrors(ctx context.Context, t *testing.T, r *repo
helperSessionReset(t, r, "bogus", true)
_, err := r.StartQuizSession(ctx, "bogus", "en")
require.NoError(t, err)
for _, n := range []uint{0, 4, 5, 6, 7, 10, 20, 100} {
for _, n := range []uint8{0, 4, 5, 6, 7, 10, 20, 100} {
_, err = r.ContinueQuizSession(ctx, "bogus", n, 1)
require.ErrorIs(t, err, ErrUnknownQuestionNumber)
}
Expand Down

0 comments on commit ecb3e45

Please sign in to comment.