Skip to content

Commit

Permalink
fix daily counter
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek committed Jan 15, 2025
1 parent aa19892 commit 7891733
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 12 additions & 11 deletions pkg/verification/service/verification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ func (s *ServiceImpl) InitVerification(ctx *gin.Context, userID, username, e164P

labelValues[toolchainv1alpha1.UserSignupUserPhoneHashLabelKey] = phoneHash

// read the current time
now := time.Now()

// If 24 hours has passed since the verification timestamp, then reset the timestamp and verification attempts
ts, parseErr := time.Parse(TimestampLayout, signup.Annotations[toolchainv1alpha1.UserSignupVerificationInitTimestampAnnotationKey])
if parseErr != nil || now.After(ts.Add(24*time.Hour)) {
// Set a new timestamp
annotationValues[toolchainv1alpha1.UserSignupVerificationInitTimestampAnnotationKey] = now.Format(TimestampLayout)
annotationValues[toolchainv1alpha1.UserSignupVerificationCounterAnnotationKey] = "0"
}

// get the verification counter (i.e. the number of times the user has initiated phone verification within
// the last 24 hours)
verificationCounter := signup.Annotations[toolchainv1alpha1.UserSignupVerificationCounterAnnotationKey]
Expand All @@ -134,6 +123,18 @@ func (s *ServiceImpl) InitVerification(ctx *gin.Context, userID, username, e164P
}
}

// read the current time
now := time.Now()

// If 24 hours has passed since the verification timestamp, then reset the timestamp and verification attempts
ts, parseErr := time.Parse(TimestampLayout, signup.Annotations[toolchainv1alpha1.UserSignupVerificationInitTimestampAnnotationKey])
if parseErr != nil || now.After(ts.Add(24*time.Hour)) {
// Set a new timestamp
annotationValues[toolchainv1alpha1.UserSignupVerificationInitTimestampAnnotationKey] = now.Format(TimestampLayout)
annotationValues[toolchainv1alpha1.UserSignupVerificationCounterAnnotationKey] = "0"
counter = 0
}

var initError error
// check if counter has exceeded the limit of daily limit - if at limit error out
if counter >= dailyLimit {
Expand Down
4 changes: 2 additions & 2 deletions pkg/verification/service/verification_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ func (s *TestVerificationServiceSuite) TestInitVerificationPassesWhenMaxCountRea
userSignup := testusersignup.NewUserSignup(
testusersignup.WithName("johny"),
testusersignup.WithLabel(toolchainv1alpha1.UserSignupUserPhoneHashLabelKey, "+1NUMBER"),
testusersignup.WithAnnotation(toolchainv1alpha1.UserSignupVerificationInitTimestampAnnotationKey, now.Format(verificationservice.TimestampLayout)),
testusersignup.WithAnnotation(toolchainv1alpha1.UserVerificationAttemptsAnnotationKey, "3"),
testusersignup.WithAnnotation(toolchainv1alpha1.UserSignupVerificationInitTimestampAnnotationKey, now.Add(-25*time.Hour).Format(verificationservice.TimestampLayout)),
testusersignup.WithAnnotation(toolchainv1alpha1.UserSignupVerificationCounterAnnotationKey, "3"),
testusersignup.WithAnnotation(toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey, "123456"),
testusersignup.VerificationRequiredAgo(time.Second))

Expand Down

0 comments on commit 7891733

Please sign in to comment.