fix: refs for account page
This commit is contained in:
@@ -1,10 +1,26 @@
|
||||
import GetAccountPage from "@/lib/graphql/Query/AccountPage.graphql"
|
||||
import {
|
||||
GetAccountPage,
|
||||
GetAccountPageRefs,
|
||||
} 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 { removeEmptyObjects } from "@/utils/contentType"
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
generateTag,
|
||||
generateTags,
|
||||
} from "@/utils/generateTag"
|
||||
|
||||
import { getAccountPageInput } from "./input"
|
||||
import { type AccountPage, validateAccountPageSchema } from "./output"
|
||||
import {
|
||||
type AccountPage,
|
||||
AccountPageRefsDataRaw,
|
||||
validateAccountPageRefsSchema,
|
||||
validateAccountPageSchema,
|
||||
} from "./output"
|
||||
import { getConnections } from "./utils"
|
||||
|
||||
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
|
||||
import { Embeds } from "@/types/requests/embeds"
|
||||
@@ -14,10 +30,55 @@ 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,
|
||||
})
|
||||
const { lang, url } = input
|
||||
|
||||
const refsResponse = await request<AccountPageRefsDataRaw>(
|
||||
GetAccountPageRefs,
|
||||
{
|
||||
locale: lang,
|
||||
url,
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, "account_page")],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
console.error("Bad response for `GetAccountPageRefs`")
|
||||
console.error({ refsResponse })
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const cleanedData = removeEmptyObjects(refsResponse.data)
|
||||
|
||||
const validatedAccountPageRefs =
|
||||
validateAccountPageRefsSchema.safeParse(cleanedData)
|
||||
if (!validatedAccountPageRefs.success) {
|
||||
console.error("Bad validation for `GetAccountPageRefs`")
|
||||
console.error(validatedAccountPageRefs.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedAccountPageRefs.data)
|
||||
|
||||
const tags = generateTags(lang, connections)
|
||||
|
||||
tags.push(
|
||||
generateTag(
|
||||
lang,
|
||||
validatedAccountPageRefs.data.all_account_page.items[0].system.uid
|
||||
)
|
||||
)
|
||||
const response = await request<AccountPage>(
|
||||
GetAccountPage,
|
||||
{
|
||||
locale: lang,
|
||||
url,
|
||||
},
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw badRequestError()
|
||||
|
||||
Reference in New Issue
Block a user