mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Merge branch 'main' into ebook-piecewise
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* Component: Admin Requests API (Paginated)
|
||||
* Documentation: documentation/admin-dashboard.md
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
import { Prisma } from '@/generated/prisma';
|
||||
|
||||
const logger = RMABLogger.create('API.Admin.Requests');
|
||||
|
||||
const VALID_SORT_FIELDS = ['createdAt', 'completedAt', 'title', 'user', 'status'] as const;
|
||||
const VALID_SORT_ORDERS = ['asc', 'desc'] as const;
|
||||
const VALID_PAGE_SIZES = [10, 25, 50, 100] as const;
|
||||
|
||||
type SortField = (typeof VALID_SORT_FIELDS)[number];
|
||||
type SortOrder = (typeof VALID_SORT_ORDERS)[number];
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
return requireAuth(request, async (req: AuthenticatedRequest) => {
|
||||
return requireAdmin(req, async () => {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
// Parse query parameters
|
||||
const page = Math.max(1, parseInt(searchParams.get('page') || '1', 10));
|
||||
const pageSizeParam = parseInt(searchParams.get('pageSize') || '25', 10);
|
||||
const pageSize = VALID_PAGE_SIZES.includes(pageSizeParam as (typeof VALID_PAGE_SIZES)[number])
|
||||
? pageSizeParam
|
||||
: 25;
|
||||
const search = searchParams.get('search')?.trim() || '';
|
||||
const status = searchParams.get('status') || '';
|
||||
const userId = searchParams.get('userId') || '';
|
||||
const sortByParam = searchParams.get('sortBy') || 'createdAt';
|
||||
const sortBy: SortField = VALID_SORT_FIELDS.includes(sortByParam as SortField)
|
||||
? (sortByParam as SortField)
|
||||
: 'createdAt';
|
||||
const sortOrderParam = searchParams.get('sortOrder') || 'desc';
|
||||
const sortOrder: SortOrder = VALID_SORT_ORDERS.includes(sortOrderParam as SortOrder)
|
||||
? (sortOrderParam as SortOrder)
|
||||
: 'desc';
|
||||
|
||||
// Build where clause
|
||||
const where: Prisma.RequestWhereInput = {
|
||||
deletedAt: null,
|
||||
};
|
||||
|
||||
// Filter by status
|
||||
if (status && status !== 'all') {
|
||||
where.status = status;
|
||||
}
|
||||
|
||||
// Filter by user
|
||||
if (userId) {
|
||||
where.userId = userId;
|
||||
}
|
||||
|
||||
// Search by title or author
|
||||
if (search) {
|
||||
where.audiobook = {
|
||||
OR: [
|
||||
{ title: { contains: search, mode: 'insensitive' } },
|
||||
{ author: { contains: search, mode: 'insensitive' } },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
// Build orderBy clause
|
||||
let orderBy: Prisma.RequestOrderByWithRelationInput;
|
||||
switch (sortBy) {
|
||||
case 'title':
|
||||
orderBy = { audiobook: { title: sortOrder } };
|
||||
break;
|
||||
case 'user':
|
||||
orderBy = { user: { plexUsername: sortOrder } };
|
||||
break;
|
||||
case 'completedAt':
|
||||
// Sort nulls last for completedAt
|
||||
orderBy = { completedAt: { sort: sortOrder, nulls: 'last' } };
|
||||
break;
|
||||
case 'status':
|
||||
orderBy = { status: sortOrder };
|
||||
break;
|
||||
case 'createdAt':
|
||||
default:
|
||||
orderBy = { createdAt: sortOrder };
|
||||
break;
|
||||
}
|
||||
|
||||
// Get total count for pagination
|
||||
const total = await prisma.request.count({ where });
|
||||
|
||||
// Get paginated requests
|
||||
const requests = await prisma.request.findMany({
|
||||
where,
|
||||
include: {
|
||||
audiobook: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
author: true,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
plexUsername: true,
|
||||
},
|
||||
},
|
||||
downloadHistory: {
|
||||
where: {
|
||||
selected: true,
|
||||
},
|
||||
select: {
|
||||
torrentUrl: true,
|
||||
},
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
orderBy,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
});
|
||||
|
||||
// Format response
|
||||
const formatted = requests.map((request) => ({
|
||||
requestId: request.id,
|
||||
title: request.audiobook.title,
|
||||
author: request.audiobook.author,
|
||||
status: request.status,
|
||||
userId: request.user.id,
|
||||
user: request.user.plexUsername,
|
||||
createdAt: request.createdAt,
|
||||
completedAt: request.completedAt,
|
||||
errorMessage: request.errorMessage,
|
||||
torrentUrl: request.downloadHistory[0]?.torrentUrl || null,
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
requests: formatted,
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil(total / pageSize),
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to fetch requests', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
return NextResponse.json({ error: 'Failed to fetch requests' }, { status: 500 });
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,13 +1,19 @@
|
||||
/**
|
||||
* Component: Admin Download Client Settings API
|
||||
* Documentation: documentation/settings-pages.md
|
||||
* Component: Admin Download Client Settings API (DEPRECATED)
|
||||
* Documentation: documentation/phase3/download-clients.md
|
||||
*
|
||||
* DEPRECATED: This route is deprecated in favor of /api/admin/settings/download-clients
|
||||
* which supports multiple download clients. This route is maintained for backward
|
||||
* compatibility but updates are written to the new multi-client format.
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { getConfigService } from '@/lib/services/config.service';
|
||||
import { getDownloadClientManager, invalidateDownloadClientManager, DownloadClientConfig } from '@/lib/services/download-client-manager.service';
|
||||
import { PathMapper } from '@/lib/utils/path-mapper';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
const logger = RMABLogger.create('API.Admin.Settings.DownloadClient');
|
||||
|
||||
@@ -26,6 +32,8 @@ export async function PUT(request: NextRequest) {
|
||||
localPath,
|
||||
} = await request.json();
|
||||
|
||||
logger.warn('DEPRECATED: Using legacy single-client API. Please use /api/admin/settings/download-clients instead.');
|
||||
|
||||
// Validate type
|
||||
if (type !== 'qbittorrent' && type !== 'sabnzbd') {
|
||||
return NextResponse.json(
|
||||
@@ -78,69 +86,51 @@ export async function PUT(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
// Update configuration
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_type' },
|
||||
update: { value: type },
|
||||
create: { key: 'download_client_type', value: type },
|
||||
});
|
||||
// Get existing clients from new format
|
||||
const config = await getConfigService();
|
||||
const manager = getDownloadClientManager(config);
|
||||
const existingClients = await manager.getAllClients();
|
||||
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_url' },
|
||||
update: { value: url },
|
||||
create: { key: 'download_client_url', value: url },
|
||||
});
|
||||
// Find existing client of same type to update, or create new
|
||||
const existingIndex = existingClients.findIndex(c => c.type === type);
|
||||
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_username' },
|
||||
update: { value: username },
|
||||
create: { key: 'download_client_username', value: username },
|
||||
});
|
||||
const updatedClient: DownloadClientConfig = {
|
||||
id: existingIndex >= 0 ? existingClients[existingIndex].id : randomUUID(),
|
||||
type,
|
||||
name: type === 'qbittorrent' ? 'qBittorrent' : 'SABnzbd',
|
||||
enabled: true,
|
||||
url,
|
||||
username: username || undefined,
|
||||
// Only update password if it's not the masked value
|
||||
password: password.startsWith('••••') && existingIndex >= 0
|
||||
? existingClients[existingIndex].password
|
||||
: password,
|
||||
disableSSLVerify: disableSSLVerify || false,
|
||||
remotePathMappingEnabled: remotePathMappingEnabled || false,
|
||||
remotePath: remotePath || undefined,
|
||||
localPath: localPath || undefined,
|
||||
category: existingIndex >= 0 ? existingClients[existingIndex].category : 'readmeabook',
|
||||
};
|
||||
|
||||
// Only update password if it's not the masked value
|
||||
if (!password.startsWith('••••')) {
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_password' },
|
||||
update: { value: password },
|
||||
create: { key: 'download_client_password', value: password },
|
||||
});
|
||||
// Update or add client
|
||||
let updatedClients: DownloadClientConfig[];
|
||||
if (existingIndex >= 0) {
|
||||
updatedClients = [...existingClients];
|
||||
updatedClients[existingIndex] = updatedClient;
|
||||
} else {
|
||||
updatedClients = [...existingClients, updatedClient];
|
||||
}
|
||||
|
||||
// Save SSL verification setting
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_disable_ssl_verify' },
|
||||
update: { value: disableSSLVerify ? 'true' : 'false' },
|
||||
create: {
|
||||
key: 'download_client_disable_ssl_verify',
|
||||
value: disableSSLVerify ? 'true' : 'false',
|
||||
},
|
||||
});
|
||||
// Save to new format
|
||||
await config.setMany([
|
||||
{ key: 'download_clients', value: JSON.stringify(updatedClients) },
|
||||
]);
|
||||
|
||||
// Save remote path mapping configuration
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_remote_path_mapping_enabled' },
|
||||
update: { value: remotePathMappingEnabled ? 'true' : 'false' },
|
||||
create: {
|
||||
key: 'download_client_remote_path_mapping_enabled',
|
||||
value: remotePathMappingEnabled ? 'true' : 'false',
|
||||
},
|
||||
});
|
||||
logger.info('Download client settings updated via legacy API', { type, id: updatedClient.id });
|
||||
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_remote_path' },
|
||||
update: { value: remotePath || '' },
|
||||
create: { key: 'download_client_remote_path', value: remotePath || '' },
|
||||
});
|
||||
// Invalidate caches
|
||||
invalidateDownloadClientManager();
|
||||
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'download_client_local_path' },
|
||||
update: { value: localPath || '' },
|
||||
create: { key: 'download_client_local_path', value: localPath || '' },
|
||||
});
|
||||
|
||||
logger.info('Download client settings updated');
|
||||
|
||||
// Invalidate download client service singleton to force reload of credentials and URL
|
||||
if (type === 'qbittorrent') {
|
||||
const { invalidateQBittorrentService } = await import('@/lib/integrations/qbittorrent.service');
|
||||
invalidateQBittorrentService();
|
||||
@@ -152,6 +142,8 @@ export async function PUT(request: NextRequest) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Download client settings updated successfully',
|
||||
deprecated: true,
|
||||
warning: 'This API is deprecated. Please use /api/admin/settings/download-clients instead.',
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to update download client settings', { error: error instanceof Error ? error.message : String(error) });
|
||||
|
||||
@@ -81,17 +81,46 @@ export async function GET(request: NextRequest) {
|
||||
url: configMap.get('prowlarr_url') || '',
|
||||
apiKey: maskValue('api_key', configMap.get('prowlarr_api_key')),
|
||||
},
|
||||
downloadClient: {
|
||||
type: configMap.get('download_client_type') || 'qbittorrent',
|
||||
url: configMap.get('download_client_url') || '',
|
||||
username: configMap.get('download_client_username') || '',
|
||||
password: maskValue('password', configMap.get('download_client_password')),
|
||||
disableSSLVerify: configMap.get('download_client_disable_ssl_verify') === 'true',
|
||||
seedingTimeMinutes: parseInt(configMap.get('seeding_time_minutes') || '0'),
|
||||
remotePathMappingEnabled: configMap.get('download_client_remote_path_mapping_enabled') === 'true',
|
||||
remotePath: configMap.get('download_client_remote_path') || '',
|
||||
localPath: configMap.get('download_client_local_path') || '',
|
||||
},
|
||||
// downloadClient is populated from multi-client format for backward compatibility
|
||||
// The DownloadTab component now uses DownloadClientManagement which reads from /api/admin/settings/download-clients
|
||||
downloadClient: (() => {
|
||||
// Try to read from new multi-client format first
|
||||
const downloadClientsJson = configMap.get('download_clients');
|
||||
if (downloadClientsJson) {
|
||||
try {
|
||||
const clients = JSON.parse(downloadClientsJson);
|
||||
// Return the first enabled client for backward compatibility
|
||||
const firstClient = clients.find((c: any) => c.enabled) || clients[0];
|
||||
if (firstClient) {
|
||||
return {
|
||||
type: firstClient.type || 'qbittorrent',
|
||||
url: firstClient.url || '',
|
||||
username: firstClient.username || '',
|
||||
password: maskValue('password', firstClient.password),
|
||||
disableSSLVerify: firstClient.disableSSLVerify === true,
|
||||
seedingTimeMinutes: parseInt(configMap.get('seeding_time_minutes') || '0'),
|
||||
remotePathMappingEnabled: firstClient.remotePathMappingEnabled === true,
|
||||
remotePath: firstClient.remotePath || '',
|
||||
localPath: firstClient.localPath || '',
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// Fall through to legacy format
|
||||
}
|
||||
}
|
||||
// Fall back to legacy flat keys
|
||||
return {
|
||||
type: configMap.get('download_client_type') || 'qbittorrent',
|
||||
url: configMap.get('download_client_url') || '',
|
||||
username: configMap.get('download_client_username') || '',
|
||||
password: maskValue('password', configMap.get('download_client_password')),
|
||||
disableSSLVerify: configMap.get('download_client_disable_ssl_verify') === 'true',
|
||||
seedingTimeMinutes: parseInt(configMap.get('seeding_time_minutes') || '0'),
|
||||
remotePathMappingEnabled: configMap.get('download_client_remote_path_mapping_enabled') === 'true',
|
||||
remotePath: configMap.get('download_client_remote_path') || '',
|
||||
localPath: configMap.get('download_client_local_path') || '',
|
||||
};
|
||||
})(),
|
||||
paths: {
|
||||
downloadDir: configMap.get('download_dir') || '/downloads',
|
||||
mediaDir: configMap.get('media_dir') || '/media/audiobooks',
|
||||
|
||||
Reference in New Issue
Block a user