x
This commit is contained in:
1 parent
bd8f5882e0
commit
e509e3f0a2
8 files changed
+581
-12
No files matched your search
@@ -13,6 +13,7 @@ import {
|
||||
EditorialCarouselSectionPayloadSchema,
|
||||
HeroArticleSectionPayloadSchema,
|
||||
RichTextSectionPayloadSchema,
|
||||
ArticleMediaSectionPayloadSchema,
|
||||
ScentNotesBreakdownSectionPayloadSchema,
|
||||
QuoteCalloutSectionPayloadSchema,
|
||||
ProductCarouselSectionPayloadSchema,
|
||||
@@ -239,6 +240,26 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
};
|
||||
}
|
||||
|
||||
const existingPage = await prisma.page.findUnique({
|
||||
where: { slug: pageInput.slug },
|
||||
select: { id: true, slug: true },
|
||||
});
|
||||
if (existingPage) {
|
||||
set.status = 409;
|
||||
return {
|
||||
success: false,
|
||||
code: "PAGE_SLUG_EXISTS",
|
||||
stage: "create_page",
|
||||
error: "A page with this slug already exists",
|
||||
diagnostic:
|
||||
"The atomic endpoint creates new pages only. Add the section to the existing page instead.",
|
||||
pageId: existingPage.id,
|
||||
slug: existingPage.slug,
|
||||
nextTool: "add_page_section",
|
||||
partialWrite: false,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
const page = await tx.page.create({
|
||||
@@ -273,7 +294,14 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
code: "PAGE_WITH_SECTION_CREATE_FAILED",
|
||||
stage: "transaction",
|
||||
error: "Failed to create page with section",
|
||||
diagnostic:
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: "Unexpected error while creating the page and its first section.",
|
||||
partialWrite: false,
|
||||
};
|
||||
}
|
||||
},
|
||||
@@ -490,13 +518,46 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
const pageId = Number(slug);
|
||||
const { type, order, payload, isActive } = body as any;
|
||||
|
||||
if (!Number.isInteger(pageId) || pageId <= 0) {
|
||||
set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
code: "INVALID_PAGE_ID",
|
||||
stage: "validate_page",
|
||||
error: "Invalid page ID",
|
||||
pageId: slug,
|
||||
partialWrite: false,
|
||||
};
|
||||
}
|
||||
|
||||
const page = await prisma.page.findUnique({
|
||||
where: { id: pageId },
|
||||
select: { id: true, slug: true },
|
||||
});
|
||||
if (!page) {
|
||||
set.status = 404;
|
||||
return {
|
||||
success: false,
|
||||
code: "PAGE_NOT_FOUND",
|
||||
stage: "find_page",
|
||||
error: "Page not found",
|
||||
pageId,
|
||||
partialWrite: false,
|
||||
};
|
||||
}
|
||||
|
||||
const validationResult = validateSectionPayload(type, payload);
|
||||
if (!validationResult.success) {
|
||||
set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
code: "SECTION_PAYLOAD_INVALID",
|
||||
stage: "validate_section",
|
||||
error: "Validation failed",
|
||||
details: validationResult.issues,
|
||||
validationIssues: summarizeValidationIssues(validationResult.issues || []),
|
||||
pageId,
|
||||
partialWrite: false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -509,7 +570,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
finalOrder = lastSection ? lastSection.order + 1 : 1;
|
||||
}
|
||||
|
||||
return await prisma.pageSection.create({
|
||||
const section = await prisma.pageSection.create({
|
||||
data: {
|
||||
pageId,
|
||||
type: type.toUpperCase(),
|
||||
@@ -518,10 +579,28 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
isActive: isActive !== false,
|
||||
},
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
pageId,
|
||||
pageSlug: page.slug,
|
||||
sectionId: section.id,
|
||||
section,
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("[PageSections] Error:", err);
|
||||
set.status = 500;
|
||||
return { error: "Failed to add section" };
|
||||
return {
|
||||
success: false,
|
||||
code: "SECTION_CREATE_FAILED",
|
||||
stage: "create_section",
|
||||
error: "Failed to add section",
|
||||
diagnostic:
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: "Unexpected error while adding the section.",
|
||||
pageId: Number(slug),
|
||||
partialWrite: false,
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -693,6 +772,9 @@ export function validateSectionPayload(
|
||||
case "RICH_TEXT":
|
||||
schema = RichTextSectionPayloadSchema;
|
||||
break;
|
||||
case "ARTICLE_MEDIA":
|
||||
schema = ArticleMediaSectionPayloadSchema;
|
||||
break;
|
||||
case "SCENT_NOTES_BREAKDOWN":
|
||||
schema = ScentNotesBreakdownSectionPayloadSchema;
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user