Skip to content

fix(pos): canonical phone_digits column for customer phone search - #116

Merged
khaira777 merged 2 commits into
FreeOpenSourcePOS:mainfrom
carvalab:feat/phone-validation-2
Jul 18, 2026
Merged

khaira777 merged 2 commits into
FreeOpenSourcePOS:mainfrom
carvalab:feat/phone-validation-2

Conversation

@carvalab

Copy link
Copy Markdown
Contributor

Problem

POS CustomerSearch and the admin Customers page filter customers by phone,
sending digit-only queries (CustomerSearch.digitsOnly). customers.phone
is stored in mixed formats:

  • e164 (+919876543210) — rows from the modern country picker
  • legacy local (9876543210) — rows from before the i18n work
  • pretty-formatted (+91 987-654-3210) — manual data entry

Plain phone LIKE ? can'''t bridge the leading +: searching
919876543210 (country code without +) misses the e164 row entirely.

Fix

A STORED generated column on customers:

phone_digits TEXT GENERATED ALWAYS AS (
  REPLACE(REPLACE(REPLACE(REPLACE(phone, '\''+'\'', '\'''\''), '\'' '\''), '\''-'\''), '\''('\'', '\'''\'')
) STORED

phone stays untouched, so printers and CRM code keep their e164 input.
Search hits the canonical form: phone_digits LIKE ? bridges every
storage format against any digit-only query.

Changes

  • main/db.ts — migration v22 adds phone_digits (guarded for retry).
  • main/routes/index.ts:85 — /api/customers-search uses phone_digits LIKE ?.
  • main/routes/customers.ts:56 — /api/customers?search= filter uses phone_digits LIKE ?.
  • main/routes/customers.ts — dropped the unused /api/customers/search route (zero callers, leftover from a half-finished REST migration).
  • tests/customer-phone-search.test.ts — unit coverage of the SQL pattern + generated column.
  • tests/phone-search-integration.test.ts — boots real Express via registerRoutes, hits both endpoints across all format combinations.
  • tests/{first-run-setup,database-tools-api}.test.ts — schema-version assertions bumped to 22.

Migration

ALTER TABLE ADD COLUMN only. STORED generation fills existing rows on
demand. The getColumns guard keeps retry from crashing on
duplicate-column if a v22 transaction fails before user_version is
bumped.

Test

npm test — full suite passes. test:phone-search-integration covers
the same scenarios I verified by hand against a running backend (mixed
seeds, short digits, intl digits, inactive, name LIKE).

@carvalab
carvalab requested a review from itsbkm as a code owner July 16, 2026 01:21
@carvalab
carvalab force-pushed the feat/phone-validation-2 branch 2 times, most recently from 884f479 to d31fb45 Compare July 17, 2026 14:06
…t search

Customer phone search sends digit-only queries against customers.phone,
which is stored in mixed formats — e164 ('+919876543210') for rows created
via the modern country picker and legacy local ('9876543210') for older
rows. Plain LIKE can't bridge the leading '+' gap.

Add a STORED generated phone_digits column on customers (digits-only by
construction) via migration v22. Update both search SQL locations to use
the canonical column: /api/customers-search (POS) and /api/customers
?search= (admin list). Drop the unused /api/customers/search route that
had zero callers.

Tests: a unit assertion over the SQL+generated column, an Express-level
integration test that hits both endpoints with a mixed-format seed set,
and schema-version bumps in first-run-setup and database-tools-api.
@carvalab
carvalab force-pushed the feat/phone-validation-2 branch from d31fb45 to 10b4f4b Compare July 17, 2026 14:08
@khaira777

Copy link
Copy Markdown
Contributor

Thanks for this, clean approach with the generated column. One bug needs fixing before merge:

Missing ) in REPLACE chain

The migration strips +, , -, and (, but not ). A US-format phone like +1 (555) 123-4567 becomes 1555)123452888 in phone_digits - searching 5551234567 won't match.

-- Current (4 REPLACEs):
REPLACE(REPLACE(REPLACE(REPLACE(phone, '+', ''), ' ', ''), '-', ''), '(', '')

-- Should be (5 REPLACEs):
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, '+', ''), ' ', ''), '-', ''), '(', ''), ')', '')

Same issue applies to any phone entered as (415) 555-1234 - common for US/Canada.

Test gap

customer-phone-search.test.ts covers +, , - but has no case with ( or ). Worth adding one to lock this down — something like +1 (555) 123-4567 matching 5551234567.

Formats like +91 99888 52888 and +91-99888-52888 already work fine — the column strips spaces and dashes. Only the closing paren is the gap.

@khaira777
khaira777 requested review from khaira777 and removed request for itsbkm July 17, 2026 14:49
@khaira777 khaira777 added the enhancement New feature or request label Jul 17, 2026
REPLACE chain missed ) and ., so US-format phones (+1 (555) 123-4567,
+1.555.123.4567) produced non-matching phone_digits like 1555)1234567.
Frontend already strips all non-digits via /\D/g, so the column must
match.

Adds a 6th REPLACE for . while the line is in hand. Test gap filled in
customer-phone-search and phone-search-integration suites.

Addresses review feedback on PR FreeOpenSourcePOS#116.
@carvalab

Copy link
Copy Markdown
Contributor Author

@khaira777 good catch. Fixed in 3dad4a0. Added the 5th REPLACE for ), plus a 6th for . since +1.555.123.4567 would have hit the same gap. Also seeded +1 (555) 123-4567 and asserted both 5551234567 and 15551234567 match through both endpoints. v22 isn't shipped yet so the in-place edit was clean.

I'm still defaulting to my LATAM phone conventions in my head, I'll keep more global formats in mind from here. Thanks for catching what I missed.

@khaira777
khaira777 merged commit 25bf0df into FreeOpenSourcePOS:main Jul 18, 2026
10 checks passed
@carvalab
carvalab deleted the feat/phone-validation-2 branch July 19, 2026 00:57
khaira777 added a commit that referenced this pull request Oct 6, 2026
Override http-cache-semantics to ^4.3.0, katex to ^0.18.2 and smol-toml to ^1.9.0 so the resolved tree clears Dependabot alerts #110, #115 and #116 (CVE-2026-93748, CVE-2026-103923, GHSA-r4xh-jqrq-34v2).

All three arrive only through dev tooling (the electron-builder download path and markdownlint-cli2) and none ship in the desktop package or the runtime servers. Effect on FloCafe is limited to npm run docs:check and packaging; the katex jump stays inside 0.18.x to keep the range micromark-extension-math expects.

sprintf-js (alert #117) has no patched release upstream, so it is left for a Dependabot dismissal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants