feat: improved news

This commit is contained in:
Ivan Rezchikov
2026-06-21 13:35:32 +03:00
parent 95fd6ae716
commit 48656b562a
13 changed files with 154 additions and 147 deletions
+10 -44
View File
@@ -37,7 +37,7 @@ export async function createPage(slug: string, title: string, type: PageType, st
status,
meta: meta ?? {},
publishedAt: status === "PUBLISHED" ? new Date() : null,
createdBy: user.id,
createdBy: user.id
},
});
@@ -53,44 +53,13 @@ export async function createPage(slug: string, title: string, type: PageType, st
});
}
if (status === "PUBLISHED") {
await prisma.pageVersion.create({
data: {
pageId: page.id,
title: page.title,
blocks: blocks,
createdBy: user.id,
}
});
}
return page;
}
export async function restoreVersion(pageId: string, versionId: string) {
const version = await prisma.pageVersion.findUnique({
where: { id: versionId }
});
await prisma.$transaction([
prisma.block.deleteMany({ where: { pageId } }),
prisma.block.createMany({
data: (version?.blocks as Block[]).map(block => ({
...block,
pageId,
id: crypto.randomUUID(),
}))
}),
prisma.page.update({
where: { id: pageId },
data: { updatedAt: new Date() }
})
]);
}
export async function updatePage(
id: string,
title: string,
type: PageType,
status: PageStatus,
blocks: Block[],
meta?: PageMeta
@@ -102,10 +71,12 @@ export async function updatePage(
where: { id },
data: {
title,
type,
status,
meta: meta ?? {},
publishedAt: status === "PUBLISHED" ? new Date() : undefined,
updatedAt: new Date(),
updatedBy: user.id
},
});
@@ -124,20 +95,15 @@ export async function updatePage(
: []),
]);
if (status === "PUBLISHED") {
await prisma.pageVersion.create({
data: {
pageId: id,
title: page.title,
blocks: blocks as any,
createdBy: user.id,
},
});
}
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");