144 lines
3.7 KiB
TypeScript
144 lines
3.7 KiB
TypeScript
import { getCurrentUser } from "../user";
|
|
import type { Block, Page, PageMeta, PagePublic, PageStatus, PageType } from "./types";
|
|
import { prisma } from "../prisma";
|
|
import { Prisma } from "@/lib/generated/prisma/client";
|
|
|
|
const publicPageSelect = {
|
|
id: true,
|
|
slug: true,
|
|
title: true,
|
|
type: true,
|
|
status: true,
|
|
meta: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
publishedAt: true,
|
|
} as const;
|
|
|
|
export async function getPages(type: PageType, status: PageStatus, limit?: number) {
|
|
return prisma.page.findMany({
|
|
where: { type, status },
|
|
select: publicPageSelect,
|
|
take: limit,
|
|
orderBy: { publishedAt: "desc" },
|
|
}) as Promise<PagePublic[]>;
|
|
}
|
|
|
|
export async function getPageById(id: string) {
|
|
return prisma.page.findUnique({ where: { id } }) as Promise<Page | null>;
|
|
}
|
|
|
|
export async function getPageBySlug(slug: string, type?: PageType) {
|
|
return prisma.page.findFirst({
|
|
where: { slug, ...(type ? { type } : {}) },
|
|
}) as Promise<Page | null>;
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
try {
|
|
const page = await prisma.page.create({
|
|
data: {
|
|
slug,
|
|
title,
|
|
type,
|
|
status,
|
|
meta: meta ?? {},
|
|
publishedAt: status === "PUBLISHED" ? new Date() : null,
|
|
createdBy: user.id,
|
|
},
|
|
});
|
|
|
|
if (blocks.length > 0) {
|
|
await prisma.block.createMany({
|
|
data: blocks.map((block) => ({
|
|
pageId: page.id,
|
|
parentId: block.parentId,
|
|
type: block.type,
|
|
position: block.position,
|
|
content: block.content as object,
|
|
})),
|
|
});
|
|
}
|
|
|
|
return page;
|
|
} catch (e) {
|
|
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2002") {
|
|
throw new Error("Page already exists");
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
export async function updatePage(
|
|
id: string,
|
|
title: string,
|
|
type: PageType,
|
|
status: PageStatus,
|
|
blocks: Block[],
|
|
meta?: PageMeta
|
|
) {
|
|
const user = await getCurrentUser();
|
|
if (!user?.isAdmin) throw new Error("Access denied");
|
|
|
|
const currentPage = await prisma.page.findUnique({ where: { id }, select: { status: true } });
|
|
const isNewlyPublished = status === "PUBLISHED" && currentPage?.status !== "PUBLISHED";
|
|
|
|
const page = await prisma.page.update({
|
|
where: { id },
|
|
data: {
|
|
title,
|
|
type,
|
|
status,
|
|
meta: meta ?? {},
|
|
publishedAt: isNewlyPublished ? new Date() : undefined,
|
|
updatedAt: new Date(),
|
|
updatedBy: user.id,
|
|
},
|
|
});
|
|
|
|
await prisma.$transaction([
|
|
prisma.block.deleteMany({ where: { pageId: id } }),
|
|
...(blocks.length > 0
|
|
? [prisma.block.createMany({
|
|
data: blocks.map((b) => ({
|
|
pageId: id,
|
|
parentId: b.parentId,
|
|
type: b.type,
|
|
position: b.position,
|
|
content: b.content as object,
|
|
})),
|
|
})]
|
|
: []),
|
|
]);
|
|
|
|
return page;
|
|
}
|
|
|
|
export async function archivePage(id: string) {
|
|
const user = await getCurrentUser();
|
|
if (!user?.isAdmin) throw new Error("Access denied");
|
|
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 } });
|
|
}
|
|
|
|
export async function getBlocksByPageId(pageId: string) {
|
|
return prisma.block.findMany({
|
|
where: { pageId },
|
|
orderBy: { position: "asc" },
|
|
}) as Promise<Block[]>;
|
|
}
|