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"; import { removeImage } from "../image"; 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; } export async function getPageById(id: string) { return prisma.page.findUnique({ where: { id } }) as Promise; } export async function getPageBySlug(slug: string, type?: PageType) { return prisma.page.findFirst({ where: { slug, ...(type ? { type } : {}) }, }) as Promise; } 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"); 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) 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"; const oldImageBlocks = await prisma.block.findMany({ where: { pageId: id, type: "IMAGE" }, select: { content: true }, }); const oldImageKeys = oldImageBlocks .map((b) => (b.content as { type: "IMAGE"; url: string }).url) .filter(Boolean); const newImageKeys = blocks .filter((b) => b.type === "IMAGE") .map((b) => (b.content as { type: "IMAGE"; url: string }).url) .filter(Boolean); const keysToRemove = oldImageKeys.filter((k) => !newImageKeys.includes(k)); 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, })), })] : []), ]); if (keysToRemove.length > 0) { await Promise.allSettled(keysToRemove.map(removeImage)); } return page; } export async function archivePage(id: string) { const user = await getCurrentUser(); 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) { await prisma.page.delete({ where: { id } }); } export async function getBlocksByPageId(pageId: string) { return prisma.block.findMany({ where: { pageId }, orderBy: { position: "asc" }, }) as Promise; }