actions: promoteTodoToIdeaAction (M12 T-499)

actions/todos.ts:
- promoteTodoToIdeaAction(todoId): auth + demo + scope + already-archived
  guards. Atomic \$transaction creates DRAFT Idea (with auto IDEA-NNN code)
  and archives source Todo + IdeaLog{NOTE}.
- Anders dan Todo→PBI/Story (die de todo deleten): we ARCHIVEREN. De idea
  wordt het nieuwe planningsartifact; de archived todo bewaart het
  vertrekpunt (zie M12 grill-keuze 12).

Tests: 5 cases — happy, auth-401, demo-403, scope-404, already-archived-422.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-05-04 19:52:37 +02:00
parent 6fee0394c5
commit 6904de9f2b
2 changed files with 168 additions and 0 deletions

View file

@ -241,6 +241,60 @@ export async function promoteTodoToStoryAction(_prevState: unknown, formData: Fo
return { success: true }
}
// M12: promote a Todo into a DRAFT Idea. Anders dan Todo→PBI/Story (die de
// todo deleteert) ARCHIVEREN we de todo hier — het idee houdt zelf de
// planningsgeschiedenis bij, en de archived todo bewaart het oorspronkelijke
// vertrekpunt.
export async function promoteTodoToIdeaAction(todoId: string): Promise<
{ success: true; idea_id: string; idea_code: string } | { error: string; code?: number }
> {
const session = await getSession()
if (!session.userId) return { error: 'Niet ingelogd', code: 401 }
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus', code: 403 }
if (!todoId) return { error: 'todoId is verplicht', code: 422 }
const todo = await prisma.todo.findFirst({
where: { id: todoId, user_id: session.userId },
select: { id: true, title: true, description: true, product_id: true, archived: true },
})
if (!todo) return { error: 'Todo niet gevonden', code: 404 }
if (todo.archived) return { error: 'Todo is al gearchiveerd', code: 422 }
const userId = session.userId
// Lazy-import om dit server-only bestand niet te dwingen in een client bundle.
const { nextIdeaCode } = await import('@/lib/idea-code-server')
const idea = await prisma.$transaction(async (tx) => {
const code = await nextIdeaCode(userId, tx)
const created = await tx.idea.create({
data: {
user_id: userId,
product_id: todo.product_id,
code,
title: todo.title,
description: todo.description ?? null,
status: 'DRAFT',
},
select: { id: true, code: true },
})
await tx.todo.update({ where: { id: todoId }, data: { archived: true } })
await tx.ideaLog.create({
data: {
idea_id: created.id,
type: 'NOTE',
content: `Promoted from Todo ${todoId}`,
metadata: { source_todo_id: todoId },
},
})
return created
})
revalidatePath('/ideas')
revalidatePath('/todos')
return { success: true, idea_id: idea.id, idea_code: idea.code }
}
export async function updateRolesAction(roles: string[]) {
const session = await getSession()
if (!session.userId) return { error: 'Niet ingelogd' }