Repository navigation
fix(platform): normalize emails to lowercase to prevent duplicates - #698
Conversation
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
📝 WalkthroughWalkthroughThe changes implement email normalization across customer, user, and vendor creation and update operations by consistently applying lowercase and trimming transformations. A new BulkCreateError class is introduced in bulk creation functions to provide domain-specific error handling with error code fields ('duplicate_email', 'duplicate_external_id', 'unknown'). The normalized email is used for all duplicate checks, database queries, and storage operations, replacing raw email inputs throughout six function files to enforce case-insensitive email validation. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@services/platform/convex/customers/bulk_create_customers.ts`:
- Around line 52-56: Normalize customerData.email into the local variable email
(as already done) and then explicitly reject or error when email === "" before
proceeding to duplicate checks or insert; update the duplicate-check branch that
uses email (variable email and ctx.db lookup) to treat an empty normalized
string as invalid and throw a validation error, and apply the same guard to the
second occurrence around lines 91-92 (where customerData.email is normalized
again) so no empty-string emails are skipped and inserted.
- Around line 101-103: The emitted result includes a new errorCode field but the
BulkCreateResult (or its error item type) is not updated, causing a type
mismatch; update the type definition for BulkCreateResult's error item to
include errorCode (e.g., as string | undefined or a specific union), or modify
the place emitting errorCode to match the existing type. Locate the
BulkCreateResult type (and any referenced error item/interface) and add an
errorCode property with the appropriate type, ensuring compatibility with
BulkCreateError.errorCode and the code that constructs the object in
bulk_create_customers.ts.
In `@services/platform/convex/customers/create_customer_public.ts`:
- Around line 33-46: After normalizing args.email in create_customer_public
(const email = args.email.toLowerCase().trim()), add a validation that rejects
an empty string (e.g., if email === '') and throw a clear error before any DB
lookup; place this check immediately after the normalization and before the
existingCustomer query so whitespace-only input cannot proceed to insertion or
uniqueness checks.
In `@services/platform/convex/vendors/mutations.ts`:
- Around line 32-34: The normalization step that lowercases and trims
updateData.email currently allows whitespace-only strings to become empty and
bypass duplicate checks; in the mutation(s) that modify vendor emails (where
updateData.email is normalized) add logic after updateData.email =
updateData.email.toLowerCase().trim() to detect an empty string and either
reject the request with a validation error or explicitly unset/null the email
field (consistent with your schema), and apply the same fix to the other
normalization block around lines 136-177 so empty normalized emails are not
saved or used in duplicate checks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 39903d2d-f177-45b7-8e27-56d0d5fbe43e
📒 Files selected for processing (6)
services/platform/convex/customers/bulk_create_customers.tsservices/platform/convex/customers/create_customer_public.tsservices/platform/convex/customers/update_customer.tsservices/platform/convex/users/create_member.tsservices/platform/convex/users/create_user_without_session.tsservices/platform/convex/vendors/mutations.ts
…e duplicates Emails are now lowercased and trimmed at the mutation boundary before duplicate checks and inserts. This covers customer creation (single, bulk, public), customer updates, vendor bulk creation, vendor updates, and user/member creation flows. Closes #690
01ce96f to
7e3f99b
Compare
Summary
.toLowerCase().trim()at the mutation boundary before duplicate checks and database inserts[email protected]vs[email protected])Closes #690
Test plan
[email protected], then try creating another with[email protected]— should fail with duplicate errorSummary by CodeRabbit