x
This commit is contained in:
1 parent
0341b30b6d
commit
bd8f5882e0
9 files changed
+1410
-25
No files matched your search
@@ -186,6 +186,113 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
},
|
||||
)
|
||||
.use(adminOnly)
|
||||
.post(
|
||||
"/with-section",
|
||||
async ({ body, set }) => {
|
||||
const { page: pageInput, section: sectionInput } = body as {
|
||||
page: {
|
||||
title: string;
|
||||
slug: string;
|
||||
isPublished?: boolean;
|
||||
};
|
||||
section: {
|
||||
type: string;
|
||||
payload: unknown;
|
||||
order?: number;
|
||||
isActive?: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
if (RESERVED_SLUGS.includes(pageInput.slug.toLowerCase())) {
|
||||
set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
error: "Validation failed",
|
||||
validationIssues: [
|
||||
{
|
||||
path: "page.slug",
|
||||
message: "Reserved slug cannot be used",
|
||||
code: "custom",
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
const validationResult = validateSectionPayload(
|
||||
sectionInput.type,
|
||||
sectionInput.payload,
|
||||
);
|
||||
if (!validationResult.success) {
|
||||
set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
error: "Validation failed",
|
||||
validationIssues: summarizeValidationIssues(
|
||||
validationResult.issues || [],
|
||||
).map((issue) => ({
|
||||
...issue,
|
||||
path:
|
||||
issue.path === "(root)"
|
||||
? "section.payload"
|
||||
: `section.payload.${issue.path}`,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
const page = await tx.page.create({
|
||||
data: {
|
||||
title: pageInput.title,
|
||||
slug: pageInput.slug,
|
||||
isPublished: pageInput.isPublished ?? false,
|
||||
},
|
||||
});
|
||||
const section = await tx.pageSection.create({
|
||||
data: {
|
||||
pageId: page.id,
|
||||
type: sectionInput.type.toUpperCase(),
|
||||
payload: validationResult.data,
|
||||
order: sectionInput.order ?? 1,
|
||||
isActive: sectionInput.isActive ?? true,
|
||||
},
|
||||
});
|
||||
|
||||
return { page, section };
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
pageId: result.page.id,
|
||||
sectionId: result.section.id,
|
||||
page: result.page,
|
||||
section: result.section,
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("[Pages] Atomic page creation error:", err);
|
||||
set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
error: "Failed to create page with section",
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
page: t.Object({
|
||||
title: t.String(),
|
||||
slug: t.String(),
|
||||
isPublished: t.Optional(t.Boolean()),
|
||||
}),
|
||||
section: t.Object({
|
||||
type: t.String(),
|
||||
payload: t.Any(),
|
||||
order: t.Optional(t.Number()),
|
||||
isActive: t.Optional(t.Boolean()),
|
||||
}),
|
||||
}),
|
||||
},
|
||||
)
|
||||
.post(
|
||||
"/",
|
||||
async ({ body, set }) => {
|
||||
|
||||
Reference in new issue
Block a user