UNPKG

@paroicms/server

Version:
82 lines 3.15 kB
import { createNoTracker } from "@paroicms/internal-server-lib"; import { encodeLNodeId } from "@paroicms/public-anywhere-lib"; import { type } from "arktype"; import { getNodeTypeLabel } from "../../helpers/label-translator.helper.js"; import { getBreadcrumb } from "./lnode.queries.js"; const RedirectLNodeRowAT = type({ nodeId: "string|number", typeName: "string", "+": "reject", }).pipe((r) => ({ nodeId: String(r.nodeId), typeName: r.typeName, })); export async function searchRedirectLNodes(siteContext, input) { const { language, queryString, offset, limit } = input; const redirectTypeNames = collectRedirectTypeNames(siteContext); if (redirectTypeNames.length === 0) return { total: 0, offset, items: [] }; const words = queryString.split(/\s+/).filter((w) => w.length >= 2); if (words.length === 0) return { total: 0, offset, items: [] }; const matchedTypeNames = redirectTypeNames.filter((typeName) => { const label = getNodeTypeLabel(siteContext.siteSchema, { typeName, language }); return words.every((w) => label.toLowerCase().includes(w.toLowerCase())); }); if (matchedTypeNames.length === 0) return { total: 0, offset, items: [] }; const baseQuery = siteContext .cn("PaNode as n") .innerJoin("PaLNode as l", function () { this.on("l.nodeId", "n.id").andOn("l.language", "=", siteContext.cn.raw("?", [language])); }) .whereIn("n.typeName", matchedTypeNames); const [{ total }] = await baseQuery.clone().count("* as total"); const rows = await baseQuery .clone() .select("n.id as nodeId", "n.typeName") .orderBy("n.id", "asc") .limit(limit) .offset(offset); const items = []; for (const row of rows) { const validatedRow = RedirectLNodeRowAT.assert(row); const title = getNodeTypeLabel(siteContext.siteSchema, { typeName: validatedRow.typeName, language, }); const breadcrumb = await getBreadcrumb(siteContext, createNoTracker(), { nodeId: validatedRow.nodeId, language, }); items.push({ nodeId: validatedRow.nodeId, language, typeName: validatedRow.typeName, title, breadcrumb: mapBreadcrumb(breadcrumb), }); } return { items, total: Number(total), offset }; } function collectRedirectTypeNames(siteContext) { const result = []; for (const [typeName, nodeType] of Object.entries(siteContext.siteSchema.nodeTypes)) { if (nodeType.kind === "document" && nodeType.documentKind === "routing" && nodeType.redirectTo !== undefined) { result.push(typeName); } } return result; } function mapBreadcrumb(breadcrumb) { return breadcrumb.map((item) => ({ language: item.documentId.language, nodeId: item.documentId.nodeId, typeName: item.typeName, documentId: encodeLNodeId(item.documentId), title: item.title, })); } //# sourceMappingURL=redirect-lnode.queries.js.map