perf(realtime+data): implement perf-data-optimization and perf-realtime-scale
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 3m33s

## 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>
This commit is contained in:
2026-03-10 15:30:54 +01:00
parent 5b45f18ad9
commit 3d4803f975
32 changed files with 987 additions and 634 deletions

View File

@@ -1,3 +1,4 @@
import { unstable_cache } from 'next/cache';
import { prisma } from '@/services/database';
import { resolveCollaborator, batchResolveCollaborators } from '@/services/auth';
import { getTeamMemberIdsForAdminTeams } from '@/services/teams';
@@ -8,38 +9,50 @@ import {
fetchTeamCollaboratorSessions,
getSessionByIdGeneric,
} from '@/services/session-queries';
import { sessionsListTag } from '@/lib/cache-tags';
import type { MotivatorType } from '@prisma/client';
const motivatorInclude = {
const motivatorListSelect = {
id: true,
title: true,
participant: true,
updatedAt: true,
userId: true,
user: { select: { id: true, name: true, email: true } },
shares: { include: { user: { select: { id: true, name: true, email: true } } } },
shares: { select: { id: true, role: true, user: { select: { id: true, name: true, email: true } } } },
_count: { select: { cards: true } },
};
} as const;
// ============================================
// Moving Motivators Session CRUD
// ============================================
export async function getMotivatorSessionsByUserId(userId: string) {
const sessions = await mergeSessionsByUserId(
(uid) =>
prisma.movingMotivatorsSession.findMany({
where: { userId: uid },
include: motivatorInclude,
orderBy: { updatedAt: 'desc' },
}),
(uid) =>
prisma.mMSessionShare.findMany({
where: { userId: uid },
include: { session: { include: motivatorInclude } },
}),
userId
);
const resolved = await batchResolveCollaborators(sessions.map((s) => s.participant));
return sessions.map((s) => ({
...s,
resolvedParticipant: resolved.get(s.participant.trim()) ?? { raw: s.participant, matchedUser: null },
}));
return unstable_cache(
async () => {
const sessions = await mergeSessionsByUserId(
(uid) =>
prisma.movingMotivatorsSession.findMany({
where: { userId: uid },
select: motivatorListSelect,
orderBy: { updatedAt: 'desc' },
}),
(uid) =>
prisma.mMSessionShare.findMany({
where: { userId: uid },
select: { role: true, createdAt: true, session: { select: motivatorListSelect } },
}),
userId
);
const resolved = await batchResolveCollaborators(sessions.map((s) => s.participant));
return sessions.map((s) => ({
...s,
resolvedParticipant: resolved.get(s.participant.trim()) ?? { raw: s.participant, matchedUser: null },
}));
},
[`motivator-sessions-list-${userId}`],
{ tags: [sessionsListTag(userId)], revalidate: 60 }
)();
}
/** Sessions owned by team members (where user is team admin) that are NOT shared with the user. */
@@ -48,7 +61,7 @@ export async function getTeamCollaboratorSessionsForAdmin(userId: string) {
(teamMemberIds, uid) =>
prisma.movingMotivatorsSession.findMany({
where: { userId: { in: teamMemberIds }, shares: { none: { userId: uid } } },
include: motivatorInclude,
select: motivatorListSelect,
orderBy: { updatedAt: 'desc' },
}),
getTeamMemberIdsForAdminTeams,