prod
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user