Scrum4Me/scripts/insert-milestone.ts
Scrum4Me Agent 93b9c03419 docs(taxonomy): move design/api/qa/backlog/assets into folders
- docs/scrum4me-styling.md        → docs/design/styling.md
- docs/MD3_Color_Scheme_Documentation.md → docs/design/md3-color-scheme.md
- docs/API.md                     → docs/api/rest-contract.md
- docs/scrum4me-test-plan.md      → docs/qa/api-test-plan.md
- docs/scrum4me-backlog.md        → docs/backlog/index.md
- docs/scrum4me-product-backlog.md → docs/backlog/product-historical.md
- docs/erd.svg                    → docs/assets/erd.svg
- docs/icons.html                 → docs/assets/icons.html

Internal links updated across CLAUDE.md, README.md, docs/, prisma/,
and scripts/. prisma/schema.prisma erd output path also updated.
2026-05-03 12:30:58 +02:00

198 lines
6.3 KiB
TypeScript

// Idempotent insert van één milestone (PBI + stories + tasks) uit de backlog.
//
// Gebruik:
// npm run db:insert-milestone -- M8 # standaard product SCRUM4ME
// npm run db:insert-milestone -- M8 --product SCRUM4ME # idem
// npm run db:insert-milestone -- M8 --dry-run # niets schrijven, alleen tonen
//
// Bron: docs/backlog/index.md (gecommit, single source of truth).
// Tweede keer draaien is veilig: bestaande PBIs/stories/tasks worden niet
// gedupliceerd. Sprint-toewijzing doe je apart via Sprint Planning in de UI.
import { PrismaClient } from '@prisma/client'
import * as dotenv from 'dotenv'
import * as path from 'path'
import { Pool } from 'pg'
import { PrismaPg } from '@prisma/adapter-pg'
import { loadBacklog } from '../prisma/seed-data/parse-backlog'
const root = path.resolve(__dirname, '..')
dotenv.config({ path: path.join(root, '.env.local'), override: true })
dotenv.config({ path: path.join(root, '.env') })
interface Args {
milestoneKey: string
productCode: string
dryRun: boolean
}
function parseArgs(argv: string[]): Args {
const positional: string[] = []
let productCode = 'SCRUM4ME'
let dryRun = false
for (let i = 0; i < argv.length; i++) {
const a = argv[i]
if (a === '--product') productCode = argv[++i] ?? ''
else if (a === '--dry-run') dryRun = true
else if (a.startsWith('--')) throw new Error(`Unknown flag: ${a}`)
else positional.push(a)
}
if (positional.length !== 1) {
throw new Error('Usage: insert-milestone <milestone-key> [--product CODE] [--dry-run]')
}
const milestoneKey = positional[0]
if (!/^M[\d.]+$/.test(milestoneKey)) {
throw new Error(`Invalid milestone key: ${milestoneKey} (expected M0, M3.5, M8, ...)`)
}
if (!productCode) throw new Error('--product cannot be empty')
return { milestoneKey, productCode, dryRun }
}
async function main() {
const args = parseArgs(process.argv.slice(2))
const url = process.env.DATABASE_URL
if (!url) throw new Error('DATABASE_URL is not set. Check .env.local')
const pool = new Pool({ connectionString: url })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })
try {
const milestones = await loadBacklog(root, { strict: false })
const ms = milestones.find((m) => m.key === args.milestoneKey)
if (!ms) {
throw new Error(
`Milestone ${args.milestoneKey} not found in docs/backlog/index.md (parsed: ${milestones.map((m) => m.key).join(', ')})`,
)
}
const product = await prisma.product.findFirst({
where: { code: args.productCode, archived: false },
select: { id: true, name: true, code: true },
})
if (!product) throw new Error(`Product with code "${args.productCode}" not found`)
console.log(
`Inserting ${ms.key} (${ms.title}) into product ${product.code}${ms.stories.length} stories${args.dryRun ? ' [DRY RUN]' : ''}`,
)
let pbiCreated = 0
let storiesCreated = 0
let storiesUpdated = 0
let tasksCreated = 0
// PBI upsert
const existingPbi = await prisma.pbi.findFirst({
where: { product_id: product.id, code: ms.key },
select: { id: true },
})
let pbiId: string
if (existingPbi) {
pbiId = existingPbi.id
} else if (args.dryRun) {
pbiId = '<new-pbi-id>'
pbiCreated = 1
} else {
const maxPbi = await prisma.pbi.aggregate({
where: { product_id: product.id },
_max: { sort_order: true },
})
const nextSort = (maxPbi._max.sort_order ?? 0) + 1
const created = await prisma.pbi.create({
data: {
product_id: product.id,
code: ms.key,
title: ms.title,
description: ms.goal,
priority: ms.priority,
sort_order: nextSort,
},
select: { id: true },
})
pbiId = created.id
pbiCreated = 1
}
// Stories + tasks
for (const s of ms.stories) {
const existingStory = await prisma.story.findFirst({
where: { product_id: product.id, code: s.ref },
select: { id: true, _count: { select: { tasks: true } } },
})
const storyData = {
pbi_id: pbiId,
product_id: product.id,
code: s.ref,
title: s.title,
description: s.tasks.map((t) => t.title).join('; '),
acceptance_criteria: s.acceptance_criteria,
priority: ms.priority,
sort_order: s.sort_order,
status: s.status === 'DONE' ? 'DONE' as const : 'OPEN' as const,
}
let storyId: string
let hadTasks = false
if (existingStory) {
hadTasks = existingStory._count.tasks > 0
storyId = existingStory.id
if (!args.dryRun) {
await prisma.story.update({
where: { id: existingStory.id },
data: storyData,
})
}
storiesUpdated++
} else if (args.dryRun) {
storyId = `<new-story-${s.ref}>`
storiesCreated++
} else {
const created = await prisma.story.create({
data: storyData,
select: { id: true },
})
storyId = created.id
storiesCreated++
}
// Tasks: alleen als de story op dit moment 0 tasks had
if (!hadTasks && s.tasks.length > 0) {
if (!args.dryRun) {
await prisma.task.createMany({
data: s.tasks.map((t) => ({
story_id: storyId,
title: t.title,
description: t.description || null,
priority: ms.priority,
sort_order: t.sort_order,
status: s.status === 'DONE' ? 'DONE' as const : 'TO_DO' as const,
})),
})
}
tasksCreated += s.tasks.length
}
}
console.log(
`Result: PBI ${pbiCreated ? 'created' : 'reused'}, stories ${storiesCreated} created / ${storiesUpdated} updated, tasks ${tasksCreated} created${args.dryRun ? ' [DRY RUN — niets geschreven]' : ''}`,
)
if (storiesCreated > 0 || pbiCreated > 0) {
console.log(
`Tip: stories staan nog niet in een sprint. Plan ze via Sprint Planning in de UI als je ze in de Solo Paneel wil zien.`,
)
}
} finally {
await prisma.$disconnect()
await pool.end()
}
}
main().catch((err) => {
console.error('insert-milestone failed:', err.message)
process.exit(1)
})