feat(ST-507): persist code in product, pbi and story actions with auto-default and uniqueness
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
8ffbc526d5
commit
16a3b139ae
3 changed files with 93 additions and 0 deletions
|
|
@ -7,13 +7,17 @@ import { z } from 'zod'
|
|||
import { prisma } from '@/lib/prisma'
|
||||
import { SessionData, sessionOptions } from '@/lib/session'
|
||||
import { getAccessibleProduct } from '@/lib/product-access'
|
||||
import { generateNextPbiCode, isValidCode, MAX_CODE_LENGTH, normalizeCode } from '@/lib/code'
|
||||
|
||||
async function getSession() {
|
||||
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
||||
}
|
||||
|
||||
const codeField = z.string().max(MAX_CODE_LENGTH).optional()
|
||||
|
||||
const createPbiSchema = z.object({
|
||||
productId: z.string(),
|
||||
code: codeField,
|
||||
title: z.string().min(1, 'Titel is verplicht').max(200),
|
||||
description: z.string().max(2000).optional(),
|
||||
priority: z.coerce.number().int().min(1).max(4),
|
||||
|
|
@ -21,6 +25,7 @@ const createPbiSchema = z.object({
|
|||
|
||||
const updatePbiSchema = z.object({
|
||||
id: z.string(),
|
||||
code: codeField,
|
||||
title: z.string().min(1, 'Titel is verplicht').max(200),
|
||||
description: z.string().max(2000).optional(),
|
||||
priority: z.coerce.number().int().min(1).max(4),
|
||||
|
|
@ -33,6 +38,7 @@ export async function createPbiAction(_prevState: unknown, formData: FormData) {
|
|||
|
||||
const parsed = createPbiSchema.safeParse({
|
||||
productId: formData.get('productId'),
|
||||
code: (formData.get('code') as string) || undefined,
|
||||
title: formData.get('title'),
|
||||
description: formData.get('description') || undefined,
|
||||
priority: formData.get('priority'),
|
||||
|
|
@ -42,6 +48,17 @@ export async function createPbiAction(_prevState: unknown, formData: FormData) {
|
|||
const product = await getAccessibleProduct(parsed.data.productId, session.userId)
|
||||
if (!product) return { error: 'Product niet gevonden' }
|
||||
|
||||
let code = normalizeCode(parsed.data.code)
|
||||
if (code !== null && !isValidCode(code)) {
|
||||
return { error: { code: ['Code mag alleen letters, cijfers, punten, koppeltekens of underscores bevatten'] } }
|
||||
}
|
||||
if (code === null) {
|
||||
code = await generateNextPbiCode(parsed.data.productId)
|
||||
} else {
|
||||
const dup = await prisma.pbi.findFirst({ where: { product_id: parsed.data.productId, code } })
|
||||
if (dup) return { error: { code: ['Deze code is al in gebruik binnen dit product'] } }
|
||||
}
|
||||
|
||||
const last = await prisma.pbi.findFirst({
|
||||
where: { product_id: parsed.data.productId, priority: parsed.data.priority },
|
||||
orderBy: { sort_order: 'desc' },
|
||||
|
|
@ -51,6 +68,7 @@ export async function createPbiAction(_prevState: unknown, formData: FormData) {
|
|||
const pbi = await prisma.pbi.create({
|
||||
data: {
|
||||
product_id: parsed.data.productId,
|
||||
code,
|
||||
title: parsed.data.title,
|
||||
description: parsed.data.description ?? null,
|
||||
priority: parsed.data.priority,
|
||||
|
|
@ -69,6 +87,7 @@ export async function updatePbiAction(_prevState: unknown, formData: FormData) {
|
|||
|
||||
const parsed = updatePbiSchema.safeParse({
|
||||
id: formData.get('id'),
|
||||
code: (formData.get('code') as string) || undefined,
|
||||
title: formData.get('title'),
|
||||
description: formData.get('description') || undefined,
|
||||
priority: formData.get('priority'),
|
||||
|
|
@ -83,9 +102,21 @@ export async function updatePbiAction(_prevState: unknown, formData: FormData) {
|
|||
const accessible = await getAccessibleProduct(pbi.product_id, session.userId)
|
||||
if (!accessible) return { error: 'PBI niet gevonden' }
|
||||
|
||||
const code = normalizeCode(parsed.data.code)
|
||||
if (code !== null && !isValidCode(code)) {
|
||||
return { error: { code: ['Code mag alleen letters, cijfers, punten, koppeltekens of underscores bevatten'] } }
|
||||
}
|
||||
if (code) {
|
||||
const dup = await prisma.pbi.findFirst({
|
||||
where: { product_id: pbi.product_id, code, NOT: { id: parsed.data.id } },
|
||||
})
|
||||
if (dup) return { error: { code: ['Deze code is al in gebruik binnen dit product'] } }
|
||||
}
|
||||
|
||||
await prisma.pbi.update({
|
||||
where: { id: parsed.data.id },
|
||||
data: {
|
||||
code,
|
||||
title: parsed.data.title,
|
||||
description: parsed.data.description ?? null,
|
||||
priority: parsed.data.priority,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue