Skip to content

Conversation

teresper-eth
Copy link
Collaborator

@teresper-eth teresper-eth commented Sep 28, 2025

Three improvements have been implemented in register.ts:

  1. Defined a RegisterRequestBody type for the request body for better type safety and clarity.
  2. Normalized the wallet address by trimming and lowercasing it before use.
  3. Added a comment explaining the handling of the P2002 race condition error from Prisma.

Summary by CodeRabbit

  • Bug Fixes
    • Registration now accepts wallet addresses regardless of case or whitespace, ensuring consistent account matching.
    • Prevents rare duplicate-account creation during simultaneous sign-ups; existing accounts are correctly recognized.
    • Improves reliability and stability of the registration flow without changing user-facing behavior.

Copy link
Contributor

coderabbitai bot commented Sep 28, 2025

Walkthrough

Updates the register route to normalize wallet addresses, centralize request body typing, and handle unique-constraint race conditions by retrying and returning the existing user on P2002 errors.

Changes

Cohort / File(s) Summary of Changes
Register route normalization & race handling
src/routes/register.ts
Introduces RegisterRequestBody type alias; normalizes walletAddress (trim + lowercase); uses normalized address for DB lookups/creates; adds P2002 unique-constraint handling by re-querying and returning existing user; minor restructuring to use the alias in handler destructuring.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor C as Client
    participant R as Register Route
    participant DB as Database

    C->>R: POST /register { walletAddress, ... }
    R->>R: Normalize walletAddress (trim + lowercase)
    R->>DB: findUnique(normalizedWalletAddress)
    alt User exists
        DB-->>R: user
        R-->>C: 200 OK (existing user)
    else Create path
        R->>DB: create(user with normalizedWalletAddress)
        DB-->>R: created user
        R-->>C: 201 Created (new user)
    end

    opt Unique constraint race
        note over DB,R: create throws P2002 (unique violation)
        R->>DB: findUnique(normalizedWalletAddress)
        alt Found after conflict
            DB-->>R: user
            R-->>C: 200 OK (existing user)
        else Unexpected error
            DB-->>R: error
            R-->>C: 500/handled error
        end
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • metanodreamer

Poem

I hop through routes with tidy grace,
Trimming wallets, lowercased.
If twins collide (P2002’s chase),
I sniff the record already placed.
New or known, the user’s traced—
Thump-thump! Registration’s paced. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly identifies the two primary refactoring efforts in this pull request—enhancing the request body type definition and normalizing the wallet address—clearly reflecting the main changes detailed in register.ts without extraneous information.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/improve-register

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/routes/register.ts (1)

15-20: Derive RegisterRequestBody directly from the schema

Nice to see the request body constrained, but the manual RegisterRequestBody definition can drift from the Type.Object validator (e.g., if we tweak field names, optionality, or formats). You can keep runtime and compile-time definitions in lockstep by lifting the schema into a constant and deriving the type via Static<typeof schema>, then reuse that schema in the route config.

-import { Type } from "@sinclair/typebox";
+import { Static, Type } from "@sinclair/typebox";-// Define the expected request body type for clarity and reusability
-type RegisterRequestBody = {
-  walletAddress: string;
-  message: string;
-  signature: string;
-};
+const registerRequestBodySchema = Type.Object({
+  walletAddress: Type.RegExp(/^0x[a-fA-F0-9]{40}$/),
+  message: Type.String({ minLength: 1, maxLength: 1000 }),
+  signature: Type.RegExp(/^0x[a-fA-F0-9]{130}$/),
+});
+type RegisterRequestBody = Static<typeof registerRequestBodySchema>;

Then wire it up with body: registerRequestBodySchema. This keeps types and validation aligned automatically.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b172120 and fa77440.

📒 Files selected for processing (1)
  • src/routes/register.ts (4 hunks)
🔇 Additional comments (1)
src/routes/register.ts (1)

54-104: Address normalization + race-condition handling looks solid

Trimming/lowercasing before both the lookup and the create, plus the fallback fetch on P2002, closes the race nicely while keeping the response consistent.

@metanodreamer metanodreamer merged commit 5916586 into main Sep 28, 2025
1 check passed
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