refactor: zod validation and pr comments
This commit is contained in:
5
server/routers/contentstack/contactConfig/input.ts
Normal file
5
server/routers/contentstack/contactConfig/input.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export const getConfigInput = z.object({ lang: z.nativeEnum(Lang) })
|
||||
60
server/routers/contentstack/contactConfig/output.ts
Normal file
60
server/routers/contentstack/contactConfig/output.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { z } from "zod"
|
||||
|
||||
// Help me write this zod schema based on the type ContactConfig
|
||||
export const validateContactConfigSchema = z.object({
|
||||
all_contact_config: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
email: z.object({
|
||||
name: z.string().nullable(),
|
||||
address: z.string().nullable(),
|
||||
}),
|
||||
email_loyalty: z.object({
|
||||
name: z.string().nullable(),
|
||||
address: z.string().nullable(),
|
||||
}),
|
||||
mailing_address: z.object({
|
||||
zip: z.string().nullable(),
|
||||
street: z.string().nullable(),
|
||||
name: z.string().nullable(),
|
||||
city: z.string().nullable(),
|
||||
country: z.string().nullable(),
|
||||
}),
|
||||
phone: z.object({
|
||||
number: z.string().nullable(),
|
||||
name: z.string().nullable(),
|
||||
}),
|
||||
phone_loyalty: z.object({
|
||||
number: z.string().nullable(),
|
||||
name: z.string().nullable(),
|
||||
}),
|
||||
visiting_address: z.object({
|
||||
zip: z.string().nullable(),
|
||||
country: z.string().nullable(),
|
||||
city: z.string().nullable(),
|
||||
street: z.string().nullable(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
export enum ContactFieldGroupsEnum {
|
||||
email = "email",
|
||||
email_loyalty = "email_loyalty",
|
||||
mailing_address = "mailing_address",
|
||||
phone = "phone",
|
||||
phone_loyalty = "phone_loyalty",
|
||||
visiting_address = "visiting_address",
|
||||
}
|
||||
|
||||
export type ContactFieldGroups = keyof typeof ContactFieldGroupsEnum
|
||||
|
||||
export type ContactConfigData = z.infer<typeof validateContactConfigSchema>
|
||||
|
||||
export type ContactConfig = ContactConfigData["all_contact_config"]["items"][0]
|
||||
|
||||
export type ContactFields = {
|
||||
display_text?: string
|
||||
contact_field: string
|
||||
}
|
||||
@@ -1,32 +1,34 @@
|
||||
import { z } from "zod"
|
||||
|
||||
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 { request } from "@/lib/graphql/request"
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import GetContactConfig from "@/lib/graphql/Query/ContactConfig.graphql"
|
||||
|
||||
import type { GetContactConfigData } from "@/types/requests/contactConfig"
|
||||
import { getConfigInput } from "./input"
|
||||
import { type ContactConfigData, validateContactConfigSchema } from "./output"
|
||||
|
||||
export const contactConfigQueryRouter = router({
|
||||
get: publicProcedure
|
||||
.input(z.object({ lang: z.nativeEnum(Lang) }))
|
||||
.query(async ({ input }) => {
|
||||
const contactConfig = await request<GetContactConfigData>(
|
||||
GetContactConfig,
|
||||
{
|
||||
locale: input.lang,
|
||||
},
|
||||
{
|
||||
tags: [`contact-config-${input.lang}`],
|
||||
}
|
||||
)
|
||||
get: publicProcedure.input(getConfigInput).query(async ({ input }) => {
|
||||
try {
|
||||
const contactConfig = await request<ContactConfigData>(GetContactConfig, {
|
||||
locale: input.lang,
|
||||
})
|
||||
|
||||
if (contactConfig.data && contactConfig.data.all_contact_config.total) {
|
||||
return contactConfig.data.all_contact_config.items[0]
|
||||
if (!contactConfig.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
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()
|
||||
}),
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { router } from "@/server/trpc"
|
||||
|
||||
import { breadcrumbsRouter } from "./breadcrumbs"
|
||||
import { loyaltyPageRouter } from "./loyaltyPage"
|
||||
import { contactConfigRouter } from "./contactConfig"
|
||||
import { loyaltyPageRouter } from "./loyaltyPage"
|
||||
|
||||
export const contentstackRouter = router({
|
||||
breadcrumbs: breadcrumbsRouter,
|
||||
|
||||
@@ -31,6 +31,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
)
|
||||
|
||||
if (!validatedLoyaltyPage.success) {
|
||||
console.error(validatedLoyaltyPage.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
@@ -68,15 +69,13 @@ export const loyaltyPageQueryRouter = router({
|
||||
cards: block.card_grid.cards.map((card) => {
|
||||
return {
|
||||
...card,
|
||||
link:
|
||||
card.referenceConnection.totalCount > 0
|
||||
? {
|
||||
href: card.referenceConnection.edges[0].node
|
||||
.url,
|
||||
title:
|
||||
card.referenceConnection.edges[0].node.title,
|
||||
}
|
||||
: undefined,
|
||||
link: card.referenceConnection.totalCount
|
||||
? {
|
||||
href: card.referenceConnection.edges[0].node.url,
|
||||
title:
|
||||
card.referenceConnection.edges[0].node.title,
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
}),
|
||||
},
|
||||
@@ -86,17 +85,16 @@ export const loyaltyPageQueryRouter = router({
|
||||
...block,
|
||||
dynamic_content: {
|
||||
...block.dynamic_content,
|
||||
link:
|
||||
block.dynamic_content.link.pageConnection.totalCount > 0
|
||||
? {
|
||||
text: block.dynamic_content.link.text,
|
||||
href: block.dynamic_content.link.pageConnection
|
||||
.edges[0].node.url,
|
||||
title:
|
||||
block.dynamic_content.link.pageConnection.edges[0]
|
||||
.node.title,
|
||||
}
|
||||
: undefined,
|
||||
link: block.dynamic_content.link.pageConnection.totalCount
|
||||
? {
|
||||
text: block.dynamic_content.link.text,
|
||||
href: block.dynamic_content.link.pageConnection
|
||||
.edges[0].node.url,
|
||||
title:
|
||||
block.dynamic_content.link.pageConnection.edges[0]
|
||||
.node.title,
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent:
|
||||
|
||||
Reference in New Issue
Block a user