Skip to content

feat(questions): phone intake-question type, stored in E.164 (#260) - #286

Open
dominik-myszkowski wants to merge 2 commits into
Calnode:mainfrom
dominik-myszkowski:feat/phone-question-type
Open

dominik-myszkowski wants to merge 2 commits into
Calnode:mainfrom
dominik-myszkowski:feat/phone-question-type

Conversation

@dominik-myszkowski

Copy link
Copy Markdown
Contributor

Implements #260 as you accepted it: a phone intake-question type that renders a tel input and stores the answer in E.164, keeping the raw input alongside and refusing what it can't normalise.

What it does

  • New question type phone, rendered as <input type="tel" inputmode="tel" autocomplete="tel"> on the booking page and in the widget.
  • Default country code per question: event_type_questions.default_country_code, e.g. +48, shown as the field's placeholder. The API accepts 48 or +48 and stores +48, refuses anything that isn't a calling code, and drops it when the question changes to another type.
  • Stored in E.164: booking_answers.value holds +48600100200. What the attendee typed goes in the new booking_answers.raw_value when it differs, and is exposed as raw_value on GET /v1/bookings/{id}/answers. Webhooks carry the E.164 value through the existing answers path.
  • No attendee_phone top-level field, as you suggested.
  • Admin: the "Phone number" type and a default-country-code field in the questions panel (en/fr). Duplicating an event type copies the code.

Normalisation (normalizePhoneAnswer)

It runs in validateAnswersCore, so the booking page, the widget, MCP and the assistant all get the same result.

  1. The input must pass the existing lenient validPhone.
  2. An explicit international prefix wins: + or 00. +44 20 7946 0958 stays +442079460958 even on a +48 question. A (0) after the country code, as in +44 (0)20 …, is dropped.
  3. Otherwise the question's default code is prepended, after dropping a national trunk 0, so 020 7946 0958 with +44 gives +442079460958. The 0 is kept for the Italian numbering plan (+39, +378, +379), where it is part of the number.
  4. Refused: a national-format number on a question with no default code (nothing to normalise against), an extension (x12 has no E.164 form), or a result outside 8–15 digits.
  5. A refusal is a 400 with a new booker key, 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 00078

It follows the 00042/00073 rebuild pattern to widen the type CHECK and add default_country_code: NO TRANSACTION plus foreign_keys=OFF. That matters more here than for event_types: booking_answers.question_id references this table ON DELETE CASCADE (00014), so a rebuild with foreign keys on deletes every intake answer. The migration test checks this: with the PRAGMA removed, it finds 0 of 2 answers left.

booking_answers.raw_value is a plain ADD 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.html and embed.js each get the tel field. manage.html has 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, phone and the new columns are accepted, an unknown type still violates the CHECK, foreign_key_check is clean, and deleting a question still cascades. TestMigrate_phoneQuestionTypeDown covers the down step.
  • TestPhoneQuestion_* through the real handlers:
    • question create/PATCH and country-code handling;
    • bookings via CreateBooking, read back via GET …/answers, with the expected value and raw_value for national, 0048, foreign and already-E.164 input;
    • 400 naming the question for unparseable input, extensions and too-short numbers, and for a required question left blank;
    • a national number refused without a default code, with international accepted and an optional question left out;
    • the tel input 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:visual and make build pass locally.

I also checked it on a running binary in Chromium, with a +48 question:

  • booking page (mobile, Polish): 600 100 200 wew. 12 was refused with the translated message, then 600 100 200 was stored as +48600100200, with raw_value 600 100 200;
  • widget on a page from another origin: +44 (0)20 7946 0958 was stored as +442079460958.

Merge notes

New booker key err_invalid_phone in 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.

…#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.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 — 00078 widens the event_type_questions type CHECK and adds default_country_code; booking_answers.raw_value is a plain ADD COLUMN. Verified the rebuild loses no index (there were none) and that the booking_answers cascade still works after the rename.
  • Normalisation — normalizePhoneAnswer / normalizeCountryCode, the trunk-zero rule and its Italian-plan exception, and the phone branch in validateAnswersCore storing E.164 plus raw_value.
  • API and admin — questionColumns unifies the question SELECTs, create/update validation normalises and clears the code correctly, duplication copies it, and QuestionsPanel.svelte plus the en/fr keys are complete.
  • Surfaces — tel inputs on book.html and embed.js; manage.html correctly needs nothing, and .field input already styles the new field.
  • Docs/i18n — the ARCHITECTURE section, the CHANGELOG entry, and err_invalid_phone in 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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash | 𝕏

Comment thread internal/handler/phone_answer.go Resolved
Comment thread internal/i18n/locales/en.json Resolved
…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.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 a 00 prefix as well as +, gated by the new hasInternationalPrefix.
  • Two refusal keys — validateAnswersCore uses err_phone_country_code only for a national number on a question with no default, and err_invalid_phone was 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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash | 𝕏

}
international := strings.HasPrefix(v, "+")
if hasInternationalPrefix(v) {
v = trunkZeroInParens.ReplaceAllString(v, "")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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*\)`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

shockalotti added a commit that referenced this pull request Dec 1, 2026
#281 holds 00075, and the queue runs 00076 #283, 00077 #285, 00078 #286.
This must merge after those, so it takes 00079. A lower number landing later
leaves a gap and goose refuses to boot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant