Skip to content

fix: a deleted request no longer accepts a torrent pick or a search - #476

Merged
kikootwo merged 4 commits into
mainfrom
fix/470-deleted-request-routes
Oct 27, 2026
Merged

kikootwo merged 4 commits into
mainfrom
fix/470-deleted-request-routes

Conversation

@kikootwo

@kikootwo kikootwo commented Oct 27, 2026 •

Copy link
Copy Markdown
Owner

Closes #470

What changed

  • POST /api/requests/[id]/select-torrent, /interactive-search, /manual-search and /fetch-ebook load the request with prisma.request.findFirst({ where: { id, deletedAt: null } }) instead of findUnique({ where: { id } }). A deleted request now gets the same 404 body each route already sends for a missing request, for its owner and for admins. fetch-ebook is admin only, so a non-admin owner still gets the admin check's 403.
  • POST /api/admin/requests/[id]/approve loads the request the same way, with the same include. A request deleted while it awaited approval now gets the route's existing 404 for both approve and deny, and none of its jobs are queued.
  • Live requests: no change.
  • Tests: new tests/api/requests-deleted-request-routes.test.ts, plus a deleted-request case for approve and deny in tests/api/requests-approval.routes.test.ts. Five existing test files (requests-actions.routes, admin-requests.routes, download-selection-guard.routes, notification-triggers.integration, pick-credentials.routes) and the approval test now mock request.findFirst for these routes instead of request.findUnique.
  • Docs: documentation/admin-features/request-deletion.md, Fixed Issues Add verbose matching logs to debugging #7.

Why

Deleting a request is a soft delete: it sets deletedAt and keeps the status. These five routes loaded the request by id alone. A deleted request could still have a torrent picked (and a download job queued), an interactive or manual search run, or an ebook request created and searched. An admin could also approve it, which queues its search or download. #463 already fixed the ebook routes (select-ebook, interactive-search-ebook) the same way.

Sweep of src/app/api/requests/[id]/ and the jobs these routes queue:

  • Already filtered: GET and PATCH on route.ts, download, download-token, select-ebook, interactive-search-ebook, and fetch-ebook's lookup of an existing ebook request.
  • DELETE on route.ts hard-deletes by id with no filter. It is admin only and starts nothing, so this PR leaves it.
  • download_torrent and search_indexers jobs already skip a cancelled or deleted request (updateRequestUnlessStopped). Notification jobs don't load the request.
  • The search_ebook job (queued by manual-search, fetch-ebook and approval) has no deleted check of its own. With these routes fixed, none of them queues it for a deleted request any more. This PR leaves the job as is.

Evidence

  • The new test file runs each /api/requests/[id]/ route with Prisma mocks that apply the where fields to fixture rows, deleted ones included. Per route: a deleted request gets a 404 with the route's not-found body for the owner and for an admin, and no search runs, no job is queued, and nothing is created or updated. A live request still gets its search or download.
  • With main's routes (08dc816) and this branch's test file: 9 failed, 5 passed, all on an assertion (each deleted case got a 200 where it should have got a 404). On this branch: 14/14 pass.
  • Approve route: the new approve and deny cases use the same where-applying mock. Against the previous branch head (a7d4b2b, approve route unchanged), both failed on an assertion (200 where it should have been 404). On this branch: 2/2 pass, and the request is not updated and no search, download or notification job is queued.

Checks

  • Unit tests for the changed code (vitest related on the four /api/requests/[id]/ routes plus the new test file: PASS, 5 files, 85/85; on the approve route plus requests-approval.routes: PASS, 4 files, 90/90)
  • Type check and lint: PASS (tsc --noEmit clean; ESLint finds no problems in the new test file, and the other touched files report the same counts as before the change)
  • Image build and E2E smoke: PASS at a7d4b2b (e2e harness image, smoke spec 1/1), before the approve route change; not re-run for it
  • Full unit suite: PR Tests (CI)

Risk and rollback

  • Only requests with deletedAt set behave differently. A live request is found exactly as before (id is unique, and deletedAt: null matches every live row).
  • A user with a deleted request open on screen now gets "Request not found" when they pick or search, instead of the action going ahead. An admin approving or denying a deleted request gets the same 404, so it leaves the approval queue untouched.
  • Rollback: revert this PR. No data is written or migrated.

Release note

🤖 Generated with Claude Code

kikootwo and others added 3 commits October 27, 2026 03:49
select-torrent, interactive-search, manual-search and fetch-ebook loaded
the request by id only, so a deleted request could still start a download
or a search. Each now loads with deletedAt: null, as select-ebook and
interactive-search-ebook already do, and a deleted request answers the
route's usual 404.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The approve route loaded the request by id only, so approving a request
deleted while it awaited approval still queued its search or download.
It now loads with deletedAt: null and the same include; a deleted
request gets the route's existing 404 for approve and deny.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo marked this pull request as ready for review October 27, 2026 08:13
…from #469 (#470)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo merged commit 4643f15 into main Oct 27, 2026
1 check passed
@kikootwo
kikootwo deleted the fix/470-deleted-request-routes branch October 27, 2026 08:24
NichCodes added a commit to NichCodes/ReadMeABook that referenced this pull request Nov 2, 2026
Conflict resolutions:
- Approve route: kept the thin wrapper around processRequestApproval and
  moved upstream's route fixes into the shared service, so the Web UI,
  API tokens and Discord all get them: release-pick validation
  (guardTorrentSelection/guardEbookSelection, kikootwo#355) in a new
  request-approval-selection helper, run before the atomic claim, with
  503 (source unreachable) and 400 (unusable admin pick) responses and the
  stored-pick fallback to automatic search; buildEbookDownloadName (kikootwo#478);
  readJsonBody body limits (kikootwo#438/kikootwo#442). The deleted-request guard (kikootwo#476)
  was already in the service.
- Init route: took upstream's secret-gated POST/runAppInitOnce; the bot
  still starts once after the first successful init.
- users/[id], notifications/test, organize-files: kept both sides.
- package.json/lock: upstream versions plus discord.js.

Follow-ups for upstream conventions and test changes:
- Discord settings routes read bodies through readJsonBody.
- Approve tests mock the updateMany claim (the shared Prisma mock now
  returns { count: 0 } by default).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A deleted request still accepts a torrent pick or an interactive search

1 participant