lib: idea schemas + status mappers + transition guards (M12 T-493)
- lib/schemas/idea.ts: ideaCreateSchema, ideaUpdateSchema, ideaPlanMdFrontmatterSchema (yaml-frontmatter contract for materialize-step parser) - lib/idea-status.ts: bidirectional DB↔API mapping, canTransition state-machine guard, isIdeaEditable + isGrillMdEditable + isPlanMdEditable helpers - includes auto-regen docs/erd.svg from prisma generate Tests: 26 cases (status round-trip, transitions valid/invalid, schema validation edge-cases, priority bounds, verify-enum). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
86fb97456e
commit
bba3f11269
5 changed files with 369 additions and 1 deletions
85
lib/idea-status.ts
Normal file
85
lib/idea-status.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
// Bidirectionele case-mapper voor IdeaStatus + transitie-guard helper.
|
||||
// DB houdt UPPER_SNAKE; API exposeert lowercase.
|
||||
// Patroon volgt lib/task-status.ts.
|
||||
|
||||
import type { IdeaStatus } from '@prisma/client'
|
||||
|
||||
const IDEA_DB_TO_API = {
|
||||
DRAFT: 'draft',
|
||||
GRILLING: 'grilling',
|
||||
GRILL_FAILED: 'grill_failed',
|
||||
GRILLED: 'grilled',
|
||||
PLANNING: 'planning',
|
||||
PLAN_FAILED: 'plan_failed',
|
||||
PLAN_READY: 'plan_ready',
|
||||
PLANNED: 'planned',
|
||||
} as const satisfies Record<IdeaStatus, string>
|
||||
|
||||
const IDEA_API_TO_DB: Record<string, IdeaStatus> = {
|
||||
draft: 'DRAFT',
|
||||
grilling: 'GRILLING',
|
||||
grill_failed: 'GRILL_FAILED',
|
||||
grilled: 'GRILLED',
|
||||
planning: 'PLANNING',
|
||||
plan_failed: 'PLAN_FAILED',
|
||||
plan_ready: 'PLAN_READY',
|
||||
planned: 'PLANNED',
|
||||
}
|
||||
|
||||
export type IdeaStatusApi = (typeof IDEA_DB_TO_API)[IdeaStatus]
|
||||
|
||||
export function ideaStatusToApi(s: IdeaStatus): IdeaStatusApi {
|
||||
return IDEA_DB_TO_API[s]
|
||||
}
|
||||
|
||||
export function ideaStatusFromApi(s: string): IdeaStatus | null {
|
||||
return IDEA_API_TO_DB[s.toLowerCase()] ?? null
|
||||
}
|
||||
|
||||
export const IDEA_STATUS_API_VALUES = Object.values(IDEA_DB_TO_API)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// State-machine transition table (zie docs/plans/M12-ideas.md state-machine).
|
||||
// Server-actions gebruiken canTransition(from, to) als guard vóór mutatie.
|
||||
//
|
||||
// Asymmetrisch: trek vanuit DRAFT alleen naar GRILLING; vanuit GRILLED kan
|
||||
// re-grill (→ GRILLING) of make-plan (→ PLANNING) gebeuren. PLANNED is een
|
||||
// terminal state; verlaat alleen via expliciete relink (PBI verwijderd → PLAN_READY).
|
||||
|
||||
const ALLOWED_TRANSITIONS: Record<IdeaStatus, ReadonlyArray<IdeaStatus>> = {
|
||||
DRAFT: ['GRILLING'],
|
||||
GRILLING: ['GRILLED', 'GRILL_FAILED'],
|
||||
GRILL_FAILED: ['GRILLING', 'DRAFT'],
|
||||
GRILLED: ['GRILLING', 'PLANNING'],
|
||||
PLANNING: ['PLAN_READY', 'PLAN_FAILED'],
|
||||
PLAN_FAILED: ['PLANNING', 'GRILLED'],
|
||||
PLAN_READY: ['PLANNING', 'PLANNED'],
|
||||
PLANNED: ['PLAN_READY'], // alleen via relinkIdeaPlanAction (PBI deleted)
|
||||
}
|
||||
|
||||
export function canTransition(from: IdeaStatus, to: IdeaStatus): boolean {
|
||||
return ALLOWED_TRANSITIONS[from].includes(to)
|
||||
}
|
||||
|
||||
// Statussen waarin een idee bewerkbaar is (form-input, niet md-velden).
|
||||
const EDITABLE_STATUSES: ReadonlyArray<IdeaStatus> = [
|
||||
'DRAFT',
|
||||
'GRILL_FAILED',
|
||||
'GRILLED',
|
||||
'PLAN_FAILED',
|
||||
'PLAN_READY',
|
||||
]
|
||||
|
||||
export function isIdeaEditable(s: IdeaStatus): boolean {
|
||||
return EDITABLE_STATUSES.includes(s)
|
||||
}
|
||||
|
||||
// Statussen waarin grill_md bewerkbaar is (handmatige finetuning).
|
||||
export function isGrillMdEditable(s: IdeaStatus): boolean {
|
||||
return s === 'GRILLED' || s === 'PLAN_READY'
|
||||
}
|
||||
|
||||
// Statussen waarin plan_md bewerkbaar is.
|
||||
export function isPlanMdEditable(s: IdeaStatus): boolean {
|
||||
return s === 'PLAN_READY'
|
||||
}
|
||||
53
lib/schemas/idea.ts
Normal file
53
lib/schemas/idea.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { z } from 'zod'
|
||||
|
||||
// Velden die de gebruiker invult bij create/edit. Status wordt door
|
||||
// server-actions gezet (niet door client-input).
|
||||
export const ideaCreateSchema = z.object({
|
||||
title: z.string().trim().min(1, 'Titel is verplicht').max(200, 'Maximaal 200 tekens'),
|
||||
description: z.string().max(4000, 'Maximaal 4000 tekens').optional().nullable(),
|
||||
product_id: z.string().cuid('Ongeldig product').optional().nullable(),
|
||||
})
|
||||
|
||||
export const ideaUpdateSchema = ideaCreateSchema.partial()
|
||||
|
||||
export type IdeaCreateInput = z.infer<typeof ideaCreateSchema>
|
||||
export type IdeaUpdateInput = z.infer<typeof ideaUpdateSchema>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// plan_md frontmatter — strict format dat door make-plan-job geproduceerd
|
||||
// wordt en door materializeIdeaPlanAction wordt geparseerd. Zie
|
||||
// docs/plans/M12-ideas.md "Plan-md formaat A".
|
||||
|
||||
const verifyRequiredEnum = z.enum(['ALIGNED', 'ALIGNED_OR_PARTIAL', 'ANY'])
|
||||
|
||||
const planTaskSchema = z.object({
|
||||
title: z.string().min(1).max(200),
|
||||
description: z.string().max(4000).optional(),
|
||||
implementation_plan: z.string().max(8000).optional(),
|
||||
priority: z.number().int().min(1).max(4),
|
||||
verify_required: verifyRequiredEnum.optional(),
|
||||
verify_only: z.boolean().optional(),
|
||||
})
|
||||
|
||||
const planStorySchema = z.object({
|
||||
title: z.string().min(1).max(200),
|
||||
description: z.string().max(4000).optional(),
|
||||
acceptance_criteria: z.string().max(4000).optional(),
|
||||
priority: z.number().int().min(1).max(4),
|
||||
tasks: z.array(planTaskSchema).min(1, 'Story moet minimaal 1 taak hebben'),
|
||||
})
|
||||
|
||||
const planPbiSchema = z.object({
|
||||
title: z.string().min(1).max(200),
|
||||
description: z.string().max(4000).optional(),
|
||||
priority: z.number().int().min(1).max(4),
|
||||
})
|
||||
|
||||
export const ideaPlanMdFrontmatterSchema = z.object({
|
||||
pbi: planPbiSchema,
|
||||
stories: z.array(planStorySchema).min(1, 'Plan moet minimaal 1 story bevatten'),
|
||||
})
|
||||
|
||||
export type IdeaPlanFrontmatter = z.infer<typeof ideaPlanMdFrontmatterSchema>
|
||||
export type IdeaPlanStory = z.infer<typeof planStorySchema>
|
||||
export type IdeaPlanTask = z.infer<typeof planTaskSchema>
|
||||
Loading…
Add table
Add a link
Reference in a new issue