Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
91
packages/trpc/lib/routers/contentstack/pageSettings/query.ts
Normal file
91
packages/trpc/lib/routers/contentstack/pageSettings/query.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import * as Sentry from "@sentry/nextjs"
|
||||
|
||||
import { contentstackBaseProcedure } from "@scandic-hotels/trpc/procedures"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { PageContentTypeEnum } from "../../../enums/contentType"
|
||||
import {
|
||||
GetAccountPageSettings,
|
||||
GetCampaignOverviewPageSettings,
|
||||
GetCampaignPageSettings,
|
||||
GetCollectionPageSettings,
|
||||
GetContentPageSettings,
|
||||
GetCurrentBlocksPageSettings,
|
||||
GetDestinationCityPageSettings,
|
||||
GetDestinationCountryPageSettings,
|
||||
GetDestinationOverviewPageSettings,
|
||||
GetHotelPageSettings,
|
||||
GetLoyaltyPageSettings,
|
||||
GetStartPageSettings,
|
||||
} from "../../../graphql/Query/PageSettings.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { langInput } from "../../../utils"
|
||||
import { generateTag } from "../../../utils/generateTag"
|
||||
import {
|
||||
DEFAULT_GET_PAGE_SETTINGS,
|
||||
type GetPageSettingsSchema,
|
||||
getPageSettingsSchema,
|
||||
} from "./output"
|
||||
import { affix } from "./utils"
|
||||
|
||||
export const pageSettingsQueryRouter = router({
|
||||
get: contentstackBaseProcedure
|
||||
.input(langInput)
|
||||
.query(async ({ input, ctx }): Promise<GetPageSettingsSchema> => {
|
||||
const { contentType, uid } = ctx
|
||||
const lang = input.lang ?? ctx.lang
|
||||
|
||||
// This condition is to handle 404 page case and booking flow
|
||||
if (!contentType || !uid) {
|
||||
return DEFAULT_GET_PAGE_SETTINGS
|
||||
}
|
||||
|
||||
const getPageSettingsQuery =
|
||||
graphqlQueriesForContentType[contentType as PageContentTypeEnum]
|
||||
|
||||
if (!getPageSettingsQuery) {
|
||||
Sentry.captureMessage(
|
||||
`GetPageSettings: No proper Content type defined for '${contentType}'`
|
||||
)
|
||||
return DEFAULT_GET_PAGE_SETTINGS
|
||||
}
|
||||
|
||||
const response = await request<GetPageSettingsSchema>(
|
||||
getPageSettingsQuery,
|
||||
{
|
||||
uid: uid,
|
||||
locale: lang,
|
||||
},
|
||||
{
|
||||
key: generateTag(lang, uid, affix),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
try {
|
||||
const result = getPageSettingsSchema.parse(response.data)
|
||||
return result
|
||||
} catch (error) {
|
||||
Sentry.captureException(error, { extra: { uid, contentType } })
|
||||
|
||||
return DEFAULT_GET_PAGE_SETTINGS
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
const graphqlQueriesForContentType: Record<PageContentTypeEnum, any> = {
|
||||
[PageContentTypeEnum.accountPage]: GetAccountPageSettings,
|
||||
[PageContentTypeEnum.campaignOverviewPage]: GetCampaignOverviewPageSettings,
|
||||
[PageContentTypeEnum.campaignPage]: GetCampaignPageSettings,
|
||||
[PageContentTypeEnum.collectionPage]: GetCollectionPageSettings,
|
||||
[PageContentTypeEnum.contentPage]: GetContentPageSettings,
|
||||
[PageContentTypeEnum.currentBlocksPage]: GetCurrentBlocksPageSettings,
|
||||
[PageContentTypeEnum.destinationCityPage]: GetDestinationCityPageSettings,
|
||||
[PageContentTypeEnum.destinationCountryPage]:
|
||||
GetDestinationCountryPageSettings,
|
||||
[PageContentTypeEnum.destinationOverviewPage]:
|
||||
GetDestinationOverviewPageSettings,
|
||||
[PageContentTypeEnum.hotelPage]: GetHotelPageSettings,
|
||||
[PageContentTypeEnum.loyaltyPage]: GetLoyaltyPageSettings,
|
||||
[PageContentTypeEnum.startPage]: GetStartPageSettings,
|
||||
}
|
||||
Reference in New Issue
Block a user