Skip to content

Commit

Permalink
fixed cfg issue in kyc/quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-ares committed Feb 1, 2024
1 parent 5c08786 commit 5bf9437
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
23 changes: 12 additions & 11 deletions kyc/quiz/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ type (
}
config struct {
alertFrequency *atomic.Pointer[stdlibtime.Duration]
MaxResetCount *uint8 `yaml:"maxResetCount"`
GlobalStartDate *time.Time `yaml:"globalStartDate" example:"YYYY-MM-DD"`
Environment string `yaml:"environment" mapstructure:"environment"`
AlertSlackWebhook string `yaml:"alert-slack-webhook" mapstructure:"alert-slack-webhook"` //nolint:tagliatelle // .
AvailabilityWindowSeconds int `yaml:"availabilityWindowSeconds"`
MaxSessionDurationSeconds int `yaml:"maxSessionDurationSeconds"`
MaxQuestionsPerSession int `yaml:"maxQuestionsPerSession"`
MaxWrongAnswersPerSession int `yaml:"maxWrongAnswersPerSession"`
SessionCoolDownSeconds int `yaml:"sessionCoolDownSeconds"`
EnableAlerts bool `yaml:"enable-alerts" mapstructure:"enable-alerts"` //nolint:tagliatelle // .
MaxAttemptsAllowed uint8 `yaml:"maxAttemptsAllowed"`
globalStartDate *time.Time
MaxResetCount *uint8 `yaml:"maxResetCount"`
GlobalStartDate string `yaml:"globalStartDate" example:"YYYY-MM-DD"` //nolint:revive // .
Environment string `yaml:"environment" mapstructure:"environment"`
AlertSlackWebhook string `yaml:"alert-slack-webhook" mapstructure:"alert-slack-webhook"` //nolint:tagliatelle // .
AvailabilityWindowSeconds int `yaml:"availabilityWindowSeconds"`
MaxSessionDurationSeconds int `yaml:"maxSessionDurationSeconds"`
MaxQuestionsPerSession int `yaml:"maxQuestionsPerSession"`
MaxWrongAnswersPerSession int `yaml:"maxWrongAnswersPerSession"`
SessionCoolDownSeconds int `yaml:"sessionCoolDownSeconds"`
EnableAlerts bool `yaml:"enable-alerts" mapstructure:"enable-alerts"` //nolint:tagliatelle // .
MaxAttemptsAllowed uint8 `yaml:"maxAttemptsAllowed"`
}
)
24 changes: 20 additions & 4 deletions kyc/quiz/quiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"github.com/ice-blockchain/eskimo/users"
appcfg "github.com/ice-blockchain/wintr/config"
"github.com/ice-blockchain/wintr/connectors/storage/v2"
"github.com/ice-blockchain/wintr/log"
"github.com/ice-blockchain/wintr/time"
)

func mustLoadConfig() config {
func mustLoadConfig() config { //nolint:funlen // .
var cfg config

appcfg.MustLoadFromKey(applicationYamlKey, &cfg)
Expand All @@ -37,9 +38,24 @@ func mustLoadConfig() config {
panic("maxResetCount is not set")
}

if cfg.GlobalStartDate == nil {
if cfg.GlobalStartDate == "" {
panic("globalStartDate is not set")
}
globalStartDate, err := stdlibtime.ParseInLocation(stdlibtime.DateOnly, cfg.GlobalStartDate, stdlibtime.UTC)
log.Panic(err) //nolint:revive // .
cfg.globalStartDate = time.New(globalStartDate)

if cfg.MaxWrongAnswersPerSession == 0 {
panic("maxWrongAnswersPerSession is not set")
}

if cfg.AvailabilityWindowSeconds == 0 {
panic("availabilityWindowSeconds is not set")
}

if cfg.MaxAttemptsAllowed == 0 {
panic("maxAttemptsAllowed is not set")
}

defaultAlertFrequency := alertFrequency
cfg.alertFrequency = new(atomic.Pointer[stdlibtime.Duration])
Expand Down Expand Up @@ -271,7 +287,7 @@ func (r *repositoryImpl) getQuizStatus(ctx context.Context, userID UserID) (*Qui
r.DB,
sql,
userID,
r.config.GlobalStartDate.Time,
r.config.globalStartDate.Time,
r.config.AvailabilityWindowSeconds,
*r.config.MaxResetCount,
r.config.MaxAttemptsAllowed,
Expand Down Expand Up @@ -331,7 +347,7 @@ where
user_id = $1 and
started_at >= GREATEST(u.created_at, $2)
`
count, err := storage.Get[int](ctx, tx, stmt, userID, r.config.GlobalStartDate.Time)
count, err := storage.Get[int](ctx, tx, stmt, userID, r.config.globalStartDate.Time)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return 0, nil
Expand Down
4 changes: 2 additions & 2 deletions kyc/quiz/quiz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func testManagerSessionStatus(ctx context.Context, t *testing.T, r *repositoryIm
require.NotNil(t, session)

configCopy := r.config
r.config.GlobalStartDate = time.New(stdlibtime.Now().Add(-stdlibtime.Hour * 100))
r.config.globalStartDate = time.New(stdlibtime.Now().Add(-stdlibtime.Hour * 100))
r.config.AvailabilityWindowSeconds = 1

reader := r.Users.(*mockUserReader)
Expand Down Expand Up @@ -593,7 +593,7 @@ func TestSessionManager(t *testing.T) {
cnt := uint8(testQuizMaxResets)
repo.config.MaxAttemptsAllowed = testQuizMaxAttempts
repo.config.MaxResetCount = &cnt
repo.config.GlobalStartDate = time.New(time.Now().Add(-stdlibtime.Hour))
repo.config.globalStartDate = time.New(time.Now().Add(-stdlibtime.Hour))
repo.config.AvailabilityWindowSeconds = 60 * 60 * 24 * 7 // 1 week.

helperInsertQuestion(t, repo)
Expand Down

0 comments on commit 5bf9437

Please sign in to comment.