mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 12:50:09 +00:00
Add reported-issues, Goodreads sync & notifs
Introduce user-reported-issues and Goodreads shelf sync features and wire them into notifications. Adds Prisma migrations and schema changes (ReportedIssue, GoodreadsShelf, GoodreadsBookMapping), API endpoints for reporting (POST /audiobooks/[asin]/report-issue) and admin management (list, resolve/dismiss, replace), and an admin UI section to view/dismiss/replace reported issues. Adds a new notification event (issue_reported) with updates to notification schemas, docs and provider handling, plus a notification-events constants file. Refactors request creation to use createRequestForUser service, adds a Goodreads sync processor/service/hooks/UI modals, a scrape-resilience util, and related tests and minor integration updates.
This commit is contained in:
@@ -7,6 +7,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { getNotificationService } from '@/lib/services/notification';
|
||||
import { NOTIFICATION_EVENT_KEYS } from '@/lib/constants/notification-events';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
import { z } from 'zod';
|
||||
|
||||
@@ -15,7 +16,7 @@ const logger = RMABLogger.create('API.Admin.Notifications.Id');
|
||||
const UpdateBackendSchema = z.object({
|
||||
name: z.string().min(1).optional(),
|
||||
config: z.record(z.any()).optional(),
|
||||
events: z.array(z.enum(['request_pending_approval', 'request_approved', 'request_available', 'request_error'])).min(1).optional(),
|
||||
events: z.array(z.enum(NOTIFICATION_EVENT_KEYS)).min(1).optional(),
|
||||
enabled: z.boolean().optional(),
|
||||
});
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { getNotificationService, getRegisteredProviderTypes } from '@/lib/services/notification';
|
||||
import { NOTIFICATION_EVENT_KEYS } from '@/lib/constants/notification-events';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
import { z } from 'zod';
|
||||
|
||||
@@ -16,7 +17,7 @@ const CreateBackendSchema = z.object({
|
||||
type: z.string().refine((val) => getRegisteredProviderTypes().includes(val), { message: 'Unsupported notification provider type' }),
|
||||
name: z.string().min(1),
|
||||
config: z.record(z.any()),
|
||||
events: z.array(z.enum(['request_pending_approval', 'request_approved', 'request_available', 'request_error'])).min(1),
|
||||
events: z.array(z.enum(NOTIFICATION_EVENT_KEYS)).min(1),
|
||||
enabled: z.boolean().default(true),
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Component: Admin Replace Audiobook API
|
||||
* Documentation: documentation/backend/services/reported-issues.md
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { replaceAudiobook, ReportedIssueError } from '@/lib/services/reported-issue.service';
|
||||
import { z } from 'zod';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Admin.ReportedIssues.Replace');
|
||||
|
||||
const ReplaceSchema = z.object({
|
||||
torrent: z.object({
|
||||
guid: z.string(),
|
||||
title: z.string(),
|
||||
size: z.number(),
|
||||
seeders: z.number().optional(),
|
||||
leechers: z.number().optional(),
|
||||
indexer: z.string(),
|
||||
indexerId: z.number().optional(),
|
||||
downloadUrl: z.string(),
|
||||
infoUrl: z.string().optional(),
|
||||
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(),
|
||||
protocol: z.enum(['torrent', 'usenet']).optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/admin/reported-issues/[id]/replace
|
||||
* Atomically replace audiobook content: delete old → create new request → start download → resolve issue
|
||||
*/
|
||||
export async function POST(
|
||||
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;
|
||||
const body = await req.json();
|
||||
const { torrent } = ReplaceSchema.parse(body);
|
||||
|
||||
const result = await replaceAudiobook(id, req.user.id, torrent);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
request: result.request,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'ValidationError', details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (error instanceof ReportedIssueError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'ReplaceError', message: error.message },
|
||||
{ status: error.statusCode }
|
||||
);
|
||||
}
|
||||
|
||||
logger.error('Failed to replace audiobook', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: 'ServerError', message: 'Failed to replace audiobook' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Component: Admin Resolve Reported Issue API
|
||||
* Documentation: documentation/backend/services/reported-issues.md
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { dismissIssue, ReportedIssueError } from '@/lib/services/reported-issue.service';
|
||||
import { z } from 'zod';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Admin.ReportedIssues.Resolve');
|
||||
|
||||
const ResolveSchema = z.object({
|
||||
action: z.enum(['dismiss']),
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/admin/reported-issues/[id]/resolve
|
||||
* Dismiss a reported issue
|
||||
*/
|
||||
export async function POST(
|
||||
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;
|
||||
const body = await req.json();
|
||||
const { action } = ResolveSchema.parse(body);
|
||||
|
||||
if (action === 'dismiss') {
|
||||
const issue = await dismissIssue(id, req.user.id);
|
||||
return NextResponse.json({ success: true, issue });
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: 'InvalidAction', message: 'Unknown action' },
|
||||
{ status: 400 }
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'ValidationError', details: error.errors },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (error instanceof ReportedIssueError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'ResolveError', message: error.message },
|
||||
{ status: error.statusCode }
|
||||
);
|
||||
}
|
||||
|
||||
logger.error('Failed to resolve issue', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: 'ServerError', message: 'Failed to resolve issue' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Component: Admin Reported Issues List API
|
||||
* Documentation: documentation/backend/services/reported-issues.md
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { getOpenIssues } from '@/lib/services/reported-issue.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Admin.ReportedIssues');
|
||||
|
||||
/**
|
||||
* GET /api/admin/reported-issues
|
||||
* Get all open reported issues with audiobook metadata and reporter info
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
return requireAuth(request, async (req: AuthenticatedRequest) => {
|
||||
return requireAdmin(req, async () => {
|
||||
try {
|
||||
const issues = await getOpenIssues();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
issues,
|
||||
count: issues.length,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to fetch reported issues', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
return NextResponse.json(
|
||||
{ error: 'ServerError', message: 'Failed to fetch reported issues' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user