84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
import GetAccountPage from "@/lib/graphql/Query/AccountPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
|
import { publicProcedure, router } from "@/server/trpc"
|
|
|
|
import { getAccountPageInput } from "./input"
|
|
import { type AccountPage, validateAccountPageSchema } from "./output"
|
|
|
|
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
|
|
import { Embeds } from "@/types/requests/embeds"
|
|
import { Edges } from "@/types/requests/utils/edges"
|
|
import { RTEDocument } from "@/types/rte/node"
|
|
|
|
export const accountPageQueryRouter = router({
|
|
get: publicProcedure.input(getAccountPageInput).query(async ({ input }) => {
|
|
try {
|
|
const response = await request<AccountPage>(GetAccountPage, {
|
|
locale: input.lang,
|
|
url: input.url,
|
|
})
|
|
|
|
if (!response.data) {
|
|
throw badRequestError()
|
|
}
|
|
|
|
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedAccountPage.success) {
|
|
throw badRequestError()
|
|
}
|
|
// TODO: Make returned data nicer
|
|
const content =
|
|
validatedAccountPage.data.all_account_page.items[0].content.map(
|
|
(block) => {
|
|
switch (block.__typename) {
|
|
case ContentEntries.AccountPageContentDynamicContent:
|
|
return block
|
|
case ContentEntries.AccountPageContentShortcuts:
|
|
return {
|
|
...block,
|
|
shortcuts: {
|
|
...block.shortcuts,
|
|
shortcuts: block.shortcuts.shortcuts.map((shortcut) => ({
|
|
text: shortcut.text,
|
|
openInNewTab: shortcut.open_in_new_tab,
|
|
...shortcut.linkConnection.edges[0].node,
|
|
url:
|
|
shortcut.linkConnection.edges[0].node.original_url ||
|
|
`/${shortcut.linkConnection.edges[0].node.system.locale}${shortcut.linkConnection.edges[0].node.url}`,
|
|
})),
|
|
},
|
|
}
|
|
case ContentEntries.AccountPageContentTextContent:
|
|
return {
|
|
...block,
|
|
text_content: {
|
|
content: {
|
|
json: block.text_content.content.json as RTEDocument,
|
|
embedded_itemsConnection: block.text_content.content
|
|
.embedded_itemsConnection as Edges<Embeds>,
|
|
},
|
|
},
|
|
}
|
|
default:
|
|
return null
|
|
}
|
|
}
|
|
)
|
|
|
|
const accountPage = {
|
|
...validatedAccountPage.data.all_account_page.items[0],
|
|
content,
|
|
} as AccountPage
|
|
return accountPage
|
|
} catch (error) {
|
|
console.info(`Get Account Page Overview Error`)
|
|
console.error(error)
|
|
throw internalServerError()
|
|
}
|
|
}),
|
|
})
|