130 lines
3.8 KiB
TypeScript
130 lines
3.8 KiB
TypeScript
import {
|
|
GetNavigationMyPages,
|
|
GetNavigationMyPagesRefs,
|
|
} from "@/lib/graphql/Query/NavigationMyPages.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
|
import { publicProcedure, router } from "@/server/trpc"
|
|
|
|
import {
|
|
generateRefsResponseTag,
|
|
generateTag,
|
|
generateTags,
|
|
} from "@/utils/generateTag"
|
|
import { removeMultipleSlashes } from "@/utils/url"
|
|
|
|
import { getNavigationInputSchema } from "./input"
|
|
import {
|
|
getNavigationSchema,
|
|
navigationPayloadSchema,
|
|
navigationRefsPayloadSchema,
|
|
} from "./output"
|
|
import { getConnections } from "./utils"
|
|
|
|
import type {
|
|
GetNavigationMyPagesData,
|
|
GetNavigationMyPagesRefsData,
|
|
MenuItem,
|
|
NavigationItem,
|
|
} from "@/types/requests/myPages/navigation"
|
|
|
|
export function mapMenuItems(navigationItems: NavigationItem[]) {
|
|
return navigationItems.map(({ item }): MenuItem => {
|
|
const { node } = item.pageConnection.edges[0]
|
|
|
|
const menuItem: MenuItem = {
|
|
lang: node.system.locale,
|
|
linkText: item.link_text || node.title,
|
|
uid: node.system.uid,
|
|
url: removeMultipleSlashes(`/${node.system.locale}/${node.url}`),
|
|
}
|
|
|
|
if ("sub_items" in item) {
|
|
menuItem.subItems = mapMenuItems(item.sub_items)
|
|
}
|
|
|
|
return menuItem
|
|
})
|
|
}
|
|
|
|
export const navigationQueryRouter = router({
|
|
get: publicProcedure.input(getNavigationInputSchema).query(async function ({
|
|
input: lang,
|
|
}) {
|
|
try {
|
|
const refsResponse = await request<GetNavigationMyPagesRefsData>(
|
|
GetNavigationMyPagesRefs,
|
|
{ locale: lang },
|
|
{
|
|
next: {
|
|
tags: [generateRefsResponseTag(lang, "navigation_my_pages")],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!refsResponse.data) {
|
|
console.error("Bad response for `GetNavigationMyPagesRefs`")
|
|
console.error({ refsResponse })
|
|
throw internalServerError()
|
|
}
|
|
|
|
const validatedMyPagesNavigationRefs =
|
|
navigationRefsPayloadSchema.safeParse(refsResponse.data)
|
|
if (!validatedMyPagesNavigationRefs.success) {
|
|
console.error("Bad validation for `GetNavigationMyPagesRefs`")
|
|
console.error(validatedMyPagesNavigationRefs.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
const connections = getConnections(validatedMyPagesNavigationRefs.data)
|
|
const tags = generateTags(lang, connections)
|
|
const navigation =
|
|
validatedMyPagesNavigationRefs.data.all_navigation_my_pages.items[0]
|
|
tags.push(generateTag(lang, navigation.system.uid))
|
|
|
|
const response = await request<GetNavigationMyPagesData>(
|
|
GetNavigationMyPages,
|
|
{ locale: lang },
|
|
{ next: { tags } }
|
|
)
|
|
|
|
if (!response.data) {
|
|
console.error("Bad response for `GetNavigationMyPages`")
|
|
console.error({ input: lang })
|
|
console.error({ response })
|
|
throw internalServerError()
|
|
}
|
|
|
|
const validatedMyPagesNavigation = navigationPayloadSchema.safeParse(
|
|
response.data
|
|
)
|
|
if (!validatedMyPagesNavigation.success) {
|
|
console.error("Bad validation for `GetNavigationMyPages`")
|
|
console.error(validatedMyPagesNavigation.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
const menuItem =
|
|
validatedMyPagesNavigation.data.all_navigation_my_pages.items[0]
|
|
|
|
const nav = {
|
|
items: mapMenuItems(menuItem.items),
|
|
title: menuItem.title,
|
|
}
|
|
|
|
const validatedNav = getNavigationSchema.safeParse(nav)
|
|
if (!validatedNav.success) {
|
|
console.error("Bad validation for `getNavigationSchema`")
|
|
console.error(validatedNav.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
return validatedNav.data
|
|
} catch (error) {
|
|
console.info(`Get My Pages Navigation Error`)
|
|
console.error(error)
|
|
throw internalServerError()
|
|
}
|
|
}),
|
|
})
|