feat: add admin content module actions

This commit is contained in:
Simo committed 2026-07-12 15:27:07 +02:00
1 parent b9525c8e6b
commit f422bb4a0b
6 files changed
+596

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
'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'
const bannerSchema = z.object({
title: z.string().min(1).max(255),
subtitle: z.string().max(500).optional().default(''),
image: z.string().max(500),
link: z.string().max(500).optional().default(''),
color: z.string().max(20).optional().default(''),
isActive: z.coerce.number().int().min(0).max(1).default(1),
sortOrder: z.coerce.number().int().min(0).default(0),
startDate: z.string().max(50).nullable().optional(),
endDate: z.string().max(50).nullable().optional(),
})
export const createBanner = adminAction(
{ permission: PERMS.BANNERS_EDIT, schema: bannerSchema },
async (ctx) => {
const banner = await prisma.websiteBanner.create({ data: ctx.data })
logAudit({
userId: ctx.session.user.id,
action: 'banner_create',
target: 'WebsiteBanner',
targetId: banner.id,
after: { title: banner.title },
})
return actionOk({ id: banner.id })
},
)
const updateBannerInput = bannerSchema.partial().extend({ id: z.coerce.number().int().positive() })
export const updateBanner = adminAction(
{ permission: PERMS.BANNERS_EDIT, schema: updateBannerInput },
async (ctx) => {
const { id, ...data } = ctx.data
const existing = await prisma.websiteBanner.findUnique({ where: { id } })
if (!existing) throw new ActionError('Banner not found')
await prisma.websiteBanner.update({ where: { id }, data })
logAudit({
userId: ctx.session.user.id,
action: 'banner_update',
target: 'WebsiteBanner',
targetId: id,
})
return actionOk({ id })
},
)
const deleteBannerInput = z.object({ id: z.coerce.number().int().positive() })
export const deleteBanner = adminAction(
{ permission: PERMS.BANNERS_EDIT, schema: deleteBannerInput },
async (ctx) => {
await prisma.websiteBanner.delete({ where: { id: ctx.data.id } })
logAudit({
userId: ctx.session.user.id,
action: 'banner_delete',
target: 'WebsiteBanner',
targetId: ctx.data.id,
})
return actionOk()
},
)
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+61
View File
@@ -0,0 +1,61 @@
import { z } from 'zod'
/** Convert empty strings to undefined so optional/nullable fields pass validation */
const emptyToUndefined = (v: unknown) => (v === '' ? undefined : v)
export const eventTypeSchema = z.object({
name: z.string().min(1, 'Name is required').max(100),
slug: z
.string()
.min(1)
.max(100)
.regex(/^[a-z0-9-]+$/, 'Slug must be lowercase with hyphens'),
description: z.string().max(500).optional().default(''),
color: z.string().max(20).optional().default('#3b82f6'),
icon: z.string().max(50).optional().default('calendar'),
isActive: z.coerce.number().int().min(0).max(1).default(1),
minRank: z.coerce.number().int().min(0).max(7).default(0),
})
export const createEventSchema = z.object({
title: z.string().min(1, 'Title is required').max(255),
description: z.string().min(1, 'Description is required'),
typeId: z.coerce.number().int().positive(),
roomId: z.preprocess(emptyToUndefined, z.coerce.number().int().positive().nullable().optional()),
startsAt: z.coerce.date(),
endsAt: z.preprocess(emptyToUndefined, z.coerce.date().nullable().optional()),
maxPlayers: z.preprocess(
emptyToUndefined,
z.coerce.number().int().positive().nullable().optional(),
),
isRecurring: z.coerce.number().int().min(0).max(1).default(0),
recurrenceRule: z.preprocess(emptyToUndefined, z.string().max(255).nullable().optional()),
status: z.enum(['draft', 'published', 'cancelled', 'completed']).default('draft'),
image: z.preprocess(emptyToUndefined, z.string().max(500).nullable().optional()),
})
export const updateEventSchema = createEventSchema.partial()
export const eventPrizeSchema = z.object({
eventId: z.coerce.number().int().positive(),
position: z.coerce.number().int().positive().default(1),
prizeType: z.enum(['badge', 'credits', 'pixels', 'points', 'item']).default('badge'),
badgeCode: z.string().max(50).nullable().optional(),
credits: z.coerce.number().int().min(0).default(0),
pixels: z.coerce.number().int().min(0).default(0),
points: z.coerce.number().int().min(0).default(0),
itemId: z.coerce.number().int().positive().nullable().optional(),
description: z.string().max(255).optional().default(''),
})
export const eventWinnerSchema = z.object({
eventId: z.coerce.number().int().positive(),
userId: z.coerce.number().int().positive(),
position: z.coerce.number().int().positive().default(1),
})
export type EventTypeInput = z.infer<typeof eventTypeSchema>
export type CreateEventInput = z.infer<typeof createEventSchema>
export type UpdateEventInput = z.infer<typeof updateEventSchema>
export type EventPrizeInput = z.infer<typeof eventPrizeSchema>
export type EventWinnerInput = z.infer<typeof eventWinnerSchema>
+31
View File
@@ -0,0 +1,31 @@
import { z } from 'zod'
export const createPollSchema = z.object({
title: z.string().min(1, 'Title is required').max(255),
description: z.string().max(2000).nullable().optional(),
status: z.enum(['draft', 'active', 'closed']).default('draft'),
showResults: z.coerce.number().int().min(0).max(1).default(1),
multipleChoice: z.coerce.number().int().min(0).max(1).default(0),
startsAt: z.coerce.date().nullable().optional(),
endsAt: z.coerce.date().nullable().optional(),
})
export const updatePollSchema = createPollSchema.partial()
export const pollQuestionSchema = z.object({
pollId: z.coerce.number().int().positive(),
question: z.string().min(1).max(500),
type: z.enum(['single', 'multiple', 'text']).default('single'),
sortOrder: z.coerce.number().int().min(0).default(0),
options: z.string().min(1, 'Options are required'),
})
export const pollVoteSchema = z.object({
questionId: z.coerce.number().int().positive(),
answer: z.string().min(1).max(500),
})
export type CreatePollInput = z.infer<typeof createPollSchema>
export type UpdatePollInput = z.infer<typeof updatePollSchema>
export type PollQuestionInput = z.infer<typeof pollQuestionSchema>
export type PollVoteInput = z.infer<typeof pollVoteSchema>