Repository navigation
feat(questions): phone intake-question type, stored in E.164 (#260) - #286
dominik-myszkowski wants to merge 2 commits into
Conversation
…#260) A 'phone' question renders a tel input on the booking page and the widget. validateAnswersCore normalises the answer to E.164 and keeps what the attendee typed in booking_answers.raw_value (exposed on GET /v1/bookings/{id}/answers). normalizePhoneAnswer: must pass validPhone; an explicit + or 00 prefix wins (+44 is never rewritten, "(0)" after it is dropped); otherwise the question's default_country_code is prepended after dropping a national trunk 0 (kept for +39/+378/+379); extensions are refused; 8-15 digits. A number that can't be normalised, or a national one without a default code, is refused with err_invalid_phone, never stored as typed. Same path for the page, widget, MCP and the assistant. No special attendee_phone field. Migration 00078 rebuilds event_type_questions to widen the type CHECK and add default_country_code, with foreign_keys off: booking_answers references it ON DELETE CASCADE, so a rebuild with them on would delete every answer (a test pins that both answers survive and the cascade still works). Down turns phone questions into text ones. Admin: the Phone number type and a default country code field in the questions panel (en/fr). Duplication copies the code.
There was a problem hiding this comment.
Important
The normaliser stores a wrong E.164 value for one realistic input form — a 00-prefixed number with the standard (0) trunk marker. Everything else here is minor.
Approach: sound — a single normaliser inside validateAnswersCore means the booking page, the widget, MCP and the assistant all get the same stored value, and the table rebuild follows the existing 00042/00073 pattern with its cascade hazard covered by a real migration test.
Reviewed changes
- Schema —
00078widens theevent_type_questionstype CHECK and addsdefault_country_code;booking_answers.raw_valueis a plainADD COLUMN. Verified the rebuild loses no index (there were none) and that thebooking_answerscascade still works after the rename. - Normalisation —
normalizePhoneAnswer/normalizeCountryCode, the trunk-zero rule and its Italian-plan exception, and the phone branch invalidateAnswersCorestoring E.164 plusraw_value. - API and admin —
questionColumnsunifies the question SELECTs, create/update validation normalises and clears the code correctly, duplication copies it, andQuestionsPanel.svelteplus the en/fr keys are complete. - Surfaces —
telinputs onbook.htmlandembed.js;manage.htmlcorrectly needs nothing, and.field inputalready styles the new field. - Docs/i18n — the ARCHITECTURE section, the CHANGELOG entry, and
err_invalid_phonein all locales. - Tests —
go test ./internal/db/ ./internal/handler/ ./internal/booking/ ./internal/i18n/all pass.
ℹ️ get_event_type and the assistant prompt still omit the default country code
Two other readers of event_type_questions were not updated, so the metadata they hand to an agent no longer describes what a valid phone answer looks like. Normalisation still works on both paths; the cost is that a caller is told to demand a full international number when a national one would be accepted.
Technical details
# `default_country_code` is missing from the MCP and assistant question lists
## Affected sites
- `internal/handler/mcp.go:164` — `mcpGetEventType` selects `id, label, type, options, required` into `eventTypeQuestion`, so `get_event_type` returns `type: "phone"` with no country code.
- `internal/handler/booking_assistant.go:272` — `assistantSystemPrompt` renders `type=%s` into the prompt from the same column list, so the model cannot tell the visitor a national number is fine.
## Required outcome
- Both readers expose the question's `default_country_code` alongside its type, so a caller can describe the accepted input.
- No behavioural change to `validateAnswersCore`.
## Suggested approach (optional)
- Add `COALESCE(default_country_code, '')` to both SELECTs; add the field to `eventTypeQuestion` and append it to the prompt line when non-empty.deepseek-v4.1-flash | 𝕏
…ode only when it is missing "(0)" was dropped only after +, and only as "(0)" or "( 0 )": 0044 (0)20 7946 0958 normalised to +4402079460958. It is now dropped after either international prefix, whatever the spacing in the brackets. The refusal always asked for the country code, also on a question with a default, where the code is applied and the number itself is the problem. err_phone_country_code now covers a national number with no default, and err_invalid_phone says to check the number and leave out an extension.
There was a problem hiding this comment.
Important
The generalised (0) strip now also removes the leading 0 from Italian numbers, where it is part of the number rather than a trunk prefix: +39 (0)6 1234 5678 → +39612345678, and 0039 (0)6 1234 5678 regressed from the correct +390612345678 to the same wrong value. The second error key, the locale entries and the new tests all check out.
Reviewed changes
Reviewed the delta since the prior pullfrog review (bebf231): the wider marker handling, the split refusal messages, their locales, docs and tests.
- Wider
(0)handling —trunkZeroInParens(\(\s*0\s*\)) now strips the marker after a00prefix as well as+, gated by the newhasInternationalPrefix. - Two refusal keys —
validateAnswersCoreuseserr_phone_country_codeonly for a national number on a question with no default, anderr_invalid_phonewas reworded so it no longer asks for a code that is already applied. - Locales — both keys are present in all 12 catalogue files; the i18n parity, printf-verb and coverage guards pass.
- Tests — new rows for
0044 (0)…,+44 ( 0)…and+44( 0 )…, plus assertions pinning which refusal message each case gets;go test ./internal/i18n/and the phone handler tests pass. - Docs — the ARCHITECTURE phone-intake paragraph names both keys and the wider marker rule.
deepseek-v4.1-flash | 𝕏
| } | ||
| international := strings.HasPrefix(v, "+") | ||
| if hasInternationalPrefix(v) { | ||
| v = trunkZeroInParens.ReplaceAllString(v, "") |
There was a problem hiding this comment.
Stripping the marker for every international input now also strips the 0 of a code whose leading 0 is part of the number: +39 (0)6 1234 5678 → +39612345678, and 0039 (0)6 1234 5678, which normalised correctly before this commit, → the same wrong value. trunkZeroIsPartOfNumber exists to keep exactly that 0, so the international path needs the same exception; a row for both Italian forms in TestNormalizePhoneAnswer would pin it.
Technical details
# `(0)` is stripped for calling codes whose leading 0 is part of the number
## Affected sites
- `internal/handler/phone_answer.go:71` — `trunkZeroInParens.ReplaceAllString` runs for any `+`/`00` input without consulting `trunkZeroIsPartOfNumber` (line 47).
- `internal/handler/phone_answer_test.go` — no case covers a `(0)` number under `+39`/`+378`/`+379`.
- `docs/ARCHITECTURE.md:414` — states the marker is dropped, with no mention of the exception.
## Required outcome
- `+39 (0)6 1234 5678` and `0039 (0)6 1234 5678` → `+390612345678`.
- `+44 (0)20 7946 0958` and `0044 (0)20 7946 0958` keep normalising to `+442079460958`.
- The result does not depend on which international prefix was typed, and agrees with what `trunkZeroIsPartOfNumber` already does on the national path (`06 1234 5678` with `+39` → `+390612345678`).
## Suggested approach (optional)
- Read the calling code from the typed prefix (`+`/`00`, then 1–3 digits) and skip the `(0)` replacement when that code is in `trunkZeroIsPartOfNumber`; the marker's `0` then survives digit extraction.
- Regression evidence: at `bebf231`, `0039 (0)6 1234 5678` produced `+390612345678` (the digits still carried the `0`); it is `+39612345678` after `422fc73`.
- ITU's dialling-procedures table lists no national prefix for Italy, and the leading `0` belongs to the E.164 number.
## Open questions for the human (optional)
- Refusing the `(0)` form for these three codes (E.123 says parentheses do not belong in an international number) is also a valid outcome — silently dropping the `0` is not.| // and is dropped: 020 7946 0958 with +44 is +442079460958. | ||
| // trunkZeroInParens is a "(0)" written after an international prefix, as in | ||
| // +44 (0)20 ... or 0044 ( 0 )20 ...: the national trunk prefix, not part of the number. | ||
| var trunkZeroInParens = regexp.MustCompile(`\(\s*0\s*\)`) |
There was a problem hiding this comment.
trunkZeroInParens was inserted between trunkZeroIsPartOfNumber's doc comment and its declaration, so the map no longer has a doc comment and the regex inherits both paragraphs. Moving the regex declaration above the map, or moving the map's comment down with it, keeps go doc accurate.

Implements #260 as you accepted it: a
phoneintake-question type that renders atelinput and stores the answer in E.164, keeping the raw input alongside and refusing what it can't normalise.What it does
phone, rendered as<input type="tel" inputmode="tel" autocomplete="tel">on the booking page and in the widget.event_type_questions.default_country_code, e.g.+48, shown as the field's placeholder. The API accepts48or+48and stores+48, refuses anything that isn't a calling code, and drops it when the question changes to another type.booking_answers.valueholds+48600100200. What the attendee typed goes in the newbooking_answers.raw_valuewhen it differs, and is exposed asraw_valueonGET /v1/bookings/{id}/answers. Webhooks carry the E.164 value through the existing answers path.attendee_phonetop-level field, as you suggested.Normalisation (
normalizePhoneAnswer)It runs in
validateAnswersCore, so the booking page, the widget, MCP and the assistant all get the same result.validPhone.+or00.+44 20 7946 0958stays+442079460958even on a+48question. A(0)after the country code, as in+44 (0)20 …, is dropped.0, so020 7946 0958with+44gives+442079460958. The0is kept for the Italian numbering plan (+39,+378,+379), where it is part of the number.x12has no E.164 form), or a result outside 8–15 digits.err_invalid_phone, naming the question. The value is never stored as typed.Your three examples (
+48 600 100 200,0048 600 100 200,600-100-200→+48600100200) are test cases.This is a shape check, not a numbering-plan validator: it guarantees an SMS-gateway-safe form, not that the number is in service. If you'd rather have per-country length rules, libphonenumber would be the way, but that's a new dependency, so I didn't add it.
Migration
00078It follows the
00042/00073rebuild pattern to widen thetypeCHECK and adddefault_country_code:NO TRANSACTIONplusforeign_keys=OFF. That matters more here than forevent_types:booking_answers.question_idreferences this tableON DELETE CASCADE(00014), so a rebuild with foreign keys on deletes every intake answer. The migration test checks this: with thePRAGMAremoved, it finds 0 of 2 answers left.booking_answers.raw_valueis a plainADD COLUMN.The down step keeps the widened CHECK, as in 00042/00071/00073. It turns phone questions into text ones, whose stored answers are already plain E.164 strings, and drops the two columns.
Surfaces
book.htmlandembed.jseach get thetelfield.manage.htmlhas no intake form (a reschedule doesn't re-ask the questions), so it needs nothing.Tests
TestNormalizePhoneAnswer/TestNormalizeCountryCode: accepted and refused forms, including your three examples, foreign prefixes,(0), trunk-zero handling and its Italian exception, extensions and length limits.TestMigrate_phoneQuestionTypeKeepsQuestionsAndAnswers: seeded at 00077. After migrating, both answers and the select question's options survive,phoneand the new columns are accepted, an unknown type still violates the CHECK,foreign_key_checkis clean, and deleting a question still cascades.TestMigrate_phoneQuestionTypeDowncovers the down step.TestPhoneQuestion_*through the real handlers:CreateBooking, read back viaGET …/answers, with the expected value andraw_valuefor national,0048, foreign and already-E.164 input;telinput with its placeholder on the booking page and in the widget.gofmt,go vet ./...,go build ./...,go test ./...,go run ./cmd/i18n-coverage,pnpm check,pnpm check:i18n,pnpm build,pnpm test:visualandmake buildpass locally.I also checked it on a running binary in Chromium, with a
+48question:600 100 200 wew. 12was refused with the translated message, then600 100 200was stored as+48600100200, withraw_value600 100 200;+44 (0)20 7946 0958was stored as+442079460958.Merge notes
New booker key
err_invalid_phonein every locale (the non-English ones are drafts). The migration number follows #273/#275/#281/#283/#285 (00073–00077). It merges cleanly with our other open PRs, except a one-hunk CHANGELOG conflict with #282.