feat(insights): add getBacklogHealth helper — stuck tasks + missing AC/plan counts (#48)

Three read-only counters: stories without acceptance_criteria, tasks without
implementation_plan, and top-10 IN_PROGRESS tasks stuck >7 days. All scoped
via productAccessFilter.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-05-02 16:17:03 +02:00 committed by GitHub
parent 219d54b3e5
commit af77553407
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 151 additions and 0 deletions

View file

@ -0,0 +1,69 @@
import { prisma } from '@/lib/prisma'
import { productAccessFilter } from '@/lib/product-access'
export interface StuckTask {
taskId: string
title: string
productId: string
productName: string
sprintGoal: string | null
daysStuck: number
updatedAt: string
}
export interface BacklogHealth {
storiesWithoutAc: number
tasksWithoutPlan: number
stuckTasks: StuckTask[]
}
const SEVEN_DAYS_MS = 7 * 86_400_000
export async function getBacklogHealth(userId: string): Promise<BacklogHealth> {
const now = new Date()
const stuckCutoff = new Date(now.getTime() - SEVEN_DAYS_MS)
const [storiesWithoutAc, tasksWithoutPlan, rawStuck] = await Promise.all([
prisma.story.count({
where: {
OR: [{ acceptance_criteria: null }, { acceptance_criteria: '' }],
product: productAccessFilter(userId),
},
}),
prisma.task.count({
where: {
implementation_plan: null,
story: { product: productAccessFilter(userId) },
},
}),
prisma.task.findMany({
where: {
status: 'IN_PROGRESS',
updated_at: { lt: stuckCutoff },
story: { product: productAccessFilter(userId) },
},
orderBy: { updated_at: 'asc' },
take: 10,
include: {
story: {
include: {
product: { select: { id: true, name: true } },
sprint: { select: { sprint_goal: true } },
},
},
},
}),
])
const stuckTasks: StuckTask[] = rawStuck.map(t => ({
taskId: t.id,
title: t.title,
productId: t.story.product.id,
productName: t.story.product.name,
sprintGoal: t.story.sprint?.sprint_goal ?? null,
daysStuck: Math.floor((now.getTime() - t.updated_at.getTime()) / 86_400_000),
updatedAt: t.updated_at.toISOString(),
}))
return { storiesWithoutAc, tasksWithoutPlan, stuckTasks }
}