diff --git a/src/app/api/audiobooks/request-with-torrent/route.ts b/src/app/api/audiobooks/request-with-torrent/route.ts index 18400c4b7..e121c1a09 100644 --- a/src/app/api/audiobooks/request-with-torrent/route.ts +++ b/src/app/api/audiobooks/request-with-torrent/route.ts @@ -64,15 +64,17 @@ export async function POST(request: NextRequest) { const body = await req.json(); const { audiobook, torrent } = RequestWithTorrentSchema.parse(body); - // First check: Is there an existing audiobook request in 'downloaded' or 'available' status? - // This catches the gap where files are organized but Plex hasn't scanned yet + // First check: Is there an existing audiobook request that is already + // fulfilled or actively being processed? Catches the gap where files are + // organized but Plex hasn't scanned yet, plus in-flight downloads, so the + // user isn't told "already requested" when the book is actually on its way. const existingActiveRequest = await prisma.request.findFirst({ where: { audiobook: { audibleAsin: audiobook.asin, }, type: 'audiobook', // Only check audiobook requests (ebook requests are separate) - status: { in: ['downloaded', 'available'] }, + status: { in: ['downloaded', 'available', 'downloading', 'processing', 'awaiting_approval'] }, deletedAt: null, }, include: { @@ -84,12 +86,24 @@ export async function POST(request: NextRequest) { const status = existingActiveRequest.status; const isOwnRequest = existingActiveRequest.userId === req.user.id; + let error: string; + let message: string; + + if (status === 'available') { + error = 'AlreadyAvailable'; + message = 'This audiobook is already available in your Plex library'; + } else if (status === 'awaiting_approval') { + error = 'AwaitingApproval'; + message = 'This request is awaiting admin approval'; + } else { + error = 'BeingProcessed'; + message = 'This audiobook is being processed and will be available soon'; + } + return NextResponse.json( { - error: status === 'available' ? 'AlreadyAvailable' : 'BeingProcessed', - message: status === 'available' - ? 'This audiobook is already available in your Plex library' - : 'This audiobook is being processed and will be available soon', + error, + message, requestStatus: status, isOwnRequest, requestedBy: existingActiveRequest.user?.plexUsername, diff --git a/tests/api/audiobooks-request-torrent.routes.test.ts b/tests/api/audiobooks-request-torrent.routes.test.ts index 02fc03fcd..b87f2b00c 100644 --- a/tests/api/audiobooks-request-torrent.routes.test.ts +++ b/tests/api/audiobooks-request-torrent.routes.test.ts @@ -70,6 +70,66 @@ describe('Request with torrent route', () => { expect(payload.error).toBe('BeingProcessed'); }); + it('returns BeingProcessed when request is downloading', async () => { + authRequest.json.mockResolvedValue({ + audiobook: { asin: 'ASIN', title: 'Title', author: 'Author' }, + torrent: { guid: 'guid', title: 'Torrent', size: 100, indexer: 'Indexer', downloadUrl: 'url', publishDate: '2024-01-01' }, + }); + prismaMock.request.findFirst.mockResolvedValueOnce({ + id: 'req-1', + status: 'downloading', + userId: 'user-1', + user: { plexUsername: 'me' }, + } as any); + + const { POST } = await import('@/app/api/audiobooks/request-with-torrent/route'); + const response = await POST({} as any); + const payload = await response.json(); + + expect(response.status).toBe(409); + expect(payload.error).toBe('BeingProcessed'); + }); + + it('returns BeingProcessed when request is processing', async () => { + authRequest.json.mockResolvedValue({ + audiobook: { asin: 'ASIN', title: 'Title', author: 'Author' }, + torrent: { guid: 'guid', title: 'Torrent', size: 100, indexer: 'Indexer', downloadUrl: 'url', publishDate: '2024-01-01' }, + }); + prismaMock.request.findFirst.mockResolvedValueOnce({ + id: 'req-1', + status: 'processing', + userId: 'user-1', + user: { plexUsername: 'me' }, + } as any); + + const { POST } = await import('@/app/api/audiobooks/request-with-torrent/route'); + const response = await POST({} as any); + const payload = await response.json(); + + expect(response.status).toBe(409); + expect(payload.error).toBe('BeingProcessed'); + }); + + it('returns AwaitingApproval when request is awaiting approval', async () => { + authRequest.json.mockResolvedValue({ + audiobook: { asin: 'ASIN', title: 'Title', author: 'Author' }, + torrent: { guid: 'guid', title: 'Torrent', size: 100, indexer: 'Indexer', downloadUrl: 'url', publishDate: '2024-01-01' }, + }); + prismaMock.request.findFirst.mockResolvedValueOnce({ + id: 'req-1', + status: 'awaiting_approval', + userId: 'user-1', + user: { plexUsername: 'me' }, + } as any); + + const { POST } = await import('@/app/api/audiobooks/request-with-torrent/route'); + const response = await POST({} as any); + const payload = await response.json(); + + expect(response.status).toBe(409); + expect(payload.error).toBe('AwaitingApproval'); + }); + it('returns 401 when user is missing', async () => { authRequest.user = null;