Story 7 van PBI "Alle dialogen conform docs/patterns/dialog.md". - lib/schemas/question-answer.ts — gedeeld zod-schema + ANSWER_MAX_CHARS constant - actions/questions.ts gebruikt het gedeelde schema - AnswerModal: entityDialog* layout-classes, useDirtyCloseGuard, useDialogSubmitShortcut, DemoTooltip rond submit + multiple-choice knoppen - docs/specs/dialogs/answer-modal.md — entity-profile Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
79 lines
2.8 KiB
TypeScript
79 lines
2.8 KiB
TypeScript
'use server'
|
|
|
|
// ST-1103: Server Action voor het beantwoorden van een Claude-vraag (M11).
|
|
//
|
|
// Volgt docs/patterns/server-action.md: getSession + Zod + demo-blok +
|
|
// productAccessFilter. Atomic updateMany sluit double-submit uit; bij race
|
|
// (count=0) doet een tweede findFirst de disambiguatie tussen 'al beantwoord',
|
|
// 'verlopen', en 'niet gevonden of geen toegang'.
|
|
//
|
|
// revalidatePath('/', 'layout') refresh't de NavBar-bell badge-count voor
|
|
// SSR-rendered pages — de Zustand store + SSE in ST-1104/1105 dekken de
|
|
// realtime updates voor andere clients.
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { getSession } from '@/lib/auth'
|
|
import { productAccessFilter } from '@/lib/product-access'
|
|
import { answerQuestionSchema } from '@/lib/schemas/question-answer'
|
|
|
|
type ActionResult = { ok: true } | { ok: false; error: string }
|
|
|
|
export async function answerQuestion(
|
|
questionId: string,
|
|
answer: string,
|
|
): Promise<ActionResult> {
|
|
const session = await getSession()
|
|
if (!session.userId) return { ok: false, error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { ok: false, error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const parsed = answerQuestionSchema.safeParse({ questionId, answer })
|
|
if (!parsed.success) {
|
|
const first = parsed.error.issues[0]?.message ?? 'Ongeldige invoer'
|
|
return { ok: false, error: first }
|
|
}
|
|
|
|
// Access-check: gebruiker moet toegang hebben tot het product van de vraag.
|
|
// Iedereen met product-membership mag antwoorden — niet alleen de story-
|
|
// assignee — consistent met Scrum self-organizing.
|
|
const question = await prisma.claudeQuestion.findFirst({
|
|
where: {
|
|
id: parsed.data.questionId,
|
|
product: productAccessFilter(session.userId),
|
|
},
|
|
select: { id: true },
|
|
})
|
|
if (!question) return { ok: false, error: 'Vraag niet gevonden of geen toegang' }
|
|
|
|
// Atomic state-transitie: alleen open + niet-verlopen vragen worden beantwoord.
|
|
// Concurrent dubbele submit: PostgreSQL row-locking laat één caller count=1
|
|
// zien, de rest count=0 → disambiguatie hieronder.
|
|
const updated = await prisma.claudeQuestion.updateMany({
|
|
where: {
|
|
id: parsed.data.questionId,
|
|
status: 'open',
|
|
expires_at: { gt: new Date() },
|
|
},
|
|
data: {
|
|
status: 'answered',
|
|
answer: parsed.data.answer,
|
|
answered_by: session.userId,
|
|
answered_at: new Date(),
|
|
},
|
|
})
|
|
|
|
if (updated.count !== 1) {
|
|
const exists = await prisma.claudeQuestion.findFirst({
|
|
where: { id: parsed.data.questionId },
|
|
select: { status: true, expires_at: true },
|
|
})
|
|
if (!exists) return { ok: false, error: 'Vraag niet gevonden' }
|
|
if (exists.status !== 'open') {
|
|
return { ok: false, error: `Vraag is al ${exists.status}` }
|
|
}
|
|
return { ok: false, error: 'Vraag is verlopen' }
|
|
}
|
|
|
|
revalidatePath('/', 'layout')
|
|
return { ok: true }
|
|
}
|