lib/rate-limit.ts: 11 nieuwe scope-configs + enforceUserRateLimit(scope, userId)
helper. Returnt { error, code: 429 } shape voor consistent foutbeleid.
Toegepast op de high-value mutation-paths:
- actions/pbis.ts createPbiAction
- actions/stories.ts createStoryAction
- actions/tasks.ts saveTask (alleen create-path) + createTaskAction
- actions/todos.ts createTodoAction
- actions/sprints.ts createSprintAction
- actions/products.ts createProductAction + createProductFormAction
- actions/api-tokens.ts createApiTokenAction
- actions/questions.ts answerQuestion
- actions/claude-jobs.ts enqueueClaudeJobAction + enqueueClaudeJobsBatchAction
- app/api/profile/avatar/route.ts POST
- app/api/stories/[id]/log/route.ts POST
Limits zijn ruim genoeg voor normaal gebruik, eng genoeg voor abuse-loops:
create-task 100/min, create-todo 60/min, create-pbi 30/min, create-product
5/min, create-token 10/uur, etc. Per-user scope (geen globale block).
Niet aangeraakt: reorder/status-toggle (intra-session frequent, lage abuse),
update/delete (laag-volume), cron-routes (CRON_SECRET-gated).
Consumer-tweaks: 'success' in result narrowing waar TS de bredere union niet
meer accepteerde. Tests: 9 nieuwe op rate-limit-helper.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
83 lines
3 KiB
TypeScript
83 lines
3 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'
|
|
import { enforceUserRateLimit } from '@/lib/rate-limit'
|
|
|
|
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 limited = enforceUserRateLimit('answer-question', session.userId)
|
|
if (limited) return { ok: false, error: limited.error }
|
|
|
|
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 }
|
|
}
|