feat(api): optimistic-concurrency contract for replace-all PUTs — PR1 infra + Block reference (#253)
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m24s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m25s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m24s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m25s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Adds the shared optimistic-concurrency contract so a stale second tab can no longer silently overwrite a fresher edit. PR1 lands the infra + the Block reference aggregate; PRs 2–4 fan the same recipe across the other 8 roots (design: #253#issuecomment-8472). Contract - `IVersionedAggregate` (`int Version`) on all 9 replace-all roots (ProgramSchedule, Block, Template, DecoTemplate, Playlist, Collection, Playout, MultiCollection, RerunCollection), EF-mapped `.IsConcurrencyToken()`; one dual-provider migration `AddAggregateVersions` (nullable:false, default 0). - Strong `ETag` of `Version` on the aggregate GET; `If-Match` on the PUT; mismatch → 412 (distinct from the §3a 409 build-lock guard). Successful PUT returns the new ETag. - `PreconditionFailedError : BaseError` → 412 in `ApiResults.ToErrorResult`; `ConcurrencyHeaders.ParseIfMatch/SetETag`; malformed If-Match → 400; `*`/absent = Phase-1 force-write. Block reference wiring - Handler: standalone `Either` via `CheckVersion` AFTER validation (never through `Apply`, which Join()-flattens the subtype to 422), unconditional `Version++`, `SaveChangesWithConcurrencyGuard` backstop (DbUpdateConcurrencyException → 412). - `BlockViewModel.Version` (header-only, not echoed in the body); controller sets the ETag on GET items and on the successful PUT. - SPA: `client.requestWithMeta` seam; `blocks.getBlockItemsWithMeta` + `replaceBlock` If-Match/ETag round-trip; `BlockEditor` holds the ETag, sends If-Match, and on 412 opens a blocking "changed elsewhere — reload" dialog. Tests - Handler contract tests: stale-If-Match → 412 (no mutation), matching/absent → success + bump, no-op save still bumps, and a two-context racing save → 412; proven non-vacuous (drop `.IsConcurrencyToken()` → the race test fails). - Controller tests: malformed If-Match → 400, If-Match threaded to the command, ETag on GET/PUT, 412 passthrough. SPA: requestWithMeta ETag, replaceBlock If-Match, 412 dialog. Docs: api-conventions §7a, spa-conventions §4a, domain-model glossary, decisions log. Refs #253 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
getBlock,
|
||||
getBlockGroups,
|
||||
getBlockItems,
|
||||
getBlockItemsWithMeta,
|
||||
getBlocks,
|
||||
previewBlock,
|
||||
replaceBlock,
|
||||
@@ -119,6 +120,44 @@ describe('blocks api client', () => {
|
||||
expect(body.items).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('getBlockItemsWithMeta returns the items and the ETag', async () => {
|
||||
vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
new Response(JSON.stringify([{ id: 1 }]), {
|
||||
headers: { ETag: '"5"', 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
})
|
||||
);
|
||||
|
||||
const result = await getBlockItemsWithMeta(4);
|
||||
|
||||
expect(result.etag).toBe('"5"');
|
||||
expect(result.data).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('replaceBlock sends If-Match when an ETag is supplied and returns the new ETag', async () => {
|
||||
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
new Response(JSON.stringify({ id: 4, items: [] }), {
|
||||
headers: { ETag: '"6"', 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
})
|
||||
);
|
||||
|
||||
const result = await replaceBlock(4, sampleReplace, '"5"');
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0];
|
||||
expect(init?.headers).toMatchObject({ 'If-Match': '"5"' });
|
||||
expect(result.etag).toBe('"6"');
|
||||
});
|
||||
|
||||
it('replaceBlock omits If-Match when no ETag is supplied', async () => {
|
||||
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ id: 4, items: [] }));
|
||||
|
||||
await replaceBlock(4, sampleReplace);
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0];
|
||||
expect(init?.headers).not.toHaveProperty('If-Match');
|
||||
});
|
||||
|
||||
it('previewBlock POSTs to the preview route', async () => {
|
||||
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse([]));
|
||||
await previewBlock(4, sampleReplace);
|
||||
|
||||
+20
-3
@@ -1,4 +1,4 @@
|
||||
import { ApiError, request } from './client';
|
||||
import { ApiError, request, requestWithMeta, type ResponseWithMeta } from './client';
|
||||
import type { components } from './generated/v1';
|
||||
|
||||
export type BlockGroup = components['schemas']['BlockGroupResponseModel'];
|
||||
@@ -49,8 +49,25 @@ export function getBlockItems(id: number): Promise<BlockItem[]> {
|
||||
return request<BlockItem[]>(`/api/blocks/${id}/items`);
|
||||
}
|
||||
|
||||
export function replaceBlock(id: number, body: ReplaceBlockRequest): Promise<BlockWithItems> {
|
||||
return request<BlockWithItems>(`/api/blocks/${id}`, { body, method: 'PUT' });
|
||||
/** Load block items together with the block's concurrency ETag (issue #253). */
|
||||
export function getBlockItemsWithMeta(id: number): Promise<ResponseWithMeta<BlockItem[]>> {
|
||||
return requestWithMeta<BlockItem[]>(`/api/blocks/${id}/items`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a block. Pass the last-seen ETag as `If-Match` to reject a stale overwrite with 412;
|
||||
* the resolved value carries the new ETag for a subsequent save (issue #253).
|
||||
*/
|
||||
export function replaceBlock(
|
||||
id: number,
|
||||
body: ReplaceBlockRequest,
|
||||
ifMatch?: string | null
|
||||
): Promise<ResponseWithMeta<BlockWithItems>> {
|
||||
return requestWithMeta<BlockWithItems>(`/api/blocks/${id}`, {
|
||||
body,
|
||||
method: 'PUT',
|
||||
headers: ifMatch ? { 'If-Match': ifMatch } : undefined
|
||||
});
|
||||
}
|
||||
|
||||
export function previewBlock(id: number, body: ReplaceBlockRequest): Promise<BlockPreviewItem[]> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { setStoredApiKey } from './auth';
|
||||
import { ApiError, request } from './client';
|
||||
import { ApiError, request, requestWithMeta } from './client';
|
||||
|
||||
describe('API request client', () => {
|
||||
beforeEach(() => {
|
||||
@@ -94,4 +94,40 @@ describe('API request client', () => {
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('requestWithMeta returns the response ETag alongside the parsed body', async () => {
|
||||
vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
new Response(JSON.stringify([{ id: 1 }]), {
|
||||
headers: { ETag: '"7"', 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
})
|
||||
);
|
||||
|
||||
const result = await requestWithMeta<{ id: number }[]>('/api/blocks/1/items');
|
||||
|
||||
expect(result.etag).toBe('"7"');
|
||||
expect(result.data).toEqual([{ id: 1 }]);
|
||||
});
|
||||
|
||||
it('requestWithMeta returns a null ETag when the response has none', async () => {
|
||||
vi.spyOn(window, 'fetch').mockResolvedValue(
|
||||
new Response(JSON.stringify({ ok: true }), {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
status: 200
|
||||
})
|
||||
);
|
||||
|
||||
const result = await requestWithMeta('/api/blocks/1/items');
|
||||
|
||||
expect(result.etag).toBeNull();
|
||||
});
|
||||
|
||||
it('requestWithMeta surfaces the ETag on a 204 (no body) response', async () => {
|
||||
vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { headers: { ETag: '"3"' }, status: 204 }));
|
||||
|
||||
const result = await requestWithMeta('/api/blocks/1', { method: 'PUT' });
|
||||
|
||||
expect(result.data).toBeUndefined();
|
||||
expect(result.etag).toBe('"3"');
|
||||
});
|
||||
});
|
||||
|
||||
+25
-4
@@ -24,10 +24,21 @@ export interface ApiRequestOptions extends Omit<RequestInit, 'body'> {
|
||||
|
||||
const mutatingMethods = new Set(['DELETE', 'PATCH', 'POST', 'PUT']);
|
||||
|
||||
export async function request<TResponse = unknown>(
|
||||
export interface ResponseWithMeta<T> {
|
||||
data: T;
|
||||
/** Strong ETag of the resource's version, when the endpoint emits one (issue #253). */
|
||||
etag: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link request} but also returns the response's ETag. Used by the optimistic-concurrency
|
||||
* editors: read the ETag from the load GET, send it back as `If-Match` on the replace PUT, and
|
||||
* replace it from the PUT response's ETag on every successful save (issue #253 / spa-conventions).
|
||||
*/
|
||||
export async function requestWithMeta<TResponse = unknown>(
|
||||
path: string,
|
||||
options: ApiRequestOptions = {}
|
||||
): Promise<TResponse> {
|
||||
): Promise<ResponseWithMeta<TResponse>> {
|
||||
const method = (options.method ?? 'GET').toUpperCase();
|
||||
const headers = normalizeHeaders({
|
||||
Accept: 'application/json',
|
||||
@@ -52,11 +63,21 @@ export async function request<TResponse = unknown>(
|
||||
throw new ApiError(response.status, await readProblemDetails(response));
|
||||
}
|
||||
|
||||
const etag = response.headers.get('ETag');
|
||||
|
||||
if (response.status === 204) {
|
||||
return undefined as TResponse;
|
||||
return { data: undefined as TResponse, etag };
|
||||
}
|
||||
|
||||
return await readJsonResponse(response) as TResponse;
|
||||
return { data: (await readJsonResponse(response)) as TResponse, etag };
|
||||
}
|
||||
|
||||
export async function request<TResponse = unknown>(
|
||||
path: string,
|
||||
options: ApiRequestOptions = {}
|
||||
): Promise<TResponse> {
|
||||
const { data } = await requestWithMeta<TResponse>(path, options);
|
||||
return data;
|
||||
}
|
||||
|
||||
function normalizeHeaders(headers: Record<string, string>): Record<string, string> {
|
||||
|
||||
@@ -205,6 +205,43 @@ describe('BlocksScreen', () => {
|
||||
expect(sent.items.map((i: { searchTitle: string }) => i.searchTitle)).toEqual(['A', 'B']);
|
||||
});
|
||||
|
||||
it('shows a conflict dialog and reloads when the block changed elsewhere (412)', async () => {
|
||||
window.history.pushState({}, '', '/app/blocks/4');
|
||||
let putCount = 0;
|
||||
const fetchMock = mockApi({
|
||||
items: [blockItem({ id: 1, searchTitle: 'A', searchQuery: 'a' })],
|
||||
onRequest: (url, method) => {
|
||||
if (url === '/api/blocks/4' && method === 'PUT') {
|
||||
putCount += 1;
|
||||
if (putCount === 1) {
|
||||
return new Response(
|
||||
JSON.stringify({ status: 412, title: 'Precondition Failed', detail: 'stale' }),
|
||||
{ headers: { 'Content-Type': 'application/json' }, status: 412 }
|
||||
);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
render(<BlocksScreen />);
|
||||
|
||||
await waitFor(() => expect(screen.getByDisplayValue('Morning')).toBeInTheDocument());
|
||||
|
||||
const itemsGetCount = () =>
|
||||
fetchMock.mock.calls.filter(([u, init]) => u === '/api/blocks/4/items' && (init?.method ?? 'GET') === 'GET')
|
||||
.length;
|
||||
const before = itemsGetCount();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /Save block/ }));
|
||||
|
||||
// A 412 opens the "changed elsewhere" dialog rather than showing a generic save error.
|
||||
expect(await screen.findByText(/Reload to get the latest version/i)).toBeInTheDocument();
|
||||
|
||||
// Reloading re-fetches the block items.
|
||||
fireEvent.click(screen.getByRole('button', { name: /^Reload$/ }));
|
||||
await waitFor(() => expect(itemsGetCount()).toBeGreaterThan(before));
|
||||
});
|
||||
|
||||
it('round-trips legacy Artist and MultiCollection items through load and save', async () => {
|
||||
window.history.pushState({}, '', '/app/blocks/4');
|
||||
const fetchMock = mockApi({
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
Spinner
|
||||
} from '../components';
|
||||
import {
|
||||
ApiError,
|
||||
copyBlock,
|
||||
createBlock,
|
||||
createBlockGroup,
|
||||
@@ -34,7 +35,7 @@ import {
|
||||
deleteBlockGroup,
|
||||
getBlock,
|
||||
getBlockGroups,
|
||||
getBlockItems,
|
||||
getBlockItemsWithMeta,
|
||||
getBlocks,
|
||||
messageFromBlockError,
|
||||
previewBlock,
|
||||
@@ -853,15 +854,21 @@ function BlockEditor({ blockId }: { blockId: number }) {
|
||||
const [previewItems, setPreviewItems] = useState<BlockPreviewItem[] | null>(null);
|
||||
const [previewOpen, setPreviewOpen] = useState(false);
|
||||
const [previewing, setPreviewing] = useState(false);
|
||||
const [conflictOpen, setConflictOpen] = useState(false);
|
||||
const [reloadKey, setReloadKey] = useState(0);
|
||||
// Concurrency ETag (issue #253): captured from the items GET, sent as If-Match on save, and
|
||||
// replaced from the PUT response on every successful save.
|
||||
const etagRef = useRef<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
|
||||
Promise.all([getBlock(blockId), getBlockItems(blockId), getWatermarks(), getGraphicsElements()])
|
||||
.then(([blockData, items, watermarkData, graphicsData]) => {
|
||||
Promise.all([getBlock(blockId), getBlockItemsWithMeta(blockId), getWatermarks(), getGraphicsElements()])
|
||||
.then(([blockData, itemsMeta, watermarkData, graphicsData]) => {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
etagRef.current = itemsMeta.etag;
|
||||
setBlock(blockData);
|
||||
setWatermarks(watermarkData);
|
||||
setGraphicsElements(graphicsData);
|
||||
@@ -870,7 +877,7 @@ function BlockEditor({ blockId }: { blockId: number }) {
|
||||
hours: Math.floor(blockData.minutes / 60),
|
||||
minutes: blockData.minutes % 60,
|
||||
stopScheduling: blockData.stopScheduling,
|
||||
items: items.map(itemFromResponse)
|
||||
items: itemsMeta.data.map(itemFromResponse)
|
||||
});
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
@@ -882,7 +889,7 @@ function BlockEditor({ blockId }: { blockId: number }) {
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [blockId]);
|
||||
}, [blockId, reloadKey]);
|
||||
|
||||
if (loadError) {
|
||||
return (
|
||||
@@ -972,15 +979,29 @@ function BlockEditor({ blockId }: { blockId: number }) {
|
||||
setSaving(true);
|
||||
setSaveError(null);
|
||||
try {
|
||||
await replaceBlock(blockId, toReplaceRequest(draft));
|
||||
const { etag } = await replaceBlock(blockId, toReplaceRequest(draft), etagRef.current);
|
||||
etagRef.current = etag;
|
||||
navigateToPath(BASE_PATH);
|
||||
} catch (error) {
|
||||
setSaveError(messageFromBlockError(error, 'Unable to save block'));
|
||||
if (error instanceof ApiError && error.status === 412) {
|
||||
// Another edit landed since we loaded — force a reload rather than overwriting it (#253).
|
||||
setConflictOpen(true);
|
||||
} else {
|
||||
setSaveError(messageFromBlockError(error, 'Unable to save block'));
|
||||
}
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const reloadAfterConflict = () => {
|
||||
setConflictOpen(false);
|
||||
setSaveError(null);
|
||||
setDraft(null);
|
||||
setBlock(null);
|
||||
setReloadKey((key) => key + 1);
|
||||
};
|
||||
|
||||
const runPreview = async () => {
|
||||
if (validationError || previewing) {
|
||||
return;
|
||||
@@ -1296,6 +1317,17 @@ function BlockEditor({ blockId }: { blockId: number }) {
|
||||
<div className="ctv-collections-empty">No preview items were produced.</div>
|
||||
)}
|
||||
</Dialog>
|
||||
|
||||
<ConfirmDialog
|
||||
cancelLabel="Keep editing"
|
||||
confirmLabel="Reload"
|
||||
message="This block was changed elsewhere since you opened it. Reload to get the latest version — your unsaved changes will be discarded."
|
||||
onCancel={() => setConflictOpen(false)}
|
||||
onConfirm={reloadAfterConflict}
|
||||
open={conflictOpen}
|
||||
title="Block changed elsewhere"
|
||||
tone="danger"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user