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[]>;
}
}
+14 -2
View File
@@ -17,12 +17,24 @@ export type Page = {
status: PageStatus;
meta: PageMeta;
createdAt: Date;
updatedAt: Date | null;
updatedAt: Date;
publishedAt: Date | null;
createdBy: string;
createdBy: string | null;
updatedBy: string | null;
}
export type PagePublic = {
id: string;
slug: string;
title: string;
type: PageType;
status: PageStatus;
meta: PageMeta;
createdAt: Date;
updatedAt: Date;
publishedAt: Date | null;
}
export const BLOCK_TYPES = ["PARAGRAPH", "HEADING", "QUOTE", "DIVIDER", "CALLOUT", "IMAGE", "LIST", "TABLE", "LINK"] as const;
export type BlockType = typeof BLOCK_TYPES[number];
+20 -1
View File
@@ -1,12 +1,31 @@
import { z } from "zod";
const blockContentSchema = z.discriminatedUnion("type", [
z.object({ type: z.literal("PARAGRAPH"), text: z.string() }),
z.object({ type: z.literal("HEADING"), text: z.string(), level: z.union([z.literal(1), z.literal(2), z.literal(3)]) }),
z.object({ type: z.literal("QUOTE"), text: z.string(), author: z.string().optional() }),
z.object({ type: z.literal("DIVIDER") }),
z.object({ type: z.literal("CALLOUT"), title: z.string(), text: z.string() }),
z.object({ type: z.literal("IMAGE"), url: z.string(), caption: z.string().optional() }),
z.object({ type: z.literal("LIST"), style: z.enum(["BULLET", "ORDERED"]), items: z.array(z.string()) }),
z.object({ type: z.literal("TABLE"), headers: z.array(z.string()), rows: z.array(z.array(z.string())) }),
z.object({ type: z.literal("LINK"), label: z.string(), href: z.string() }),
]);
export const pageSchema = z.object({
id: z.string().nullable(),
title: z.string().min(1, "Title is required"),
slug: z.string().min(1, "Slug is required"),
type: z.enum(["NEWS", "PAGE"]),
status: z.enum(["DRAFT", "PUBLISHED", "ARCHIVED"]),
blocks: z.array(z.any()),
blocks: z.array(z.object({
id: z.string(),
pageId: z.string(),
parentId: z.string().nullable(),
type: z.enum(["PARAGRAPH", "HEADING", "QUOTE", "DIVIDER", "CALLOUT", "IMAGE", "LIST", "TABLE", "LINK"]),
position: z.number(),
content: blockContentSchema,
})),
meta: z.object({
location: z.string().optional(),
tags: z.array(z.string()).optional(),