48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
'use server'
|
|
|
|
import { z } from 'zod'
|
|
import { PERMS } from '@/lib/permissions'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { adminAction } from '@/lib/safe-action'
|
|
import { ActionError, actionOk } from '@/lib/safe-action-shared'
|
|
|
|
const templateSchema = z.object({
|
|
title: z.string().min(1).max(255),
|
|
content: z.string().min(1),
|
|
category: z.string().max(50).optional().default('general'),
|
|
sortOrder: z.coerce.number().int().min(0).default(0),
|
|
})
|
|
|
|
export const createTemplate = adminAction(
|
|
{ permission: PERMS.TICKETS_EDIT, schema: templateSchema },
|
|
async (ctx) => {
|
|
const tpl = await prisma.websiteTicketTemplate.create({ data: ctx.data })
|
|
return actionOk({ id: tpl.id })
|
|
},
|
|
)
|
|
|
|
const updateTemplateInput = templateSchema
|
|
.partial()
|
|
.extend({ id: z.coerce.number().int().positive() })
|
|
|
|
export const updateTemplate = adminAction(
|
|
{ permission: PERMS.TICKETS_EDIT, schema: updateTemplateInput },
|
|
async (ctx) => {
|
|
const { id, ...data } = ctx.data
|
|
const existing = await prisma.websiteTicketTemplate.findUnique({ where: { id } })
|
|
if (!existing) throw new ActionError('Template not found')
|
|
await prisma.websiteTicketTemplate.update({ where: { id }, data })
|
|
return actionOk({ id })
|
|
},
|
|
)
|
|
|
|
const deleteTemplateInput = z.object({ id: z.coerce.number().int().positive() })
|
|
|
|
export const deleteTemplate = adminAction(
|
|
{ permission: PERMS.TICKETS_EDIT, schema: deleteTemplateInput },
|
|
async (ctx) => {
|
|
await prisma.websiteTicketTemplate.delete({ where: { id: ctx.data.id } })
|
|
return actionOk()
|
|
},
|
|
)
|