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

@@ -71,6 +71,21 @@ describe("entwuerfeAbfrage", () => {
expect(parameters).toContain("bösartig'; drop table hire_drafts; --");
});
it("lässt die Datenbank entscheiden, ob die Sperre noch gilt", () => {
// Mit der Uhr des Servers, der gerade rendert, gerechnet wäre es eine
// zweite Uhr und eine zweite Fassung der Frist — die Regeln in der
// Datenbank benutzen dieselbe Funktion.
const s = text();
expect(s).toContain('not app_entwurf_frei("d"."locked_by", "d"."locked_at")');
expect(s).toContain('left join "profiles" as "sp" on "sp"."id" = "d"."locked_by"');
});
it("filtert gesperrte Entwürfe nicht heraus", () => {
// Sie sollen dastehen, nur nicht anfassbar sein. Verschwänden sie,
// suchte man einen Entwurf, den es gibt.
expect(text()).not.toMatch(/where[^;]*locked_by/);
});
it("formatiert den Zeitstempel innerhalb von JSON ausdrücklich", () => {
// In JSON gibt Postgres ihn anders aus als der Treiber es täte, und der
// Unterschied fällt erst beim Vergleichen zweier Listen auf.
@@ -88,6 +103,9 @@ describe("baueEntwuerfe", () => {
created_by: ANDERE,
author_name: "Anna Berger",
author_email: "a@example.test",
gesperrt: false,
sperrer_name: null,
sperrer_email: null,
...teil,
};
}
@@ -129,6 +147,37 @@ describe("baueEntwuerfe", () => {
expect(e.vonMir).toBe(false);
});
it("lässt einen freien Entwurf ohne Sperrvermerk", () => {
// Leer heisst hier „bearbeitbar" — die Karte hängt daran, ob sie
// „Fortsetzen" anbietet.
expect(baueEntwuerfe([zeile()], ICH)[0].gesperrtVon).toBeNull();
});
it("nennt, wer den Entwurf gerade offen hat", () => {
const [e] = baueEntwuerfe([zeile({ gesperrt: true, sperrer_name: "Bernd Huber" })], ICH);
expect(e.gesperrtVon).toBe("Bernd Huber");
});
it("fällt beim Sperrer ohne Namen auf die E-Mail zurück", () => {
const [e] = baueEntwuerfe([zeile({ gesperrt: true, sperrer_name: " ", sperrer_email: "b@example.test" })], ICH);
expect(e.gesperrtVon).toBe("b@example.test");
});
it("sagt auch ohne jeden Namen, dass gesperrt ist", () => {
// Sonst wirkte „Fortsetzen" grundlos abgeschaltet, und die nächste
// Handlung wäre, es gleich noch einmal zu versuchen.
const [e] = baueEntwuerfe([zeile({ gesperrt: true, sperrer_name: null, sperrer_email: null })], ICH);
expect(e.gesperrtVon).toBe("jemandem");
});
it("unterscheidet fremd von gesperrt", () => {
// Ein fremder Entwurf ist bearbeitbar, ein gesperrter nicht — würden die
// beiden verwechselt, wäre die ganze Freigabe wirkungslos.
const [e] = baueEntwuerfe([zeile()], ICH);
expect(e.vonMir).toBe(false);
expect(e.gesperrtVon).toBeNull();
});
it("reicht Inhalt und Zeitpunkt unverändert weiter", () => {
const [e] = baueEntwuerfe([zeile()], ICH);
expect(e).toMatchObject({ id: "d1", step: 2, updated_at: "2026-09-09T08:00:00.000Z" });