Let colleagues finish each other's drafts, one at a time
All checks were successful
CI / Lint, Typen, Tests, Build (push) Successful in 11m18s
CI / Migrationen auf leerer Datenbank (push) Successful in 10m45s

Seeing a colleague's draft turned out to be half a feature: the point of
sharing it is to finish it while they are away. So writing is allowed
now -- but never by two people at once.

A draft is a single JSONB field. Whoever saves writes the whole state,
not the changed field, so two open wizards overwrite each other
completely and the second person sees nothing wrong: their own state is
right there on screen. That is why writing stayed with the owner until
now, and a lock is what makes giving that up safe.

The lock lives in the row (locked_by, locked_at) and is enforced by the
update and delete policies, not by the application. It expires, and that
is the important half: releasing happens when the wizard closes, and a
closed laptop never closes a wizard. Without expiry one crashed tab
would take a draft away for good -- worse than the problem being solved.
The wizard refreshes its lock while open so a long form does not lose it
mid-way.

Delete had to widen too, which reads like more than was asked for: the
wizard deletes the draft once the person is hired. Without it the hire
would go through and the draft would sit there forever. The card still
only offers delete on your own drafts.

Four of five mutations against the lock go red. The fifth -- dropping
`!open` from the refresh guard -- does not, because freigeben() already
nulls the ref the interval checks. The condition stays as the readable
statement of intent, now with a comment saying so.

Not run against a live database here; the CI migration job is the first
real execution.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-10 17:43:43 +02:00
parent f17d299045
commit 99e4357c02
10 changed files with 687 additions and 65 deletions

View File

@@ -28,6 +28,7 @@ function entwurf(teil: Partial<Entwurf> = {}): Entwurf {
updated_at: "2026-09-09T08:00:00.000Z",
vonMir: true,
autor: "Max Stubhan",
gesperrtVon: null,
...teil,
};
}
@@ -75,26 +76,42 @@ describe("DraftsCard", () => {
expect(within(zeile("Eva Novak")).getByText("von Anna Berger")).toBeInTheDocument();
});
it("bietet an fremden Entwürfen weder Fortsetzen noch Löschen", () => {
// Die Datenbank wiese beides ab (hire_drafts_update, hire_drafts_delete).
// Eine Schaltfläche, die verlässlich in einen Fehler führt, ist ein
// Versprechen ohne Deckung.
it("lässt auch fremde Entwürfe fortsetzen", () => {
// Der Sinn der ganzen Freigabe: wer vertreten wird, soll die angefangene
// Einstellung zu Ende bringen können.
zeige([fremd]);
const z = zeile("Eva Novak");
expect(within(z).queryByRole("button", { name: "Fortsetzen" })).not.toBeInTheDocument();
expect(within(z).queryByRole("button", { name: "Entwurf löschen" })).not.toBeInTheDocument();
expect(within(zeile("Eva Novak")).getByRole("button", { name: "Fortsetzen" })).toBeInTheDocument();
});
it("sagt an fremden Entwürfen, warum dort nichts steht", () => {
// Ohne den Hinweis sähe die Zeile aus wie eine, an der die Schaltflächen
// fehlen.
it("bietet an fremden Entwürfen kein Löschen", () => {
// Die Regel liesse es zu — sie muss, weil der Assistent den Entwurf nach
// dem Anlegen selbst wegräumt. Eine Schaltfläche, die fremde halbfertige
// Arbeit wegwirft, gehört trotzdem nicht neben eine, die sie fortsetzt.
zeige([fremd]);
expect(within(zeile("Eva Novak")).getByText("Nur zur Ansicht")).toBeInTheDocument();
expect(within(zeile("Eva Novak")).queryByRole("button", { name: "Entwurf löschen" })).not.toBeInTheDocument();
});
it("trennt die Zeilen: eigene bedienbar, fremde nicht", () => {
it("nimmt Fortsetzen weg, solange jemand anderes drin ist", () => {
zeige([entwurf({ gesperrtVon: "Bernd Huber" })]);
expect(within(zeile("Manuel Aigner")).queryByRole("button", { name: "Fortsetzen" })).not.toBeInTheDocument();
});
it("sagt, wer den gesperrten Entwurf gerade bearbeitet", () => {
// Eine graue Schaltfläche ohne Erklärung liest sich wie ein Fehler, und
// die nächste Handlung wäre, es gleich noch einmal zu versuchen.
zeige([entwurf({ gesperrtVon: "Bernd Huber" })]);
expect(within(zeile("Manuel Aigner")).getByText(/Bernd Huber bearbeitet gerade/)).toBeInTheDocument();
});
it("sperrt auch das Löschen des eigenen Entwurfs, solange jemand drin ist", () => {
// Sonst risse man jemandem den Entwurf unter den Händen weg.
zeige([entwurf({ gesperrtVon: "Bernd Huber" })]);
expect(within(zeile("Manuel Aigner")).getByRole("button", { name: "Entwurf löschen" })).toBeDisabled();
});
it("trennt die Zeilen: freie bedienbar, gesperrte nicht", () => {
// Beide nebeneinander — der Fall, in dem eine Verwechslung teuer wird.
zeige([entwurf(), fremd]);
zeige([entwurf(), entwurf({ id: "d9", payload: { firstName: "Eva", lastName: "Novak" }, gesperrtVon: "Bernd Huber" })]);
expect(within(zeile("Manuel Aigner")).getByRole("button", { name: "Fortsetzen" })).toBeInTheDocument();
expect(within(zeile("Eva Novak")).queryByRole("button", { name: "Fortsetzen" })).not.toBeInTheDocument();
});