feat: citizenship built

This commit is contained in:
Ivan Rezchikov
2026-07-03 15:03:35 +03:00
parent c60ab9e8f9
commit 2eb0de5357
46 changed files with 1150 additions and 257 deletions
+57 -37
View File
@@ -1,19 +1,37 @@
import { getCurrentUser } from "../user";
import type { Block, Page, PageMeta, PageStatus, PageType } from "./types";
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 await prisma.page.findMany({ where: { type, status }, take: limit, orderBy: { publishedAt: "desc" }, }) as Page[];
return prisma.page.findMany({
where: { type, status },
select: publicPageSelect,
take: limit,
orderBy: { publishedAt: "desc" },
}) as Promise<PagePublic[]>;
}
export async function getPageById(id: string) {
return await prisma.page.findUnique({ where: { id: id } });
return prisma.page.findUnique({ where: { id } }) as Promise<Page | null>;
}
export async function getPageBySlug(slug: string, type?: PageType) {
return await prisma.page.findFirst({
return prisma.page.findFirst({
where: { slug, ...(type ? { type } : {}) },
}) as Page | null;
}) as Promise<Page | null>;
}
export async function createPage(slug: string, title: string, type: PageType, status: PageStatus, blocks: Block[], meta?: PageMeta) {
@@ -23,39 +41,41 @@ export async function createPage(slug: string, title: string, type: PageType, st
}
if (!user.isAdmin) {
throw new Error("Access denied")
throw new Error("Access denied");
}
const existingPage = await prisma.page.findUnique({ where: { slug } });
if (existingPage) {
throw new Error("Page already exists");
}
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,
})),
try {
const page = await prisma.page.create({
data: {
slug,
title,
type,
status,
meta: meta ?? {},
publishedAt: status === "PUBLISHED" ? new Date() : null,
createdBy: user.id,
},
});
}
return page;
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(
@@ -81,7 +101,7 @@ export async function updatePage(
meta: meta ?? {},
publishedAt: isNewlyPublished ? new Date() : undefined,
updatedAt: new Date(),
updatedBy: user.id
updatedBy: user.id,
},
});
@@ -94,7 +114,7 @@ export async function updatePage(
parentId: b.parentId,
type: b.type,
position: b.position,
content: b.content as any,
content: b.content as object,
})),
})]
: []),
@@ -120,4 +140,4 @@ export async function getBlocksByPageId(pageId: string) {
where: { pageId },
orderBy: { position: "asc" },
}) as Promise<Block[]>;
}
}