mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
- Introduce dedicated error pages for dashboard and video routes - Add specific not-found pages for dashboard, projects, videos, and settings - Implement global `not-found.tsx` for general unhandled routes - Integrate root and dashboard layouts with ErrorBoundary and Suspense - Add new UI components: Accordion, Hover Card, Menubar, Navigation Menu, Select, Tabs - Update Navbar to utilize the new Navigation Menu component - Enhance `button` component with a `link` variant for better styling - Refine existing UI components (dialog, dropdown, input, etc.) - Update Tailwind config with new colors and animation extensions
171 lines
6.0 KiB
TypeScript
171 lines
6.0 KiB
TypeScript
import { NextRequest } from 'next/server';
|
|
import { db } from '@/lib/db';
|
|
import { auth } from '@/lib/auth';
|
|
import { ProjectMemberRole } from '@prisma/client';
|
|
import { rateLimit } from '@/lib/rate-limit';
|
|
import { cleanupVideoVoiceFiles } from '@/lib/r2-cleanup';
|
|
import { apiErrors, successResponse } from '@/lib/api-response';
|
|
|
|
type RouteParams = { params: Promise<{ projectId: string; videoId: string }> };
|
|
|
|
// GET /api/projects/[projectId]/videos/[videoId]
|
|
export async function GET(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const session = await auth();
|
|
const { projectId, videoId } = await params;
|
|
|
|
const video = await db.video.findFirst({
|
|
where: { id: videoId, projectId },
|
|
include: {
|
|
project: {
|
|
include: { members: { where: { userId: session?.user?.id || '' } } },
|
|
},
|
|
versions: {
|
|
orderBy: { versionNumber: 'desc' },
|
|
include: {
|
|
comments: {
|
|
orderBy: { timestamp: 'asc' },
|
|
include: {
|
|
author: { select: { id: true, name: true, image: true } },
|
|
tag: { select: { id: true, name: true, color: true } },
|
|
replies: {
|
|
orderBy: { createdAt: 'asc' },
|
|
include: {
|
|
author: { select: { id: true, name: true, image: true } },
|
|
tag: { select: { id: true, name: true, color: true } },
|
|
},
|
|
},
|
|
},
|
|
where: { parentId: null }, // Only top-level comments
|
|
},
|
|
_count: { select: { comments: true } },
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!video) {
|
|
return apiErrors.notFound('Video');
|
|
}
|
|
|
|
// Check access
|
|
const isOwner = session?.user?.id === video.project.ownerId;
|
|
const isMember = video.project.members.length > 0;
|
|
const isPublic = video.project.visibility === 'PUBLIC';
|
|
|
|
if (!isOwner && !isMember && !isPublic) {
|
|
return apiErrors.forbidden('Access denied');
|
|
}
|
|
|
|
return successResponse({
|
|
...video,
|
|
isAuthenticated: !!session?.user?.id,
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching video:', error);
|
|
return apiErrors.internalError('Failed to fetch video');
|
|
}
|
|
}
|
|
|
|
// PATCH /api/projects/[projectId]/videos/[videoId]
|
|
export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const limited = await rateLimit(request, 'mutate');
|
|
if (limited) return limited;
|
|
|
|
const session = await auth();
|
|
const { projectId, videoId } = await params;
|
|
|
|
if (!session?.user?.id) {
|
|
return apiErrors.unauthorized();
|
|
}
|
|
|
|
const video = await db.video.findFirst({
|
|
where: { id: videoId, projectId },
|
|
include: {
|
|
project: { include: { members: { where: { userId: session.user.id } } } },
|
|
},
|
|
});
|
|
|
|
if (!video) {
|
|
return apiErrors.notFound('Video');
|
|
}
|
|
|
|
const isOwner = video.project.ownerId === session.user.id;
|
|
const membership = video.project.members[0];
|
|
const canEdit = isOwner ||
|
|
membership?.role === ProjectMemberRole.ADMIN;
|
|
|
|
if (!canEdit) {
|
|
return apiErrors.forbidden('Access denied');
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { title, description, position } = body;
|
|
|
|
const updateData: Record<string, unknown> = {};
|
|
if (title !== undefined) updateData.title = title.trim();
|
|
if (description !== undefined) updateData.description = description?.trim() || null;
|
|
if (position !== undefined) updateData.position = position;
|
|
|
|
const updatedVideo = await db.video.update({
|
|
where: { id: videoId },
|
|
data: updateData,
|
|
include: {
|
|
versions: { orderBy: { versionNumber: 'desc' } },
|
|
_count: { select: { versions: true } },
|
|
},
|
|
});
|
|
|
|
return successResponse(updatedVideo);
|
|
} catch (error) {
|
|
console.error('Error updating video:', error);
|
|
return apiErrors.internalError('Failed to update video');
|
|
}
|
|
}
|
|
|
|
// DELETE /api/projects/[projectId]/videos/[videoId]
|
|
export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const limited = await rateLimit(request, 'mutate');
|
|
if (limited) return limited;
|
|
|
|
const session = await auth();
|
|
const { projectId, videoId } = await params;
|
|
|
|
if (!session?.user?.id) {
|
|
return apiErrors.unauthorized();
|
|
}
|
|
|
|
const video = await db.video.findFirst({
|
|
where: { id: videoId, projectId },
|
|
include: {
|
|
project: { include: { members: { where: { userId: session.user.id } } } },
|
|
},
|
|
});
|
|
|
|
if (!video) {
|
|
return apiErrors.notFound('Video');
|
|
}
|
|
|
|
const isOwner = video.project.ownerId === session.user.id;
|
|
const membership = video.project.members[0];
|
|
// Destructive actions limited to OWNER and ADMIN only
|
|
const canDelete = isOwner || membership?.role === ProjectMemberRole.ADMIN;
|
|
|
|
if (!canDelete) {
|
|
return apiErrors.forbidden('Only project owner or admin can delete videos');
|
|
}
|
|
|
|
// Clean up voice files from R2 before cascade delete removes comment rows
|
|
await cleanupVideoVoiceFiles(videoId);
|
|
|
|
await db.video.delete({ where: { id: videoId } });
|
|
|
|
return successResponse({ message: 'Video deleted' });
|
|
} catch (error) {
|
|
console.error('Error deleting video:', error);
|
|
return apiErrors.internalError('Failed to delete video');
|
|
}
|
|
}
|