mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
feat: Implement robust error/not-found pages & UI components
- 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
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
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 { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
type RouteParams = { params: Promise<{ projectId: string; memberId: string }> };
|
||||
|
||||
@@ -16,7 +17,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId, memberId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const project = await db.project.findUnique({
|
||||
@@ -25,14 +26,14 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
const isOwner = project.ownerId === session.user.id;
|
||||
const isAdmin = project.members[0]?.role === ProjectMemberRole.ADMIN;
|
||||
|
||||
if (!isOwner && !isAdmin) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
@@ -40,10 +41,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
|
||||
const validRoles = ['ADMIN', 'COMMENTATOR'];
|
||||
if (!validRoles.includes(role)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid role. Must be ADMIN or COMMENTATOR.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
return apiErrors.badRequest('Invalid role. Must be ADMIN or COMMENTATOR.');
|
||||
}
|
||||
|
||||
const member = await db.projectMember.update({
|
||||
@@ -54,13 +52,10 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(member);
|
||||
return successResponse(member);
|
||||
} catch (error) {
|
||||
console.error('Error updating member role:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update member role' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to update member role');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +69,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId, memberId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const project = await db.project.findUnique({
|
||||
@@ -83,7 +78,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
const isOwner = project.ownerId === session.user.id;
|
||||
@@ -94,23 +89,20 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!memberToRemove) {
|
||||
return NextResponse.json({ error: 'Member not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Member');
|
||||
}
|
||||
|
||||
const isSelf = memberToRemove.userId === session.user.id;
|
||||
|
||||
if (!isOwner && !isAdmin && !isSelf) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
await db.projectMember.delete({ where: { id: memberId } });
|
||||
|
||||
return NextResponse.json({ success: true, message: 'Member removed' });
|
||||
return successResponse({ message: 'Member removed' });
|
||||
} catch (error) {
|
||||
console.error('Error removing member:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to remove member' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to remove member');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
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 { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
type RouteParams = { params: Promise<{ projectId: string }> };
|
||||
|
||||
@@ -13,7 +14,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const project = await db.project.findUnique({
|
||||
@@ -24,14 +25,14 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
const isOwner = project.ownerId === session.user.id;
|
||||
const isMember = project.members.length > 0;
|
||||
|
||||
if (!isOwner && !isMember) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const members = await db.projectMember.findMany({
|
||||
@@ -47,13 +48,10 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
select: { id: true, name: true, image: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ members, owner });
|
||||
return successResponse({ members, owner });
|
||||
} catch (error) {
|
||||
console.error('Error fetching project members:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch members' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to fetch members');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +65,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
// Check if user is owner or admin
|
||||
@@ -77,27 +75,21 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
const isOwner = project.ownerId === session.user.id;
|
||||
const isAdmin = project.members[0]?.role === ProjectMemberRole.ADMIN;
|
||||
|
||||
if (!isOwner && !isAdmin) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Only project owners and admins can invite members' },
|
||||
{ status: 403 }
|
||||
);
|
||||
return apiErrors.forbidden('Only project owners and admins can invite members');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { email, role } = body;
|
||||
|
||||
if (!email || typeof email !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
return apiErrors.badRequest('Email is required');
|
||||
}
|
||||
|
||||
// Validate role
|
||||
@@ -110,17 +102,11 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!userToInvite) {
|
||||
return NextResponse.json(
|
||||
{ message: 'If the user exists, an invitation has been sent.' },
|
||||
{ status: 200 }
|
||||
);
|
||||
return successResponse({ message: 'If the user exists, an invitation has been sent.' });
|
||||
}
|
||||
|
||||
if (userToInvite.id === project.ownerId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Cannot invite the project owner as a member' },
|
||||
{ status: 400 }
|
||||
);
|
||||
return apiErrors.badRequest('Cannot invite the project owner as a member');
|
||||
}
|
||||
|
||||
// Check if already a member
|
||||
@@ -129,10 +115,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (existingMember) {
|
||||
return NextResponse.json(
|
||||
{ error: 'User is already a member of this project' },
|
||||
{ status: 409 }
|
||||
);
|
||||
return apiErrors.conflict('User is already a member of this project');
|
||||
}
|
||||
|
||||
const member = await db.projectMember.create({
|
||||
@@ -146,12 +129,9 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(member, { status: 201 });
|
||||
return successResponse(member, 201);
|
||||
} catch (error) {
|
||||
console.error('Error inviting project member:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to invite member' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to invite member');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { auth } from '@/lib/auth';
|
||||
import { ProjectMemberRole, ProjectVisibility } from '@prisma/client';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { cleanupProjectVoiceFiles } from '@/lib/r2-cleanup';
|
||||
import { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
type RouteParams = { params: Promise<{ projectId: string }> };
|
||||
|
||||
@@ -76,7 +77,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
// Check access
|
||||
@@ -103,16 +104,13 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
}
|
||||
|
||||
if (!isPublic && !isOwner && !isMember && !isWorkspaceMember) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
return NextResponse.json(project);
|
||||
return successResponse(project);
|
||||
} catch (error) {
|
||||
console.error('Error fetching project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to fetch project');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,12 +124,12 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const { canEdit } = await checkProjectAccess(projectId, session.user.id);
|
||||
if (!canEdit) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
@@ -151,13 +149,10 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(project);
|
||||
return successResponse(project);
|
||||
} catch (error) {
|
||||
console.error('Error updating project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to update project');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,20 +166,17 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const { canDelete, project } = await checkProjectAccess(projectId, session.user.id);
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
if (!canDelete) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Only the project owner can delete it' },
|
||||
{ status: 403 }
|
||||
);
|
||||
return apiErrors.forbidden('Only the project owner can delete it');
|
||||
}
|
||||
|
||||
// Clean up voice files from R2 before cascade delete removes comment rows
|
||||
@@ -192,12 +184,9 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
|
||||
await db.project.delete({ where: { id: projectId } });
|
||||
|
||||
return NextResponse.json({ success: true, message: 'Project deleted' });
|
||||
return successResponse({ message: 'Project deleted' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to delete project');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { NextRequest } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
type RouteParams = { params: Promise<{ projectId: string; tagId: string }> };
|
||||
|
||||
@@ -46,15 +47,15 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId, tagId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const { canEdit, project } = await checkProjectAccess(projectId, session.user.id);
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
if (!canEdit) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
// Verify tag belongs to this project
|
||||
@@ -62,7 +63,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
where: { id: tagId },
|
||||
});
|
||||
if (!existingTag || existingTag.projectId !== projectId) {
|
||||
return NextResponse.json({ error: 'Tag not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Tag');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
@@ -71,13 +72,13 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
const updateData: Record<string, unknown> = {};
|
||||
if (name !== undefined) {
|
||||
if (!name.trim()) {
|
||||
return NextResponse.json({ error: 'Name cannot be empty' }, { status: 400 });
|
||||
return apiErrors.badRequest('Name cannot be empty');
|
||||
}
|
||||
updateData.name = name.trim();
|
||||
}
|
||||
if (color !== undefined) {
|
||||
if (!/^#[0-9A-Fa-f]{6}$/.test(color)) {
|
||||
return NextResponse.json({ error: 'Invalid color format' }, { status: 400 });
|
||||
return apiErrors.badRequest('Invalid color format');
|
||||
}
|
||||
updateData.color = color.toUpperCase();
|
||||
}
|
||||
@@ -90,13 +91,13 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return NextResponse.json(tag);
|
||||
return successResponse(tag);
|
||||
} catch (error) {
|
||||
console.error('Error updating tag:', error);
|
||||
if ((error as { code?: string }).code === 'P2002') {
|
||||
return NextResponse.json({ error: 'Tag name already exists' }, { status: 409 });
|
||||
return apiErrors.conflict('Tag name already exists');
|
||||
}
|
||||
return NextResponse.json({ error: 'Failed to update tag' }, { status: 500 });
|
||||
return apiErrors.internalError('Failed to update tag');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,15 +111,15 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId, tagId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const { canEdit, project } = await checkProjectAccess(projectId, session.user.id);
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
if (!canEdit) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
// Verify tag belongs to this project
|
||||
@@ -126,14 +127,14 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
where: { id: tagId },
|
||||
});
|
||||
if (!existingTag || existingTag.projectId !== projectId) {
|
||||
return NextResponse.json({ error: 'Tag not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Tag');
|
||||
}
|
||||
|
||||
await db.commentTag.delete({ where: { id: tagId } });
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
return successResponse({ message: 'Tag deleted' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting tag:', error);
|
||||
return NextResponse.json({ error: 'Failed to delete tag' }, { status: 500 });
|
||||
return apiErrors.internalError('Failed to delete tag');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { NextRequest } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
type RouteParams = { params: Promise<{ projectId: string }> };
|
||||
|
||||
@@ -52,12 +53,12 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const { project } = await checkProjectAccess(projectId, session.user.id);
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
let tags = await db.commentTag.findMany({
|
||||
@@ -78,10 +79,10 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(tags);
|
||||
return successResponse(tags);
|
||||
} catch (error) {
|
||||
console.error('Error fetching tags:', error);
|
||||
return NextResponse.json({ error: 'Failed to fetch tags' }, { status: 500 });
|
||||
return apiErrors.internalError('Failed to fetch tags');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,27 +96,27 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const { canEdit, project } = await checkProjectAccess(projectId, session.user.id);
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
if (!canEdit) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { name, color } = body;
|
||||
|
||||
if (!name?.trim() || !color?.trim()) {
|
||||
return NextResponse.json({ error: 'Name and color are required' }, { status: 400 });
|
||||
return apiErrors.badRequest('Name and color are required');
|
||||
}
|
||||
|
||||
// Hex color validation
|
||||
if (!/^#[0-9A-Fa-f]{6}$/.test(color)) {
|
||||
return NextResponse.json({ error: 'Invalid color format' }, { status: 400 });
|
||||
return apiErrors.badRequest('Invalid color format');
|
||||
}
|
||||
|
||||
// Get max position
|
||||
@@ -133,12 +134,12 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(tag, { status: 201 });
|
||||
return successResponse(tag, 201);
|
||||
} catch (error) {
|
||||
console.error('Error creating tag:', error);
|
||||
if ((error as { code?: string }).code === 'P2002') {
|
||||
return NextResponse.json({ error: 'Tag name already exists' }, { status: 409 });
|
||||
return apiErrors.conflict('Tag name already exists');
|
||||
}
|
||||
return NextResponse.json({ error: 'Failed to create tag' }, { status: 500 });
|
||||
return apiErrors.internalError('Failed to create tag');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
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 }> };
|
||||
|
||||
@@ -44,7 +45,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!video) {
|
||||
return NextResponse.json({ error: 'Video not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Video');
|
||||
}
|
||||
|
||||
// Check access
|
||||
@@ -53,19 +54,16 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
const isPublic = video.project.visibility === 'PUBLIC';
|
||||
|
||||
if (!isOwner && !isMember && !isPublic) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
return successResponse({
|
||||
...video,
|
||||
isAuthenticated: !!session?.user?.id,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching video:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch video' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to fetch video');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +77,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId, videoId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const video = await db.video.findFirst({
|
||||
@@ -90,7 +88,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!video) {
|
||||
return NextResponse.json({ error: 'Video not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Video');
|
||||
}
|
||||
|
||||
const isOwner = video.project.ownerId === session.user.id;
|
||||
@@ -99,7 +97,7 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
membership?.role === ProjectMemberRole.ADMIN;
|
||||
|
||||
if (!canEdit) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
@@ -119,13 +117,10 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updatedVideo);
|
||||
return successResponse(updatedVideo);
|
||||
} catch (error) {
|
||||
console.error('Error updating video:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update video' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to update video');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,7 +134,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId, videoId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const video = await db.video.findFirst({
|
||||
@@ -150,7 +145,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!video) {
|
||||
return NextResponse.json({ error: 'Video not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Video');
|
||||
}
|
||||
|
||||
const isOwner = video.project.ownerId === session.user.id;
|
||||
@@ -159,10 +154,7 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
const canDelete = isOwner || membership?.role === ProjectMemberRole.ADMIN;
|
||||
|
||||
if (!canDelete) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Only project owner or admin can delete videos' },
|
||||
{ status: 403 }
|
||||
);
|
||||
return apiErrors.forbidden('Only project owner or admin can delete videos');
|
||||
}
|
||||
|
||||
// Clean up voice files from R2 before cascade delete removes comment rows
|
||||
@@ -170,12 +162,9 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
||||
|
||||
await db.video.delete({ where: { id: videoId } });
|
||||
|
||||
return NextResponse.json({ success: true, message: 'Video deleted' });
|
||||
return successResponse({ message: 'Video deleted' });
|
||||
} catch (error) {
|
||||
console.error('Error deleting video:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete video' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to delete video');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { NextRequest } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { ProjectMemberRole } from '@prisma/client';
|
||||
import { validateUrl, validateOptionalUrl } from '@/lib/validation';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
type RouteParams = { params: Promise<{ projectId: string; videoId: string }> };
|
||||
|
||||
@@ -23,7 +24,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!video) {
|
||||
return NextResponse.json({ error: 'Video not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Video');
|
||||
}
|
||||
|
||||
const isOwner = session?.user?.id === video.project.ownerId;
|
||||
@@ -31,7 +32,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
const isPublic = video.project.visibility === 'PUBLIC';
|
||||
|
||||
if (!isOwner && !isMember && !isPublic) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const versions = await db.videoVersion.findMany({
|
||||
@@ -42,13 +43,10 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ versions });
|
||||
return successResponse({ versions });
|
||||
} catch (error) {
|
||||
console.error('Error fetching versions:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch versions' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to fetch versions');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +60,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId, videoId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const video = await db.video.findFirst({
|
||||
@@ -74,7 +72,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!video) {
|
||||
return NextResponse.json({ error: 'Video not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Video');
|
||||
}
|
||||
|
||||
const isOwner = video.project.ownerId === session.user.id;
|
||||
@@ -83,28 +81,25 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
membership?.role === ProjectMemberRole.ADMIN;
|
||||
|
||||
if (!canEdit) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { videoUrl, providerId, providerVideoId, versionLabel, thumbnailUrl, duration, setActive } = body;
|
||||
|
||||
if (!videoUrl) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Video URL is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
return apiErrors.badRequest('Video URL is required');
|
||||
}
|
||||
|
||||
// Validate URLs use safe schemes (http/https only)
|
||||
const videoUrlError = validateUrl(videoUrl, 'Video URL');
|
||||
if (videoUrlError) {
|
||||
return NextResponse.json({ error: videoUrlError }, { status: 400 });
|
||||
return apiErrors.badRequest(videoUrlError);
|
||||
}
|
||||
|
||||
const thumbnailUrlError = validateOptionalUrl(thumbnailUrl, 'Thumbnail URL');
|
||||
if (thumbnailUrlError) {
|
||||
return NextResponse.json({ error: thumbnailUrlError }, { status: 400 });
|
||||
return apiErrors.badRequest(thumbnailUrlError);
|
||||
}
|
||||
|
||||
const nextVersionNumber = (video.versions[0]?.versionNumber || 0) + 1;
|
||||
@@ -138,12 +133,9 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
});
|
||||
|
||||
return NextResponse.json(version, { status: 201 });
|
||||
return successResponse(version, 201);
|
||||
} catch (error) {
|
||||
console.error('Error creating version:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create version' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to create version');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { NextRequest } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { ProjectMemberRole } from '@prisma/client';
|
||||
import { validateUrl, validateOptionalUrl } from '@/lib/validation';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { notifyProjectOwner } from '@/lib/notifications';
|
||||
import { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
type RouteParams = { params: Promise<{ projectId: string }> };
|
||||
|
||||
@@ -21,7 +22,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
const isOwner = session?.user?.id === project.ownerId;
|
||||
@@ -29,7 +30,7 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
const isPublic = project.visibility === 'PUBLIC';
|
||||
|
||||
if (!isOwner && !isMember && !isPublic) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const videos = await db.video.findMany({
|
||||
@@ -46,13 +47,10 @@ export async function GET(request: NextRequest, { params }: RouteParams) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ videos });
|
||||
return successResponse({ videos });
|
||||
} catch (error) {
|
||||
console.error('Error fetching videos:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch videos' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to fetch videos');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +64,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
const { projectId } = await params;
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
// Check project access (must be owner or admin)
|
||||
@@ -76,7 +74,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Project');
|
||||
}
|
||||
|
||||
const isOwner = project.ownerId === session.user.id;
|
||||
@@ -85,28 +83,25 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
membership?.role === ProjectMemberRole.ADMIN;
|
||||
|
||||
if (!canEdit) {
|
||||
return NextResponse.json({ error: 'Access denied' }, { status: 403 });
|
||||
return apiErrors.forbidden('Access denied');
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { title, description, videoUrl, providerId, videoId, thumbnailUrl, duration } = body;
|
||||
|
||||
if (!title || !videoUrl) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Title and video URL are required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
return apiErrors.badRequest('Title and video URL are required');
|
||||
}
|
||||
|
||||
// Validate URLs use safe schemes (http/https only)
|
||||
const videoUrlError = validateUrl(videoUrl, 'Video URL');
|
||||
if (videoUrlError) {
|
||||
return NextResponse.json({ error: videoUrlError }, { status: 400 });
|
||||
return apiErrors.badRequest(videoUrlError);
|
||||
}
|
||||
|
||||
const thumbnailUrlError = validateOptionalUrl(thumbnailUrl, 'Thumbnail URL');
|
||||
if (thumbnailUrlError) {
|
||||
return NextResponse.json({ error: thumbnailUrlError }, { status: 400 });
|
||||
return apiErrors.badRequest(thumbnailUrlError);
|
||||
}
|
||||
|
||||
// Get the next position
|
||||
@@ -154,12 +149,9 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
||||
}).catch((err) => console.error('Notification failed:', err));
|
||||
}
|
||||
|
||||
return NextResponse.json(video, { status: 201 });
|
||||
return successResponse(video, 201);
|
||||
} catch (error) {
|
||||
console.error('Error creating video:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create video' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to create video');
|
||||
}
|
||||
}
|
||||
|
||||
+16
-29
@@ -3,6 +3,7 @@ import { db } from '@/lib/db';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { ProjectVisibility } from '@prisma/client';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { apiErrors, successResponse } from '@/lib/api-response';
|
||||
|
||||
// GET /api/projects - List all projects for the authenticated user
|
||||
export async function GET(request: NextRequest) {
|
||||
@@ -10,7 +11,7 @@ export async function GET(request: NextRequest) {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
@@ -55,21 +56,19 @@ export async function GET(request: NextRequest) {
|
||||
}),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
projects,
|
||||
pagination: {
|
||||
return successResponse(
|
||||
{ projects },
|
||||
200,
|
||||
{
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error fetching projects:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch projects' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to fetch projects');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,24 +81,18 @@ export async function POST(request: NextRequest) {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { name, description, visibility, workspaceId } = body;
|
||||
|
||||
if (!name || typeof name !== 'string' || name.trim().length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Project name is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
return apiErrors.badRequest('Project name is required');
|
||||
}
|
||||
|
||||
if (!workspaceId || typeof workspaceId !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ error: 'A workspace is required. Every project must belong to a workspace.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
return apiErrors.badRequest('A workspace is required. Every project must belong to a workspace.');
|
||||
}
|
||||
|
||||
// Generate URL-friendly slug
|
||||
@@ -127,17 +120,14 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
|
||||
if (!workspace) {
|
||||
return NextResponse.json({ error: 'Workspace not found' }, { status: 404 });
|
||||
return apiErrors.notFound('Workspace');
|
||||
}
|
||||
|
||||
const isWsOwner = workspace.ownerId === session.user.id;
|
||||
const isWsAdmin = workspace.members[0]?.role === 'ADMIN';
|
||||
|
||||
if (!isWsOwner && !isWsAdmin) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Only workspace owners and admins can create projects' },
|
||||
{ status: 403 }
|
||||
);
|
||||
return apiErrors.forbidden('Only workspace owners and admins can create projects');
|
||||
}
|
||||
|
||||
const project = await db.project.create({
|
||||
@@ -155,12 +145,9 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(project, { status: 201 });
|
||||
return successResponse(project, 201);
|
||||
} catch (error) {
|
||||
console.error('Error creating project:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create project' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return apiErrors.internalError('Failed to create project');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user