feat: stage new items as pending until an admin publishes them (#90)
SonarQube Analysis / sonarqube (pull_request) Failing after 38m41s
Tests / lint (pull_request) Successful in 8m37s
Tests / backend-unit (pull_request) Successful in 1m22s
Tests / frontend-e2e (pull_request) Failing after 30m44s

An item used to be live on the storefront the instant it was created. Now it starts pending, and a customer sees it only once it is published.

The migration changes the column default and nothing else. Backfilling would un-publish the entire live catalogue, which is the one thing it must not do.

Hiding a pending item took four separate changes, not one, and that is the part worth knowing. The storefront's item routes had no status filter at all — sold items are listed and rendered with a Sold badge deliberately — so pending could not be expressed as one more optional filter. GET /api/items now carries an exclusion the caller cannot opt out of; GET /api/items/:id carries the same, because hiding an item from the list while still serving it by id would leave it reachable to anyone who kept a link; and GET /api/filters excludes pending from both aggregates it computes. That last one is the least obvious: a pending item would have inflated its tags' counts, so a customer would read "Rare (1)", filter by it, and be told nothing matches — and its price would have stretched the slider to a range no visible item occupies.

The tag count is computed over the joined items rather than filtered with a WHERE. A WHERE would have dropped the row for a tag whose only item is pending, and the tag would have vanished from the drawer instead of showing zero. There is a test for exactly that, because the first version of this query had that bug.

parseItemFilters is shared by the storefront and admin routes, so 'pending' parses on both. The public route refuses it explicitly rather than answering with an empty list, which would read as "no items match" instead of "you may not ask that". The storefront's URL reader is deliberately left not accepting it either, with a comment saying so, since a request guaranteed to fail is not worth constructing.

Publishing is the existing mark-available: same transition, same UPDATE, so the admin UI labels that button "Publish" when the item is pending rather than adding a second endpoint that does the same thing. Unpublish is new and is not symmetrical — it is refused for a reserved item, which someone is holding in their cart right now, and for a sold one, which is a record of something that happened rather than a draft. Both refusals name their reason, and the buttons are hidden in those states so the refusal is not how you find out.

Changing a column default has reach, and it surfaced eight test fixtures that silently depended on it. Each is now explicit about the status it wants rather than inheriting one — better practice regardless, and immune to the next default change. Two tests also used 'pending' as their example of an *unknown* status; both would have quietly become tautologies, so they now use one that is genuinely unknown.

Verified: 98 unit, 160 integration and 94 end-to-end passing, the last on a freshly created container. One earlier run showed a single failure in favorites.spec.ts; it passes in isolation and on a clean container, and is the cross-spec interference already recorded against the suite rather than anything from this change.

Refs #90
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-21 12:40:08 -05:00
co-authored by Claude Opus 5
parent adf480faf6
commit ecc2219fa5
24 changed files with 495 additions and 26 deletions
@@ -0,0 +1,78 @@
import { test, expect } from './fixtures';
const suffix = () => `s${Date.now().toString(36)}${Math.random().toString(36).slice(2, 7)}`;
// Creates an item and leaves it as it arrives — pending. Deliberately does not
// publish, unlike the other specs' fixtures, because the staged state is what
// is being tested here.
async function createStagedItem(page: import('@playwright/test').Page, name: string) {
const created = await page.request.post('/api/admin/items', {
multipart: { name, description: '', price: '55', category_id: '', tags: '[]' }
});
expect(created.ok()).toBeTruthy();
const body = await created.json();
// The whole premise: creating something does not publish it.
expect(body.status).toBe('pending');
return body.id as number;
}
const rowFor = (page: import('@playwright/test').Page, name: string) =>
page.getByRole('row').filter({ hasText: name });
test.describe('Staging an item until it is published', () => {
test('a new item is held back from the storefront until published', async ({ page }) => {
const name = `Staged ${suffix()}`;
await createStagedItem(page, name);
// Not in the catalogue while pending.
await page.goto('/');
await expect(page.getByText(name)).toHaveCount(0);
// It is in the admin, marked as pending.
await page.goto('/admin');
const row = rowFor(page, name);
await expect(row).toBeVisible();
await expect(row.getByText('PENDING')).toBeVisible();
await row.getByRole('button', { name: 'Publish' }).click();
await expect(row.getByText('AVAILABLE')).toBeVisible();
// And now a customer can see it.
await page.goto('/');
await expect(page.getByText(name)).toBeVisible();
});
test('publishing can be undone while nobody is holding the item', async ({ page }) => {
const name = `Staged ${suffix()}`;
const id = await createStagedItem(page, name);
expect((await page.request.post(`/api/admin/items/${id}/mark-available`)).ok()).toBeTruthy();
await page.goto('/');
await expect(page.getByText(name)).toBeVisible();
await page.goto('/admin');
const row = rowFor(page, name);
await row.getByRole('button', { name: 'Unpublish' }).click();
await expect(row.getByText('PENDING')).toBeVisible();
await page.goto('/');
await expect(page.getByText(name)).toHaveCount(0);
});
// The two features together, which is the reason for wanting both: a staged
// item is previewed as it will look once live, because a customer never sees
// the pending state and "how will this look" is the question being asked.
test('a pending item previews as it will look once published', async ({ page }) => {
const name = `Staged ${suffix()}`;
await createStagedItem(page, name);
await page.goto('/admin');
await page.getByRole('button', { name }).click();
const drawer = page.getByRole('dialog', { name: `Preview: ${name}` });
await expect(drawer).toBeVisible();
await expect(drawer.getByText('$55.00')).toBeVisible();
// The live card's action, not a pending placeholder.
await expect(drawer.getByRole('button', { name: 'Add to Cart' })).toBeVisible();
});
});