fix: improve accountPage tRPC query
This commit is contained in:
@@ -1,52 +1,82 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import GetAccountPage from "@/lib/graphql/Query/AccountPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { badRequestError } from "@/server/errors/trpc"
|
||||
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
|
||||
import { getAccountPageInput } from "./input"
|
||||
import { validateAccountPageSchema } from "./output"
|
||||
|
||||
import type { GetAccountPageData } from "@/types/requests/myPages/accountpage"
|
||||
|
||||
export const accountPageQueryRouter = router({
|
||||
getOverview: publicProcedure
|
||||
.input(z.object({ lang: z.nativeEnum(Lang) }))
|
||||
.input(getAccountPageInput)
|
||||
.query(async ({ input }) => {
|
||||
const url = "/my-pages/overview"
|
||||
const accountPage = await request<GetAccountPageData>(
|
||||
GetAccountPage,
|
||||
{
|
||||
locale: input.lang,
|
||||
url,
|
||||
},
|
||||
{
|
||||
tags: [`${url}-${input.lang}`],
|
||||
}
|
||||
)
|
||||
if (accountPage.data && accountPage.data.all_account_page.total) {
|
||||
return accountPage.data.all_account_page.items[0]
|
||||
}
|
||||
try {
|
||||
const url = "/my-pages/overview"
|
||||
const response = await request<GetAccountPageData>(
|
||||
GetAccountPage,
|
||||
{
|
||||
locale: input.lang,
|
||||
url,
|
||||
},
|
||||
{
|
||||
tags: [`${url}-${input.lang}`],
|
||||
}
|
||||
)
|
||||
|
||||
throw badRequestError()
|
||||
if (!response.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedAccountPage.success) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return response.data.all_account_page.items[0]
|
||||
} catch (error) {
|
||||
console.info(`Get Account Page Overview Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
}),
|
||||
getBenefits: publicProcedure
|
||||
.input(z.object({ lang: z.nativeEnum(Lang) }))
|
||||
.input(getAccountPageInput)
|
||||
.query(async ({ input }) => {
|
||||
const url = "/my-pages/benefits"
|
||||
const accountPage = await request<GetAccountPageData>(
|
||||
GetAccountPage,
|
||||
{
|
||||
locale: input.lang,
|
||||
url,
|
||||
},
|
||||
{
|
||||
tags: [`${url}-${input.lang}`],
|
||||
}
|
||||
)
|
||||
if (accountPage.data && accountPage.data.all_account_page.total) {
|
||||
return accountPage.data.all_account_page.items[0]
|
||||
}
|
||||
try {
|
||||
const url = "/my-pages/benefits"
|
||||
const response = await request<GetAccountPageData>(
|
||||
GetAccountPage,
|
||||
{
|
||||
locale: input.lang,
|
||||
url,
|
||||
},
|
||||
{
|
||||
tags: [`${url}-${input.lang}`],
|
||||
}
|
||||
)
|
||||
|
||||
throw badRequestError()
|
||||
if (!response.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedAccountPage.success) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return response.data.all_account_page.items[0]
|
||||
} catch (error) {
|
||||
console.info(`Get Account Page Benefits Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user