Skip to content

Standardize customer phone numbers to strict E.164 and migrate legacy data #117

Closed

Description

@khaira777

Background

In PR #116, we fixed a bug where customer phone search was failing due to mixed formatting in the database. Because older versions of the app lacked strict validation, the customers.phone column currently contains a mix of formats:

  • Raw local digits (e.g., 9876543210)
  • Manual entries with formatting (e.g., +1 (555) 123-4567)
  • Strict E.164 from the modern country picker (e.g., +919876543210)

PR #116 resolved the search issue safely by introducing a phone_digits generated column to strip formatting on the fly. However, it does not prevent new messy data from being saved, nor does it clean up the original phone column.

The Problem

Storing phone numbers with formatting characters (dashes, spaces, brackets) is an anti-pattern. It forces downstream consumers (receipt printers, CRM integrations, SMS services) to constantly parse and clean the data before use.

Proposed Solution

Going forward, we should treat the database as the source of truth for strict data and handle visual formatting exclusively on the frontend.

We need to implement the following:

  1. Backend Validation: Add strict validation to the /api/customers endpoints to reject any phone number that doesn't conform to the E.164 format.
  2. Frontend Enforcement: Ensure all frontend forms (admin panel, POS) parse and strip visual formatting before submitting payloads to the API.
  3. Legacy Data Migration: Write a one-off database migration script to clean up the existing customers.phone rows across all databases and convert them to standard E.164.
  4. Cleanup: Once all data is strictly E.164, we can evaluate dropping the phone_digits generated column introduced in fix(pos): canonical phone_digits column for customer phone search #116, as it will no longer be necessary for bridging formats during search.

Activity

  1. carvalab commented on Jul 18, 2026

    @carvalab
    Contributor

    @khaira777, before implementing the migration, I think we need to define the fallback rules for legacy numbers.

    1. For numbers without an international prefix, should we use the tenant country when available and fall back to India (+91) when it is missing? The current schema defaults country_code to +91, but some older rows may have no reliable country information.
    2. If libphonenumber-js cannot safely parse a legacy number, should we set it to NULL and accept losing that phone number, or preserve the original value? Manual review is not currently possible because the migration runs in the backend and users cannot access its results. Preserving the value avoids data loss, but the database would still contain non-E.164 values.
    3. If two customers normalize to the same E.164 number, should we merge them, keep both, or flag the duplicate?

    Since phone_digits is not in a released version yet, I suggest landing #116 first, then running the normalization migration, and removing phone_digits only after these rules are decided.

  2. khaira777 commented on Jul 18, 2026

    @khaira777
    ContributorAuthor

    @carvalab great questions — we've worked through all three and here's where we landed.


    Answers to your three questions

    1. Country fallback for numbers without an international prefix?

    Use the customer row's own country_code column as the hint to libphonenumber-js. If that's NULL/empty, fall back to the tenant's country from the settings table, then finally fall back to +91 (India) as the last resort. This matches the schema's existing default and covers virtually all real installs given the app's current geography.

    2. If libphonenumber-js cannot parse a legacy number — NULL or preserve?

    Preserve the original raw value. Data loss is worse than a single dirty row. Unparseable rows will be logged at startup ([MIGRATION v23] unparseable: <id> <raw_phone>) so the operator can see them in the console. Given the software is only 2–3 months old, we expect this count to be near zero in practice.

    3. If two customers normalise to the same E.164 number — merge, keep, or flag?

    True merge. At this stage of the product we can safely assume all format variants of a phone number belong to the same person. Merge rules:

    • Winner: oldest created_at (most likely the canonical first entry)
    • Coalesce: winner keeps its id/name/phone; any NULL fields (email, address, notes) filled from the losing record if winner's field is empty
    • Reassign: all orders.customer_id, bills.customer_id, held_orders.customer_id, and loyalty_ledger.customer_id rows updated to the winner — no financial data lost
    • Hard-delete: loser rows removed after reassignment
    • Log: [MIGRATION v23] merged <loser_id> → <winner_id> (phone: +919...)

    Additional decisions

    • After migration: add CREATE UNIQUE INDEX … ON customers(phone_digits) WHERE phone_digits IS NOT NULL so the DB enforces no future duplicates at the data layer
    • phone_digits column: keep it even after full E.164 normalisation — still useful as a search bridge (cashier types 9876543210 without country code and still finds the customer)
    • Admin Customers page: POST /customers returns HTTP 409 if the normalised phone already exists; the UI also checks on phone-blur and surfaces the existing match inline before Save

    Implementation plan (migration v23: normalize_customer_phones)

    1. Read tenant country from settings
    2. For each customer row: parse phone with country_code → tenant country → +91 fallback chain; store E.164 if parseable, log + skip if not
    3. Detect collision groups (same phone_digits post-normalisation)
    4. For each group: coalesce fields + reassign all four tables + hard-delete losers
    5. Add UNIQUE INDEX on phone_digits
    6. Harden POST/PUT /customers to reject non-E.164 going forward

    Happy to hear if you see any edge cases we've missed before we start coding this up.

  3. self-assigned this
    on Jul 18, 2026
  4. added a commit that references this issue on Jul 20, 2026
    60bdf17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions