mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Add request approval system and audiobook path template
Implements admin approval workflow for user requests with global and per-user auto-approve controls. Adds new request statuses ('awaiting_approval', 'denied'), related API endpoints, and UI for pending approvals. Introduces configurable audiobook organization path template with validation and preview in settings, updates database schema and migrations for new fields.
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Component: Admin Auto-Approve Settings API
|
||||
* Documentation: documentation/settings-pages.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';
|
||||
|
||||
const logger = RMABLogger.create('API.Admin.Settings.AutoApprove');
|
||||
|
||||
/**
|
||||
* GET /api/admin/settings/auto-approve
|
||||
* Get current global auto-approve setting
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
return requireAuth(request, async (req: AuthenticatedRequest) => {
|
||||
return requireAdmin(req, async () => {
|
||||
try {
|
||||
const config = await prisma.configuration.findUnique({
|
||||
where: { key: 'auto_approve_requests' },
|
||||
});
|
||||
|
||||
// Default to true if not configured (backward compatibility)
|
||||
const autoApproveRequests = config === null ? true : config.value === 'true';
|
||||
|
||||
return NextResponse.json({ autoApproveRequests });
|
||||
} catch (error) {
|
||||
logger.error('Failed to fetch auto-approve setting', {
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch auto-approve setting' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/admin/settings/auto-approve
|
||||
* Update global auto-approve setting
|
||||
*/
|
||||
export async function PATCH(request: NextRequest) {
|
||||
return requireAuth(request, async (req: AuthenticatedRequest) => {
|
||||
return requireAdmin(req, async () => {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { autoApproveRequests } = body;
|
||||
|
||||
// Validate input
|
||||
if (typeof autoApproveRequests !== 'boolean') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid input. autoApproveRequests must be a boolean' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update configuration
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'auto_approve_requests' },
|
||||
create: {
|
||||
key: 'auto_approve_requests',
|
||||
value: autoApproveRequests.toString(),
|
||||
},
|
||||
update: {
|
||||
value: autoApproveRequests.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
logger.info(`Auto-approve setting updated to: ${autoApproveRequests}`, {
|
||||
userId: req.user?.sub,
|
||||
});
|
||||
|
||||
return NextResponse.json({ autoApproveRequests });
|
||||
} catch (error) {
|
||||
logger.error('Failed to update auto-approve setting', {
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update auto-approve setting' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -14,7 +14,7 @@ export async function PUT(request: NextRequest) {
|
||||
return requireAuth(request, async (req: AuthenticatedRequest) => {
|
||||
return requireAdmin(req, async () => {
|
||||
try {
|
||||
const { downloadDir, mediaDir, metadataTaggingEnabled, chapterMergingEnabled } = await request.json();
|
||||
const { downloadDir, mediaDir, audiobookPathTemplate, metadataTaggingEnabled, chapterMergingEnabled } = await request.json();
|
||||
|
||||
if (!downloadDir || !mediaDir) {
|
||||
return NextResponse.json(
|
||||
@@ -44,6 +44,20 @@ export async function PUT(request: NextRequest) {
|
||||
create: { key: 'media_dir', value: mediaDir },
|
||||
});
|
||||
|
||||
// Update audiobook path template
|
||||
if (audiobookPathTemplate !== undefined) {
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'audiobook_path_template' },
|
||||
update: { value: audiobookPathTemplate },
|
||||
create: {
|
||||
key: 'audiobook_path_template',
|
||||
value: audiobookPathTemplate,
|
||||
category: 'automation',
|
||||
description: 'Template for organizing audiobook files in media directory',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Update metadata tagging setting
|
||||
await prisma.configuration.upsert({
|
||||
where: { key: 'metadata_tagging_enabled' },
|
||||
|
||||
@@ -86,6 +86,7 @@ export async function GET(request: NextRequest) {
|
||||
paths: {
|
||||
downloadDir: configMap.get('download_dir') || '/downloads',
|
||||
mediaDir: configMap.get('media_dir') || '/media/audiobooks',
|
||||
audiobookPathTemplate: configMap.get('audiobook_path_template') || '{author}/{title} {asin}',
|
||||
metadataTaggingEnabled: configMap.get('metadata_tagging_enabled') === 'true',
|
||||
chapterMergingEnabled: configMap.get('chapter_merging_enabled') === 'true',
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user