This commit is contained in:
Ivan Rezchikov
2026-07-11 21:21:38 +03:00
parent 18161f4604
commit 4c42b5aa1d
20 changed files with 369 additions and 260 deletions
+18
View File
@@ -2,6 +2,7 @@ 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,
@@ -86,6 +87,19 @@ export async function updatePage(
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: {
@@ -114,6 +128,10 @@ export async function updatePage(
: []),
]);
if (keysToRemove.length > 0) {
await Promise.allSettled(keysToRemove.map(removeImage));
}
return page;
}