- lib/user-settings.ts: activeSprints values nullable in Zod-schema. Key-aanwezigheid heeft nu betekenis (key+null = bewust geen sprint; key ontbreekt = fallback-cascade). - lib/active-sprint.ts: nieuwe readStoredActiveSprintState helper + resolveActiveSprint respecteert expliciet 'cleared' state zonder fallback. clearActiveSprintInSettings schrijft null i.p.v. de key te verwijderen. - actions/active-sprint.ts: nieuwe clearActiveSprintAction met auth + membership-check. - components/shared/sprint-switcher.tsx: '— Geen actieve sprint —'-optie in dropdown, disabled wanneer er geen actieve sprint is. - Tests: nieuwe active-sprint.test.ts (resolver-paden + clear), active-sprint-action.test.ts (action-laag), uitbreiding user-settings.test.ts. Plan: docs/plans/PBI-79-backlog-sprint-workflow.md Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
89 lines
2.9 KiB
TypeScript
89 lines
2.9 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 {
|
|
clearActiveSprintInSettings,
|
|
setActiveSprintInSettings,
|
|
} from '@/lib/active-sprint'
|
|
|
|
async function getSession() {
|
|
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
|
}
|
|
|
|
const setSchema = z.object({
|
|
productId: z.string().min(1),
|
|
sprintId: z.string().min(1),
|
|
})
|
|
|
|
const clearSchema = z.object({
|
|
productId: z.string().min(1),
|
|
})
|
|
|
|
export async function setActiveSprintAction(productId: string, sprintId: string) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const parsed = setSchema.safeParse({ productId, sprintId })
|
|
if (!parsed.success) return { error: 'Ongeldig product- of sprint-id' }
|
|
|
|
const sprint = await prisma.sprint.findFirst({
|
|
where: {
|
|
id: parsed.data.sprintId,
|
|
product_id: parsed.data.productId,
|
|
product: productAccessFilter(session.userId),
|
|
},
|
|
select: { id: true },
|
|
})
|
|
if (!sprint) return { error: 'Sprint niet gevonden of niet toegankelijk' }
|
|
|
|
await setActiveSprintInSettings(session.userId, parsed.data.productId, parsed.data.sprintId)
|
|
revalidatePath('/', 'layout')
|
|
return { success: true, sprintId: parsed.data.sprintId }
|
|
}
|
|
|
|
export async function clearActiveSprintAction(productId: string) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const parsed = clearSchema.safeParse({ productId })
|
|
if (!parsed.success) return { error: 'Ongeldig product-id' }
|
|
|
|
const product = await prisma.product.findFirst({
|
|
where: { id: parsed.data.productId, ...productAccessFilter(session.userId) },
|
|
select: { id: true },
|
|
})
|
|
if (!product) return { error: 'Product niet gevonden of niet toegankelijk' }
|
|
|
|
await clearActiveSprintInSettings(session.userId, parsed.data.productId)
|
|
revalidatePath('/', 'layout')
|
|
return { success: true }
|
|
}
|
|
|
|
export async function syncActiveSprintCookieAction(productId: string, sprintId: string) {
|
|
const session = await getSession()
|
|
if (!session.userId) return
|
|
if (session.isDemo) return
|
|
|
|
const parsed = setSchema.safeParse({ productId, sprintId })
|
|
if (!parsed.success) return
|
|
|
|
const sprint = await prisma.sprint.findFirst({
|
|
where: {
|
|
id: parsed.data.sprintId,
|
|
product_id: parsed.data.productId,
|
|
product: productAccessFilter(session.userId),
|
|
},
|
|
select: { id: true },
|
|
})
|
|
if (!sprint) return
|
|
|
|
await setActiveSprintInSettings(session.userId, parsed.data.productId, parsed.data.sprintId)
|
|
}
|