Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(OTP): simplify code by using upsert() #1283

Merged
merged 2 commits into from
Apr 11, 2024

Conversation

timotheeg
Copy link
Contributor

@timotheeg timotheeg commented Apr 9, 2024

Problem

The code for sendEmailOtp() and sendSmsOtp() have a behaviour of find-then-create-or-update. However that not done in a transaction, and is not sync-safe.

Sequelize has findOrCreate() which does create transaction by default, so we can just use that instead.

Additionally the method createOtpEntry() is not very useful since there are 2 code path which are conditional on the otp types, and these are already distinct in the methods sendEmailOtp() and sendSmsOtp() themselves.

Also there was one possible issue (not sure): The 2 OTP methods did not have the same behaviour to reset the OTP expiry (only the email OTP would reset the expiry). I'm not sure if it's on purpose or not, so it's worth having someone commenting on it. I'll start a thread in the PR to indicate the potential issue.

Solution

  • Use upsert() as needed
  • remove unnecessary method createOtpEntry()

Verified on staging (sample trace here)

image

Tests

  • Login via email OTP

@timotheeg timotheeg requested a review from a team April 9, 2024 04:04
if (!otpEntry) {
await this.createOtpEntry(mobileNumber, OtpType.Mobile, hashedOtp)
} else {
await otpEntry?.update({ hashedOtp, attempts: 0 })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this update not updating expiresAt, like what sendEmailOtp() does?

@timotheeg timotheeg changed the title refactor: simplify code by using findOrCreate() refactor(OTP): simplify code by using findOrCreate() Apr 9, 2024
Copy link
Contributor

@seaerchin seaerchin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comment, behaviour seems consistent anyways

src/services/identity/UsersService.ts Outdated Show resolved Hide resolved
@timotheeg timotheeg changed the title refactor(OTP): simplify code by using findOrCreate() refactor(OTP): simplify code by using upsert() Apr 11, 2024
@timotheeg timotheeg merged commit 67f0660 into develop Apr 11, 2024
21 checks passed
@timotheeg timotheeg deleted the refactor_simplify_code branch April 11, 2024 01:25
@harishv7 harishv7 mentioned this pull request Apr 11, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants