133 lines
4.2 KiB
TypeScript
133 lines
4.2 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 { Embeds } from "@/types/requests/embeds"
|
|
import {
|
|
ContentEntries,
|
|
GetAccountPageData,
|
|
} from "@/types/requests/myPages/accountpage"
|
|
import { Edges } from "@/types/requests/utils/edges"
|
|
import { RTEDocument } from "@/types/rte/node"
|
|
|
|
export const accountPageQueryRouter = router({
|
|
getOverview: publicProcedure
|
|
.input(getAccountPageInput)
|
|
.query(async ({ input }) => {
|
|
try {
|
|
const url = "/my-pages/overview"
|
|
const response = await request<GetAccountPageData>(GetAccountPage, {
|
|
locale: input.lang,
|
|
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:
|
|
case ContentEntries.AccountPageContentShortcuts:
|
|
return block
|
|
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()
|
|
}
|
|
}),
|
|
getBenefits: publicProcedure
|
|
.input(getAccountPageInput)
|
|
.query(async ({ input }) => {
|
|
try {
|
|
const url = "/my-pages/benefits"
|
|
const response = await request<GetAccountPageData>(GetAccountPage, {
|
|
locale: input.lang,
|
|
url,
|
|
})
|
|
|
|
if (!response.data) {
|
|
throw badRequestError()
|
|
}
|
|
|
|
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedAccountPage.success) {
|
|
throw badRequestError()
|
|
}
|
|
|
|
const content =
|
|
validatedAccountPage.data.all_account_page.items[0].content.map(
|
|
(block) => {
|
|
switch (block.__typename) {
|
|
case ContentEntries.AccountPageContentDynamicContent:
|
|
case ContentEntries.AccountPageContentShortcuts:
|
|
return block
|
|
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 Benefits Error`)
|
|
console.error(error)
|
|
throw internalServerError()
|
|
}
|
|
}),
|
|
})
|