feat(intake): regenerate, discard and restore a draft (#225)

Regenerate clears attempts along with the state. The worker only picks up rows below the attempt cap, so re-queueing a draft that has already failed three times without clearing them would produce a button that appears to work, does nothing, and leaves nothing anywhere to say why.

Discard deletes nothing — not the item, not the photographs. It is one click away in what amounts to an inbox, and the photos are often the only copy of something no longer in the sender's hands, so the destructive reading of the word is deliberately not available here. The item returns to pending, because a discarded submission must not stay on sale.

Restore returns a draft at the state its own contents justify rather than unconditionally ready. A submission discarded before it was ever drafted has no copy, and coming back as ready would present an empty draft as a finished one. Judged on whether a name was ever written, because the state held before discarding is not stored.

Backend now at 346 unit and 317 integration tests, all passing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-01 14:15:22 -05:00
co-authored by Claude Opus 5
parent 44a9037121
commit d8a7588685
2 changed files with 164 additions and 0 deletions
+78
View File
@@ -132,4 +132,82 @@ router.post(
})
);
/**
* Regenerate: hand it back to the worker.
*
* attempts is reset along with the state. The worker only picks up rows below
* the attempt cap, so re-queueing a draft that has already failed three times
* without clearing them produces a button that appears to work, does nothing,
* and leaves nothing anywhere to say why.
*/
router.post(
'/:itemId/regenerate',
asyncRoute(async (req: Request, res: Response) => {
const { rowCount } = await pool.query(
`UPDATE item_drafts SET state = 'queued', attempts = 0, ai_error = NULL WHERE item_id = $1`,
[req.params.itemId]
);
if (rowCount === 0) return res.status(404).json({ error: 'no draft for this item' });
res.json({ state: 'queued' });
})
);
/**
* Discard: out of the queue, off the storefront, and entirely recoverable.
*
* Nothing is deleted — not the item, not the photographs. This is one click
* away in what amounts to an inbox, and the photos are often the only copy of
* something no longer in the sender's hands, so the destructive reading of
* "discard" is deliberately not available here. The item returns to pending
* because a discarded submission must not stay on sale.
*/
router.post(
'/:itemId/discard',
asyncRoute(async (req: Request, res: Response) => {
const client = await pool.connect();
try {
await client.query('BEGIN');
const { rowCount } = await client.query(
`UPDATE item_drafts SET state = 'discarded' WHERE item_id = $1`,
[req.params.itemId]
);
if (rowCount === 0) {
await client.query('ROLLBACK');
return res.status(404).json({ error: 'no draft for this item' });
}
await client.query(`UPDATE items SET status = 'pending' WHERE id = $1`, [req.params.itemId]);
await client.query('COMMIT');
res.json({ state: 'discarded' });
} catch (err) {
await client.query('ROLLBACK');
console.error(err);
res.status(500).json({ error: 'internal error' });
} finally {
client.release();
}
})
);
/**
* Restore: back into the queue, at the state the draft's own contents justify.
*
* Not unconditionally 'ready'. A submission discarded before it was ever
* drafted has no copy, and returning it as ready would present an empty draft
* as a finished one. Judged on whether a name was ever written, because the
* state it held before being discarded is not stored anywhere.
*/
router.post(
'/:itemId/restore',
asyncRoute(async (req: Request, res: Response) => {
const { rowCount } = await pool.query(
`UPDATE item_drafts
SET state = CASE WHEN ai_name IS NULL THEN 'failed' ELSE 'ready' END
WHERE item_id = $1`,
[req.params.itemId]
);
if (rowCount === 0) return res.status(404).json({ error: 'no draft for this item' });
res.json({ restored: true });
})
);
export default router;