Files
workshop-manager/src/app/api/sessions/route.ts
Froidefond Julien 3d4803f975
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 3m33s
perf(realtime+data): implement perf-data-optimization and perf-realtime-scale
## perf-data-optimization
- Add @@index([name]) on User model (migration)
- Add WEATHER_HISTORY_LIMIT=90 constant, apply take/orderBy on weather history queries
- Replace deep includes with explicit select on all 6 list service queries
- Add unstable_cache layer with revalidateTag on all list service functions
- Add cache-tags.ts helpers (sessionTag, sessionsListTag, userStatsTag)
- Invalidate sessionsListTag in all create/delete Server Actions

## perf-realtime-scale
- Create src/lib/broadcast.ts: generic createBroadcaster factory with shared polling
  (one interval per active session, starts on first subscriber, stops on last)
- Migrate all 6 SSE routes to use createBroadcaster — removes per-connection setInterval
- Add broadcastToXxx() calls in all Server Actions after mutations for immediate push
- Add SESSIONS_PAGE_SIZE=20, pagination on sessions page with loadMoreSessions action
- Add "Charger plus" button with loading state and "X sur Y" counter in WorkshopTabs

## Tests
- Add 19 unit tests for broadcast.ts (polling lifecycle, userId filtering,
  formatEvent, error resilience, session isolation)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-10 15:30:54 +01:00

78 lines
2.0 KiB
TypeScript

import { NextResponse } from 'next/server';
import { revalidateTag } from 'next/cache';
import { auth } from '@/lib/auth';
import { prisma } from '@/services/database';
import { shareSession } from '@/services/sessions';
import { sessionsListTag } from '@/lib/cache-tags';
export async function GET() {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const sessions = await prisma.session.findMany({
where: { userId: session.user.id },
include: {
_count: {
select: {
items: true,
actions: true,
},
},
},
orderBy: { updatedAt: 'desc' },
});
return NextResponse.json(sessions);
} catch (error) {
console.error('Error fetching sessions:', error);
return NextResponse.json(
{ error: 'Erreur lors de la récupération des sessions' },
{ status: 500 }
);
}
}
export async function POST(request: Request) {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const body = await request.json();
const { title, collaborator } = body;
if (!title || !collaborator) {
return NextResponse.json({ error: 'Titre et collaborateur requis' }, { status: 400 });
}
const newSession = await prisma.session.create({
data: {
title,
collaborator,
userId: session.user.id,
},
});
try {
await shareSession(newSession.id, session.user.id, collaborator, 'EDITOR');
} catch (shareError) {
console.error('Auto-share failed:', shareError);
}
revalidateTag(sessionsListTag(session.user.id), 'default');
return NextResponse.json(newSession, { status: 201 });
} catch (error) {
console.error('Error creating session:', error);
return NextResponse.json(
{ error: 'Erreur lors de la création de la session' },
{ status: 500 }
);
}
}