# Audit trail for polls – concept

Status: proposal for discussion with Mitko, 13 October 2026. Nothing is built yet.
Author: Andreas (with Claude Code). Related: `SECURITY-BACKLOG.md`, `docs/premium.md`, `V3-PARITY-PLAN.md`.

## 1. Why

1. **Data processing agreement (AVV, GDPR Art. 28 and 32).** For our customers (poll admins) Kulibri processes personal data of
   their participants: names, email addresses, answers, custom fields, comments. Our AVV promises, in rather vague words, that
   changes to this data can be traced. The usual TOM item behind it is *input control* (Eingabekontrolle): being able to check
   afterwards whether, when and by whom personal data was entered, changed or removed. Today we cannot do that (section 2).
2. **Recovery after a user error.** "I deleted a participant / a date by mistake, with all answers" can today only be fixed by
   restoring a whole database backup, which is not realistic for a single poll.
3. **A recurring user question.** "Who changed what, and when?" – for the poll itself and for the answers.

Not the same thing, but related: GDPR Art. 32(1)(c) asks that availability can be restored after a technical incident. That is
the job of the database backups (section 9), not of the audit trail.

## 2. What exists today (checked on the production copy, 13 Oct 2026)

| What | Today |
|---|---|
| Timestamps | Every table has `created_at` / `updated_at` (polls 125k rows, choices 673k, participants 705k, preferences 7.0M, comments 53k, invitations 526k). They say *when a row last changed*, not *who* changed it and not *what it was before*. |
| Deletes | Hard deletes everywhere, no `deleted_at`. `POST /poll/choices/{id}` deletes every choice that is not sent again (full sync). |
| "Last changed" per row | Available: every save of answers touches the participant (`ParticipantController`, `touch()`), so `participants.updated_at` is the last change of the row, answers or name and address alike. Checked on the production copy (14 Oct 2026, 1,373 recent participants): no real answer (value other than -1) is newer than it. Newer `preferences.updated_at` values do exist, but they are the empty answers (-1) the server adds when the admin adds an option, not answers. It says *when*, not *what* or *who* (the admin and the participant touch it alike). |
| Request log | `LoggerMiddleware` writes path, method and time to the log file. No user, no participant, no content. |
| Who is acting | `App\Services\PollAccessResolver` already works out, per request, whether the caller is the admin, a sub-admin or which participant (`PollAccess`: `isAdmin`, `isSubAdmin`, `requestingParticipantId`). `SupportLoginBackdoor` lets support act as a customer. |
| Backups | `backup:database` daily at 00:10, `BackupInspector` rejects truncated dumps, `backup:cleanup` deletes dumps older than `backup.retention_days`. |

So a history cannot be rebuilt from today's data. It starts on the day we begin to record.

## 3. Goals and non-goals

Goals
- An append-only record per poll: who, when, what, with the old and the new value.
- A full snapshot for destructive actions, so a deleted participant, choice or comment can be restored.
- Recorded on the server, so the classic and the new UI are covered alike, and no client can skip it.
- A "Verlauf" (history) view for the poll admin in the new UI; restore for the cases in section 7.
- Retention and erasure rules that fit the GDPR (section 8).

Non-goals (for now)
- Full versioning of a poll with "restore the poll as of date X" (conflicts with answers given since).
- Account-level events (login, password change, billing). Different purpose, can reuse the same table later.
- A history view in the classic UI.

## 4. Data model

One new table, `poll_events`:

| Column | Type | Notes |
|---|---|---|
| `id` | bigint | |
| `poll_id` | bigint, indexed | no foreign key cascade: events must survive the poll's deletion until retention ends |
| `occurred_at` | datetime(3), indexed with `poll_id` | |
| `actor_type` | enum | `account`, `admin_link`, `participant`, `public_link`, `support`, `system` |
| `actor_user_id` | bigint null | the account, if logged in (for `support`: the support account) |
| `actor_participant_id` | bigint null | the participant acting through their link |
| `actor_name` | varchar(255) null | name **at that time** (participants rename themselves) |
| `ip_hash` | char(64) null | truncated IP (last octet zeroed) and hashed; see section 8 |
| `action` | varchar(40) | fixed list, see below |
| `subject_type` / `subject_id` | varchar / bigint null | the participant, choice, comment … concerned |
| `changes` | json | old/new values, or the snapshot for deletes |
| `request_id` | char(36) | groups the events of one save (one "Speichern" can change title, options and choices) |

Size: one event per save, not per cell. Rough estimate from today's volumes: a few million rows over the years, a few hundred
bytes each. Index `(poll_id, occurred_at)` for the history view; `subject_type, subject_id` for "history of this participant".

### Actions

| Action | Written by (route → method) | `changes` |
|---|---|---|
| `poll.created` | `POST /new`, `/create` → `PollController::postNew` | type, title |
| `poll.details_changed` | `POST /poll/details/{id}` → `postDetails` | changed fields only: title, description, deadline, type, owner (reassign) |
| `poll.options_changed` | `POST /poll/options/{id}`, `PUT /poll/option/update/{id}`, `POST {id}/choice-configure` | changed option keys only (hidden mode, seats, lock after submit …) |
| `poll.choices_changed` | `POST /poll/choices/{id}` → `postChoices` | added / changed (old→new) / removed choices; removed ones as snapshot **with their answers** |
| `poll.appearance_changed` | `POST /poll/image/{id}`, `/poll/logo/{id}`, design assignment | old/new reference only |
| `poll.closed` / `poll.reopened` / `poll.archived` | close panel, `PUT /poll/{id}/archive` | final choice(s) |
| `poll.deleted` | `DELETE /poll/{id}`, editor "cancel while creating" (type 0) | snapshot of the poll row |
| `poll.fields_changed` | `PUT /participantattributes` | field definitions old/new (incl. visibility, mandatory) |
| `participant.added` | `POST /poll/{id}` (`addNewParticipants`), self-registration | name, email, custom field values, initial answers |
| `participant.answers_changed` | `POST /participant/{id}`, `POST /v2/participant/{id}` | per choice old→new value (only the changed cells), guest list choice/guests |
| `participant.booking_cancelled` | `POST /participant/cancelBooking/{id}` | choice, old value |
| `participant.changed` | `PUT /participant/{id}`, `PUT /participant/name/{id}` | name, email, custom fields, sub-admin flag old/new |
| `participant.removed` | `DELETE /participant/{id}` | **full snapshot**: participant row, answers, custom field values, invitation link |
| `answers.removed` | `POST {id}/remove-choice-answers` | snapshot of the removed answers |
| `invitation.sent` / `invitation.removed` | `POST /invitation`, `DELETE /invitation/{id}` | recipients (count and addresses) / snapshot |
| `comment.added` / `comment.removed` | `POST /comment/{pollId}`, `DELETE /comment/{id}` | text / snapshot |
| `task.*`, `resource.*` | task and resource routes | old/new |
| `message.sent` | `POST /reminder-to-participant`, `/message-to-admin` | recipients count, subject (not the body) |

Reading (`GET` routes, `POST /invitation/visit`, Excel export) is not recorded; the export could be, if the AVV asks for it
(open question 6).

## 5. Who is acting

Resolved once per request by a small service, reusing `PollAccessResolver`:

1. Support login active (`SupportLoginBackdoor`) → `support`, with the support account. This must be visible as such in the history.
2. Logged-in owner or sub-admin → `account`.
3. Admin key in the link, no session → `admin_link`.
4. Participant key / personal link (`requestingParticipantId`) → `participant`.
5. Otherwise (public link, anonymous) → `public_link`; `actor_name` = the name typed in, if any.
6. Console commands and jobs → `system`.

## 6. Writing

- One service, e.g. `App\Services\PollAuditLog::record(Poll $poll, string $action, ?Model $subject, array $changes)`, called
  in the controllers listed above, **inside the same transaction as the change**: either both are stored or neither.
- Diffs are computed in the service from the model state before and after (`getOriginal()` / a fetched copy); for the
  choices full sync, the service compares the old and the new list by id.
- Several of these routes are public-link routes without `auth:sanctum`; the service must not trust ids from the request body
  for the actor, only what `PollAccessResolver` resolves.
- Tests: one feature test per action (the event is written, with the right actor and diff) and one that a failing write rolls
  the change back.

## 7. Reading and restoring

- `GET /api/poll/{id}/events?before=…&limit=50` – admin and sub-admin only (`PollAccessResolver`), newest first, paged.
  Names of the actors are returned as stored; on hidden polls only the admin sees anything.
- New UI: a tab "Verlauf" next to "Kommentare" in the admin view: a timeline, filter by person and by kind (answers / poll),
  e.g. "12.10. 14:03 · Anna hat ihre Antworten geändert: Mo 3.8. Nein → Ja".
- Restore (admin only), from the snapshot of the event:
  - `participant.removed` → participant with answers and custom field values, as a new row (new id), event `participant.restored`.
  - `poll.choices_changed` (a removed choice) → the choice with its answers.
  - `comment.removed` → the comment.
  A restore is refused when it conflicts (e.g. the email address is in the poll again); the UI says so.

## 8. Privacy (to be confirmed with our data protection officer)

- The audit trail is itself personal data: it goes into the record of processing activities and the TOM description.
- Retention proposal: events are kept for 12 months, and at most until 12 months after the poll is deleted; a daily job removes
  older events (like `backup:cleanup`).
- Right to erasure: when a participant's data is erased (participant deleted *by request*, account deleted), their events are
  anonymised (name, email, custom field values in `changes` replaced by "gelöschter Teilnehmer"), not removed, so the history
  stays consistent. Snapshots of that participant are removed with it.
- IP addresses only truncated and hashed, or not at all (open question 5).
- Access to the history by Kulibri support is itself logged (`support` actor).

## 9. Backups (GDPR Art. 32(1)(c))

Separate from the audit trail, but part of the same TOM chapter. What is there: a daily dump, a check for truncated dumps, a
cleanup by age. What is missing: a documented and tested restore. Proposal: restore the latest dump into a scratch database
once a quarter and record the result.

## 10. Effort and phases

| Phase | Content | Estimate |
|---|---|---|
| 1 | Table, `PollAuditLog`, actor resolution, events for answers and participants (the most valuable part), tests | 2 days |
| 2 | Events for poll details, options, choices (with snapshots), comments, invitations, tasks; tests | 1.5 days |
| 3 | Read API, "Verlauf" tab in the new UI | 1 day |
| 4 | Restore for participant, choice, comment; retention job; anonymisation on erasure | 1.5 days |

Phase 1 alone already answers "who changed which answers when". Without any backend change the new UI can already show *when* a row was last changed (`participants.updated_at`, in the poll response today).

## 11. Open questions

For Mitko
1. Agreed to record in the controllers (explicit calls) rather than with model events / a package like
   `spatie/laravel-activitylog`? Reason: many writes bypass Eloquent events (query builder, the choices full sync), and the
   actor is often not a logged-in user.
2. Same transaction as the change, or a queued write? (Proposal: same transaction; the TOM promise is only as good as the
   gaps.)
3. Which write paths are missing from section 4 (jobs, admin tools, the Flutter API)?
4. Retention job and anonymisation: where do participant deletions "by request" come from today?

For the product owner / data protection
5. Store a truncated, hashed IP, or none?
6. Should reads (Excel export, viewing a hidden poll by support) be recorded too?
7. History for everyone or a Premium feature? Restore for everyone?
8. Rewording of the AVV/TOM annex once phase 1 to 4 are live, so the promise matches what is built.
9. Who created a participant row? (product owner, 5 Nov 2026) The grid's "Meine Zeilen zuerst" has to guess from the browser today
   (see `ROADMAP.md`, "Know who created a participant row"). The first event of each row in this trail answers it, so the
   history should record the creator for every way a row comes into being: own answer, admin adds with or without an address,
   invitation, import.
