feat: add admin content module actions
This commit is contained in:
1 parent
b9525c8e6b
commit
f422bb4a0b
6 files changed
+596
No files matched your search
@@ -0,0 +1,107 @@
|
||||
'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'
|
||||
import { logAudit } from '@/lib/services/audit'
|
||||
import { createPollSchema, pollQuestionSchema, updatePollSchema } from '@/lib/validators/poll'
|
||||
|
||||
// ── Polls ───────────────────────────────────────────────────────────
|
||||
|
||||
export const createPoll = adminAction(
|
||||
{ permission: PERMS.POLLS_EDIT, schema: createPollSchema },
|
||||
async (ctx) => {
|
||||
const poll = await prisma.websitePoll.create({ data: ctx.data })
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: 'poll_create',
|
||||
target: 'WebsitePoll',
|
||||
targetId: poll.id,
|
||||
after: { title: poll.title },
|
||||
})
|
||||
return actionOk({ id: poll.id })
|
||||
},
|
||||
)
|
||||
|
||||
const updatePollInput = updatePollSchema.extend({
|
||||
id: z.coerce.number().int().positive(),
|
||||
})
|
||||
|
||||
export const updatePoll = adminAction(
|
||||
{ permission: PERMS.POLLS_EDIT, schema: updatePollInput },
|
||||
async (ctx) => {
|
||||
const { id, ...data } = ctx.data
|
||||
const existing = await prisma.websitePoll.findUnique({ where: { id } })
|
||||
if (!existing) throw new ActionError('Poll not found')
|
||||
|
||||
await prisma.websitePoll.update({ where: { id }, data })
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: 'poll_update',
|
||||
target: 'WebsitePoll',
|
||||
targetId: id,
|
||||
before: { title: existing.title, status: existing.status },
|
||||
after: data,
|
||||
})
|
||||
return actionOk({ id })
|
||||
},
|
||||
)
|
||||
|
||||
const deletePollInput = z.object({
|
||||
id: z.coerce.number().int().positive(),
|
||||
})
|
||||
|
||||
export const deletePoll = adminAction(
|
||||
{ permission: PERMS.POLLS_EDIT, schema: deletePollInput },
|
||||
async (ctx) => {
|
||||
const existing = await prisma.websitePoll.findUnique({ where: { id: ctx.data.id } })
|
||||
if (!existing) throw new ActionError('Poll not found')
|
||||
|
||||
await prisma.websitePoll.delete({ where: { id: ctx.data.id } })
|
||||
logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: 'poll_delete',
|
||||
target: 'WebsitePoll',
|
||||
targetId: ctx.data.id,
|
||||
before: { title: existing.title },
|
||||
})
|
||||
return actionOk()
|
||||
},
|
||||
)
|
||||
|
||||
// ── Questions ───────────────────────────────────────────────────────
|
||||
|
||||
export const addPollQuestion = adminAction(
|
||||
{ permission: PERMS.POLLS_EDIT, schema: pollQuestionSchema },
|
||||
async (ctx) => {
|
||||
const question = await prisma.websitePollQuestion.create({ data: ctx.data })
|
||||
return actionOk({ id: question.id })
|
||||
},
|
||||
)
|
||||
|
||||
const updateQuestionInput = pollQuestionSchema.partial().extend({
|
||||
id: z.coerce.number().int().positive(),
|
||||
})
|
||||
|
||||
export const updatePollQuestion = adminAction(
|
||||
{ permission: PERMS.POLLS_EDIT, schema: updateQuestionInput },
|
||||
async (ctx) => {
|
||||
const { id, ...data } = ctx.data
|
||||
await prisma.websitePollQuestion.update({ where: { id }, data })
|
||||
return actionOk({ id })
|
||||
},
|
||||
)
|
||||
|
||||
const deleteQuestionInput = z.object({
|
||||
id: z.coerce.number().int().positive(),
|
||||
})
|
||||
|
||||
export const deletePollQuestion = adminAction(
|
||||
{ permission: PERMS.POLLS_EDIT, schema: deleteQuestionInput },
|
||||
async (ctx) => {
|
||||
await prisma.websitePollQuestion.delete({ where: { id: ctx.data.id } })
|
||||
return actionOk()
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user