feat(editorial): validate publications and preserve partial event updates
This commit is contained in:
1 parent
76f0420d64
commit
db4acbb46e
14 files changed
+613
-16
No files matched your search
@@ -12,6 +12,7 @@ import {
|
||||
WebsiteEventWinner,
|
||||
} from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { publicationIssues } from "@/lib/publication-preflight";
|
||||
import { adminAction, authAction } from "@/lib/safe-action";
|
||||
import { ActionError, actionError, actionOk } from "@/lib/safe-action-shared";
|
||||
import { logAudit } from "@/lib/services/audit";
|
||||
@@ -108,6 +109,15 @@ export const createEvent = adminAction(
|
||||
{ permission: PERMS.EVENTS_EDIT, schema: createEventSchema },
|
||||
async (ctx) => {
|
||||
const now = new Date();
|
||||
if (
|
||||
ctx.data.status === "published" &&
|
||||
publicationIssues({ kind: "event", ...ctx.data }).some(
|
||||
(issue) => issue.severity === "error",
|
||||
)
|
||||
)
|
||||
throw new ActionError(
|
||||
"Check the event image and schedule before publishing",
|
||||
);
|
||||
const [result] = await db.insert(WebsiteEvent).values({
|
||||
...ctx.data,
|
||||
hostUserId: Number(ctx.session.user.id),
|
||||
@@ -143,11 +153,27 @@ export const updateEvent = adminAction(
|
||||
id: WebsiteEvent.id,
|
||||
title: WebsiteEvent.title,
|
||||
status: WebsiteEvent.status,
|
||||
image: WebsiteEvent.image,
|
||||
startsAt: WebsiteEvent.startsAt,
|
||||
endsAt: WebsiteEvent.endsAt,
|
||||
})
|
||||
.from(WebsiteEvent)
|
||||
.where(eq(WebsiteEvent.id, id))
|
||||
.limit(1);
|
||||
if (!existing) throw new ActionError("Event not found");
|
||||
if (
|
||||
(data.status ?? existing.status) === "published" &&
|
||||
publicationIssues({
|
||||
kind: "event",
|
||||
image: data.image === undefined ? existing.image : data.image,
|
||||
startsAt:
|
||||
data.startsAt === undefined ? existing.startsAt : data.startsAt,
|
||||
endsAt: data.endsAt === undefined ? existing.endsAt : data.endsAt,
|
||||
}).some((issue) => issue.severity === "error")
|
||||
)
|
||||
throw new ActionError(
|
||||
"Check the event image and schedule before publishing",
|
||||
);
|
||||
|
||||
await db
|
||||
.update(WebsiteEvent)
|
||||
|
||||
Reference in new issue
Block a user