Add admin request deletion with soft delete and cleanup

Implements admin ability to delete requests with soft delete, media file cleanup, and seeding-aware torrent management. Adds new API endpoint, frontend confirmation dialog, and request actions dropdown. Updates database schema with deletedAt and deletedBy fields, and ensures all queries filter out deleted requests. Documentation added for feature and user flow.
This commit is contained in:
kikootwo
2025-12-22 20:24:43 -05:00
parent bba4af7398
commit 174e9f05b6
26 changed files with 1936 additions and 200 deletions
+130
View File
@@ -0,0 +1,130 @@
/**
* Component: Confirm Dialog
* Documentation: documentation/frontend/components.md
*
* Reusable confirmation dialog for destructive actions
*/
'use client';
import { Fragment } from 'react';
export interface ConfirmDialogProps {
isOpen: boolean;
title: string;
message: string | React.ReactNode;
confirmLabel?: string;
cancelLabel?: string;
confirmVariant?: 'danger' | 'primary';
onConfirm: () => void;
onCancel: () => void;
}
export function ConfirmDialog({
isOpen,
title,
message,
confirmLabel = 'Confirm',
cancelLabel = 'Cancel',
confirmVariant = 'danger',
onConfirm,
onCancel,
}: ConfirmDialogProps) {
if (!isOpen) return null;
const confirmButtonClasses =
confirmVariant === 'danger'
? 'bg-red-600 hover:bg-red-700 text-white'
: 'bg-blue-600 hover:bg-blue-700 text-white';
return (
<div className="fixed inset-0 z-50 overflow-y-auto">
{/* Backdrop */}
<div
className="fixed inset-0 bg-black bg-opacity-50 transition-opacity"
onClick={onCancel}
aria-hidden="true"
/>
{/* Dialog */}
<div className="flex min-h-full items-center justify-center p-4 text-center sm:p-0">
<div className="relative transform overflow-hidden rounded-lg bg-white dark:bg-gray-800 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg">
<div className="bg-white dark:bg-gray-800 px-4 pb-4 pt-5 sm:p-6 sm:pb-4">
<div className="sm:flex sm:items-start">
{/* Icon */}
<div
className={`mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full ${
confirmVariant === 'danger'
? 'bg-red-100 dark:bg-red-900'
: 'bg-blue-100 dark:bg-blue-900'
} sm:mx-0 sm:h-10 sm:w-10`}
>
<svg
className={`h-6 w-6 ${
confirmVariant === 'danger'
? 'text-red-600 dark:text-red-400'
: 'text-blue-600 dark:text-blue-400'
}`}
fill="none"
viewBox="0 0 24 24"
strokeWidth="1.5"
stroke="currentColor"
>
{confirmVariant === 'danger' ? (
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z"
/>
) : (
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.879 7.519c1.171-1.025 3.071-1.025 4.242 0 1.172 1.025 1.172 2.687 0 3.712-.203.179-.43.326-.67.442-.745.361-1.45.999-1.45 1.827v.75M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-9 5.25h.008v.008H12v-.008z"
/>
)}
</svg>
</div>
{/* Content */}
<div className="mt-3 text-center sm:ml-4 sm:mt-0 sm:text-left flex-1">
<h3 className="text-lg font-semibold leading-6 text-gray-900 dark:text-gray-100">
{title}
</h3>
<div className="mt-2">
{typeof message === 'string' ? (
<p className="text-sm text-gray-500 dark:text-gray-400 whitespace-pre-line">
{message}
</p>
) : (
<div className="text-sm text-gray-500 dark:text-gray-400">
{message}
</div>
)}
</div>
</div>
</div>
</div>
{/* Actions */}
<div className="bg-gray-50 dark:bg-gray-900 px-4 py-3 sm:flex sm:flex-row-reverse sm:px-6 gap-2">
<button
type="button"
onClick={onConfirm}
className={`inline-flex w-full justify-center rounded-lg px-4 py-2 text-sm font-semibold shadow-sm sm:w-auto transition-colors ${confirmButtonClasses}`}
>
{confirmLabel}
</button>
<button
type="button"
onClick={onCancel}
className="mt-3 inline-flex w-full justify-center rounded-lg bg-white dark:bg-gray-700 px-4 py-2 text-sm font-semibold text-gray-900 dark:text-gray-100 shadow-sm ring-1 ring-inset ring-gray-300 dark:ring-gray-600 hover:bg-gray-50 dark:hover:bg-gray-600 sm:mt-0 sm:w-auto transition-colors"
>
{cancelLabel}
</button>
</div>
</div>
</div>
</div>
);
}
@@ -5,7 +5,12 @@
'use client';
import { useState } from 'react';
import { formatDistanceToNow } from 'date-fns';
import { ConfirmDialog } from './ConfirmDialog';
import { RequestActionsDropdown } from './RequestActionsDropdown';
import { mutate } from 'swr';
import { fetchWithAuth } from '@/lib/utils/api';
interface RecentRequest {
requestId: string;
@@ -57,6 +62,120 @@ function getStatusBadge(status: string) {
}
export function RecentRequestsTable({ requests }: RecentRequestsTableProps) {
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
const [selectedRequest, setSelectedRequest] = useState<{
id: string;
title: string;
} | null>(null);
const [isDeleting, setIsDeleting] = useState(false);
const handleDeleteClick = (requestId: string, title: string) => {
setSelectedRequest({ id: requestId, title });
setShowDeleteConfirm(true);
};
const handleDeleteConfirm = async () => {
if (!selectedRequest) return;
setIsDeleting(true);
try {
const response = await fetchWithAuth(`/api/admin/requests/${selectedRequest.id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Failed to delete request');
}
const result = await response.json();
// Show success message
console.log('[Admin] Request deleted:', result);
// Refresh the requests list
await mutate('/api/admin/requests/recent');
await mutate('/api/admin/metrics');
// Close dialog
setShowDeleteConfirm(false);
setSelectedRequest(null);
} catch (error) {
console.error('[Admin] Failed to delete request:', error);
alert(
`Failed to delete request: ${
error instanceof Error ? error.message : 'Unknown error'
}`
);
} finally {
setIsDeleting(false);
}
};
const handleDeleteCancel = () => {
setShowDeleteConfirm(false);
setSelectedRequest(null);
};
const handleManualSearch = async (requestId: string) => {
try {
const response = await fetchWithAuth(`/api/requests/${requestId}/manual-search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Failed to trigger manual search');
}
console.log('[Admin] Manual search triggered for request:', requestId);
// Refresh the requests list
await mutate('/api/admin/requests/recent');
} catch (error) {
console.error('[Admin] Failed to trigger manual search:', error);
alert(
`Failed to trigger manual search: ${
error instanceof Error ? error.message : 'Unknown error'
}`
);
}
};
const handleCancel = async (requestId: string) => {
try {
const response = await fetchWithAuth(`/api/requests/${requestId}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ action: 'cancel' }),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || 'Failed to cancel request');
}
console.log('[Admin] Request cancelled:', requestId);
// Refresh the requests list
await mutate('/api/admin/requests/recent');
} catch (error) {
console.error('[Admin] Failed to cancel request:', error);
alert(
`Failed to cancel request: ${
error instanceof Error ? error.message : 'Unknown error'
}`
);
}
};
if (requests.length === 0) {
return (
<div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-8">
@@ -107,6 +226,9 @@ export function RecentRequestsTable({ requests }: RecentRequestsTableProps) {
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Completed
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Actions
</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-200 dark:divide-gray-700">
@@ -144,11 +266,53 @@ export function RecentRequestsTable({ requests }: RecentRequestsTableProps) {
})
: '-'}
</td>
<td className="px-6 py-4">
<RequestActionsDropdown
request={{
requestId: request.requestId,
title: request.title,
author: request.author,
status: request.status,
}}
onDelete={handleDeleteClick}
onManualSearch={handleManualSearch}
onCancel={handleCancel}
isLoading={isDeleting}
/>
</td>
</tr>
))}
</tbody>
</table>
</div>
{/* Confirm Dialog */}
<ConfirmDialog
isOpen={showDeleteConfirm}
title="Delete Request?"
message={
selectedRequest ? (
<div>
<p className="mb-3">
This will delete the request for &quot;{selectedRequest.title}&quot; and:
</p>
<ul className="list-disc list-inside space-y-1 text-sm">
<li>Remove the request (allowing it to be re-requested)</li>
<li>Delete files from the media directory</li>
<li>Keep torrent seeding if time remaining</li>
</ul>
<p className="mt-3 font-semibold">Are you sure?</p>
</div>
) : (
''
)
}
confirmLabel={isDeleting ? 'Deleting...' : 'Delete'}
cancelLabel="Cancel"
confirmVariant="danger"
onConfirm={handleDeleteConfirm}
onCancel={handleDeleteCancel}
/>
</div>
);
}
@@ -0,0 +1,232 @@
/**
* Component: Request Actions Dropdown
* Documentation: documentation/admin-features/request-deletion.md
*
* Dropdown menu for admin actions on requests
*/
'use client';
import { useState, useRef, useEffect } from 'react';
import { InteractiveTorrentSearchModal } from '@/components/requests/InteractiveTorrentSearchModal';
export interface RequestActionsDropdownProps {
request: {
requestId: string;
title: string;
author: string;
status: string;
};
onDelete: (requestId: string, title: string) => void;
onManualSearch: (requestId: string) => Promise<void>;
onCancel: (requestId: string) => Promise<void>;
isLoading?: boolean;
}
export function RequestActionsDropdown({
request,
onDelete,
onManualSearch,
onCancel,
isLoading = false,
}: RequestActionsDropdownProps) {
const [isOpen, setIsOpen] = useState(false);
const [showInteractiveSearch, setShowInteractiveSearch] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
// Determine available actions based on status
const canSearch = ['pending', 'failed', 'awaiting_search'].includes(request.status);
const canCancel = ['pending', 'searching', 'downloading'].includes(request.status);
const canDelete = true; // Admins can always delete
// Close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
};
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isOpen]);
const handleManualSearch = async () => {
setIsOpen(false);
try {
await onManualSearch(request.requestId);
} catch (error) {
console.error('Failed to trigger manual search:', error);
}
};
const handleInteractiveSearch = () => {
setIsOpen(false);
setShowInteractiveSearch(true);
};
const handleCancel = async () => {
setIsOpen(false);
if (window.confirm(`Are you sure you want to cancel the request for "${request.title}"?`)) {
try {
await onCancel(request.requestId);
} catch (error) {
console.error('Failed to cancel request:', error);
}
}
};
const handleDelete = () => {
setIsOpen(false);
onDelete(request.requestId, request.title);
};
return (
<div className="relative" ref={dropdownRef}>
{/* Three-dot menu button */}
<button
onClick={() => setIsOpen(!isOpen)}
disabled={isLoading}
className="inline-flex items-center justify-center w-8 h-8 rounded-full hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
title="Actions"
>
<svg
className="w-5 h-5 text-gray-600 dark:text-gray-400"
fill="currentColor"
viewBox="0 0 20 20"
>
<path d="M10 6a2 2 0 110-4 2 2 0 010 4zM10 12a2 2 0 110-4 2 2 0 010 4zM10 18a2 2 0 110-4 2 2 0 010 4z" />
</svg>
</button>
{/* Dropdown menu */}
{isOpen && (
<div className="absolute right-0 mt-2 w-56 rounded-lg shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 z-50">
<div className="py-1" role="menu">
{/* Manual Search */}
{canSearch && (
<button
onClick={handleManualSearch}
className="w-full text-left px-4 py-2 text-sm text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 flex items-center gap-2 transition-colors"
role="menuitem"
>
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
/>
</svg>
Manual Search
</button>
)}
{/* Interactive Search */}
{canSearch && (
<button
onClick={handleInteractiveSearch}
className="w-full text-left px-4 py-2 text-sm text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-700 flex items-center gap-2 transition-colors"
role="menuitem"
>
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"
/>
</svg>
Interactive Search
</button>
)}
{/* Divider if we have search actions and other actions */}
{canSearch && (canCancel || canDelete) && (
<div className="border-t border-gray-200 dark:border-gray-700 my-1" />
)}
{/* Cancel */}
{canCancel && (
<button
onClick={handleCancel}
className="w-full text-left px-4 py-2 text-sm text-orange-600 dark:text-orange-400 hover:bg-orange-50 dark:hover:bg-orange-900/20 flex items-center gap-2 transition-colors"
role="menuitem"
>
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
Cancel Request
</button>
)}
{/* Divider before delete */}
{canDelete && (canSearch || canCancel) && (
<div className="border-t border-gray-200 dark:border-gray-700 my-1" />
)}
{/* Delete */}
{canDelete && (
<button
onClick={handleDelete}
className="w-full text-left px-4 py-2 text-sm text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 flex items-center gap-2 transition-colors"
role="menuitem"
>
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
/>
</svg>
Delete Request
</button>
)}
</div>
</div>
)}
{/* Interactive Search Modal */}
<InteractiveTorrentSearchModal
isOpen={showInteractiveSearch}
onClose={() => setShowInteractiveSearch(false)}
requestId={request.requestId}
audiobook={{
title: request.title,
author: request.author,
}}
/>
</div>
);
}
@@ -15,6 +15,7 @@ export async function GET(request: NextRequest) {
const activeDownloads = await prisma.request.findMany({
where: {
status: 'downloading',
deletedAt: null,
},
include: {
audiobook: {
+10 -2
View File
@@ -22,13 +22,18 @@ export async function GET(request: NextRequest) {
failedLast30Days,
totalUsers,
] = await Promise.all([
// Total requests (all time)
prisma.request.count(),
// Total requests (all time, only active)
prisma.request.count({
where: {
deletedAt: null,
},
}),
// Active downloads (downloading status)
prisma.request.count({
where: {
status: 'downloading',
deletedAt: null,
},
}),
@@ -41,6 +46,7 @@ export async function GET(request: NextRequest) {
completedAt: {
gte: thirtyDaysAgo,
},
deletedAt: null,
},
}),
@@ -51,6 +57,7 @@ export async function GET(request: NextRequest) {
updatedAt: {
gte: thirtyDaysAgo,
},
deletedAt: null,
},
}),
@@ -103,6 +110,7 @@ async function checkSystemHealth(): Promise<{
updatedAt: {
lt: oneDayAgo,
},
deletedAt: null,
},
});
+77
View File
@@ -0,0 +1,77 @@
/**
* Component: Admin Request Management API
* Documentation: documentation/admin-features/request-deletion.md
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
import { deleteRequest } from '@/lib/services/request-delete.service';
/**
* DELETE /api/admin/requests/[id]
* Soft delete a request with intelligent cleanup (admin only)
*
* This endpoint:
* 1. Validates admin authorization
* 2. Soft deletes the request (sets deletedAt timestamp)
* 3. Deletes media files from the title folder
* 4. Handles torrents based on seeding configuration:
* - Unlimited seeding (0): Keeps torrent, stops monitoring
* - Seeding complete: Deletes torrent + files
* - Still seeding: Keeps torrent for cleanup job
* 5. Allows re-requesting the same audiobook after deletion
*/
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
if (!req.user) {
return NextResponse.json(
{ error: 'Unauthorized', message: 'User not authenticated' },
{ status: 401 }
);
}
const { id } = await params;
// Perform soft delete with cleanup
const result = await deleteRequest(id, req.user.id);
if (!result.success) {
return NextResponse.json(
{
error: result.error || 'DeleteFailed',
message: result.message,
},
{ status: result.error === 'NotFound' ? 404 : 500 }
);
}
// Return detailed result
return NextResponse.json({
success: true,
message: result.message,
details: {
filesDeleted: result.filesDeleted,
torrentsRemoved: result.torrentsRemoved,
torrentsKeptSeeding: result.torrentsKeptSeeding,
torrentsKeptUnlimited: result.torrentsKeptUnlimited,
},
});
} catch (error) {
console.error('[Admin] Failed to delete request:', error);
return NextResponse.json(
{
error: 'DeleteError',
message: 'Failed to delete request',
details: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
);
}
});
});
}
+4 -1
View File
@@ -11,8 +11,11 @@ export async function GET(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
// Get recent requests
// Get recent requests (only active, non-deleted)
const recentRequests = await prisma.request.findMany({
where: {
deletedAt: null,
},
include: {
audiobook: {
select: {
@@ -0,0 +1,187 @@
/**
* Component: Request with Specific Torrent API
* Documentation: documentation/phase3/prowlarr.md
*
* Create a request and immediately download a specific torrent
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { getJobQueueService } from '@/lib/services/job-queue.service';
import { findPlexMatch } from '@/lib/utils/audiobook-matcher';
import { z } from 'zod';
const RequestWithTorrentSchema = z.object({
audiobook: z.object({
asin: z.string(),
title: z.string(),
author: z.string(),
narrator: z.string().optional(),
description: z.string().optional(),
coverArtUrl: z.string().optional(),
durationMinutes: z.number().optional(),
releaseDate: z.string().optional(),
rating: z.number().optional(),
}),
torrent: z.object({
guid: z.string(),
title: z.string(),
size: z.number(),
seeders: z.number(),
leechers: z.number(),
indexer: z.string(),
downloadUrl: z.string(),
publishDate: z.string().transform((str) => new Date(str)),
infoHash: z.string().optional(),
format: z.enum(['M4B', 'M4A', 'MP3', 'OTHER']).optional(),
bitrate: z.string().optional(),
hasChapters: z.boolean().optional(),
}),
});
/**
* POST /api/audiobooks/request-with-torrent
* Create a request and download a specific torrent in one operation
*/
export async function POST(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
try {
if (!req.user) {
return NextResponse.json(
{ error: 'Unauthorized', message: 'User not authenticated' },
{ status: 401 }
);
}
const body = await req.json();
const { audiobook, torrent } = RequestWithTorrentSchema.parse(body);
// Check if audiobook is already available in Plex library
const plexMatch = await findPlexMatch({
asin: audiobook.asin,
title: audiobook.title,
author: audiobook.author,
narrator: audiobook.narrator,
});
if (plexMatch) {
return NextResponse.json(
{
error: 'AlreadyAvailable',
message: 'This audiobook is already available in your Plex library',
plexGuid: plexMatch.plexGuid,
},
{ status: 409 }
);
}
// Try to find existing audiobook record by ASIN
let audiobookRecord = await prisma.audiobook.findFirst({
where: { audibleAsin: audiobook.asin },
});
// If not found, create new audiobook record
if (!audiobookRecord) {
audiobookRecord = await prisma.audiobook.create({
data: {
audibleAsin: audiobook.asin,
title: audiobook.title,
author: audiobook.author,
narrator: audiobook.narrator,
description: audiobook.description,
coverArtUrl: audiobook.coverArtUrl,
status: 'requested',
},
});
}
// Check if user already has an active request for this audiobook
const existingRequest = await prisma.request.findFirst({
where: {
userId: req.user.id,
audiobookId: audiobookRecord.id,
},
});
if (existingRequest) {
const canReRequest = ['failed', 'warn', 'cancelled'].includes(existingRequest.status);
if (!canReRequest) {
return NextResponse.json(
{
error: 'DuplicateRequest',
message: 'You have already requested this audiobook',
request: existingRequest,
},
{ status: 409 }
);
}
// Delete the existing failed/warn/cancelled request
console.log(`[RequestWithTorrent] Deleting existing ${existingRequest.status} request ${existingRequest.id}`);
await prisma.request.delete({
where: { id: existingRequest.id },
});
}
// Create request with downloading status
const newRequest = await prisma.request.create({
data: {
userId: req.user.id,
audiobookId: audiobookRecord.id,
status: 'downloading',
progress: 0,
},
include: {
audiobook: true,
user: {
select: {
id: true,
plexUsername: true,
},
},
},
});
// Queue download job with the selected torrent
const jobQueue = getJobQueueService();
await jobQueue.addDownloadJob(
newRequest.id,
{
id: audiobookRecord.id,
title: audiobookRecord.title,
author: audiobookRecord.author,
},
torrent
);
console.log(`[RequestWithTorrent] Queued download monitor job for request ${newRequest.id}`);
return NextResponse.json({
success: true,
request: newRequest,
}, { status: 201 });
} catch (error) {
console.error('Failed to create request with torrent:', error);
if (error instanceof z.ZodError) {
return NextResponse.json(
{
error: 'ValidationError',
details: error.errors,
},
{ status: 400 }
);
}
return NextResponse.json(
{
error: 'RequestError',
message: error instanceof Error ? error.message : 'Failed to create request and download torrent',
},
{ status: 500 }
);
}
});
}
@@ -0,0 +1,114 @@
/**
* Component: Audiobook Torrent Search API
* Documentation: documentation/phase3/prowlarr.md
*
* Search for torrents without creating a request first
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
import { getProwlarrService } from '@/lib/integrations/prowlarr.service';
import { rankTorrents } from '@/lib/utils/ranking-algorithm';
import { z } from 'zod';
const SearchSchema = z.object({
title: z.string(),
author: z.string(),
});
/**
* POST /api/audiobooks/search-torrents
* Search for torrents for an audiobook (no request required)
*/
export async function POST(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
try {
if (!req.user) {
return NextResponse.json(
{ error: 'Unauthorized', message: 'User not authenticated' },
{ status: 401 }
);
}
const body = await req.json();
const { title, author } = SearchSchema.parse(body);
// Get enabled indexers from configuration
const { getConfigService } = await import('@/lib/services/config.service');
const configService = getConfigService();
const indexersConfigStr = await configService.get('prowlarr_indexers');
if (!indexersConfigStr) {
return NextResponse.json(
{ error: 'ConfigError', message: 'No indexers configured. Please configure indexers in settings.' },
{ status: 400 }
);
}
const indexersConfig = JSON.parse(indexersConfigStr);
const enabledIndexerIds = indexersConfig.map((indexer: any) => indexer.id);
if (enabledIndexerIds.length === 0) {
return NextResponse.json(
{ error: 'ConfigError', message: 'No indexers enabled. Please enable at least one indexer in settings.' },
{ status: 400 }
);
}
// Search Prowlarr for torrents - ONLY enabled indexers
const prowlarr = await getProwlarrService();
const searchQuery = `${title} ${author}`;
console.log(`[AudiobookSearch] Searching ${enabledIndexerIds.length} enabled indexers for: ${searchQuery}`);
const results = await prowlarr.search(searchQuery, {
indexerIds: enabledIndexerIds,
});
if (results.length === 0) {
return NextResponse.json({
success: true,
results: [],
message: 'No torrents found',
});
}
// Rank torrents using the ranking algorithm
const rankedResults = rankTorrents(results, { title, author });
// Add rank position to each result
const resultsWithRank = rankedResults.map((result, index) => ({
...result,
rank: index + 1,
}));
console.log(`[AudiobookSearch] Found ${resultsWithRank.length} results for "${title}" by ${author}`);
return NextResponse.json({
success: true,
results: resultsWithRank,
message: `Found ${resultsWithRank.length} torrents`,
});
} catch (error) {
console.error('Failed to search for torrents:', error);
if (error instanceof z.ZodError) {
return NextResponse.json(
{
error: 'ValidationError',
details: error.errors,
},
{ status: 400 }
);
}
return NextResponse.json(
{
error: 'SearchError',
message: error instanceof Error ? error.message : 'Failed to search for torrents',
},
{ status: 500 }
);
}
});
}
+21 -9
View File
@@ -26,8 +26,11 @@ export async function GET(
const { id } = await params;
const requestRecord = await prisma.request.findUnique({
where: { id },
const requestRecord = await prisma.request.findFirst({
where: {
id,
deletedAt: null, // Only show active requests
},
include: {
audiobook: true,
user: {
@@ -100,13 +103,16 @@ export async function PATCH(
const body = await req.json();
const { action } = body;
const requestRecord = await prisma.request.findUnique({
where: { id },
const requestRecord = await prisma.request.findFirst({
where: {
id,
deletedAt: null, // Only allow updates to active requests
},
});
if (!requestRecord) {
return NextResponse.json(
{ error: 'NotFound', message: 'Request not found' },
{ error: 'NotFound', message: 'Request not found or already deleted' },
{ status: 404 }
);
}
@@ -161,8 +167,11 @@ export async function PATCH(
if (requestRecord.status === 'warn' || requestRecord.status === 'awaiting_import') {
// Retry import
const requestWithData = await prisma.request.findUnique({
where: { id },
const requestWithData = await prisma.request.findFirst({
where: {
id,
deletedAt: null,
},
include: {
audiobook: true,
downloadHistory: {
@@ -213,8 +222,11 @@ export async function PATCH(
jobType = 'import';
} else {
// Retry search
const requestWithData = await prisma.request.findUnique({
where: { id },
const requestWithData = await prisma.request.findFirst({
where: {
id,
deletedAt: null,
},
include: {
audiobook: true,
},
+21 -15
View File
@@ -80,13 +80,12 @@ export async function POST(request: NextRequest) {
});
}
// Check if user already has a request for this audiobook
const existingRequest = await prisma.request.findUnique({
// Check if user already has an active (non-deleted) request for this audiobook
const existingRequest = await prisma.request.findFirst({
where: {
userId_audiobookId: {
userId: req.user.id,
audiobookId: audiobookRecord.id,
},
userId: req.user.id,
audiobookId: audiobookRecord.id,
deletedAt: null, // Only check active requests
},
});
@@ -112,12 +111,15 @@ export async function POST(request: NextRequest) {
});
}
// Create request
// Check if we should skip auto-search (for interactive search)
const skipAutoSearch = req.nextUrl.searchParams.get('skipAutoSearch') === 'true';
// Create request with appropriate status
const newRequest = await prisma.request.create({
data: {
userId: req.user.id,
audiobookId: audiobookRecord.id,
status: 'pending',
status: skipAutoSearch ? 'awaiting_search' : 'pending',
progress: 0,
},
include: {
@@ -131,13 +133,15 @@ export async function POST(request: NextRequest) {
},
});
// Trigger search job
const jobQueue = getJobQueueService();
await jobQueue.addSearchJob(newRequest.id, {
id: audiobookRecord.id,
title: audiobookRecord.title,
author: audiobookRecord.author,
});
// Trigger search job only if not skipped
if (!skipAutoSearch) {
const jobQueue = getJobQueueService();
await jobQueue.addSearchJob(newRequest.id, {
id: audiobookRecord.id,
title: audiobookRecord.title,
author: audiobookRecord.author,
});
}
return NextResponse.json({
success: true,
@@ -194,6 +198,8 @@ export async function GET(request: NextRequest) {
if (status) {
where.status = status;
}
// Only show active (non-deleted) requests
where.deletedAt = null;
const requests = await prisma.request.findMany({
where,