116 lines
3.8 KiB
TypeScript
116 lines
3.8 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { cookies } from 'next/headers'
|
|
import { getIronSession } from 'iron-session'
|
|
import { z } from 'zod'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { SessionData, sessionOptions } from '@/lib/session'
|
|
import { getAccessibleProduct } from '@/lib/product-access'
|
|
|
|
async function getSession() {
|
|
return getIronSession<SessionData>(await cookies(), sessionOptions)
|
|
}
|
|
|
|
const createPbiSchema = z.object({
|
|
productId: z.string(),
|
|
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),
|
|
})
|
|
|
|
const updatePbiSchema = z.object({
|
|
id: z.string(),
|
|
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),
|
|
})
|
|
|
|
export async function createPbiAction(_prevState: unknown, formData: FormData) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const parsed = createPbiSchema.safeParse({
|
|
productId: formData.get('productId'),
|
|
title: formData.get('title'),
|
|
description: formData.get('description') || undefined,
|
|
priority: formData.get('priority'),
|
|
})
|
|
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
|
|
|
|
const product = await getAccessibleProduct(parsed.data.productId, session.userId)
|
|
if (!product) return { error: 'Product niet gevonden' }
|
|
|
|
const last = await prisma.pbi.findFirst({
|
|
where: { product_id: parsed.data.productId, priority: parsed.data.priority },
|
|
orderBy: { sort_order: 'desc' },
|
|
})
|
|
const sort_order = (last?.sort_order ?? 0) + 1.0
|
|
|
|
const pbi = await prisma.pbi.create({
|
|
data: {
|
|
product_id: parsed.data.productId,
|
|
title: parsed.data.title,
|
|
description: parsed.data.description ?? null,
|
|
priority: parsed.data.priority,
|
|
sort_order,
|
|
},
|
|
})
|
|
|
|
revalidatePath(`/products/${parsed.data.productId}`)
|
|
return { success: true, pbi }
|
|
}
|
|
|
|
export async function updatePbiAction(_prevState: unknown, formData: FormData) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const parsed = updatePbiSchema.safeParse({
|
|
id: formData.get('id'),
|
|
title: formData.get('title'),
|
|
description: formData.get('description') || undefined,
|
|
priority: formData.get('priority'),
|
|
})
|
|
if (!parsed.success) return { error: parsed.error.flatten().fieldErrors }
|
|
|
|
const pbi = await prisma.pbi.findFirst({
|
|
where: { id: parsed.data.id },
|
|
include: { product: true },
|
|
})
|
|
if (!pbi) return { error: 'PBI niet gevonden' }
|
|
const accessible = await getAccessibleProduct(pbi.product_id, session.userId)
|
|
if (!accessible) return { error: 'PBI niet gevonden' }
|
|
|
|
await prisma.pbi.update({
|
|
where: { id: parsed.data.id },
|
|
data: {
|
|
title: parsed.data.title,
|
|
description: parsed.data.description ?? null,
|
|
priority: parsed.data.priority,
|
|
},
|
|
})
|
|
|
|
revalidatePath(`/products/${pbi.product_id}`)
|
|
return { success: true }
|
|
}
|
|
|
|
export async function deletePbiAction(id: string) {
|
|
const session = await getSession()
|
|
if (!session.userId) return { error: 'Niet ingelogd' }
|
|
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
|
|
|
const pbi = await prisma.pbi.findFirst({
|
|
where: { id },
|
|
include: { product: true },
|
|
})
|
|
if (!pbi) return { error: 'PBI niet gevonden' }
|
|
const accessible = await getAccessibleProduct(pbi.product_id, session.userId)
|
|
if (!accessible) return { error: 'PBI niet gevonden' }
|
|
|
|
await prisma.pbi.delete({ where: { id } })
|
|
|
|
revalidatePath(`/products/${pbi.product_id}`)
|
|
return { success: true }
|
|
}
|