feat: improve structure and error handling
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { mergeRouters } from "@/server/trpc"
|
||||
|
||||
import { accountPageQueryRouter } from "./query"
|
||||
|
||||
export const accountPageRouter = mergeRouters(accountPageQueryRouter)
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export const getAccountPageInput = z.object({
|
||||
url: z.string(),
|
||||
lang: z.nativeEnum(Lang),
|
||||
})
|
||||
@@ -120,24 +120,18 @@ const accountPageContentItem = z.discriminatedUnion("__typename", [
|
||||
])
|
||||
|
||||
export const validateAccountPageSchema = z.object({
|
||||
all_account_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
content: z.array(accountPageContentItem),
|
||||
})
|
||||
),
|
||||
account_page: z.object({
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
content: z.array(accountPageContentItem),
|
||||
}),
|
||||
})
|
||||
|
||||
type AccountPageDataRaw = z.infer<typeof validateAccountPageSchema>
|
||||
export type AccountPageDataRaw = z.infer<typeof validateAccountPageSchema>
|
||||
|
||||
type AccountPageRaw = AccountPageDataRaw["all_account_page"]["items"][0]
|
||||
type AccountPageRaw = AccountPageDataRaw["account_page"]
|
||||
|
||||
export type AccountPage = Omit<AccountPageRaw, "content"> & {
|
||||
url: string
|
||||
title: string
|
||||
content: AccountPageContentItem[]
|
||||
}
|
||||
|
||||
@@ -182,16 +176,12 @@ const accountPageContentItemRefs = z.discriminatedUnion("__typename", [
|
||||
])
|
||||
|
||||
export const validateAccountPageRefsSchema = z.object({
|
||||
all_account_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
content: z.array(accountPageContentItemRefs),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
account_page: z.object({
|
||||
content: z.array(accountPageContentItemRefs),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@ import {
|
||||
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 { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
} from "@/utils/generateTag"
|
||||
|
||||
import { removeEmptyObjects } from "../../utils"
|
||||
import { getAccountPageInput } from "./input"
|
||||
import {
|
||||
type AccountPage,
|
||||
AccountPageRefsDataRaw,
|
||||
@@ -28,122 +27,108 @@ 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 { lang, url } = input
|
||||
get: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const refsResponse = await request<AccountPageRefsDataRaw>(
|
||||
GetAccountPageRefs,
|
||||
{
|
||||
locale: lang,
|
||||
url,
|
||||
const refsResponse = await request<AccountPageRefsDataRaw>(
|
||||
GetAccountPageRefs,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, uid)],
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, "account_page")],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
console.error("Bad response for `GetAccountPageRefs`")
|
||||
console.error({ refsResponse })
|
||||
throw internalServerError()
|
||||
}
|
||||
)
|
||||
|
||||
// Remove empty objects from a fetched content type. Needed since
|
||||
// Contentstack returns empty objects for all non queried blocks in modular blocks.
|
||||
// This is an ongoing support case in Contentstack, ticker number #00031579
|
||||
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<AccountPageRefsDataRaw>(
|
||||
GetAccountPage,
|
||||
{
|
||||
locale: lang,
|
||||
url,
|
||||
},
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedAccountPage.success) {
|
||||
console.info(`Get Account Page Validation Error`)
|
||||
console.error(validatedAccountPage.error)
|
||||
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()
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
}
|
||||
|
||||
// Remove empty objects from a fetched content type. Needed since
|
||||
// Contentstack returns empty objects for all non queried blocks in modular blocks.
|
||||
// This is an ongoing support case in Contentstack, ticker number #00031579
|
||||
const cleanedData = removeEmptyObjects(refsResponse.data)
|
||||
|
||||
const validatedAccountPageRefs =
|
||||
validateAccountPageRefsSchema.safeParse(cleanedData)
|
||||
if (!validatedAccountPageRefs.success) {
|
||||
throw internalServerError(validatedAccountPageRefs.error)
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedAccountPageRefs.data)
|
||||
|
||||
const tags = [
|
||||
generateTags(lang, connections),
|
||||
generateTag(lang, validatedAccountPageRefs.data.account_page.system.uid),
|
||||
].flat()
|
||||
|
||||
const response = await request<AccountPageRefsDataRaw>(
|
||||
GetAccountPage,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedAccountPage.success) {
|
||||
throw internalServerError(validatedAccountPage.error)
|
||||
}
|
||||
|
||||
// TODO: Make returned data nicer
|
||||
const content = validatedAccountPage.data.account_page.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.account_page,
|
||||
content,
|
||||
} as AccountPage
|
||||
|
||||
return accountPage
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -6,28 +6,26 @@ import type { NodeRefs } from "@/types/requests/utils/refs"
|
||||
|
||||
export function getConnections(refs: AccountPageRefsDataRaw) {
|
||||
const connections: Edges<NodeRefs>[] = []
|
||||
refs.all_account_page.items.forEach((ref) => {
|
||||
if (ref.content) {
|
||||
ref.content.forEach((item) => {
|
||||
switch (item.__typename) {
|
||||
case ContentEntries.AccountPageContentShortcuts: {
|
||||
item.shortcuts.shortcuts.forEach((shortcut) => {
|
||||
if (shortcut.linkConnection.edges.length) {
|
||||
connections.push(shortcut.linkConnection)
|
||||
}
|
||||
})
|
||||
break
|
||||
}
|
||||
case ContentEntries.AccountPageContentDynamicContent: {
|
||||
if (item.dynamic_content.link.linkConnection.edges.length) {
|
||||
connections.push(item.dynamic_content.link.linkConnection)
|
||||
if (refs.account_page.content) {
|
||||
refs.account_page.content.forEach((item) => {
|
||||
switch (item.__typename) {
|
||||
case ContentEntries.AccountPageContentShortcuts: {
|
||||
item.shortcuts.shortcuts.forEach((shortcut) => {
|
||||
if (shortcut.linkConnection.edges.length) {
|
||||
connections.push(shortcut.linkConnection)
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
case ContentEntries.AccountPageContentDynamicContent: {
|
||||
if (item.dynamic_content.link.linkConnection.edges.length) {
|
||||
connections.push(item.dynamic_content.link.linkConnection)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return connections
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export const getBreadcrumbsInput = z.object({
|
||||
href: z.string().min(1, { message: "href is required" }),
|
||||
locale: z.nativeEnum(Lang),
|
||||
})
|
||||
@@ -3,8 +3,12 @@ import {
|
||||
GetMyPagesBreadcrumbsRefs,
|
||||
} from "@/lib/graphql/Query/BreadcrumbsMyPages.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
import {
|
||||
badRequestError,
|
||||
internalServerError,
|
||||
notFound,
|
||||
} from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
@@ -13,7 +17,6 @@ import {
|
||||
} from "@/utils/generateTag"
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import { getBreadcrumbsInput } from "./input"
|
||||
import {
|
||||
getBreadcrumbsSchema,
|
||||
validateBreadcrumbsConstenstackSchema,
|
||||
@@ -27,98 +30,81 @@ import type {
|
||||
} from "@/types/requests/myPages/breadcrumbs"
|
||||
|
||||
export const breadcrumbsQueryRouter = router({
|
||||
get: publicProcedure.input(getBreadcrumbsInput).query(async ({ input }) => {
|
||||
try {
|
||||
const refsResponse = await request<GetMyPagesBreadcrumbsRefsData>(
|
||||
GetMyPagesBreadcrumbsRefs,
|
||||
{ locale: input.locale, url: input.href },
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(input.locale, input.href, affix)],
|
||||
},
|
||||
get: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const refsResponse = await request<GetMyPagesBreadcrumbsRefsData>(
|
||||
GetMyPagesBreadcrumbsRefs,
|
||||
{ locale: ctx.lang, url: ctx.pathname },
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(ctx.lang, ctx.pathname, affix)],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
}
|
||||
|
||||
const validatedRefsData =
|
||||
validateBreadcrumbsRefsConstenstackSchema.safeParse(refsResponse.data)
|
||||
if (!validatedRefsData.success) {
|
||||
throw internalServerError(validatedRefsData.error)
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedRefsData.data)
|
||||
const tags = generateTags(ctx.lang, connections)
|
||||
const page = validatedRefsData.data.all_account_page.items[0]
|
||||
tags.push(generateTag(ctx.lang, page.system.uid, affix))
|
||||
|
||||
const response = await request<GetMyPagesBreadcrumbsData>(
|
||||
GetMyPagesBreadcrumbs,
|
||||
{ locale: ctx.lang, url: ctx.pathname },
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedBreadcrumbsData =
|
||||
validateBreadcrumbsConstenstackSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedBreadcrumbsData.success) {
|
||||
throw internalServerError(validatedBreadcrumbsData.error)
|
||||
}
|
||||
|
||||
const parentBreadcrumbs =
|
||||
validatedBreadcrumbsData.data.all_account_page.items[0].breadcrumbs.parentsConnection.edges.map(
|
||||
(breadcrumb) => {
|
||||
return {
|
||||
href: removeMultipleSlashes(
|
||||
`/${breadcrumb.node.system.locale}/${breadcrumb.node.url}`
|
||||
),
|
||||
title: breadcrumb.node.breadcrumbs.title,
|
||||
uid: breadcrumb.node.system.uid,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
console.error("Bad response for `GetMyPagesBreadcrumbsRefs`")
|
||||
console.error({ refsResponse })
|
||||
throw internalServerError()
|
||||
}
|
||||
const pageBreadcrumb =
|
||||
validatedBreadcrumbsData.data.all_account_page.items.map((breadcrumb) => {
|
||||
return {
|
||||
title: breadcrumb.breadcrumbs.title,
|
||||
uid: breadcrumb.system.uid,
|
||||
}
|
||||
})
|
||||
|
||||
const validatedRefsData =
|
||||
validateBreadcrumbsRefsConstenstackSchema.safeParse(refsResponse.data)
|
||||
if (!validatedRefsData.success) {
|
||||
console.info("Bad validation for `GetMyPagesBreadcrumbsRefs`")
|
||||
console.error(validatedRefsData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
const breadcrumbs = [
|
||||
homeBreadcrumbs[ctx.lang],
|
||||
parentBreadcrumbs,
|
||||
pageBreadcrumb,
|
||||
].flat()
|
||||
|
||||
const connections = getConnections(validatedRefsData.data)
|
||||
const tags = generateTags(input.locale, connections)
|
||||
const page = validatedRefsData.data.all_account_page.items[0]
|
||||
tags.push(generateTag(input.locale, page.system.uid, affix))
|
||||
|
||||
const response = await request<GetMyPagesBreadcrumbsData>(
|
||||
GetMyPagesBreadcrumbs,
|
||||
{ locale: input.locale, url: input.href },
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
console.error("Bad response for `GetMyPagesBreadcrumbs`")
|
||||
console.error({ input })
|
||||
console.error({ response })
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const validatedBreadcrumbsData =
|
||||
validateBreadcrumbsConstenstackSchema.safeParse(response.data)
|
||||
if (!validatedBreadcrumbsData.success) {
|
||||
console.error("Bad validation for `GetMyPagesBreadcrumbs`")
|
||||
console.error(validatedBreadcrumbsData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const parentBreadcrumbs =
|
||||
validatedBreadcrumbsData.data.all_account_page.items[0].breadcrumbs.parentsConnection.edges.map(
|
||||
(breadcrumb) => {
|
||||
return {
|
||||
href: removeMultipleSlashes(
|
||||
`/${breadcrumb.node.system.locale}/${breadcrumb.node.url}`
|
||||
),
|
||||
title: breadcrumb.node.breadcrumbs.title,
|
||||
uid: breadcrumb.node.system.uid,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const pageBreadcrumb =
|
||||
validatedBreadcrumbsData.data.all_account_page.items.map(
|
||||
(breadcrumb) => {
|
||||
return {
|
||||
title: breadcrumb.breadcrumbs.title,
|
||||
uid: breadcrumb.system.uid,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const breadcrumbs = [
|
||||
homeBreadcrumbs[input.locale],
|
||||
parentBreadcrumbs,
|
||||
pageBreadcrumb,
|
||||
].flat()
|
||||
const validatedBreadcrumbs = getBreadcrumbsSchema.safeParse(breadcrumbs)
|
||||
if (!validatedBreadcrumbs.success) {
|
||||
console.info("Bad validation for `validatedBreadcrumbs`")
|
||||
console.error(validatedBreadcrumbs.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return validatedBreadcrumbs.data
|
||||
} catch (error) {
|
||||
console.info(`Get My Pages Breadcrumbs Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
const validatedBreadcrumbs = getBreadcrumbsSchema.safeParse(breadcrumbs)
|
||||
if (!validatedBreadcrumbs.success) {
|
||||
throw internalServerError(validatedBreadcrumbs.error)
|
||||
}
|
||||
|
||||
return validatedBreadcrumbs.data
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export const getConfigInput = z.object({ lang: z.nativeEnum(Lang) })
|
||||
@@ -1,34 +1,30 @@
|
||||
import { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { badRequestError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
import { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, router } from "@/server/trpc"
|
||||
|
||||
import { getConfigInput } from "./input"
|
||||
import { type ContactConfigData, validateContactConfigSchema } from "./output"
|
||||
|
||||
export const contactConfigQueryRouter = router({
|
||||
get: publicProcedure.input(getConfigInput).query(async ({ input }) => {
|
||||
try {
|
||||
const contactConfig = await request<ContactConfigData>(GetContactConfig, {
|
||||
locale: input.lang,
|
||||
})
|
||||
get: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const { lang } = ctx
|
||||
|
||||
if (!contactConfig.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
const response = await request<ContactConfigData>(GetContactConfig, {
|
||||
locale: lang,
|
||||
})
|
||||
|
||||
const validatedContactConfigConfig =
|
||||
validateContactConfigSchema.safeParse(contactConfig.data)
|
||||
|
||||
if (!validatedContactConfigConfig.success) {
|
||||
console.error(validatedContactConfigConfig.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
throw badRequestError()
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedContactConfigConfig = validateContactConfigSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedContactConfigConfig.success) {
|
||||
throw internalServerError(validatedContactConfigConfig.error)
|
||||
}
|
||||
|
||||
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
const langs = Object.keys(Lang) as [keyof typeof Lang]
|
||||
|
||||
export const getLoyaltyPageInput = z.object({
|
||||
href: z.string().min(1, { message: "href is required" }),
|
||||
locale: z.nativeEnum(Lang),
|
||||
})
|
||||
@@ -168,16 +168,12 @@ const loyaltyPageSidebarItem = z.discriminatedUnion("__typename", [
|
||||
])
|
||||
|
||||
export const validateLoyaltyPageSchema = z.object({
|
||||
all_loyalty_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
title: z.string(),
|
||||
heading: z.string().nullable(),
|
||||
blocks: z.array(loyaltyPageBlockItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarItem).nullable(),
|
||||
system: z.object({ uid: z.string() }),
|
||||
})
|
||||
),
|
||||
loyalty_page: z.object({
|
||||
title: z.string(),
|
||||
heading: z.string().nullable(),
|
||||
blocks: z.array(loyaltyPageBlockItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarItem).nullable(),
|
||||
system: z.object({ uid: z.string() }),
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -257,7 +253,7 @@ export type Sidebar = JoinLoyaltyContact | RteSidebarContent
|
||||
|
||||
export type LoyaltyPageDataRaw = z.infer<typeof validateLoyaltyPageSchema>
|
||||
|
||||
type LoyaltyPageRaw = LoyaltyPageDataRaw["all_loyalty_page"]["items"][0]
|
||||
type LoyaltyPageRaw = LoyaltyPageDataRaw["loyalty_page"]
|
||||
|
||||
export type LoyaltyPage = Omit<LoyaltyPageRaw, "blocks" | "sidebar"> & {
|
||||
blocks: Block[]
|
||||
@@ -342,17 +338,13 @@ const loyaltyPageSidebarRefsItem = z.discriminatedUnion("__typename", [
|
||||
])
|
||||
|
||||
export const validateLoyaltyPageRefsSchema = z.object({
|
||||
all_loyalty_page: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
blocks: z.array(loyaltyPageBlocRefsItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarRefsItem).nullable(),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
loyalty_page: z.object({
|
||||
blocks: z.array(loyaltyPageBlocRefsItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarRefsItem).nullable(),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ import {
|
||||
} from "@/lib/graphql/Query/LoyaltyPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { _ } from "@/lib/translation"
|
||||
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
import { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, publicProcedure, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
} from "@/utils/generateTag"
|
||||
|
||||
import { removeEmptyObjects } from "../../utils"
|
||||
import { getLoyaltyPageInput } from "./input"
|
||||
import {
|
||||
type LoyaltyPage,
|
||||
type LoyaltyPageDataRaw,
|
||||
@@ -33,184 +32,160 @@ import { Edges } from "@/types/requests/utils/edges"
|
||||
import { RTEDocument } from "@/types/rte/node"
|
||||
|
||||
export const loyaltyPageQueryRouter = router({
|
||||
get: publicProcedure.input(getLoyaltyPageInput).query(async ({ input }) => {
|
||||
try {
|
||||
const { locale } = input
|
||||
get: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const refsResponse = await request<LoyaltyPageRefsDataRaw>(
|
||||
GetLoyaltyPageRefs,
|
||||
{
|
||||
locale,
|
||||
url: input.href,
|
||||
const refsResponse = await request<LoyaltyPageRefsDataRaw>(
|
||||
GetLoyaltyPageRefs,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, uid)],
|
||||
},
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(locale, "loyalty_page")],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
console.error("Bad response for `GetLoyaltyPageRefs`")
|
||||
console.error({ refsResponse })
|
||||
throw internalServerError()
|
||||
}
|
||||
)
|
||||
|
||||
// Remove empty objects from a fetched content type. Needed since
|
||||
// Contentstack returns empty objects for all non queried blocks in modular blocks.
|
||||
// This is an ongoing support case in Contentstack, ticker number #00031579
|
||||
const cleanedData = removeEmptyObjects(refsResponse.data)
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
}
|
||||
|
||||
const validatedLoyaltyPageRefs =
|
||||
validateLoyaltyPageRefsSchema.safeParse(cleanedData)
|
||||
if (!validatedLoyaltyPageRefs.success) {
|
||||
console.error("Bad validation for `GetLoyaltyPageRefs`")
|
||||
console.error(validatedLoyaltyPageRefs.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
// Remove empty objects from a fetched content type. Needed since
|
||||
// Contentstack returns empty objects for all non queried blocks in modular blocks.
|
||||
// This is an ongoing support case in Contentstack, ticker number #00031579
|
||||
const cleanedData = removeEmptyObjects(refsResponse.data)
|
||||
|
||||
const connections = getConnections(validatedLoyaltyPageRefs.data)
|
||||
const validatedLoyaltyPageRefs =
|
||||
validateLoyaltyPageRefsSchema.safeParse(cleanedData)
|
||||
if (!validatedLoyaltyPageRefs.success) {
|
||||
throw internalServerError(validatedLoyaltyPageRefs.error)
|
||||
}
|
||||
|
||||
const tags = generateTags(locale, connections)
|
||||
const connections = getConnections(validatedLoyaltyPageRefs.data)
|
||||
|
||||
tags.push(
|
||||
generateTag(
|
||||
locale,
|
||||
validatedLoyaltyPageRefs.data.all_loyalty_page.items[0].system.uid
|
||||
)
|
||||
)
|
||||
const tags = [
|
||||
generateTags(lang, connections),
|
||||
generateTag(lang, validatedLoyaltyPageRefs.data.loyalty_page.system.uid),
|
||||
].flat()
|
||||
|
||||
const loyaltyPageRes = await request<LoyaltyPageDataRaw>(
|
||||
GetLoyaltyPage,
|
||||
{
|
||||
locale,
|
||||
url: input.href,
|
||||
},
|
||||
{ next: { tags } }
|
||||
)
|
||||
const response = await request<LoyaltyPageDataRaw>(
|
||||
GetLoyaltyPage,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!loyaltyPageRes.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedLoyaltyPage = validateLoyaltyPageSchema.safeParse(
|
||||
loyaltyPageRes.data
|
||||
)
|
||||
const validatedLoyaltyPage = validateLoyaltyPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
if (!validatedLoyaltyPage.success) {
|
||||
throw internalServerError(validatedLoyaltyPage.error)
|
||||
}
|
||||
|
||||
if (!validatedLoyaltyPage.success) {
|
||||
console.error("Bad validation for `validatedLoyaltyPage`")
|
||||
console.error(validatedLoyaltyPage.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const sidebar = validatedLoyaltyPage.data.all_loyalty_page.items[0]
|
||||
.sidebar
|
||||
? validatedLoyaltyPage.data.all_loyalty_page.items[0].sidebar.map(
|
||||
(block) => {
|
||||
if (
|
||||
block.__typename ==
|
||||
SidebarTypenameEnum.LoyaltyPageSidebarContent
|
||||
) {
|
||||
return {
|
||||
...block,
|
||||
content: {
|
||||
content: {
|
||||
json: block.content.content.json as RTEDocument,
|
||||
embedded_itemsConnection: block.content.content
|
||||
.embedded_itemsConnection as Edges<Embeds>,
|
||||
},
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return block
|
||||
}
|
||||
const sidebar = validatedLoyaltyPage.data.loyalty_page.sidebar
|
||||
? validatedLoyaltyPage.data.loyalty_page.sidebar.map((block) => {
|
||||
if (
|
||||
block.__typename == SidebarTypenameEnum.LoyaltyPageSidebarContent
|
||||
) {
|
||||
return {
|
||||
...block,
|
||||
content: {
|
||||
content: {
|
||||
json: block.content.content.json as RTEDocument,
|
||||
embedded_itemsConnection: block.content.content
|
||||
.embedded_itemsConnection as Edges<Embeds>,
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
: null
|
||||
} else {
|
||||
return block
|
||||
}
|
||||
})
|
||||
: null
|
||||
|
||||
const blocks = validatedLoyaltyPage.data.all_loyalty_page.items[0].blocks
|
||||
? validatedLoyaltyPage.data.all_loyalty_page.items[0].blocks.map(
|
||||
(block) => {
|
||||
switch (block.__typename) {
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid:
|
||||
return {
|
||||
...block,
|
||||
card_grid: {
|
||||
...block.card_grid,
|
||||
cards: block.card_grid.cards.map((card) => {
|
||||
return {
|
||||
...card,
|
||||
link: card.referenceConnection.totalCount
|
||||
? {
|
||||
href: `/${card.referenceConnection.edges[0].node.system.locale}${card.referenceConnection.edges[0].node.url}`,
|
||||
title: card.cta_text || _("Read more"),
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
}),
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent:
|
||||
return {
|
||||
...block,
|
||||
dynamic_content: {
|
||||
...block.dynamic_content,
|
||||
link: block.dynamic_content.link.pageConnection.totalCount
|
||||
const blocks = validatedLoyaltyPage.data.loyalty_page.blocks
|
||||
? validatedLoyaltyPage.data.loyalty_page.blocks.map((block) => {
|
||||
switch (block.__typename) {
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid:
|
||||
return {
|
||||
...block,
|
||||
card_grid: {
|
||||
...block.card_grid,
|
||||
cards: block.card_grid.cards.map((card) => {
|
||||
return {
|
||||
...card,
|
||||
link: card.referenceConnection.totalCount
|
||||
? {
|
||||
text: block.dynamic_content.link.text,
|
||||
href: `/${block.dynamic_content.link.pageConnection.edges[0].node.system.locale}${block.dynamic_content.link.pageConnection.edges[0].node.url}`,
|
||||
title:
|
||||
block.dynamic_content.link.pageConnection.edges[0]
|
||||
.node.title,
|
||||
href: `/${card.referenceConnection.edges[0].node.system.locale}${card.referenceConnection.edges[0].node.url}`,
|
||||
title: card.cta_text || _("Read more"),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent:
|
||||
return {
|
||||
...block,
|
||||
content: {
|
||||
content: {
|
||||
json: block.content.content.json as RTEDocument,
|
||||
embedded_itemsConnection: block.content.content
|
||||
.embedded_itemsConnection as Edges<Embeds>,
|
||||
},
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts:
|
||||
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.web
|
||||
?.original_url ||
|
||||
`/${shortcut.linkConnection.edges[0].node.system.locale}${shortcut.linkConnection.edges[0].node.url}`,
|
||||
})),
|
||||
},
|
||||
}
|
||||
default:
|
||||
return block
|
||||
}
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
: null
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent:
|
||||
return {
|
||||
...block,
|
||||
dynamic_content: {
|
||||
...block.dynamic_content,
|
||||
link: block.dynamic_content.link.pageConnection.totalCount
|
||||
? {
|
||||
text: block.dynamic_content.link.text,
|
||||
href: `/${block.dynamic_content.link.pageConnection.edges[0].node.system.locale}${block.dynamic_content.link.pageConnection.edges[0].node.url}`,
|
||||
title:
|
||||
block.dynamic_content.link.pageConnection.edges[0]
|
||||
.node.title,
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent:
|
||||
return {
|
||||
...block,
|
||||
content: {
|
||||
content: {
|
||||
json: block.content.content.json as RTEDocument,
|
||||
embedded_itemsConnection: block.content.content
|
||||
.embedded_itemsConnection as Edges<Embeds>,
|
||||
},
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts:
|
||||
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.web?.original_url ||
|
||||
`/${shortcut.linkConnection.edges[0].node.system.locale}${shortcut.linkConnection.edges[0].node.url}`,
|
||||
})),
|
||||
},
|
||||
}
|
||||
default:
|
||||
return block
|
||||
}
|
||||
})
|
||||
: null
|
||||
|
||||
const loyaltyPage = {
|
||||
...validatedLoyaltyPage.data.all_loyalty_page.items[0],
|
||||
blocks,
|
||||
sidebar,
|
||||
} as LoyaltyPage
|
||||
const loyaltyPage = {
|
||||
...validatedLoyaltyPage.data.loyalty_page,
|
||||
blocks,
|
||||
sidebar,
|
||||
} as LoyaltyPage
|
||||
|
||||
return loyaltyPage
|
||||
} catch (error) {
|
||||
console.info(`Get Loyalty Page Error`)
|
||||
console.error(error)
|
||||
throw badRequestError()
|
||||
}
|
||||
return loyaltyPage
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -6,49 +6,47 @@ import type { NodeRefs } from "@/types/requests/utils/refs"
|
||||
|
||||
export function getConnections(refs: LoyaltyPageRefsDataRaw) {
|
||||
const connections: Edges<NodeRefs>[] = []
|
||||
refs.all_loyalty_page.items.forEach((ref) => {
|
||||
if (ref.blocks) {
|
||||
ref.blocks.forEach((item) => {
|
||||
switch (item.__typename) {
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent: {
|
||||
if (item.content.content.embedded_itemsConnection.edges.length) {
|
||||
connections.push(item.content.content.embedded_itemsConnection)
|
||||
}
|
||||
break
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid: {
|
||||
item.card_grid.cards.forEach((card) => {
|
||||
if (card.referenceConnection.edges.length) {
|
||||
connections.push(card.referenceConnection)
|
||||
}
|
||||
})
|
||||
break
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts: {
|
||||
item.shortcuts.shortcuts.forEach((shortcut) => {
|
||||
if (shortcut.linkConnection.edges.length) {
|
||||
connections.push(shortcut.linkConnection)
|
||||
}
|
||||
})
|
||||
break
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent: {
|
||||
if (item.dynamic_content.link.pageConnection.edges.length) {
|
||||
connections.push(item.dynamic_content.link.pageConnection)
|
||||
}
|
||||
break
|
||||
if (refs.loyalty_page.blocks) {
|
||||
refs.loyalty_page.blocks.forEach((item) => {
|
||||
switch (item.__typename) {
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent: {
|
||||
if (item.content.content.embedded_itemsConnection.edges.length) {
|
||||
connections.push(item.content.content.embedded_itemsConnection)
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
if (ref.sidebar) {
|
||||
ref.sidebar?.forEach((item) => {
|
||||
if (item.content.content.embedded_itemsConnection.edges.length) {
|
||||
connections.push(item.content.content.embedded_itemsConnection)
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid: {
|
||||
item.card_grid.cards.forEach((card) => {
|
||||
if (card.referenceConnection.edges.length) {
|
||||
connections.push(card.referenceConnection)
|
||||
}
|
||||
})
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts: {
|
||||
item.shortcuts.shortcuts.forEach((shortcut) => {
|
||||
if (shortcut.linkConnection.edges.length) {
|
||||
connections.push(shortcut.linkConnection)
|
||||
}
|
||||
})
|
||||
break
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent: {
|
||||
if (item.dynamic_content.link.pageConnection.edges.length) {
|
||||
connections.push(item.dynamic_content.link.pageConnection)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
if (refs.loyalty_page.sidebar) {
|
||||
refs.loyalty_page.sidebar?.forEach((item) => {
|
||||
if (item.content.content.embedded_itemsConnection.edges.length) {
|
||||
connections.push(item.content.content.embedded_itemsConnection)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return connections
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { Lang } from "@/constants/languages";
|
||||
|
||||
export const getNavigationInputSchema = z.nativeEnum(Lang)
|
||||
@@ -66,7 +66,7 @@ export const navigationRefsPayloadSchema = z.object({
|
||||
return input.length === 1
|
||||
},
|
||||
{
|
||||
message: `Expected navigationRefsPayloadSchema 1 all_navigation_my_pages item`,
|
||||
message: `Expected all_navigation_my_pages items to only contain 1 in navigationRefsPayloadSchema`,
|
||||
}
|
||||
),
|
||||
}),
|
||||
@@ -101,7 +101,7 @@ export const navigationPayloadSchema = z.object({
|
||||
return input.length === 1
|
||||
},
|
||||
{
|
||||
message: `Expected navigationPayloadSchema to containt 1 all_navigation_my_pages item`,
|
||||
message: `Expected all_navigation_my_pages items to only contain 1 in navigationPayloadSchema`,
|
||||
}
|
||||
),
|
||||
}),
|
||||
|
||||
@@ -3,8 +3,8 @@ import {
|
||||
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 { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, publicProcedure, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
} from "@/utils/generateTag"
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import { getNavigationInputSchema } from "./input"
|
||||
import {
|
||||
getNavigationSchema,
|
||||
navigationPayloadSchema,
|
||||
@@ -48,82 +47,70 @@ export function mapMenuItems(navigationItems: NavigationItem[]) {
|
||||
}
|
||||
|
||||
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")],
|
||||
},
|
||||
}
|
||||
)
|
||||
get: contentstackProcedure.query(async function ({ ctx }) {
|
||||
const { lang } = ctx
|
||||
|
||||
if (!refsResponse.data) {
|
||||
console.error("Bad response for `GetNavigationMyPagesRefs`")
|
||||
console.error({ refsResponse })
|
||||
throw internalServerError()
|
||||
const refsResponse = await request<GetNavigationMyPagesRefsData>(
|
||||
GetNavigationMyPagesRefs,
|
||||
{ locale: lang },
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, "navigation_my_pages")],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
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()
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
}
|
||||
|
||||
const validatedMyPagesNavigationRefs =
|
||||
navigationRefsPayloadSchema.safeParse(refsResponse.data)
|
||||
if (!validatedMyPagesNavigationRefs.success) {
|
||||
throw internalServerError(validatedMyPagesNavigationRefs.error)
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedMyPagesNavigationRefs.data)
|
||||
|
||||
const tags = [
|
||||
generateTags(lang, connections),
|
||||
generateTag(
|
||||
lang,
|
||||
validatedMyPagesNavigationRefs.data.all_navigation_my_pages.items[0]
|
||||
.system.uid
|
||||
),
|
||||
].flat()
|
||||
|
||||
const response = await request<GetNavigationMyPagesData>(
|
||||
GetNavigationMyPages,
|
||||
{ locale: lang },
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedMyPagesNavigation = navigationPayloadSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
if (!validatedMyPagesNavigation.success) {
|
||||
throw internalServerError(validatedMyPagesNavigation.error)
|
||||
}
|
||||
|
||||
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) {
|
||||
throw internalServerError(validatedNav.error)
|
||||
}
|
||||
|
||||
return validatedNav.data
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
badRequestError,
|
||||
forbiddenError,
|
||||
internalServerError,
|
||||
notFound,
|
||||
unauthorizedError,
|
||||
} from "@/server/errors/trpc"
|
||||
import { protectedProcedure, router } from "@/server/trpc"
|
||||
@@ -21,50 +22,43 @@ function fakingRequest<T>(payload: T): Promise<T> {
|
||||
|
||||
export const userQueryRouter = router({
|
||||
get: protectedProcedure.query(async function ({ ctx }) {
|
||||
try {
|
||||
const apiResponse = await api.get(api.endpoints.v0.profile, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
default:
|
||||
throw internalServerError()
|
||||
}
|
||||
}
|
||||
const apiResponse = await api.get(api.endpoints.v0.profile, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
|
||||
if (!apiJson.data?.length) {
|
||||
throw internalServerError()
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError(apiResponse)
|
||||
case 401:
|
||||
throw unauthorizedError(apiResponse)
|
||||
case 403:
|
||||
throw forbiddenError(apiResponse)
|
||||
default:
|
||||
throw internalServerError(apiResponse)
|
||||
}
|
||||
}
|
||||
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data[0].attributes)
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get User - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data?.length) {
|
||||
throw notFound(apiJson)
|
||||
}
|
||||
|
||||
return {
|
||||
...extendedUser,
|
||||
...verifiedData.data,
|
||||
firstName: verifiedData.data.name,
|
||||
name: `${verifiedData.data.name} ${verifiedData.data.lastName}`,
|
||||
}
|
||||
} catch (error) {
|
||||
console.info(`Get User Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
const verifiedData = getUserSchema.safeParse(apiJson.data[0].attributes)
|
||||
if (!verifiedData.success) {
|
||||
throw internalServerError(verifiedData.error)
|
||||
}
|
||||
|
||||
return {
|
||||
...extendedUser,
|
||||
...verifiedData.data,
|
||||
firstName: verifiedData.data.name,
|
||||
name: `${verifiedData.data.name} ${verifiedData.data.lastName}`,
|
||||
}
|
||||
}),
|
||||
|
||||
benefits: router({
|
||||
current: protectedProcedure.query(async function (opts) {
|
||||
// TODO: Make request to get user data from Scandic API
|
||||
@@ -77,9 +71,10 @@ export const userQueryRouter = router({
|
||||
}),
|
||||
|
||||
stays: router({
|
||||
previous: protectedProcedure.input(staysInput).query(async (opts) => {
|
||||
try {
|
||||
const { limit, cursor } = opts.input
|
||||
previous: protectedProcedure
|
||||
.input(staysInput)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { limit, cursor } = input
|
||||
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", limit.toString())
|
||||
@@ -92,7 +87,7 @@ export const userQueryRouter = router({
|
||||
api.endpoints.v1.previousStays,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${opts.ctx.session.token.access_token}`,
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
},
|
||||
params
|
||||
@@ -101,28 +96,24 @@ export const userQueryRouter = router({
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
throw badRequestError(apiResponse)
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
throw unauthorizedError(apiResponse)
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
throw forbiddenError(apiResponse)
|
||||
default:
|
||||
throw internalServerError()
|
||||
throw internalServerError(apiResponse)
|
||||
}
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data) {
|
||||
console.error(`Get Previous Stays - No data found from api call`)
|
||||
throw internalServerError()
|
||||
if (!apiJson.data?.length) {
|
||||
throw notFound(apiJson)
|
||||
}
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get Previous Stays - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
throw internalServerError(verifiedData.error)
|
||||
}
|
||||
|
||||
const nextCursor =
|
||||
@@ -135,16 +126,12 @@ export const userQueryRouter = router({
|
||||
data: verifiedData.data.data,
|
||||
nextCursor,
|
||||
}
|
||||
} catch (error) {
|
||||
console.info(`Get Previous Stays Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
}),
|
||||
}),
|
||||
|
||||
upcoming: protectedProcedure.input(staysInput).query(async (opts) => {
|
||||
try {
|
||||
const { limit, cursor } = opts.input
|
||||
upcoming: protectedProcedure
|
||||
.input(staysInput)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { limit, cursor } = input
|
||||
|
||||
const params = new URLSearchParams()
|
||||
params.set("limit", limit.toString())
|
||||
@@ -157,7 +144,7 @@ export const userQueryRouter = router({
|
||||
api.endpoints.v1.upcomingStays,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${opts.ctx.session.token.access_token}`,
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
},
|
||||
params
|
||||
@@ -166,28 +153,24 @@ export const userQueryRouter = router({
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
throw badRequestError()
|
||||
throw badRequestError(apiResponse)
|
||||
case 401:
|
||||
throw unauthorizedError()
|
||||
throw unauthorizedError(apiResponse)
|
||||
case 403:
|
||||
throw forbiddenError()
|
||||
throw forbiddenError(apiResponse)
|
||||
default:
|
||||
throw internalServerError()
|
||||
throw internalServerError(apiResponse)
|
||||
}
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
if (!apiJson.data) {
|
||||
console.error(`Get Upcoming Stays - No data found from api call`)
|
||||
throw internalServerError()
|
||||
if (!apiJson.data?.length) {
|
||||
throw notFound(apiJson)
|
||||
}
|
||||
|
||||
const verifiedData = getStaysSchema.safeParse(apiJson)
|
||||
|
||||
if (!verifiedData.success) {
|
||||
console.info(`Get Upcoming Stays - Verified Data Error`)
|
||||
console.error(verifiedData.error)
|
||||
throw badRequestError()
|
||||
throw internalServerError(verifiedData.error)
|
||||
}
|
||||
|
||||
const nextCursor =
|
||||
@@ -200,11 +183,6 @@ export const userQueryRouter = router({
|
||||
data: verifiedData.data.data,
|
||||
nextCursor,
|
||||
}
|
||||
} catch (error) {
|
||||
console.info(`Get Upcoming Stays Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user