# Admin backend

The page `/backend` of the new UI, for kulibri admins only (the Admin role bit, `User::ROLE_ADMIN`). Decided with the
product owner on 26 Oct 2026: "we can no longer work blind". It is reached from the user menu ("Backend"); there is no
classic counterpart, so an admin who chose classic opens it with `?ui=v3` or the switch.

## Who counts as what

"Admin" here is the owner of a poll, as kulibri calls them, not a kulibri admin.

| Stage | Rule | Date |
|---|---|---|
| Account | registered | `users.created_at` |
| Admin | has a saved poll: type > 0, or type 0 after use (`options.initialised`); a poll cancelled while being created or never saved does not count | **admin since** = the first such poll |
| New admin | admin since less than 30 days | |
| Active admin | a *qualified* poll (types 1 to 4, at least two participants within 14 days, the definition of `docs/metrics.md`) | the first one makes an admin active |

The registration date says little: of the 47,535 accounts registered in the 12 months to July 2026, 44,189 (93 %) never
created a poll (participants who signed up through an invitation or with Google). The one SQL test for "saved" is
`DailyMetrics::everSavedSql()`.

## The page

- **Head ("Überblick"), by calendar week** (Monday to Sunday; product owner, 28 Nov 2026): four cards, the **running
  week** (Monday to today), the **last week**, the **last 4** and the **last 13 weeks**, each against the whole weeks 52 weeks
  before: the **first tries** (e-mail addresses that save a poll for the first time, until 28 Nov 2026 called "neue
  Admins"), split into organic and referred; how many first polls found at least 2 participants and their share of the first
  tries of the same weeks; the **real polls**; the **participations the new polls brought** and their average per saved
  poll; the **new accounts**; the first tries per 100 new accounts. Below, the last **60 weeks and the running one** of each
  as charts on one week axis, with the year before dashed and months below.
  - The running week is shown as it stands, next to the whole week a year before, **without a change in percent**.
  - **Counted at once:** where a figure of the newest 21 days (today included) is not stored yet, the server counts it when
    the page is opened (`DailyMetrics::live`); nothing is written, and a stored figure wins.
  - Everything about the polls grows for 14 days: until then it is marked **preliminary** (pale in the charts) and has no
    change; the 4 and 13 week cards take only final weeks for these figures and name them. Organic/referred and the first
    polls with participants come from nightly runs only; their cards name the weeks they cover, or show a dash.
  - Days are UTC. Definitions: `docs/metrics.md`.
- **"Woher neue Umfragen kommen"** (28 Nov 2026), under the head: per place (poll page new, poll page classic, dashboard,
  website, other) the openings of the new-poll page, the polls started and saved and the first tries, live, for the running
  week, the last week and the last 4 weeks (`GET /api/admin/new-poll-sources`; `docs/metrics.md`, "Where new polls come
  from").
- **Tab Admins:** 50 accounts per page; search by name or address; "Alle Admins" (default, newest first), "Aktiv (12
  Monate)", "Alle Konten" (participants and deleted accounts too). Per row: name, address, account since, stage and role
  labels, admin since, last poll and last login, saved polls in 4 weeks / 12 months / all, participants in them, the
  latest subscription, the chosen UI, a Beta switch and "Löschen". A filter "Registrierung" (29 Oct 2026).
  - **"nicht registriert"** marks an account that cannot sign in: created along the way (a poll made without registering,
    an invitation; `users.active` false) and never verified. Registering, a Google sign-in or "Passwort vergessen" (which
    verifies the address) make it registered. In the local copy 14,737 of 21,186 admins (70 %) were not registered. Such a
    person gets into their polls through "Passwort vergessen" (FAQ).
  - **"Zu den Umfragen"** opens the polls of the account (`/backend/accounts/{id}`): title, type, created, last answer,
    participants, options, status (open, deadline passed, closed, deleted) with the deadline, settings (hidden, seats,
    waitlist, only one option, answers locked, comments), the UI mode, and per poll "Admin-Link kopieren",
    "Teilnehmer-Link kopieren" and "Öffnen". The admin link is a master key: it is not in the list, it is fetched one
    poll at a time and every fetch is logged (`admin_link`). Newest first, at most 300.
  - **Beta on** asks first and sends the "Beta access granted" mail only when ticked; **off** saves at once. Both are logged.
  - **Löschen** is the account deletion that exists (`POST /api/user/delete`, anonymises: address `removed_<id>`, roles 0),
    confirmed by typing the address. Not offered for kulibri admins, deleted accounts or a running paid subscription; the
    server refuses deleting another kulibri admin (403). Logged.
- **Tab Umfragen** (29 Oct 2026): the polls of all accounts, 50 per page, newest first or by last change; the same
  table as "Zu den Umfragen" with a column **Admin** (the owner: name, address, "nicht registriert", a link to their
  polls). Search by title or by the owner's name or address; a type filter, where "Gelöscht" shows the deleted polls
  (otherwise left out). Admin links one at a time, logged, as above. Under 0.5 s per page on the local copy (101,000
  polls, also with a search).
- **Tab Feedback:** every stored feedback, newest first, with the sender's stage, roles, UI, page and window width, and a
  line about their polls of the last 30 days ("3 angelegt, 2 gespeichert (2 Termin), 1 mit Teilnehmern, 1 abgebrochen").
  A status for the team: new, read, answered, done ("Erledigt", product owner 11 Nov 2026). Only done is closed: answered
  means we replied and still wait for something. The filter starts on "Offen" (new + read + answered) every time, and
  each choice shows its number over all pages ("Offen (7)"; `counts` in `GET /api/admin/feedback`, `status=open`).
- **Answering a feedback** (product owner, 3 Nov 2026): "Antworten" under a feedback opens a text field and the name to
  sign with ("Alex" until changed, remembered in the browser). The mail goes to the address of the feedback (else the
  account's), from the usual sender with a reply to support, subject "Re: Dein Feedback", with the usual head and footer
  (variant `feedback`), "Mit besten Grüßen / Dein kulibri-Team / - Alex -" and the original message below. The answer,
  its time and the admin are kept (`feedback.reply`, `replied_at`, `replied_by`; migration of 3 Nov 2026) and the
  feedback is marked answered, or done with the tick "Danach als erledigt markieren" (`done: true`, 11 Nov 2026); the
  list shows the answer. Without an address there is no button.
- **Tab Kennzahlen** (1 Nov 2026): the funnel from a poll started to an admin's second poll, with the watchers beside it;
  the definitions and the reading rules are in `docs/metrics.md`, "Funnel view".

## Server

| Route (all `auth:sanctum, verified, hashids, logger` + `kulibri.admin`) | |
|---|---|
| `GET /api/admin/funnel` | `metrics_daily` summed per ISO week, rows `all`, `v3`, `legacy`, last 2 years and a quarter; a week's figure is null until its seven days are ready |
| `GET /api/admin/overview` | `metrics_daily` row `all`, last 2 years and 130 days, the newest 21 days counted live where not stored: `admins_new`, `admins_organic` and `admins_referred` (the new admins split by `admin_referrals`, null on a day the table does not cover yet; `docs/metrics.md`, "Organic and referred new admins"), `registrations` (the new accounts, 27 Nov 2026), `polls_real`, `polls_saved_14d`, `poll_participations` (28 Nov 2026), `admins_active` (sum of `cohort_admins_*`), `feedback` |
| `GET /api/admin/accounts?q=&scope=admins\|active\|all&sort=admin_since\|last_poll_at\|created_at&page=` | counts only for the rows of the page |
| `POST /api/admin/accounts/{id}/beta` `{on, notify}` | |
| `GET /api/admin/accounts/{id}/polls` | the polls of one account, no admin links; `registered=yes\|no` also filters the list above |
| `GET /api/admin/polls?q=&type=1\|2\|3\|4\|deleted&sort=created_at\|updated_at&page=` | all polls with `owner {id, name, email, registered}`, no admin links; without `type` no deleted polls |
| `POST /api/admin/polls/{id}/admin-link` | `{url}`, logged as `admin_link` with the poll id |
| `GET /api/admin/feedback?status=&page=` | 30 per page |
| `PUT /api/admin/feedback/{id}` `{status}` | |
| `POST /api/admin/feedback/{id}/reply` `{text, signature}` | the team's answer by mail; 422 `no_address` without an address |

- `App\Http\Middleware\KulibriAdmin` (alias `kulibri.admin`) answers 403 to anyone without the Admin role bit.
- `admin_actions`: who did what to which account and when (`beta_on`, `beta_off`, `account_deleted`, `admin_link`); written once.
- `feedback`: the stored feedback (`FeedbackController`, `docs/feedback-analytics.md`). The account comes from the session.
- SEC-004 is closed on the way: `POST /api/users/{id}/assign-beta` now lets an account add only itself (the classic
  promotion dialog), and only a kulibri admin anyone else.
- Speed on a copy of production (283,000 accounts, 125,000 polls): 2 to 4 s per page of the account list (the
  "admin since" of every account is grouped each time). Enough for a few admins; if it gets slow, a nightly summary per
  account replaces the live grouping.

## Later

- The old Blade report `/admin/reports/polls` (`Admin\PollReportController`) can go once the tab "Umfragen" is in use.
- The feedback button in classic (`docs/feedback-analytics.md`, stage 1).
- Where people drop out (stage 3 there).
