Files
web/apps/scandic-web/server/routers/contentstack/pageSettings/query.ts
Anton Gunnarsson 846fd904a6 Merged in feat/sw-2859-set-up-shared-trpc-package (pull request #2319)
feat(SW-2859): Create trpc package

* Add isEdge, safeTry and dataCache to new common package

* Add eslint and move prettier config

* Clean up tests

* Create trpc package and move initialization

* Move errors and a few procedures

* Move telemetry to common package

* Move tokenManager to common package

* Add Sentry to procedures

* Clean up procedures

* Fix self-referencing imports

* 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

* Fix lang imports


Approved-by: Linus Flood
2025-06-18 12:14:20 +00:00

93 lines
2.9 KiB
TypeScript

import * as Sentry from "@sentry/nextjs"
import { router } from "@scandic-hotels/trpc"
import { contentstackBaseProcedure } from "@scandic-hotels/trpc/procedures"
import {
GetAccountPageSettings,
GetCampaignPageSettings,
GetCollectionPageSettings,
GetContentPageSettings,
GetCurrentBlocksPageSettings,
GetDestinationCityPageSettings,
GetDestinationCountryPageSettings,
GetDestinationOverviewPageSettings,
GetHotelPageSettings,
GetLoyaltyPageSettings,
GetStartPageSettings,
} from "@/lib/graphql/Query/PageSettings.graphql"
import { request } from "@/lib/graphql/request"
import { langInput } from "@/server/utils"
import { generateTag } from "@/utils/generateTag"
import {
DEFAULT_GET_PAGE_SETTINGS,
type GetPageSettingsSchema,
getPageSettingsSchema,
} from "./output"
import { affix } from "./utils"
import { PageContentTypeEnum } from "@/types/requests/contentType"
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.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,
}