feat(codes): generateNextTaskCode + CODE_REGEX export + Zod regex op alle 3 schemas

- lib/code.ts: rename VALID_CODE_RE naar geexporteerde CODE_REGEX,
  verwijder ongebruikte deriveTaskCode
- lib/code-server.ts: generateNextTaskCode(productId) — flat per-product
  T-N teller, hergebruikt nextSequential helper. Export
  isCodeUniqueConflict zodat callers P2002 op code-veld kunnen detecteren
- Zod schemas (pbi/story/task): codeField met trim + max-length + regex,
  optional input (server vult bij ontbreken). Task krijgt voor het
  eerst een codeField

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-05-04 08:36:28 +02:00
parent 611b621d75
commit 829122d437
5 changed files with 38 additions and 12 deletions

View file

@ -3,7 +3,7 @@ import { prisma } from '@/lib/prisma'
const MAX_AUTO_CODE_ATTEMPTS = 3
function isCodeUniqueConflict(error: unknown): boolean {
export function isCodeUniqueConflict(error: unknown): boolean {
if (!(error instanceof Prisma.PrismaClientKnownRequestError)) return false
if (error.code !== 'P2002') return false
const target = (error.meta as { target?: string[] | string } | undefined)?.target
@ -40,6 +40,7 @@ export async function createWithCodeRetry<T>(
const STORY_AUTO_RE = /^ST-(\d+)$/
const PBI_AUTO_RE = /^PBI-(\d+)$/
const TASK_AUTO_RE = /^T-(\d+)$/
function nextSequential(existing: (string | null)[], pattern: RegExp): number {
let max = 0
@ -71,3 +72,13 @@ export async function generateNextPbiCode(productId: string): Promise<string> {
const next = nextSequential(pbis.map((p) => p.code), PBI_AUTO_RE)
return `PBI-${next}`
}
export async function generateNextTaskCode(productId: string): Promise<string> {
const tasks = await prisma.task.findMany({
where: { product_id: productId },
select: { code: true },
})
const next = nextSequential(tasks.map((t) => t.code), TASK_AUTO_RE)
return `T-${next}`
}