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.
This commit is contained in:
Scrum4Me Agent 2026-05-07 15:12:52 +02:00
parent 9a04dc3a31
commit 33a36d616b

View file

@ -540,6 +540,7 @@ function nextNumber(existing: (string | null)[], re: RegExp): number {
export async function materializeIdeaPlanAction(
id: string,
options?: { allowAlongside?: boolean },
): Promise<ActionResult<{ pbi_id: string; pbi_code: string; story_ids: string[]; task_ids: string[] }>> {
const session = await getSession()
if (!session.userId) return { error: 'Niet ingelogd', code: 401 }
@ -550,7 +551,7 @@ export async function materializeIdeaPlanAction(
const idea = await prisma.idea.findFirst({
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.status !== 'PLAN_READY') {
@ -574,8 +575,36 @@ export async function materializeIdeaPlanAction(
const productId = idea.product_id
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 {
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
// (race met andere materialize) abort de transactie en gooien we 409.
const [existingPbis, existingStories, existingTasks] = await Promise.all([