Files
web/apps/scandic-web/lib/trpc/memoizedRequests/index.ts

349 lines
9.3 KiB
TypeScript

import { isDefined } from "@/server/utils"
import { getLang } from "@/i18n/serverContext"
import { cache } from "@/utils/cache"
import { serverClient } from "../server"
import type { Country } from "@/types/enums/country"
import type {
AncillaryPackagesInput,
BreackfastPackagesInput,
PackagesInput,
} from "@/types/requests/packages"
import type {
CityCoordinatesInput,
HotelInput,
} from "@/types/trpc/routers/hotel/hotel"
import type { Lang } from "@/constants/languages"
import type {
GetHotelsByCSFilterInput,
GetSelectedRoomAvailabilityInput,
} from "@/server/routers/hotels/input"
import type { GetSavedPaymentCardsInput } from "@/server/routers/user/input"
export const getLocations = cache(async function getMemoizedLocations() {
const lang = getLang()
return serverClient().hotel.locations.get({ lang })
})
export const getProfile = cache(async function getMemoizedProfile() {
return serverClient().user.get()
})
export const getName = cache(async function getMemoizedName() {
return serverClient().user.name()
})
export const getProfileSafely = cache(
async function getMemoizedProfileSafely() {
return serverClient().user.getSafely()
}
)
export const getSavedPaymentCardsSafely = cache(
async function getMemoizedSavedPaymentCardsSafely(
input: GetSavedPaymentCardsInput
) {
return serverClient().user.safePaymentCards(input)
}
)
export const getMembershipLevel = cache(
async function getMemoizedMembershipLevel() {
return serverClient().user.membershipLevel()
}
)
export const getMembershipLevelSafely = cache(
async function getMemoizedMembershipLevelSafely() {
return serverClient().user.safeMembershipLevel()
}
)
export const getMembershipCards = cache(
async function getMemoizedMembershipCards() {
return serverClient().user.membershipCards()
}
)
export const getHotelsByCSFilter = cache(async function getMemoizedHotels(
input: GetHotelsByCSFilterInput
) {
return serverClient().hotel.hotels.byCSFilter.get(input)
})
export const getHotel = cache(async function getMemoizedHotelData(
input: HotelInput
) {
if (!input.isCardOnlyPayment) {
input.isCardOnlyPayment = false
}
return serverClient().hotel.get(input)
})
export const getHotelPage = cache(async function getMemoizedHotelPage() {
return serverClient().contentstack.hotelPage.get()
})
export const getSelectedRoomAvailability = cache(
function getMemoizedSelectedRoomAvailability(
input: GetSelectedRoomAvailabilityInput
) {
return serverClient().hotel.availability.room(input)
}
)
export const getFooter = cache(async function getMemoizedFooter() {
return serverClient().contentstack.base.footer()
})
export const getHeader = cache(async function getMemoizedHeader() {
return serverClient().contentstack.base.header()
})
export const getCurrentHeader = cache(async function getMemoizedCurrentHeader(
lang: Lang
) {
return serverClient().contentstack.base.currentHeader({ lang })
})
export const getCurrentFooter = cache(async function getMemoizedCurrentFooter(
lang: Lang
) {
return serverClient().contentstack.base.currentFooter({ lang })
})
export const getSiteConfig = cache(async function getMemoizedSiteConfig(
lang: Lang
) {
return serverClient().contentstack.base.siteConfig({ lang })
})
export const getBreakfastPackages = cache(
async function getMemoizedBreakfastPackages(input: BreackfastPackagesInput) {
return serverClient().hotel.packages.breakfast(input)
}
)
export const getAncillaryPackages = cache(
async function getMemoizedAncillaryPackages(input: AncillaryPackagesInput) {
return serverClient().hotel.packages.ancillary(input)
}
)
export const getPackages = cache(async function getMemoizedPackages(
input: PackagesInput
) {
return serverClient().hotel.packages.get(input)
})
export const getBookingConfirmation = cache(
async function getMemoizedBookingConfirmation(confirmationNumber: string) {
return serverClient().booking.confirmation({ confirmationNumber })
}
)
export const getCityCoordinates = cache(
async function getMemoizedCityCoordinates(input: CityCoordinatesInput) {
return serverClient().hotel.map.city(input)
}
)
export const getCurrentRewards = cache(
async function getMemoizedCurrentRewards() {
return serverClient().contentstack.rewards.current()
}
)
export const getMeetingRooms = cache(
async function getMemoizedMeetingRooms(input: {
hotelId: string
language: Lang
}) {
return serverClient().hotel.meetingRooms(input)
}
)
export const getAdditionalData = cache(
async function getMemoizedAdditionalData(input: {
hotelId: string
language: Lang
}) {
return serverClient().hotel.additionalData(input)
}
)
export const getDestinationOverviewPage = cache(
async function getMemoizedDestinationOverviewPage() {
return serverClient().contentstack.destinationOverviewPage.get()
}
)
export const getDestinationsList = cache(
async function getMemoizedDestinationsList() {
return serverClient().contentstack.destinationOverviewPage.destinations.get()
}
)
export const getDestinationCountryPage = cache(
async function getMemoizedDestinationCountryPage() {
return serverClient().contentstack.destinationCountryPage.get()
}
)
export const getDestinationCityPagesByCountry = cache(
async function getMemoizedDestinationCityPagesByCountry(country: Country) {
return serverClient().contentstack.destinationCountryPage.cityPages({
country,
})
}
)
export const getHotelsByCountry = cache(
async function getMemoizedHotelsByCountry(country: Country) {
return serverClient().hotel.hotels.byCountry.get({
country,
})
}
)
export const getHotelsByCityIdentifier = cache(
async function getMemoizedHotelsByCityIdentifier(cityIdentifier: string) {
return serverClient().hotel.hotels.byCityIdentifier.get({
cityIdentifier,
})
}
)
export const getDestinationsMapData = cache(
async function getMemoizedDestinationsMapData() {
return serverClient().hotel.hotels.getDestinationsMapData()
}
)
export const getDestinationCityPage = cache(
async function getMemoizedDestinationCityPage() {
return serverClient().contentstack.destinationCityPage.get()
}
)
export const getStartPage = cache(async function getMemoizedStartPage() {
return serverClient().contentstack.startPage.get()
})
export const getPageSettings = cache(async function getMemoizedPageSettings(
lang: Lang
) {
return serverClient().contentstack.pageSettings.get({ lang })
})
export const isBookingWidgetHidden = cache(
async function isMemoizedBookingWidgetHidden() {
const lang = getLang()
const [pageSettingsResult, siteConfigResults] = await Promise.allSettled([
getPageSettings(lang),
getSiteConfig(lang),
])
const pageSettings =
pageSettingsResult.status === "fulfilled"
? pageSettingsResult.value
: null
const siteConfig =
siteConfigResults.status === "fulfilled" ? siteConfigResults.value : null
if (pageSettings) {
return pageSettings.page.settings.hide_booking_widget
}
if (siteConfig) {
return siteConfig.bookingWidgetDisabled
}
return false
}
)
export const getPageSettingsBookingCode = cache(
async function getMemoizedPageSettingsBookingCode() {
const lang = getLang()
const pageSettings = await getPageSettings(lang)
return pageSettings?.page.settings.booking_code ?? ""
}
)
export const getJobylonFeed = cache(async function getMemoizedJobylonFeed() {
return serverClient().partner.jobylon.feed.get()
})
export const getJumpToData = cache(async function getMemoizedJumpToData() {
const lang = getLang()
const [locationsResults, urlsResults] = await Promise.allSettled([
getLocations(),
serverClient().hotel.locations.urls({ lang }),
])
if (
locationsResults.status === "fulfilled" &&
urlsResults.status === "fulfilled"
) {
const locations = locationsResults.value
const urls = urlsResults.value
if (!locations || !urls) {
return null
}
return locations
.map((location) => {
const { id, name, type } = location
const isCity = type === "cities"
const isHotel = type === "hotels"
let url: string | undefined
if (isCity) {
url = urls.cities.find(
(c) =>
c.id &&
location.cityIdentifier &&
c.id === location.cityIdentifier
)?.url
} else if (isHotel) {
url = urls.hotels.find(
(h) => h.id && location.id && h.id === location.id
)?.url
}
if (!url) {
return null
}
let description = ""
if (isCity) {
description = location.country
} else if (isHotel) {
description = location.relationships.city.name
}
const rankingNames: string[] = [location.name]
if (isCity) {
if (location.cityIdentifier) {
rankingNames.push(location.cityIdentifier)
}
}
const rankingKeywords = location.keyWords || []
return {
id,
displayName: name,
type,
description,
url,
rankingNames: rankingNames.map((v) => v.toLowerCase()),
rankingKeywords: rankingKeywords.map((v) => v.toLowerCase()),
}
})
.filter(isDefined)
}
return null
})