Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/app/api/audiobooks/request-with-torrent/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
Expand Down
60 changes: 60 additions & 0 deletions tests/api/audiobooks-request-torrent.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down