Scrum4Me/lib/sprint-conflicts.ts
Madhura68 db37b66d17 fix(sprint-conflicts): stories uit CLOSED/ARCHIVED/FAILED sprints zijn weer eligible
Bug: bij sprint-aanmaken (en story-toevoegen aan een actieve sprint) gaf de
backend "Geen eligible stories voor deze sprint" zodra je stories aanvinkte
die ooit in een sprint hadden gezeten — ook als die sprint allang gesloten
of gearchiveerd was. partitionByEligibility checkte alleen story.sprint_id,
nooit sprint.status, terwijl getBlockingSprintMap in dezelfde file wél al
filterde op sprint: { status: 'OPEN' }. Inconsistent.

Fix: partitionByEligibility en isEligibleForSprint wegen nu sprint.status
mee. Een story blokkeert alleen als hij in een ANDERE sprint zit DIE NOG
OPEN is. Stories uit CLOSED/ARCHIVED/FAILED sprints worden weer vrij voor
planning — story.sprint_id blijft als historische referentie staan tot de
volgende updateMany hem overschrijft naar de nieuwe sprint.

Neveneffect: een DONE story in een gesloten sprint krijgt nu reason='DONE'
i.p.v. het misleidende reason='IN_OTHER_SPRINT'.

Tests: 3 nieuwe scenario's in __tests__/lib/sprint-conflicts.test.ts
(CLOSED/ARCHIVED/FAILED → eligible, DONE-in-CLOSED → reason=DONE).
De oude test 'does NOT mark crossSprint for stories in CLOSED other sprint'
is vervangen omdat hij het bug-gedrag vastlegde.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 15:28:24 +02:00

129 lines
3.7 KiB
TypeScript

import type {
Prisma,
PrismaClient,
SprintStatus,
StoryStatus,
} from '@prisma/client'
export type EligibilityReason = 'DONE' | 'IN_OTHER_SPRINT'
export type CrossSprintBlock = {
storyId: string
sprintId: string
sprintName: string
}
export type EligibilityPartition = {
eligible: string[]
notEligible: { storyId: string; reason: EligibilityReason }[]
crossSprint: CrossSprintBlock[]
}
type StoryEligibilityInput = {
sprint_id: string | null
status: StoryStatus
// De bijbehorende sprint, indien sprint_id !== null. Alleen sprints met
// status='OPEN' blokkeren eligibility — een story uit een CLOSED/ARCHIVED/
// FAILED sprint is weer beschikbaar voor planning (consistent met
// getBlockingSprintMap, dat alleen OPEN sprints als blocking telt).
sprint?: { status: SprintStatus } | null
}
export function isEligibleForSprint(story: StoryEligibilityInput): boolean {
if (story.status === 'DONE') return false
if (story.sprint_id === null) return true
// Story heeft een sprint_id — sprint-status MOET bekend zijn om eligibility
// te bepalen. Ontbrekende sprint-data is conservatief: niet eligible.
if (!story.sprint) return false
return story.sprint.status !== 'OPEN'
}
type PrismaLike = Pick<PrismaClient, 'story'> | Prisma.TransactionClient
export async function partitionByEligibility(
prisma: PrismaLike,
storyIds: string[],
excludeSprintId?: string,
): Promise<EligibilityPartition> {
if (storyIds.length === 0) {
return { eligible: [], notEligible: [], crossSprint: [] }
}
const stories = await prisma.story.findMany({
where: { id: { in: storyIds } },
select: {
id: true,
sprint_id: true,
status: true,
sprint: { select: { id: true, code: true, status: true } },
},
})
const eligible: string[] = []
const notEligible: { storyId: string; reason: EligibilityReason }[] = []
const crossSprint: CrossSprintBlock[] = []
for (const story of stories) {
// Een story blokkeert alleen als hij in een ANDERE sprint zit DIE NOG OPEN
// is. Stories die in een CLOSED/ARCHIVED/FAILED sprint zaten, zijn weer
// vrij voor planning — consistent met getBlockingSprintMap.
const inOtherActiveSprint =
story.sprint_id !== null &&
story.sprint_id !== excludeSprintId &&
story.sprint?.status === 'OPEN'
if (inOtherActiveSprint && story.sprint) {
crossSprint.push({
storyId: story.id,
sprintId: story.sprint.id,
sprintName: story.sprint.code,
})
notEligible.push({ storyId: story.id, reason: 'IN_OTHER_SPRINT' })
continue
}
if (story.status === 'DONE') {
notEligible.push({ storyId: story.id, reason: 'DONE' })
continue
}
eligible.push(story.id)
}
return { eligible, notEligible, crossSprint }
}
export async function getBlockingSprintMap(
prisma: PrismaLike,
productId: string,
storyIds: string[],
excludeSprintId?: string,
): Promise<Map<string, { sprintId: string; sprintName: string }>> {
const out = new Map<string, { sprintId: string; sprintName: string }>()
if (storyIds.length === 0) return out
const stories = await prisma.story.findMany({
where: {
id: { in: storyIds },
product_id: productId,
sprint_id: { not: null },
sprint: { status: 'OPEN' },
},
select: {
id: true,
sprint_id: true,
sprint: { select: { id: true, code: true, status: true } },
},
})
for (const story of stories) {
if (!story.sprint) continue
if (excludeSprintId !== undefined && story.sprint.id === excludeSprintId) continue
out.set(story.id, {
sprintId: story.sprint.id,
sprintName: story.sprint.code,
})
}
return out
}