Files
OpenFrame/app/api/workspaces/route.ts
T
Yusuf İpek 88c74d646e feat: add video watch progress tracking with resume functionality
Implements a complete watch progress system that allows users to:
- Save playback position automatically every 5 seconds while watching
- Resume from last position when returning to a video
- Save progress on page leave using sendBeacon for reliability

Also includes:
- Optimized slug generation in projects and workspaces APIs (single query vs loop)
- Added isActive filter to version queries across all video endpoints
- Added pagination support for video and comment queries
- Enhanced database pool management with connection limits and graceful shutdown
- New WatchProgress Prisma model with user-version relations
2026-02-14 15:25:32 +03:00

101 lines
3.3 KiB
TypeScript

import { NextRequest } from 'next/server';
import { db } from '@/lib/db';
import { auth } from '@/lib/auth';
import { rateLimit } from '@/lib/rate-limit';
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
// GET /api/workspaces - List all workspaces for the authenticated user
export async function GET() {
try {
const session = await auth();
if (!session?.user?.id) {
return apiErrors.unauthorized();
}
// Get workspaces where user is owner OR a member
const workspaces = await db.workspace.findMany({
where: {
OR: [
{ ownerId: session.user.id },
{ members: { some: { userId: session.user.id } } },
],
},
include: {
owner: { select: { id: true, name: true, image: true } },
_count: { select: { projects: true, members: true } },
},
orderBy: { updatedAt: 'desc' },
});
const response = successResponse({ workspaces });
return withCacheControl(response, 'private, max-age=60, stale-while-revalidate=120');
} catch (error) {
console.error('Error fetching workspaces:', error);
return apiErrors.internalError('Failed to fetch workspaces');
}
}
// POST /api/workspaces - Create a new workspace
export async function POST(request: NextRequest) {
try {
const limited = await rateLimit(request, 'create-workspace');
if (limited) return limited;
const session = await auth();
if (!session?.user?.id) {
return apiErrors.unauthorized();
}
const body = await request.json();
const { name, description } = body;
if (!name || typeof name !== 'string' || name.trim().length === 0) {
return apiErrors.badRequest('Workspace name is required');
}
// Generate slug
const baseSlug = name
.toLowerCase()
.trim()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
// Find all existing slugs with the same prefix in a single query
const existingWorkspaces = await db.workspace.findMany({
where: { slug: { startsWith: baseSlug } },
select: { slug: true },
});
// Generate unique slug from the results
const usedSlugs = new Set(existingWorkspaces.map(w => w.slug));
let slug = baseSlug;
let counter = 1;
while (usedSlugs.has(slug)) {
slug = `${baseSlug}-${counter}`;
counter++;
}
const workspace = await db.workspace.create({
data: {
name: name.trim(),
description: description?.trim() || null,
slug,
ownerId: session.user.id,
},
include: {
owner: { select: { id: true, name: true, image: true } },
_count: { select: { projects: true, members: true } },
},
});
const response = successResponse(workspace, 201);
return withCacheControl(response, 'private, no-store');
} catch (error) {
console.error('Error creating workspace:', error);
return apiErrors.internalError('Failed to create workspace');
}
}