This commit is contained in:
Ivan Rezchikov
2026-07-11 21:20:57 +03:00
parent e192717466
commit 18161f4604
57 changed files with 1211 additions and 615 deletions
+36 -23
View File
@@ -6,6 +6,9 @@ import { archivePage, createPage, deletePage, updatePage } from "./repository";
import { revalidatePath } from "next/cache";
import { deleteNavLinkByPageId } from "../navlinks/repository";
import { validatePage } from "./validation";
import { requireAdmin } from "../user/require";
import { runAction } from "../run-action";
import type { ActionResult } from "../action-result";
export async function savePageAction(params: {
id: string | null,
@@ -15,31 +18,41 @@ export async function savePageAction(params: {
status: PageStatus,
blocks: Block[],
meta?: PageMeta
}) {
const parsed = validatePage(params);
if (!parsed.success) {
throw new Error(parsed.errors.join("; "));
}
}): Promise<ActionResult> {
return runAction(async () => {
await requireAdmin();
if (params.id) {
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
revalidatePath(`/${params.type.toLowerCase()}/${params.slug}`);
revalidatePath(`/${params.type.toLowerCase()}`);
} else {
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
revalidatePath(`/${params.type.toLowerCase()}`);
redirect(`/${params.type.toLowerCase()}/${page.slug}`);
}
const parsed = validatePage(params);
if (!parsed.success) {
throw new Error(parsed.errors.join("; "));
}
if (params.id) {
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
revalidatePath(`/${params.type.toLowerCase()}/${params.slug}`);
revalidatePath(`/${params.type.toLowerCase()}`);
} else {
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
revalidatePath(`/${params.type.toLowerCase()}`);
redirect(`/${params.type.toLowerCase()}/${page.slug}`);
}
});
}
export async function archivePageAction(id: string, slug: string, type: PageType) {
await archivePage(id);
await deleteNavLinkByPageId(id);
revalidatePath(`/${type.toLowerCase()}/${slug}`);
revalidatePath(`/${type.toLowerCase()}`);
export async function archivePageAction(id: string, slug: string, type: PageType): Promise<ActionResult> {
return runAction(async () => {
await requireAdmin();
await archivePage(id);
await deleteNavLinkByPageId(id);
revalidatePath(`/${type.toLowerCase()}/${slug}`);
revalidatePath(`/${type.toLowerCase()}`);
});
}
export async function deletePageAction(id: string, type: PageType) {
await deletePage(id);
revalidatePath(`/${type.toLowerCase()}`);
}
export async function deletePageAction(id: string, type: PageType): Promise<ActionResult> {
return runAction(async () => {
await requireAdmin();
await deletePage(id);
revalidatePath(`/${type.toLowerCase()}`);
});
}
+3 -11
View File
@@ -36,13 +36,7 @@ export async function getPageBySlug(slug: string, type?: PageType) {
export async function createPage(slug: string, title: string, type: PageType, status: PageStatus, blocks: Block[], meta?: PageMeta) {
const user = await getCurrentUser();
if (!user) {
throw new Error("Failed to fetch user");
}
if (!user.isAdmin) {
throw new Error("Access denied");
}
if (!user) throw new Error("Failed to fetch user");
try {
const page = await prisma.page.create({
@@ -87,7 +81,7 @@ export async function updatePage(
meta?: PageMeta
) {
const user = await getCurrentUser();
if (!user?.isAdmin) throw new Error("Access denied");
if (!user) throw new Error("Failed to fetch user");
const currentPage = await prisma.page.findUnique({ where: { id }, select: { status: true } });
const isNewlyPublished = status === "PUBLISHED" && currentPage?.status !== "PUBLISHED";
@@ -125,13 +119,11 @@ export async function updatePage(
export async function archivePage(id: string) {
const user = await getCurrentUser();
if (!user?.isAdmin) throw new Error("Access denied");
if (!user) throw new Error("Failed to fetch user");
await prisma.page.update({ where: { id }, data: { status: "ARCHIVED", updatedBy: user.id } });
}
export async function deletePage(id: string) {
const user = await getCurrentUser();
if (!user?.isAdmin) throw new Error("Access denied");
await prisma.page.delete({ where: { id } });
}