Skip to content

fix(platform): normalize emails to lowercase to prevent duplicates - #698

Merged
Israeltheminer merged 2 commits into
mainfrom
fix/email-case-insensitive-validation
Mar 6, 2026
Merged

Israeltheminer merged 2 commits into
mainfrom
fix/email-case-insensitive-validation

Conversation

@Israeltheminer

@Israeltheminer Israeltheminer commented Mar 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Normalize all email addresses to lowercase with .toLowerCase().trim() at the mutation boundary before duplicate checks and database inserts
  • Covers customer creation (single, bulk, public), customer updates, vendor bulk creation, vendor updates, and user/member creation flows
  • Prevents duplicate accounts when emails differ only by casing (e.g., [email protected] vs [email protected])

Closes #690

Test plan

  • Create a customer with [email protected], then try creating another with [email protected] — should fail with duplicate error
  • Verify bulk import rejects duplicate emails regardless of casing
  • Verify vendor creation/import has the same case-insensitive behavior
  • Verify member creation normalizes email before lookup

Summary by CodeRabbit

  • Bug Fixes
    • Improved email handling consistency by normalizing emails (case-insensitive) across customer, user, and vendor operations.
    • Enhanced duplicate detection with clearer error codes for duplicate emails and external IDs.
    • Better error reporting in bulk operations with standardized error codes for improved troubleshooting.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@coderabbitai

coderabbitai Bot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The 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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: normalizing emails to lowercase to prevent duplicates, which directly aligns with the primary objective of the pull request.
Linked Issues check ✅ Passed The pull request comprehensively addresses issue #690 by implementing case-insensitive email validation across all customer, vendor, and user creation flows, preventing duplicate accounts when emails differ only by casing.
Out of Scope Changes check ✅ Passed All changes are directly related to email normalization and case-insensitive validation across customer, vendor, and user creation flows, with no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/email-case-insensitive-validation

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6152263 and 9f0921b.

📒 Files selected for processing (6)
  • services/platform/convex/customers/bulk_create_customers.ts
  • services/platform/convex/customers/create_customer_public.ts
  • services/platform/convex/customers/update_customer.ts
  • services/platform/convex/users/create_member.ts
  • services/platform/convex/users/create_user_without_session.ts
  • services/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
@Israeltheminer
Israeltheminer force-pushed the fix/email-case-insensitive-validation branch from 01ce96f to 7e3f99b Compare March 6, 2026 16:21
@Israeltheminer
Israeltheminer merged commit 108297c into main Mar 6, 2026
15 checks passed
@Israeltheminer
Israeltheminer deleted the fix/email-case-insensitive-validation branch March 6, 2026 16:28
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.

Bug: Duplicate customers can be created due to case-sensitive email validation

1 participant