Sprint: Idee regril mogelijkheid (#144)

* feat(ST-cmovhveef): add PLANNED to GRILL_TRIGGERABLE_FROM and PLANNED→GRILLING transition

- GRILL_TRIGGERABLE_FROM now includes 'PLANNED' in actions/ideas.ts
- ALLOWED_TRANSITIONS PLANNED entry extended with 'GRILLING' in lib/idea-status.ts
- Updated canTransition test to reflect the new re-grill-from-PLANNED behavior

* test(ST-cmovhvef3): add exhaustive re-grill canTransition test covering PLANNED

Adds a loop test that asserts canTransition(status, 'GRILLING') for all
statuses in GRILL_TRIGGERABLE_FROM that support the transition, explicitly
documenting PLANNED as a valid re-grill entry point.

* feat(ST-cmovhvegf): add existingPbi pre-check in materializeIdeaPlanAction

- Adds options.allowAlongside parameter to control behaviour when a PBI
  with executed tasks already exists.
- Returns 409 PBI_HAS_ACTIVE_TASKS:<code> when tasks are DONE/IN_PROGRESS
  and allowAlongside is not set.
- Auto-deletes the old PBI inside the transaction when no tasks have been
  executed (atomic replace).
- Alongside mode (allowAlongside=true) skips deletion and creates a new PBI.

* test(ST-cmovhveh3): add pre-check integration tests for materializeIdeaPlanAction

Three new scenarios in ideas-crud.test.ts:
- auto-vervang: old PBI deleted in transaction when no executed tasks
- conflict-409: returns PBI_HAS_ACTIVE_TASKS:<code> with active tasks
- alongside: skips delete and creates new PBI when allowAlongside=true
Also adds task.count, pbi.findUnique, pbi.delete to prisma mock.

* feat(ST-cmovhveih): remove PLANNED-blokkering in idea-row-actions, add inline Bekijk-PBI button

- Removed grillBlockedReason guard for status==='planned', enabling re-grill from PLANNED
- Removed the early return for PLANNED that hid all standard buttons
- Added conditional 'Bekijk <code>' button at the start of the standard button set,
  visible only when status==='planned' and PBI + product_id are present

* feat(ST-cmovhvej7): add PBI_HAS_ACTIVE_TASKS alongside-dialoog in materialize handler

When materializeIdeaPlanAction returns code 409 with PBI_HAS_ACTIVE_TASKS:<code>,
a confirm dialog offers the user a choice: create new PBI alongside the existing one
or cancel. Alongside=true retries the action; cancel leaves the idea in PLAN_READY.
This commit is contained in:
Janpeter Visser 2026-05-07 15:27:43 +02:00 committed by GitHub
parent 2d27c41d38
commit 5cb3abbd3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 130 additions and 34 deletions

View file

@ -35,7 +35,9 @@ vi.mock('@/lib/prisma', () => ({
pbi: { pbi: {
findFirst: vi.fn(), findFirst: vi.fn(),
findMany: vi.fn(), findMany: vi.fn(),
findUnique: vi.fn(),
create: vi.fn(), create: vi.fn(),
delete: vi.fn(),
}, },
story: { story: {
findMany: vi.fn(), findMany: vi.fn(),
@ -44,6 +46,7 @@ vi.mock('@/lib/prisma', () => ({
task: { task: {
findMany: vi.fn(), findMany: vi.fn(),
create: vi.fn(), create: vi.fn(),
count: vi.fn(),
}, },
$transaction: vi.fn(), $transaction: vi.fn(),
$executeRaw: vi.fn().mockResolvedValue(0), $executeRaw: vi.fn().mockResolvedValue(0),
@ -71,9 +74,9 @@ type MockIdea = {
ideaLog: { create: ReturnType<typeof vi.fn> } ideaLog: { create: ReturnType<typeof vi.fn> }
claudeJob: { findFirst: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> } claudeJob: { findFirst: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn>; update: ReturnType<typeof vi.fn> }
claudeWorker: { count: ReturnType<typeof vi.fn> } claudeWorker: { count: ReturnType<typeof vi.fn> }
pbi: { findFirst: ReturnType<typeof vi.fn>; findMany: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn> } pbi: { findFirst: ReturnType<typeof vi.fn>; findMany: ReturnType<typeof vi.fn>; findUnique: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn>; delete: ReturnType<typeof vi.fn> }
story: { findMany: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn> } story: { findMany: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn> }
task: { findMany: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn> } task: { findMany: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn>; count: ReturnType<typeof vi.fn> }
$transaction: ReturnType<typeof vi.fn> $transaction: ReturnType<typeof vi.fn>
$executeRaw: ReturnType<typeof vi.fn> $executeRaw: ReturnType<typeof vi.fn>
} }
@ -476,6 +479,69 @@ body
}) })
}) })
describe('materializeIdeaPlanAction — existing PBI pre-check', () => {
const VALID_PLAN = `---
pbi:
title: New PBI
priority: 2
stories:
- title: Story A
priority: 2
tasks:
- title: Task A1
priority: 2
---
body
`
beforeEach(() => {
// Use a distinct userId to avoid sharing the rate-limit bucket with the
// materializeIdeaPlanAction describe block above.
mockSession.userId = 'user-precheck'
m.idea.findFirst.mockResolvedValue({
id: 'idea-1',
status: 'PLAN_READY',
product_id: 'prod-1',
plan_md: VALID_PLAN,
pbi_id: 'old-pbi',
})
m.pbi.findMany.mockResolvedValue([])
m.story.findMany.mockResolvedValue([])
m.task.findMany.mockResolvedValue([])
m.pbi.findFirst.mockResolvedValue(null)
m.pbi.findUnique.mockResolvedValue({ code: 'PBI-X' })
m.pbi.create.mockResolvedValue({ id: 'pbi-new', code: 'PBI-2' })
m.pbi.delete.mockResolvedValue({})
m.story.create.mockResolvedValue({ id: 's-1' })
m.task.create.mockResolvedValue({ id: 't-1' })
})
it('auto-vervang: deletes old PBI in transaction when no tasks executed', async () => {
m.task.count.mockResolvedValueOnce(0)
const r = await materializeIdeaPlanAction('idea-1')
expect(r).toMatchObject({ success: true, data: { pbi_id: 'pbi-new' } })
expect(m.pbi.delete).toHaveBeenCalledWith({ where: { id: 'old-pbi' } })
expect(m.pbi.create).toHaveBeenCalledTimes(1)
})
it('conflict-409: returns PBI_HAS_ACTIVE_TASKS when executed tasks exist', async () => {
m.task.count.mockResolvedValueOnce(1)
const r = await materializeIdeaPlanAction('idea-1')
expect(r).toMatchObject({ code: 409, error: 'PBI_HAS_ACTIVE_TASKS:PBI-X' })
expect(m.pbi.create).not.toHaveBeenCalled()
expect(m.pbi.delete).not.toHaveBeenCalled()
})
it('alongside: skips old PBI delete and creates new PBI when allowAlongside=true', async () => {
m.task.count.mockResolvedValueOnce(1)
const r = await materializeIdeaPlanAction('idea-1', { allowAlongside: true })
expect(r).toMatchObject({ success: true, data: { pbi_id: 'pbi-new' } })
expect(m.pbi.delete).not.toHaveBeenCalled()
expect(m.pbi.create).toHaveBeenCalledTimes(1)
})
})
describe('relinkIdeaPlanAction', () => { describe('relinkIdeaPlanAction', () => {
it('happy: PLANNED with pbi_id=null → PLAN_READY', async () => { it('happy: PLANNED with pbi_id=null → PLAN_READY', async () => {
m.idea.findFirst.mockResolvedValueOnce({ m.idea.findFirst.mockResolvedValueOnce({

View file

@ -53,12 +53,20 @@ describe('canTransition', () => {
expect(canTransition('PLAN_FAILED', 'GRILLED')).toBe(true) expect(canTransition('PLAN_FAILED', 'GRILLED')).toBe(true)
}) })
it('only allows PLANNED → PLAN_READY (relink path)', () => { it('allows PLANNED → PLAN_READY (relink) and PLANNED → GRILLING (re-grill)', () => {
expect(canTransition('PLANNED', 'PLAN_READY')).toBe(true) expect(canTransition('PLANNED', 'PLAN_READY')).toBe(true)
expect(canTransition('PLANNED', 'GRILLING')).toBe(false) expect(canTransition('PLANNED', 'GRILLING')).toBe(true)
expect(canTransition('PLANNED', 'DRAFT')).toBe(false) expect(canTransition('PLANNED', 'DRAFT')).toBe(false)
}) })
it('canTransition to GRILLING from all statuses that allow re-grill', () => {
// DRAFT, GRILLED, GRILL_FAILED, PLANNED are in GRILL_TRIGGERABLE_FROM and support the transition.
const regrill = ['DRAFT', 'GRILLED', 'GRILL_FAILED', 'PLANNED'] as const
for (const status of regrill) {
expect(canTransition(status, 'GRILLING')).toBe(true)
}
})
it('rejects invalid jumps', () => { it('rejects invalid jumps', () => {
expect(canTransition('DRAFT', 'PLANNED')).toBe(false) expect(canTransition('DRAFT', 'PLANNED')).toBe(false)
expect(canTransition('DRAFT', 'PLAN_READY')).toBe(false) expect(canTransition('DRAFT', 'PLAN_READY')).toBe(false)

View file

@ -338,7 +338,7 @@ export async function downloadIdeaMdAction(
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Job-triggers (Grill Me / Make Plan / Cancel) // Job-triggers (Grill Me / Make Plan / Cancel)
const GRILL_TRIGGERABLE_FROM: IdeaStatus[] = ['DRAFT', 'GRILLED', 'GRILL_FAILED', 'PLAN_READY'] const GRILL_TRIGGERABLE_FROM: IdeaStatus[] = ['DRAFT', 'GRILLED', 'GRILL_FAILED', 'PLAN_READY', 'PLANNED']
const MAKE_PLAN_TRIGGERABLE_FROM: IdeaStatus[] = ['GRILLED', 'PLAN_FAILED', 'PLAN_READY'] const MAKE_PLAN_TRIGGERABLE_FROM: IdeaStatus[] = ['GRILLED', 'PLAN_FAILED', 'PLAN_READY']
export async function startGrillJobAction(id: string): Promise<ActionResult<{ job_id: string }>> { export async function startGrillJobAction(id: string): Promise<ActionResult<{ job_id: string }>> {
@ -540,6 +540,7 @@ function nextNumber(existing: (string | null)[], re: RegExp): number {
export async function materializeIdeaPlanAction( export async function materializeIdeaPlanAction(
id: string, id: string,
options?: { allowAlongside?: boolean },
): Promise<ActionResult<{ pbi_id: string; pbi_code: string; story_ids: string[]; task_ids: string[] }>> { ): Promise<ActionResult<{ pbi_id: string; pbi_code: string; story_ids: string[]; task_ids: string[] }>> {
const session = await getSession() const session = await getSession()
if (!session.userId) return { error: 'Niet ingelogd', code: 401 } if (!session.userId) return { error: 'Niet ingelogd', code: 401 }
@ -550,7 +551,7 @@ export async function materializeIdeaPlanAction(
const idea = await prisma.idea.findFirst({ const idea = await prisma.idea.findFirst({
where: { id, user_id: session.userId }, where: { id, user_id: session.userId },
select: { id: true, status: true, product_id: true, plan_md: true }, select: { id: true, status: true, product_id: true, plan_md: true, pbi_id: true },
}) })
if (!idea) return { error: 'Idee niet gevonden', code: 404 } if (!idea) return { error: 'Idee niet gevonden', code: 404 }
if (idea.status !== 'PLAN_READY') { if (idea.status !== 'PLAN_READY') {
@ -574,8 +575,36 @@ export async function materializeIdeaPlanAction(
const productId = idea.product_id const productId = idea.product_id
const plan = parsed.plan const plan = parsed.plan
let oldPbiId: string | null = null
if (idea.pbi_id) {
const executedCount = await prisma.task.count({
where: {
story: { pbi_id: idea.pbi_id },
status: { in: ['DONE', 'IN_PROGRESS'] },
},
})
if (executedCount > 0 && !options?.allowAlongside) {
const existingPbi = await prisma.pbi.findUnique({
where: { id: idea.pbi_id },
select: { code: true },
})
return {
error: `PBI_HAS_ACTIVE_TASKS:${existingPbi?.code ?? idea.pbi_id}`,
code: 409,
}
}
if (executedCount === 0) {
oldPbiId = idea.pbi_id
}
// executedCount > 0 && allowAlongside: doorgaan zonder delete
}
try { try {
const result = await prisma.$transaction(async (tx) => { const result = await prisma.$transaction(async (tx) => {
if (oldPbiId) {
await tx.pbi.delete({ where: { id: oldPbiId } })
}
// Codes: één keer SELECT max per type binnen de transactie. Bij P2002 // Codes: één keer SELECT max per type binnen de transactie. Bij P2002
// (race met andere materialize) abort de transactie en gooien we 409. // (race met andere materialize) abort de transactie en gooien we 409.
const [existingPbis, existingStories, existingTasks] = await Promise.all([ const [existingPbis, existingStories, existingTasks] = await Promise.all([

View file

@ -61,7 +61,6 @@ export function IdeaRowActions({ idea, isDemo, onArchive }: IdeaRowActionsProps)
// ---- Grill Me ---- // ---- Grill Me ----
const grillBlockedReason = (() => { const grillBlockedReason = (() => {
if (status === 'grilling' || status === 'planning') return 'Job loopt al' if (status === 'grilling' || status === 'planning') return 'Job loopt al'
if (status === 'planned') return 'Idee is gepland — open de PBI'
if (!hasProductWithRepo) return 'Idee heeft een product met repo nodig' if (!hasProductWithRepo) return 'Idee heeft een product met repo nodig'
if (!workerOk) return 'Geen Claude-worker actief' if (!workerOk) return 'Geen Claude-worker actief'
return null return null
@ -108,15 +107,24 @@ export function IdeaRowActions({ idea, isDemo, onArchive }: IdeaRowActionsProps)
function handleMaterialize() { function handleMaterialize() {
if (!confirm('Plan materialiseren? Dit maakt PBI + stories + taken aan.')) return if (!confirm('Plan materialiseren? Dit maakt PBI + stories + taken aan.')) return
startTransition(async () => { startTransition(async () => {
const r = await materializeIdeaPlanAction(idea.id) let r = await materializeIdeaPlanAction(idea.id)
if ('error' in r && r.code === 409 && r.error.startsWith('PBI_HAS_ACTIVE_TASKS:')) {
const pbiCode = r.error.split(':')[1]
const alongside = confirm(
`De bestaande PBI (${pbiCode}) heeft uitgevoerde taken.\n` +
`OK = nieuwe PBI naast bestaande aanmaken.\n` +
`Annuleren = stoppen.`
)
if (!alongside) return
r = await materializeIdeaPlanAction(idea.id, { allowAlongside: true })
}
if ('error' in r) { if ('error' in r) {
toast.error(r.error) toast.error(r.error)
return return
} }
toast.success(`Gematerialiseerd als ${r.data?.pbi_code}`) toast.success(`Gematerialiseerd als ${r.data?.pbi_code}`)
// Navigeer naar de product-backlog. Anchor-scrolling per-PBI bestaat
// (nog) niet in pbi-list, dus gewoon naar de overview-pagina; de nieuwe
// PBI is de meest recente.
if (idea.product_id) { if (idea.product_id) {
router.push(`/products/${idea.product_id}`) router.push(`/products/${idea.product_id}`)
} else { } else {
@ -125,35 +133,20 @@ export function IdeaRowActions({ idea, isDemo, onArchive }: IdeaRowActionsProps)
}) })
} }
// PLANNED-state: kortere variant met "Bekijk PBI"-link return (
if (status === 'planned' && idea.pbi && idea.product_id) { <div className="flex items-center gap-1">
return ( {/* Bekijk PBI — alleen zichtbaar in PLANNED */}
<div className="flex items-center gap-1.5"> {status === 'planned' && idea.pbi && idea.product_id && (
<Button <Button
size="sm"
variant="outline" variant="outline"
onClick={() => size="sm"
router.push(`/products/${idea.product_id}`) onClick={() => router.push(`/products/${idea.product_id!}`)}
}
> >
Bekijk {idea.pbi.code} Bekijk {idea.pbi.code}
<ExternalLink className="ml-1 size-3.5" /> <ExternalLink className="ml-1 size-3.5" />
</Button> </Button>
<Button )}
size="sm"
variant="ghost"
onClick={() => router.push(`/ideas/${idea.id}`)}
aria-label="Open idee"
title="Open idee"
>
<ArrowRight className="size-4" />
</Button>
</div>
)
}
return (
<div className="flex items-center gap-1">
{/* Grill Me */} {/* Grill Me */}
<ActionButton <ActionButton
label="Grill" label="Grill"

View file

@ -54,7 +54,7 @@ const ALLOWED_TRANSITIONS: Record<IdeaStatus, ReadonlyArray<IdeaStatus>> = {
PLANNING: ['PLAN_READY', 'PLAN_FAILED'], PLANNING: ['PLAN_READY', 'PLAN_FAILED'],
PLAN_FAILED: ['PLANNING', 'GRILLED'], PLAN_FAILED: ['PLANNING', 'GRILLED'],
PLAN_READY: ['PLANNING', 'PLANNED'], PLAN_READY: ['PLANNING', 'PLANNED'],
PLANNED: ['PLAN_READY'], // alleen via relinkIdeaPlanAction (PBI deleted) PLANNED: ['PLAN_READY', 'GRILLING'], // PLAN_READY via relinkIdeaPlanAction; GRILLING via startGrillJobAction
} }
export function canTransition(from: IdeaStatus, to: IdeaStatus): boolean { export function canTransition(from: IdeaStatus, to: IdeaStatus): boolean {