Symptoom op feat/ST-801-realtime-triggers initial implementation: elke task-update sloot de open SSE-stream af en triggerde een herverbinding met backoff. In de tussentijd gemiste events. Oorzaak: Server Actions in App Router doen een impliciete route-tree refresh die client components remount; daarmee killt React de useEffect die de EventSource beheert. Fix in twee delen: 1. Hef de realtime-hook op naar de (app)-layout via een nieuwe `SoloRealtimeBridge`-component. Layouts overleven Server- Action-refreshes beter dan pages, en de bridge leest het product-id uit de URL via usePathname. Connection-status (status, showConnectingIndicator) gaat naar de solo-store zodat SoloBoard 'm uit een gedeelde plek kan lezen. 2. Vervang updateTaskStatusAction en updateTaskPlanAction in de Solo-componenten door fetch naar de bestaande Route Handler `PATCH /api/tasks/[id]`. Route Handlers triggeren geen page-refresh, dus de SSE-stream blijft staan. lib/api-auth.ts accepteert nu naast Bearer-tokens ook iron-session cookies zodat browser-fetches zonder token werken. Bijkomend: actions/tasks.ts laat /solo bewust niet meer revalideren (wordt nu via realtime gedekt). Sprint/planning blijft wel revalidaten — geen realtime daar. Toegevoegd: - components/solo/realtime-bridge.tsx — mount in (app) layout - scripts/realtime-mutate.ts — handige test-helper voor externe mutaties (alsof MCP/REST schrijft) tijdens acceptance Debug-logs in app/api/realtime/solo/route.ts staan nog aan voor ST-806 acceptance; worden later gestript. Bekend issue: Chrome op localhost (HTTP/1.1) cycle't EventSource om de paar seconden vanwege de 6-connectie-limiet en retry- heuristiek. Safari werkt stabiel. Productie op Vercel (HTTP/2 multiplexing) zou beide browsers stabiel moeten houden — Vercel preview test is volgende stap. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
179 lines
6.1 KiB
TypeScript
179 lines
6.1 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { cookies } from 'next/headers'
|
|
import { getIronSession } from 'iron-session'
|
|
import { z } from 'zod'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { SessionData, sessionOptions } from '@/lib/session'
|
|
import { productAccessFilter } from '@/lib/product-access'
|
|
import { requireProductWriter } from '@/lib/auth'
|
|
|
|
async function getSession() {
|
|
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
|
}
|
|
|
|
const taskSchema = z.object({
|
|
title: z.string().min(1, 'Titel is verplicht').max(200),
|
|
description: z.string().max(1000).optional(),
|
|
priority: z.coerce.number().int().min(1).max(4),
|
|
})
|
|
|
|
export async function createTaskAction(_prevState: unknown, formData: FormData) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const storyId = formData.get('storyId') as string
|
|
const sprintId = formData.get('sprintId') as string
|
|
|
|
const parsed = taskSchema.safeParse({
|
|
title: formData.get('title'),
|
|
description: formData.get('description') || undefined,
|
|
priority: formData.get('priority') ?? 2,
|
|
})
|
|
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
|
|
|
|
const story = await prisma.story.findFirst({
|
|
where: { id: storyId, product: productAccessFilter(session.userId) },
|
|
})
|
|
if (!story) return { error: 'Story niet gevonden' }
|
|
|
|
const last = await prisma.task.findFirst({
|
|
where: { story_id: storyId },
|
|
orderBy: { sort_order: 'desc' },
|
|
})
|
|
|
|
const task = await prisma.task.create({
|
|
data: {
|
|
story_id: storyId,
|
|
sprint_id: sprintId || null,
|
|
title: parsed.data.title,
|
|
description: parsed.data.description ?? null,
|
|
priority: parsed.data.priority,
|
|
sort_order: (last?.sort_order ?? 0) + 1.0,
|
|
status: 'TO_DO',
|
|
},
|
|
})
|
|
|
|
revalidatePath(`/products/${story.product_id}/sprint/planning`)
|
|
return { success: true, task }
|
|
}
|
|
|
|
export async function updateTaskAction(_prevState: unknown, formData: FormData) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const id = formData.get('id') as string
|
|
const parsed = taskSchema.safeParse({
|
|
title: formData.get('title'),
|
|
description: formData.get('description') || undefined,
|
|
priority: formData.get('priority'),
|
|
})
|
|
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
|
|
|
|
const task = await prisma.task.findFirst({
|
|
where: { id, story: { product: productAccessFilter(session.userId) } },
|
|
include: { story: true },
|
|
})
|
|
if (!task) return { error: 'Taak niet gevonden' }
|
|
|
|
await prisma.task.update({
|
|
where: { id },
|
|
data: { title: parsed.data.title, description: parsed.data.description ?? null, priority: parsed.data.priority },
|
|
})
|
|
|
|
revalidatePath(`/products/${task.story.product_id}/sprint/planning`)
|
|
return { success: true }
|
|
}
|
|
|
|
export async function updateTaskStatusAction(id: string, status: 'TO_DO' | 'IN_PROGRESS' | 'DONE') {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const task = await prisma.task.findFirst({
|
|
where: { id, story: { product: productAccessFilter(session.userId) } },
|
|
include: { story: true },
|
|
})
|
|
if (!task) return { error: 'Taak niet gevonden' }
|
|
|
|
await prisma.task.update({ where: { id }, data: { status } })
|
|
|
|
// /solo bewust niet revalideren: dat zou de page soft-navigaten en de
|
|
// open SSE-stream sluiten. De Solo Paneel-flow leunt op optimistic
|
|
// store-updates + realtime echo (M8). Sprint/planning heeft geen
|
|
// realtime en moet wèl revalidaten.
|
|
revalidatePath(`/products/${task.story.product_id}/sprint/planning`)
|
|
return { success: true }
|
|
}
|
|
|
|
export async function deleteTaskAction(id: string) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const task = await prisma.task.findFirst({
|
|
where: { id, story: { product: productAccessFilter(session.userId) } },
|
|
include: { story: true },
|
|
})
|
|
if (!task) return { error: 'Taak niet gevonden' }
|
|
|
|
await prisma.task.delete({ where: { id } })
|
|
|
|
revalidatePath(`/products/${task.story.product_id}/sprint/planning`)
|
|
return { success: true }
|
|
}
|
|
|
|
const updateTaskPlanSchema = z.object({
|
|
taskId: z.string().min(1),
|
|
productId: z.string().min(1),
|
|
implementationPlan: z.string().max(10000),
|
|
})
|
|
|
|
export async function updateTaskPlanAction(taskId: string, productId: string, implementationPlan: string) {
|
|
try {
|
|
await requireProductWriter(productId)
|
|
} catch (e) {
|
|
return { error: e instanceof Error ? e.message : 'Niet geautoriseerd' }
|
|
}
|
|
|
|
const parsed = updateTaskPlanSchema.safeParse({ taskId, productId, implementationPlan })
|
|
if (!parsed.success) return { error: 'Ongeldige invoer' }
|
|
|
|
const task = await prisma.task.findFirst({
|
|
where: { id: taskId, story: { product_id: productId } },
|
|
include: { story: true },
|
|
})
|
|
if (!task) return { error: 'Taak niet gevonden' }
|
|
|
|
await prisma.task.update({
|
|
where: { id: taskId },
|
|
data: { implementation_plan: implementationPlan || null },
|
|
})
|
|
|
|
// /solo bewust niet revalideren — zie updateTaskStatusAction.
|
|
revalidatePath(`/products/${productId}/sprint/planning`)
|
|
return { success: true }
|
|
}
|
|
|
|
export async function reorderTasksAction(storyId: string, orderedIds: string[]) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const story = await prisma.story.findFirst({
|
|
where: { id: storyId, product: productAccessFilter(session.userId) },
|
|
})
|
|
if (!story) return { error: 'Story niet gevonden' }
|
|
|
|
await prisma.$transaction(
|
|
orderedIds.map((id, i) =>
|
|
prisma.task.update({ where: { id }, data: { sort_order: i + 1.0 } })
|
|
)
|
|
)
|
|
|
|
revalidatePath(`/products/${story.product_id}/sprint/planning`)
|
|
return { success: true }
|
|
}
|