x
This commit is contained in:
1 parent
ac387185e0
commit
6a35dcf955
6 files changed
+437
-67
No files matched your search
@@ -64,14 +64,37 @@ function resolveCatName(cat: any, lang: string): any {
|
||||
return trans?.name ?? cat.name;
|
||||
}
|
||||
|
||||
function summarizeValidationIssues(issues: z.ZodIssue[]) {
|
||||
return issues.map((issue) => ({
|
||||
path: issue.path.join(".") || "(root)",
|
||||
message: issue.message,
|
||||
code: issue.code,
|
||||
}));
|
||||
}
|
||||
|
||||
export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
.get("/", async () => {
|
||||
return await prisma.page.findMany({
|
||||
include: {
|
||||
sections: {
|
||||
orderBy: { order: "asc" },
|
||||
.get("/", async ({ query }) => {
|
||||
const view = String((query as any)?.view || "summary").toLowerCase();
|
||||
if (view === "full") {
|
||||
return await prisma.page.findMany({
|
||||
include: {
|
||||
sections: {
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return await prisma.page.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
slug: true,
|
||||
title: true,
|
||||
isPublished: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
})
|
||||
.get("/:slug", async ({ params: { slug }, query, set }) => {
|
||||
@@ -114,6 +137,8 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
})
|
||||
.get("/sections/:sectionId", async ({ params: { sectionId }, query, set }) => {
|
||||
const lang = (query as any)?.lang || "th";
|
||||
const hydrate =
|
||||
String((query as any)?.hydrate ?? "true").toLowerCase() !== "false";
|
||||
try {
|
||||
const section = await prisma.pageSection.findUnique({
|
||||
where: { id: Number(sectionId) },
|
||||
@@ -122,7 +147,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
set.status = 404;
|
||||
return { error: "Section not found" };
|
||||
}
|
||||
return await hydrateSectionPayload(section, lang);
|
||||
return hydrate ? await hydrateSectionPayload(section, lang) : section;
|
||||
} catch (err) {
|
||||
set.status = 500;
|
||||
return { error: "Failed to fetch page section" };
|
||||
@@ -182,11 +207,60 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
};
|
||||
}
|
||||
|
||||
let finalIsPublished = true;
|
||||
if (metadata !== undefined) {
|
||||
set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
operation,
|
||||
pageId: id ? Number(id) : null,
|
||||
slug,
|
||||
title,
|
||||
error: "Unsupported field",
|
||||
diagnostic:
|
||||
"Page metadata is not supported by the current Page model. Remove 'metadata' from the request.",
|
||||
};
|
||||
}
|
||||
|
||||
if (status !== undefined) {
|
||||
finalIsPublished = status === "published";
|
||||
} else if (isPublished !== undefined) {
|
||||
finalIsPublished = isPublished;
|
||||
set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
operation,
|
||||
pageId: id ? Number(id) : null,
|
||||
slug,
|
||||
title,
|
||||
error: "Unsupported publish field",
|
||||
diagnostic:
|
||||
"Use 'isPublished' only. The legacy 'status' field is no longer accepted.",
|
||||
};
|
||||
}
|
||||
|
||||
let finalIsPublished = isPublished;
|
||||
let publishBehavior: "request" | "default-create-false" | "preserve-existing" = "request";
|
||||
if (finalIsPublished === undefined) {
|
||||
if (id) {
|
||||
const existingPage = await prisma.page.findUnique({
|
||||
where: { id: Number(id) },
|
||||
select: { isPublished: true },
|
||||
});
|
||||
if (!existingPage) {
|
||||
set.status = 404;
|
||||
return {
|
||||
success: false,
|
||||
operation,
|
||||
pageId: Number(id),
|
||||
slug,
|
||||
title,
|
||||
error: "Page not found",
|
||||
diagnostic: "The requested page ID does not exist.",
|
||||
};
|
||||
}
|
||||
finalIsPublished = existingPage.isPublished;
|
||||
publishBehavior = "preserve-existing";
|
||||
} else {
|
||||
finalIsPublished = false;
|
||||
publishBehavior = "default-create-false";
|
||||
}
|
||||
}
|
||||
|
||||
const page = id
|
||||
@@ -205,11 +279,16 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
slug: page.slug,
|
||||
title: page.title,
|
||||
isPublished: page.isPublished,
|
||||
publishBehavior,
|
||||
page,
|
||||
diagnostic:
|
||||
operation === "create"
|
||||
? "Page created successfully."
|
||||
: "Page updated successfully.",
|
||||
? publishBehavior === "default-create-false"
|
||||
? "Page created successfully. Defaulted to isPublished=false because the field was omitted."
|
||||
: "Page created successfully."
|
||||
: publishBehavior === "preserve-existing"
|
||||
? "Page updated successfully. Existing publish state was preserved because isPublished was omitted."
|
||||
: "Page updated successfully.",
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("[Pages] Error:", err);
|
||||
@@ -234,8 +313,6 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
slug: t.String(),
|
||||
title: t.String(),
|
||||
isPublished: t.Optional(t.Boolean()),
|
||||
status: t.Optional(t.String()),
|
||||
metadata: t.Optional(t.Any()),
|
||||
}),
|
||||
},
|
||||
)
|
||||
@@ -308,6 +385,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
return {
|
||||
error: "Validation failed",
|
||||
details: validationResult.issues,
|
||||
validationIssues: summarizeValidationIssues(validationResult.issues || []),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -380,6 +458,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
return {
|
||||
error: "Validation failed",
|
||||
details: validationResult.issues,
|
||||
validationIssues: summarizeValidationIssues(validationResult.issues || []),
|
||||
};
|
||||
}
|
||||
finalPayload = validationResult.data;
|
||||
|
||||
Reference in new issue
Block a user