You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Standardize customer phone numbers to strict E.164 and migrate legacy data #117
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:
Backend Validation: Add strict validation to the /api/customers endpoints to reject any phone number that doesn't conform to the E.164 format.
Frontend Enforcement: Ensure all frontend forms (admin panel, POS) parse and strip visual formatting before submitting payloads to the API.
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.
@khaira777, before implementing the migration, I think we need to define the fallback rules for legacy numbers.
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.
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.
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.
@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
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)
Read tenant country from settings
For each customer row: parse phone with country_code → tenant country → +91 fallback chain; store E.164 if parseable, log + skip if not
Detect collision groups (same phone_digits post-normalisation)
For each group: coalesce fields + reassign all four tables + hard-delete losers
Add UNIQUE INDEX on phone_digits
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.
Activity
carvalab commentedon Jul 18, 2026
@khaira777, before implementing the migration, I think we need to define the fallback rules for legacy numbers.
country_codeto+91, but some older rows may have no reliable country information.libphonenumber-jscannot safely parse a legacy number, should we set it toNULLand 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.Since
phone_digitsis not in a released version yet, I suggest landing #116 first, then running the normalization migration, and removingphone_digitsonly after these rules are decided.khaira777 commentedon Jul 18, 2026
@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_codecolumn as the hint tolibphonenumber-js. If that's NULL/empty, fall back to the tenant's country from thesettingstable, 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-jscannot 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:
created_at(most likely the canonical first entry)id/name/phone; any NULL fields (email,address,notes) filled from the losing record if winner's field is emptyorders.customer_id,bills.customer_id,held_orders.customer_id, andloyalty_ledger.customer_idrows updated to the winner — no financial data lost[MIGRATION v23] merged <loser_id> → <winner_id> (phone: +919...)Additional decisions
CREATE UNIQUE INDEX … ON customers(phone_digits) WHERE phone_digits IS NOT NULLso the DB enforces no future duplicates at the data layerphone_digitscolumn: keep it even after full E.164 normalisation — still useful as a search bridge (cashier types9876543210without country code and still finds the customer)POST /customersreturns HTTP 409 if the normalised phone already exists; the UI also checks on phone-blur and surfaces the existing match inline before SaveImplementation plan (migration v23:
normalize_customer_phones)settingsphonewithcountry_code→ tenant country →+91fallback chain; store E.164 if parseable, log + skip if notphone_digitspost-normalisation)phone_digitsPOST/PUT /customersto reject non-E.164 going forwardHappy to hear if you see any edge cases we've missed before we start coding this up.
feat: normalize customer phones to E.164 (FreeOpenSourcePOS#119)
chore(deps): pin patched dev-tooling transitive dependencies (#982)