Skip to content

fix(customers): normalize formatted phone queries in customer list search #391

Closed

Description

@khaira777

Refs: #388

Context

The admin customer management page (/customers) allows searching customers by name, phone, or email via the GET /api/customers?search=... endpoint. Customer phone numbers are stored in canonical E.164 format and indexed via a stored generated column phone_digits containing normalized digits.

Verified Current Behavior

  • Route Query (main/routes/customers.ts lines 170-174):
    if (req.query.search) {
      const search = `%${req.query.search}%`;
      query += ' AND (c.name LIKE ? OR c.phone_digits LIKE ? OR c.email LIKE ?)';
      params.push(search, search, search);
    }
  • POS Search Comparison (frontend/src/components/pos/CustomerSearch.tsx): Strips non-digit characters before sending queries to /api/customers-search.
  • Admin Customer Page (frontend/src/app/(dashboard)/customers/page.tsx): Sends the raw user input directly to GET /api/customers?search=....
  • Existing Helper (main/lib/phone.ts line 16): Already exports stripPhoneDigits(input: string): string.
  • Existing Integration Test (tests/phone-search-integration.test.ts lines 97-116): Currently tests only digit-only inputs (e.g. 9876543210, 5551234567) against /api/customers?search=, omitting formatted strings.

Problem

When an admin types or pastes a formatted phone number into the search bar (such as +1 (555) 123-4567, 555-123-4567, or +91 98765-43210), the backend executes c.phone_digits LIKE '%+1 (555) 123-4567%'. Because phone_digits contains only raw digits (e.g. 15551234567), the query yields zero results, frustrating users who copy-paste customer phone numbers.

Architectural Decision

  • Preserve the original raw query for c.name LIKE ? and c.email LIKE ?.
  • Derive a digit-only query using the existing backend helper stripPhoneDigits(rawQuery) for c.phone_digits LIKE ?.
  • If the digit-only version is non-empty, include the phone_digits match branch with the stripped digits. If the query contains no digits (e.g. searching for "Alice"), ensure the phone match branch does not match every row or is skipped cleanly.
  • Do NOT introduce new regex phone parsing libraries or rewrite the database schema.

Exact Implementation Scope

  1. Backend Customer List Route (main/routes/customers.ts):
    • In GET /, derive:
      const rawSearch = String(req.query.search || '').trim();
      const digitsSearch = stripPhoneDigits(rawSearch);
    • Construct the SQL WHERE clause such that c.name LIKE ? and c.email LIKE ? use %${rawSearch}%, while c.phone_digits LIKE ? uses %${digitsSearch}% (when digitsSearch.length > 0).
    • Use the existing stripPhoneDigits helper from main/lib/phone.ts.
  2. Integration Tests (tests/phone-search-integration.test.ts):
    • Add test assertions for formatted phone search strings against GET /api/customers?search=... covering US, India, and international formats (e.g. +1 (555) 123-4567, 555-123-4567, +91 98765-43210).

Required Behavior

  • Searching +1 (555) 123-4567 matches customer record with phone digits 15551234567.
  • Searching 555-123-4567 matches customer record with phone digits 15551234567.
  • Searching +91 98765-43210 matches customer record with phone digits 919876543210.
  • Searching [email protected] or Alice Smith matches name and email records without interference.
  • Pagination, sorting, and is_active = 1 filtering remain fully preserved.

Backward Compatibility / Upgrade Behavior

  • No database migration or schema alteration needed.
  • Uses existing phone_digits stored generated column.

Offline-First Requirements

  • Operates 100% locally in SQLite.

Non-Goals / What NOT to Do

  • Do NOT alter the database schema or rewrite generated column definitions.
  • Do NOT change E.164 storage contracts.
  • Do NOT add external fuzzy phone search algorithms.
  • Do NOT break name or email search capabilities.

Acceptance Criteria

  • Formatted phone queries (+1 (555) 123-4567, 555-123-4567, +91 98765-43210) successfully find matching customers via GET /api/customers?search=....
  • Plain text queries continue searching name and email fields.
  • Direct digit queries continue searching as before.
  • All existing phone search integration tests in tests/phone-search-integration.test.ts pass.

Verification

npm run lint:backend
npm run test:customer-phone-search
npm run test:phone-search-integration
npm run test:phone
npm test

Dependencies

  • None. Independent backend task.

Relationship to Epic

Activity

  1. added
    area:backend
    on Aug 18, 2026
  2. khaira777 commented on Aug 20, 2026

    @khaira777
    ContributorAuthor

    Status update — 2026-08-20

    🔄 Implementation complete; validation in progress. Commit 0eb0b2ff contains the backend fix and regression coverage. The no-mistakes pipeline is currently running its review step; no PR has been opened yet. Next stage: complete the gate, then review the generated PR and CI.

  3. khaira777 commented on Aug 20, 2026

    @khaira777
    ContributorAuthor

    Status update — 2026-08-20

    ✅ Complete. PR #433 is merged at e5ddfd3c with the follow-up mixed name/email digit-search correction included. GitHub CI and Greptile checks are green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:backend

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions