# Account registration

The public `konto/zarejestruj_sie/` form requires a choice between an adult
participant and a parent/guardian registering children. Both paths create an
independent account owned by the adult. The choice is input to registration,
not a permanent account role: either owner can later add children.

The owner supplies email, password, first and last name, mobile and the existing
required consents. The required terms checkbox also includes a declaration that
the submitted data is truthful and acknowledgement that accounts with false
data may be blocked or deleted. This uses the existing terms acceptance field;
it does not introduce automatic blocking or deletion.
Nickname and birth date are optional and appear with owner
identity fields. Marketing consents and RollPoints referral handling retain
their existing rules. City and district are not collected: registration always
writes SQL NULL to `city` and `city_area`, even if supplied in a crafted request.
Existing accounts and their stored locations are unchanged.

When `SURVEYS_ENABLED` is on, an optional survey-invitation switch appears in
its own **Zaproszenia do ankiet** section below marketing. It starts off, remains
independent of email/SMS marketing, and is preserved after validation errors.
The primary account stores `survey_invitations` and the explicit decision date;
children do not inherit the preference. The survey migration supplies these
columns; registration adds no further schema. With the module off, new survey
opt-ins are ignored. See `docs/surveys.md` and `tests/survey_registration_test.php`
(also `--disabled`) for the complete preference rules and isolated checks.

The parent path requires 1–5 children. The adult path supports an optional
switch at the end of the form; when disabled, child values are ignored. Each
child requires first name, last name and birth date; nickname is optional.
`User::validateChildIdentity()` shares normalization and validation with the
existing child editor. The owner retains the existing independent-account
birth-date validation in `docs/user-identity-data.md` (including its existing
age threshold). No new age rule is introduced for child profiles.

The server rejects a missing/unknown choice, malformed input, invalid child
identity and more than five submitted children. A failed request redisplays
safe owner/child input and the selected path, but never the password. Browser
validation supplements these checks. JavaScript progressively hides unrelated
sections; without it, the form exposes all sections and remains server-validated.

## Mobile availability and privacy

Before public registration creates an owner, children, password or referral binding,
`User::assertRegistrationMobileAvailable()` checks `user.mobile` and
`user.pending_mobile` across all records, including inactive, unconfirmed and child
profiles. No activity or confirmation filter allows an existing number to be reused.
Input formatting is stripped using the existing nine-digit validation. Both database
columns are integers; the mobile lock uses the same numeric identity.

A conflict returns only “Nie możemy założyć tego konta. Prosimy o kontakt z biurem.”
The error is attached to the form, without identifying the phone field or disclosing
an existing account. No activation notifications are requested for a rejected account.
The mobile check precedes the existing email check so a request with both conflicts
still receives this generic message.

Registration obtains database advisory locks for the normalized email and mobile
before checking availability, holding them through the family transaction. Requests
with different emails and the same phone are serialized; locks are released on
success or failure. Existing duplicates and historical records remain unchanged.
Public registration and self-service mobile changes share this availability rule.
Administrative editing and child creation retain their current behavior. It does not impose a global unique
index or rewrite numbers. No migration or configuration change is needed.

## Self-service mobile changes and administrator overrides

`User::saveUser()` reserves an unused replacement in `pending_mobile` under the same
numeric-mobile advisory lock as registration. `isMobileAvailable()` checks current
and pending numbers on all other user records, excluding the edited account.
A conflict throws a validation error displayed by the existing account editor:
“Nie możemy zmienić numeru telefonu. Prosimy o kontakt z biurem.” It neither saves
other submitted account fields nor requests an SMS. No other account is identified.

Keeping the current number, including a historical duplicate, remains allowed.
Entering it also cancels any pending replacement. Re-entering the own pending number
is allowed without another notification, provided no other record now uses it.
The current number stays active until the replacement is confirmed through SMS.
SMS confirmation checks replacement availability again. If an administrator has
assigned it elsewhere in the meantime, confirmation returns failure without changing
the account. Confirmation of a current historical number remains allowed.

The failed SMS confirmation page directs users with expired links to the login
form: after supplying their registration credentials and confirming email, they
can request a fresh SMS link themselves. Expiration alone does not require another
registration or office assistance. Office contact is suggested only if confirmation
still fails after renewal.

`saveUserByAdmin()` intentionally bypasses availability checks: an authorized
administrator can set any valid nine-digit phone, including an existing or reserved
number. No historical data is corrected, merged or deleted automatically. No new
migration, configuration or deployment operation is required.

Run `tests/user_mobile_change_test.php` on a fresh isolated schema prepared by
`scripts/prepare-rollpoints-tests.php`. It covers active/inactive and pending
conflicts, privacy, unchanged data/history/notifications on rejection, own current
and pending numbers, cancellation, admin overrides, SMS rechecks and promotion,
and concurrent change/change and change/registration attempts for the same phone.

## Persistence and activation

The owner, password, children and optional referral binding are committed in
one existing registration transaction. Child save failure rolls back the whole
family. Notifications are requested only after commit, using the existing
activation-delivery flow.

New children created by `saveChild()` under an owner whose email or mobile is
unconfirmed have `is_active = 0` and `is_registration_pending = 1`. They remain
attached through `id_parent`, but do not appear in active-child lists. A child
added under a fully confirmed active owner is immediately active. Saving an
existing child does not reset its activation state.

`activateChannel()` locks the active independent owner and confirms the selected
channel in one transaction. After both confirmations it activates only that
owner's inactive children marked `is_registration_pending`, clearing the flag.
Either confirmation order works; repeated valid links are harmless. Expired or
invalid links and inactive owners cannot activate children. Ordinary inactive
children (flag 0) are never reactivated by this process. Owner deactivation
already detaches children, so detached profiles cannot be activated through an
old owner's link. Existing administrative/manual mail-only confirmation does
not bypass the requirement for mobile confirmation.

## Deployment and verification

Migration `20261003120000_add_registration_pending_children.sql` adds the flag
with default 0 using an idempotent information-schema check. Apply it before
new code. Existing users are not reclassified or activated. No new configuration,
server files, assets outside Git or historical-data changes are required.
A code rollback leaves pending children inactive until this activation logic is
restored; do not bulk-activate inactive child accounts.

Run `tests/account_registration_test.php` with `RP_TEST_DATABASE` pointing to an
isolated schema prepared by `scripts/prepare-rollpoints-tests.php`. The test
applies the additive migration only there and suppresses real notifications.
It covers adult/parent paths, five children, invalid/oversized input, atomic
rollback, both confirmation orders, expired links and unrelated inactive
children, mobile conflicts (including inactive/pending numbers), unchanged historical
records, suppressed notifications and concurrent registrations with different emails
and the same mobile. Also run identity and referral regression tests. Browser checks cover
the initial choice, dynamic labels, optional children, adding/removing up to five
forms, and keeping typed child values when changing the selected path.

The central SEO policy keeps this account route titled `Rejestracja` and
`noindex, nofollow`; it does not belong in the public sitemap.
