Merged in feat/SW-3526-show-sas-eb-points-rate-in- (pull request #2933)

feat(SW-3526): Show EB points rate and label in booking flow

* feat(SW-3526): Show EB points rate and label in booking flow

* feat(SW-3526) Optimized points currency code

* feat(SW-3526) Removed extra multiplication for token expiry after rebase

* feat(SW-3526): Updated to exhaustive check and thow if type error

Approved-by: Anton Gunnarsson
This commit is contained in:
Hrishikesh Vaipurkar
2025-10-15 06:54:44 +00:00
parent 73af1eed9b
commit 78ede453a2
27 changed files with 281 additions and 176 deletions

View File

@@ -2,8 +2,9 @@
import { createContext, useContext } from "react" import { createContext, useContext } from "react"
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import type { BookingFlowConfig } from "./bookingFlowConfig" import type { BookingFlowConfig } from "./bookingFlowConfig"
import type { BookingFlowVariant } from "./bookingFlowVariants"
type BookingFlowConfigContextData = BookingFlowConfig type BookingFlowConfigContextData = BookingFlowConfig
@@ -11,18 +12,6 @@ const BookingFlowConfigContext = createContext<
BookingFlowConfigContextData | undefined BookingFlowConfigContextData | undefined
>(undefined) >(undefined)
export const useIsPartner = (variant: BookingFlowVariant) => {
const context = useContext(BookingFlowConfigContext)
if (!context) {
throw new Error(
"useBookingFlowConfig must be used within a BookingFlowConfigContextProvider. Did you forget to use BookingFlowConfig in the consuming app?"
)
}
return context.variant === variant
}
export const useBookingFlowConfig = (): BookingFlowConfigContextData => { export const useBookingFlowConfig = (): BookingFlowConfigContextData => {
const context = useContext(BookingFlowConfigContext) const context = useContext(BookingFlowConfigContext)
@@ -35,6 +24,19 @@ export const useBookingFlowConfig = (): BookingFlowConfigContextData => {
return context return context
} }
export const useGetPointsCurrency = () => {
const config = useBookingFlowConfig()
switch (config.variant) {
case "scandic":
return CurrencyEnum.POINTS
case "partner-sas":
return CurrencyEnum.EUROBONUS
default:
throw new Error(`Unknown variant: ${config.variant}`)
}
}
export function BookingFlowConfigContextProvider({ export function BookingFlowConfigContextProvider({
children, children,
config, config,

View File

@@ -12,6 +12,7 @@ import { useScrollToTop } from "@scandic-hotels/common/hooks/useScrollToTop"
import { BackToTopButton } from "@scandic-hotels/design-system/BackToTopButton" import { BackToTopButton } from "@scandic-hotels/design-system/BackToTopButton"
import { HotelCard } from "@scandic-hotels/design-system/HotelCard" import { HotelCard } from "@scandic-hotels/design-system/HotelCard"
import { useGetPointsCurrency } from "../../bookingFlowConfig/bookingFlowConfigContext"
import { useIsLoggedIn } from "../../hooks/useIsLoggedIn" import { useIsLoggedIn } from "../../hooks/useIsLoggedIn"
import useLang from "../../hooks/useLang" import useLang from "../../hooks/useLang"
import { mapApiImagesToGalleryImages } from "../../misc/imageGallery" import { mapApiImagesToGalleryImages } from "../../misc/imageGallery"
@@ -57,6 +58,7 @@ export default function HotelCardListing({
const { activeHotel, activate, disengage, engage } = useHotelsMapStore() const { activeHotel, activate, disengage, engage } = useHotelsMapStore()
const { showBackToTop, scrollToTop } = useScrollToTop({ threshold: 490 }) const { showBackToTop, scrollToTop } = useScrollToTop({ threshold: 490 })
const activeCardRef = useRef<HTMLDivElement | null>(null) const activeCardRef = useRef<HTMLDivElement | null>(null)
const pointsCurrency = useGetPointsCurrency()
const sortBy = searchParams.get("sort") ?? DEFAULT_SORT const sortBy = searchParams.get("sort") ?? DEFAULT_SORT
@@ -159,6 +161,7 @@ export default function HotelCardListing({
tripAdvisor: hotel.hotel.ratings?.tripAdvisor.rating, tripAdvisor: hotel.hotel.ratings?.tripAdvisor.rating,
}, },
}} }}
pointsCurrency={pointsCurrency}
lang={lang} lang={lang}
fullPrice={!hotel.availability.bookingCode} fullPrice={!hotel.availability.bookingCode}
prices={ prices={

View File

@@ -16,6 +16,7 @@ import { OldDSButton as Button } from "@scandic-hotels/design-system/OldDSButton
import Subtitle from "@scandic-hotels/design-system/Subtitle" import Subtitle from "@scandic-hotels/design-system/Subtitle"
import { Typography } from "@scandic-hotels/design-system/Typography" import { Typography } from "@scandic-hotels/design-system/Typography"
import { useGetPointsCurrency } from "../../bookingFlowConfig/bookingFlowConfigContext"
import { useIsLoggedIn } from "../../hooks/useIsLoggedIn" import { useIsLoggedIn } from "../../hooks/useIsLoggedIn"
import useLang from "../../hooks/useLang" import useLang from "../../hooks/useLang"
@@ -34,6 +35,7 @@ export default function ListingHotelCardDialog({
}: ListingHotelCardProps) { }: ListingHotelCardProps) {
const intl = useIntl() const intl = useIntl()
const lang = useLang() const lang = useLang()
const pointsCurrency = useGetPointsCurrency()
const [imageError, setImageError] = useState(false) const [imageError, setImageError] = useState(false)
@@ -153,7 +155,10 @@ export default function ListingHotelCardDialog({
</Subtitle> </Subtitle>
)} )}
{redemptionPrice && ( {redemptionPrice && (
<HotelPointsRow pointsPerStay={redemptionPrice} /> <HotelPointsRow
pointsPerStay={redemptionPrice}
pointsCurrency={pointsCurrency}
/>
)} )}
{chequePrice && ( {chequePrice && (
<Subtitle type="two"> <Subtitle type="two">

View File

@@ -1,14 +1,16 @@
"use client" "use client"
import { useIntl } from "react-intl" import { useIntl } from "react-intl"
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting" import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting"
import { useGetPointsCurrency } from "../../../../../bookingFlowConfig/bookingFlowConfigContext"
import BoldRow from "../Bold" import BoldRow from "../Bold"
import RegularRow from "../Regular" import RegularRow from "../Regular"
import BedTypeRow from "./BedType" import BedTypeRow from "./BedType"
import PackagesRow from "./Packages" import PackagesRow from "./Packages"
import type { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import type { SharedPriceRowProps } from "./price" import type { SharedPriceRowProps } from "./price"
export interface RedemptionPriceType { export interface RedemptionPriceType {
@@ -34,6 +36,7 @@ export default function RedemptionPrice({
price, price,
}: RedemptionPriceProps) { }: RedemptionPriceProps) {
const intl = useIntl() const intl = useIntl()
const pointsCurrency = useGetPointsCurrency()
if (!price) { if (!price) {
return null return null
@@ -50,7 +53,7 @@ export default function RedemptionPrice({
: null : null
const additionalCurrency = price.currency ?? currency const additionalCurrency = price.currency ?? currency
let averagePricePerNight = `${price.pointsPerNight} ${CurrencyEnum.POINTS}` let averagePricePerNight = `${price.pointsPerNight} ${pointsCurrency}`
if (averageAdditionalPricePerNight) { if (averageAdditionalPricePerNight) {
averagePricePerNight = `${averagePricePerNight} + ${averageAdditionalPricePerNight} ${additionalCurrency}` averagePricePerNight = `${averagePricePerNight} + ${averageAdditionalPricePerNight} ${additionalCurrency}`
} }
@@ -62,7 +65,7 @@ export default function RedemptionPrice({
value={formatPrice( value={formatPrice(
intl, intl,
price.pointsPerStay, price.pointsPerStay,
CurrencyEnum.POINTS, pointsCurrency,
additionalPricePerStay, additionalPricePerStay,
additionalCurrency additionalCurrency
)} )}

View File

@@ -19,6 +19,7 @@ import { InteractiveMap } from "@scandic-hotels/design-system/Map/InteractiveMap
import { Typography } from "@scandic-hotels/design-system/Typography" import { Typography } from "@scandic-hotels/design-system/Typography"
import { trackEvent } from "@scandic-hotels/tracking/base" import { trackEvent } from "@scandic-hotels/tracking/base"
import { useGetPointsCurrency } from "../../../../bookingFlowConfig/bookingFlowConfigContext"
import { useIsLoggedIn } from "../../../../hooks/useIsLoggedIn" import { useIsLoggedIn } from "../../../../hooks/useIsLoggedIn"
import useLang from "../../../../hooks/useLang" import useLang from "../../../../hooks/useLang"
import { mapApiImagesToGalleryImages } from "../../../../misc/imageGallery" import { mapApiImagesToGalleryImages } from "../../../../misc/imageGallery"
@@ -76,6 +77,7 @@ export function SelectHotelMapContent({
const activeFilters = useHotelFilterStore((state) => state.activeFilters) const activeFilters = useHotelFilterStore((state) => state.activeFilters)
const setResultCount = useHotelFilterStore((state) => state.setResultCount) const setResultCount = useHotelFilterStore((state) => state.setResultCount)
const pointsCurrency = useGetPointsCurrency()
const hotelMapStore = useHotelsMapStore() const hotelMapStore = useHotelsMapStore()
@@ -254,6 +256,7 @@ export function SelectHotelMapContent({
)} )}
</div> </div>
<InteractiveMap <InteractiveMap
pointsCurrency={pointsCurrency}
closeButton={closeButton} closeButton={closeButton}
coordinates={coordinates} coordinates={coordinates}
hotelPins={filteredHotelPins.map((pin) => { hotelPins={filteredHotelPins.map((pin) => {

View File

@@ -3,6 +3,7 @@ import { useIntl } from "react-intl"
import PointsRateCard from "@scandic-hotels/design-system/PointsRateCard" import PointsRateCard from "@scandic-hotels/design-system/PointsRateCard"
import { useGetPointsCurrency } from "../../../../../../../bookingFlowConfig/bookingFlowConfigContext"
import { useSelectRateContext } from "../../../../../../../contexts/SelectRate/SelectRateContext" import { useSelectRateContext } from "../../../../../../../contexts/SelectRate/SelectRateContext"
import { BookingCodeFilterEnum } from "../../../../../../../stores/bookingCode-filter" import { BookingCodeFilterEnum } from "../../../../../../../stores/bookingCode-filter"
import { sumPackages } from "../../../../../../../utils/SelectRate" import { sumPackages } from "../../../../../../../utils/SelectRate"
@@ -33,6 +34,7 @@ export default function Redemptions({
actions: { selectRate }, actions: { selectRate },
selectedRates, selectedRates,
} = useSelectRateContext() } = useSelectRateContext()
const pointsCurrency = useGetPointsCurrency()
// TODO: Replace with context value when we have support for dropdown "Show all rates" // TODO: Replace with context value when we have support for dropdown "Show all rates"
const selectedFilter = BookingCodeFilterEnum.All as BookingCodeFilterEnum const selectedFilter = BookingCodeFilterEnum.All as BookingCodeFilterEnum
@@ -86,7 +88,7 @@ export default function Redemptions({
price: additionalPrice.toString(), price: additionalPrice.toString(),
} }
: undefined, : undefined,
currency: "PTS", currency: pointsCurrency ?? "PTS",
isDisabled: !r.redemption.hasEnoughPoints, isDisabled: !r.redemption.hasEnoughPoints,
points: r.redemption.localPrice.pointsPerStay.toString(), points: r.redemption.localPrice.pointsPerStay.toString(),
rateCode: r.redemption.rateCode, rateCode: r.redemption.rateCode,

View File

@@ -6,6 +6,7 @@ import { createContext, useEffect, useRef, useState } from "react"
import { dt } from "@scandic-hotels/common/dt" import { dt } from "@scandic-hotels/common/dt"
import { LoadingSpinner } from "@scandic-hotels/design-system/LoadingSpinner" import { LoadingSpinner } from "@scandic-hotels/design-system/LoadingSpinner"
import { useGetPointsCurrency } from "../../bookingFlowConfig/bookingFlowConfigContext"
import { getMultiroomDetailsSchema } from "../../components/EnterDetails/Details/Multiroom/schema" import { getMultiroomDetailsSchema } from "../../components/EnterDetails/Details/Multiroom/schema"
import { guestDetailsSchema } from "../../components/EnterDetails/Details/RoomOne/schema" import { guestDetailsSchema } from "../../components/EnterDetails/Details/RoomOne/schema"
import { import {
@@ -60,6 +61,7 @@ export default function EnterDetailsProvider({
// rendering the form until that has been done. // rendering the form until that has been done.
const [hasInitializedStore, setHasInitializedStore] = useState(false) const [hasInitializedStore, setHasInitializedStore] = useState(false)
const storeRef = useRef<EnterDetailsStore>(undefined) const storeRef = useRef<EnterDetailsStore>(undefined)
const pointsCurrency = useGetPointsCurrency()
if (!storeRef.current) { if (!storeRef.current) {
const initialData: InitialState = { const initialData: InitialState = {
booking, booking,
@@ -99,7 +101,8 @@ export default function EnterDetailsProvider({
searchParamsStr, searchParamsStr,
user, user,
breakfastPackages, breakfastPackages,
lang lang,
pointsCurrency
) )
} }

View File

@@ -18,6 +18,7 @@ import { RoomPackageCodeEnum } from "@scandic-hotels/trpc/enums/roomFilter"
import { AvailabilityEnum } from "@scandic-hotels/trpc/enums/selectHotel" import { AvailabilityEnum } from "@scandic-hotels/trpc/enums/selectHotel"
import { selectRateRoomsAvailabilityInputSchema } from "@scandic-hotels/trpc/routers/hotels/availability/selectRate/rooms/schema" import { selectRateRoomsAvailabilityInputSchema } from "@scandic-hotels/trpc/routers/hotels/availability/selectRate/rooms/schema"
import { useGetPointsCurrency } from "../../../bookingFlowConfig/bookingFlowConfigContext"
import { useIsLoggedIn } from "../../../hooks/useIsLoggedIn" import { useIsLoggedIn } from "../../../hooks/useIsLoggedIn"
import useLang from "../../../hooks/useLang" import useLang from "../../../hooks/useLang"
import { BookingCodeFilterEnum } from "../../../stores/bookingCode-filter" import { BookingCodeFilterEnum } from "../../../stores/bookingCode-filter"
@@ -68,6 +69,7 @@ export function SelectRateProvider({
const updateBooking = useUpdateBooking() const updateBooking = useUpdateBooking()
const isUserLoggedIn = useIsLoggedIn() const isUserLoggedIn = useIsLoggedIn()
const intl = useIntl() const intl = useIntl()
const pointsCurrency = useGetPointsCurrency()
const [activeRoomIndex, setInternalActiveRoomIndex] = useQueryState<number>( const [activeRoomIndex, setInternalActiveRoomIndex] = useQueryState<number>(
"activeRoomIndex", "activeRoomIndex",
@@ -233,6 +235,7 @@ export function SelectRateProvider({
roomConfiguration: roomAvailability[ix]?.[0], roomConfiguration: roomAvailability[ix]?.[0],
})), })),
isMember: isUserLoggedIn, isMember: isUserLoggedIn,
pointsCurrency,
}) })
const getPriceForRoom = useCallback( const getPriceForRoom = useCallback(
@@ -253,9 +256,10 @@ export function SelectRateProvider({
], ],
isMember: isUserLoggedIn && roomIndex === 0, isMember: isUserLoggedIn && roomIndex === 0,
addAdditionalCost: false, addAdditionalCost: false,
pointsCurrency,
}) })
}, },
[selectedRates, roomAvailability, isUserLoggedIn] [selectedRates, roomAvailability, isUserLoggedIn, pointsCurrency]
) )
const setActiveRoomIndex = useCallback( const setActiveRoomIndex = useCallback(

View File

@@ -17,10 +17,12 @@ export function getTotalPrice({
selectedRates, selectedRates,
isMember, isMember,
addAdditionalCost = true, addAdditionalCost = true,
pointsCurrency,
}: { }: {
selectedRates: Array<SelectedRate | null> selectedRates: Array<SelectedRate | null>
isMember: boolean isMember: boolean
addAdditionalCost?: boolean addAdditionalCost?: boolean
pointsCurrency?: CurrencyEnum
}): Price | null { }): Price | null {
const mainRoom = selectedRates[0] const mainRoom = selectedRates[0]
const mainRoomRate = mainRoom?.rate const mainRoomRate = mainRoom?.rate
@@ -45,7 +47,8 @@ export function getTotalPrice({
mainRoom.roomConfiguration?.selectedPackages.filter( mainRoom.roomConfiguration?.selectedPackages.filter(
(pkg) => "localPrice" in pkg (pkg) => "localPrice" in pkg
) ?? null, ) ?? null,
addAdditionalCost addAdditionalCost,
pointsCurrency
) )
} }
if ("voucher" in mainRoomRate) { if ("voucher" in mainRoomRate) {
@@ -156,7 +159,8 @@ function calculateTotalPrice(
function calculateRedemptionTotalPrice( function calculateRedemptionTotalPrice(
redemption: RedemptionProduct["redemption"], redemption: RedemptionProduct["redemption"],
packages: RoomPackage[] | null, packages: RoomPackage[] | null,
addAdditonalCost: boolean addAdditonalCost: boolean,
pointsCurrency?: CurrencyEnum
) { ) {
const pkgsSum = addAdditonalCost const pkgsSum = addAdditonalCost
? sumPackages(packages) ? sumPackages(packages)
@@ -179,7 +183,7 @@ function calculateRedemptionTotalPrice(
local: { local: {
additionalPrice, additionalPrice,
additionalPriceCurrency, additionalPriceCurrency,
currency: CurrencyEnum.POINTS, currency: pointsCurrency ?? CurrencyEnum.POINTS,
price: redemption.localPrice.pointsPerStay, price: redemption.localPrice.pointsPerStay,
}, },
} }

View File

@@ -52,7 +52,11 @@ function add(...nums: (number | string | undefined)[]) {
}, 0) }, 0)
} }
export function getRoomPrice(roomRate: Product, isMember: boolean) { export function getRoomPrice(
roomRate: Product,
isMember: boolean,
pointsCurrency?: CurrencyEnum
) {
if (isMember && "member" in roomRate && roomRate.member) { if (isMember && "member" in roomRate && roomRate.member) {
let publicRate let publicRate
if ( if (
@@ -196,7 +200,7 @@ export function getRoomPrice(roomRate: Product, isMember: boolean) {
perNight: { perNight: {
requested: undefined, requested: undefined,
local: { local: {
currency: CurrencyEnum.POINTS, currency: pointsCurrency ?? CurrencyEnum.POINTS,
price: roomRate.redemption.localPrice.pointsPerStay, price: roomRate.redemption.localPrice.pointsPerStay,
additionalPrice: additionalPrice:
roomRate.redemption.localPrice.additionalPricePerStay, roomRate.redemption.localPrice.additionalPricePerStay,
@@ -207,7 +211,7 @@ export function getRoomPrice(roomRate: Product, isMember: boolean) {
perStay: { perStay: {
requested: undefined, requested: undefined,
local: { local: {
currency: CurrencyEnum.POINTS, currency: pointsCurrency ?? CurrencyEnum.POINTS,
price: roomRate.redemption.localPrice.pointsPerStay, price: roomRate.redemption.localPrice.pointsPerStay,
additionalPrice: additionalPrice:
roomRate.redemption.localPrice.additionalPricePerStay, roomRate.redemption.localPrice.additionalPricePerStay,
@@ -440,7 +444,11 @@ interface TRoomRedemption extends TRoom {
roomRate: RedemptionProduct roomRate: RedemptionProduct
} }
function getRedemptionPrice(rooms: TRoom[], nights: number) { function getRedemptionPrice(
rooms: TRoom[],
nights: number,
pointsCurrency?: CurrencyEnum
) {
return rooms return rooms
.filter((room): room is TRoomRedemption => "redemption" in room.roomRate) .filter((room): room is TRoomRedemption => "redemption" in room.roomRate)
.reduce<Price>( .reduce<Price>(
@@ -466,7 +474,7 @@ function getRedemptionPrice(rooms: TRoom[], nights: number) {
}, },
{ {
local: { local: {
currency: CurrencyEnum.POINTS, currency: pointsCurrency ?? CurrencyEnum.POINTS,
price: 0, price: 0,
}, },
requested: undefined, requested: undefined,
@@ -575,7 +583,8 @@ function getRegularPrice(rooms: TRoom[], isMember: boolean, nights: number) {
export function getTotalPrice( export function getTotalPrice(
rooms: TRoom[], rooms: TRoom[],
isMember: boolean, isMember: boolean,
nights: number nights: number,
pointsCurrency?: CurrencyEnum
) { ) {
const hasCorpChqRates = rooms.some( const hasCorpChqRates = rooms.some(
(room) => "corporateCheque" in room.roomRate (room) => "corporateCheque" in room.roomRate
@@ -586,7 +595,7 @@ export function getTotalPrice(
const hasRedemptionRates = rooms.some((room) => "redemption" in room.roomRate) const hasRedemptionRates = rooms.some((room) => "redemption" in room.roomRate)
if (hasRedemptionRates) { if (hasRedemptionRates) {
return getRedemptionPrice(rooms, nights) return getRedemptionPrice(rooms, nights, pointsCurrency)
} }
const hasVoucherRates = rooms.some((room) => "voucher" in room.roomRate) const hasVoucherRates = rooms.some((room) => "voucher" in room.roomRate)

View File

@@ -16,6 +16,7 @@ import {
writeToSessionStorage, writeToSessionStorage,
} from "./helpers" } from "./helpers"
import type { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import type { Lang } from "@scandic-hotels/common/constants/language" import type { Lang } from "@scandic-hotels/common/constants/language"
import type { BreakfastPackages } from "@scandic-hotels/trpc/routers/hotels/output" import type { BreakfastPackages } from "@scandic-hotels/trpc/routers/hotels/output"
import type { User } from "@scandic-hotels/trpc/types/user" import type { User } from "@scandic-hotels/trpc/types/user"
@@ -43,7 +44,8 @@ export function createDetailsStore(
searchParams: string, searchParams: string,
user: User | null, user: User | null,
breakfastPackages: BreakfastPackages, breakfastPackages: BreakfastPackages,
lang: Lang lang: Lang,
pointsCurrency?: CurrencyEnum
) { ) {
const isMember = !!user const isMember = !!user
const nights = dt(initialState.booking.toDate).diff( const nights = dt(initialState.booking.toDate).diff(
@@ -68,14 +70,23 @@ export function createDetailsStore(
...defaultGuestState, ...defaultGuestState,
phoneNumberCC: getDefaultCountryFromLang(lang), phoneNumberCC: getDefaultCountryFromLang(lang),
}, },
roomPrice: getRoomPrice(room.roomRate, isMember && idx === 0), roomPrice: getRoomPrice(
room.roomRate,
isMember && idx === 0,
pointsCurrency
),
specialRequest: { specialRequest: {
comment: "", comment: "",
}, },
} }
}) })
const initialTotalPrice = getTotalPrice(initialRooms, isMember, nights) const initialTotalPrice = getTotalPrice(
initialRooms,
isMember,
nights,
pointsCurrency
)
const availableBeds = initialState.rooms.reduce< const availableBeds = initialState.rooms.reduce<
DetailsState["availableBeds"] DetailsState["availableBeds"]
@@ -175,7 +186,8 @@ export function createDetailsStore(
state.totalPrice = getTotalPrice( state.totalPrice = getTotalPrice(
state.rooms.map((r) => r.room), state.rooms.map((r) => r.room),
isMember, isMember,
nights nights,
pointsCurrency
) )
const isAllStepsCompleted = checkRoomProgress( const isAllStepsCompleted = checkRoomProgress(
@@ -206,7 +218,8 @@ export function createDetailsStore(
} }
currentRoom.roomPrice = getRoomPrice( currentRoom.roomPrice = getRoomPrice(
currentRoom.roomRate, currentRoom.roomRate,
isValidMembershipNo || currentRoom.guest.join isValidMembershipNo || currentRoom.guest.join,
pointsCurrency
) )
const nights = dt(state.booking.toDate).diff( const nights = dt(state.booking.toDate).diff(
@@ -217,7 +230,8 @@ export function createDetailsStore(
state.totalPrice = getTotalPrice( state.totalPrice = getTotalPrice(
state.rooms.map((r) => r.room), state.rooms.map((r) => r.room),
isMember, isMember,
nights nights,
pointsCurrency
) )
writeToSessionStorage({ writeToSessionStorage({
@@ -240,7 +254,8 @@ export function createDetailsStore(
currentRoom.roomPrice = getRoomPrice( currentRoom.roomPrice = getRoomPrice(
currentRoom.roomRate, currentRoom.roomRate,
join || !!currentRoom.guest.membershipNo join || !!currentRoom.guest.membershipNo,
pointsCurrency
) )
const nights = dt(state.booking.toDate).diff( const nights = dt(state.booking.toDate).diff(
@@ -251,7 +266,8 @@ export function createDetailsStore(
state.totalPrice = getTotalPrice( state.totalPrice = getTotalPrice(
state.rooms.map((r) => r.room), state.rooms.map((r) => r.room),
isMember, isMember,
nights nights,
pointsCurrency
) )
writeToSessionStorage({ writeToSessionStorage({
@@ -316,7 +332,8 @@ export function createDetailsStore(
currentRoom.roomPrice = getRoomPrice( currentRoom.roomPrice = getRoomPrice(
currentRoom.roomRate, currentRoom.roomRate,
Boolean(data.join || data.membershipNo || isMemberAndRoomOne) Boolean(data.join || data.membershipNo || isMemberAndRoomOne),
pointsCurrency
) )
const nights = dt(state.booking.toDate).diff( const nights = dt(state.booking.toDate).diff(
@@ -327,7 +344,8 @@ export function createDetailsStore(
state.totalPrice = getTotalPrice( state.totalPrice = getTotalPrice(
state.rooms.map((r) => r.room), state.rooms.map((r) => r.room),
isMember, isMember,
nights nights,
pointsCurrency
) )
const isAllStepsCompleted = checkRoomProgress( const isAllStepsCompleted = checkRoomProgress(

View File

@@ -10,4 +10,5 @@ export enum CurrencyEnum {
Voucher = "Voucher", Voucher = "Voucher",
CC = "CC", CC = "CC",
Unknown = "Unknown", Unknown = "Unknown",
EUROBONUS = "EB Points",
} }

View File

@@ -22,6 +22,7 @@ import { FacilityToIcon } from '../../../FacilityToIcon'
import { HotelPin } from '../../../Map/types' import { HotelPin } from '../../../Map/types'
import { HotelPointsRow } from '../../HotelPointsRow' import { HotelPointsRow } from '../../HotelPointsRow'
import styles from './standaloneHotelCardDialog.module.css' import styles from './standaloneHotelCardDialog.module.css'
import { CurrencyEnum } from '@scandic-hotels/common/constants/currency'
interface StandaloneHotelCardProps { interface StandaloneHotelCardProps {
data: HotelPin data: HotelPin
@@ -29,6 +30,7 @@ interface StandaloneHotelCardProps {
isUserLoggedIn: boolean isUserLoggedIn: boolean
handleClose: () => void handleClose: () => void
onClick?: () => void onClick?: () => void
pointsCurrency?: CurrencyEnum
} }
export function StandaloneHotelCardDialog({ export function StandaloneHotelCardDialog({
@@ -37,6 +39,7 @@ export function StandaloneHotelCardDialog({
handleClose, handleClose,
isUserLoggedIn, isUserLoggedIn,
onClick, onClick,
pointsCurrency,
}: StandaloneHotelCardProps) { }: StandaloneHotelCardProps) {
const intl = useIntl() const intl = useIntl()
const [imageError, setImageError] = useState(false) const [imageError, setImageError] = useState(false)
@@ -224,7 +227,10 @@ export function StandaloneHotelCardDialog({
</Subtitle> </Subtitle>
)} )}
{redemptionPrice && ( {redemptionPrice && (
<HotelPointsRow pointsPerStay={redemptionPrice} /> <HotelPointsRow
pointsPerStay={redemptionPrice}
pointsCurrency={pointsCurrency}
/>
)} )}
</div> </div>
{shouldShowNotEnoughPoints ? ( {shouldShowNotEnoughPoints ? (

View File

@@ -4,16 +4,19 @@ import Caption from '../../Caption'
import Subtitle from '../../Subtitle' import Subtitle from '../../Subtitle'
import styles from './hotelPointsRow.module.css' import styles from './hotelPointsRow.module.css'
import { CurrencyEnum } from '@scandic-hotels/common/constants/currency'
export type PointsRowProps = { export type PointsRowProps = {
pointsPerStay: number pointsPerStay: number
additionalPricePerStay?: number additionalPricePerStay?: number
additionalPriceCurrency?: string additionalPriceCurrency?: string
pointsCurrency?: CurrencyEnum
} }
export function HotelPointsRow({ export function HotelPointsRow({
pointsPerStay, pointsPerStay,
additionalPricePerStay, additionalPricePerStay,
additionalPriceCurrency, additionalPriceCurrency,
pointsCurrency,
}: PointsRowProps) { }: PointsRowProps) {
const intl = useIntl() const intl = useIntl()
@@ -23,9 +26,10 @@ export function HotelPointsRow({
{pointsPerStay} {pointsPerStay}
</Subtitle> </Subtitle>
<Caption color="uiTextHighContrast"> <Caption color="uiTextHighContrast">
{intl.formatMessage({ {pointsCurrency ??
defaultMessage: 'Points', intl.formatMessage({
})} defaultMessage: 'Points',
})}
</Caption> </Caption>
{additionalPricePerStay ? ( {additionalPricePerStay ? (
<> <>

View File

@@ -106,6 +106,7 @@ export type HotelCardProps = {
state?: 'default' | 'active' state?: 'default' | 'active'
bookingCode?: string | null bookingCode?: string | null
isAlternative?: boolean isAlternative?: boolean
pointsCurrency?: CurrencyEnum
fullPrice: boolean fullPrice: boolean
lang: Lang lang: Lang
@@ -127,6 +128,7 @@ export const HotelCard = memo(
type = 'pageListing', type = 'pageListing',
bookingCode = '', bookingCode = '',
isAlternative, isAlternative,
pointsCurrency,
images, images,
lang, lang,
belowInfoSlot, belowInfoSlot,
@@ -307,6 +309,7 @@ export const HotelCard = memo(
additionalPriceCurrency={ additionalPriceCurrency={
redemption.localPrice.currency ?? undefined redemption.localPrice.currency ?? undefined
} }
pointsCurrency={pointsCurrency}
/> />
))} ))}
</div> </div>

View File

@@ -10,6 +10,7 @@ import type { HotelPin as HotelPinType } from '../../types'
import styles from './hotelListingMapContent.module.css' import styles from './hotelListingMapContent.module.css'
import { StandaloneHotelCardDialog } from '../../../HotelCard/HotelDialogCard/StandaloneHotelCardDialog' import { StandaloneHotelCardDialog } from '../../../HotelCard/HotelDialogCard/StandaloneHotelCardDialog'
import { Lang } from '@scandic-hotels/common/constants/language' import { Lang } from '@scandic-hotels/common/constants/language'
import { CurrencyEnum } from '@scandic-hotels/common/constants/currency'
export type HotelListingMapContentProps = { export type HotelListingMapContentProps = {
hotelPins: HotelPinType[] hotelPins: HotelPinType[]
@@ -17,6 +18,7 @@ export type HotelListingMapContentProps = {
hoveredHotel?: string | null hoveredHotel?: string | null
lang: Lang lang: Lang
isUserLoggedIn: boolean isUserLoggedIn: boolean
pointsCurrency?: CurrencyEnum
onClickHotel?: (hotelId: string) => void onClickHotel?: (hotelId: string) => void
setActiveHotel?: (args: { hotelName: string; hotelId: string } | null) => void setActiveHotel?: (args: { hotelName: string; hotelId: string } | null) => void
setHoveredHotel?: ( setHoveredHotel?: (
@@ -32,6 +34,7 @@ export function HotelListingMapContent({
setHoveredHotel, setHoveredHotel,
lang, lang,
onClickHotel, onClickHotel,
pointsCurrency,
}: HotelListingMapContentProps) { }: HotelListingMapContentProps) {
const isDesktop = useMediaQuery('(min-width: 900px)') const isDesktop = useMediaQuery('(min-width: 900px)')
@@ -104,6 +107,7 @@ export function HotelListingMapContent({
onClick={() => { onClick={() => {
onClickHotel?.(pin.operaId) onClickHotel?.(pin.operaId)
}} }}
pointsCurrency={pointsCurrency}
/> />
</InfoWindow> </InfoWindow>
)} )}

View File

@@ -18,6 +18,7 @@ import styles from './interactiveMap.module.css'
import { HotelPin, MarkerInfo, PointOfInterest } from '../types' import { HotelPin, MarkerInfo, PointOfInterest } from '../types'
import { Lang } from '@scandic-hotels/common/constants/language' import { Lang } from '@scandic-hotels/common/constants/language'
import { CurrencyEnum } from '@scandic-hotels/common/constants/currency'
export type InteractiveMapProps = { export type InteractiveMapProps = {
lang: Lang lang: Lang
@@ -27,6 +28,7 @@ export type InteractiveMapProps = {
} }
activePoi?: string | null activePoi?: string | null
hotelPins?: HotelPin[] hotelPins?: HotelPin[]
pointsCurrency?: CurrencyEnum
pointsOfInterest?: PointOfInterest[] pointsOfInterest?: PointOfInterest[]
markerInfo?: MarkerInfo markerInfo?: MarkerInfo
mapId: string mapId: string
@@ -73,6 +75,7 @@ export function InteractiveMap({
hoveredHotelPin, hoveredHotelPin,
activeHotelPin, activeHotelPin,
isUserLoggedIn, isUserLoggedIn,
pointsCurrency,
onClickHotel, onClickHotel,
onHoverHotelPin, onHoverHotelPin,
onSetActiveHotelPin, onSetActiveHotelPin,
@@ -122,6 +125,7 @@ export function InteractiveMap({
activeHotel={activeHotelPin} activeHotel={activeHotelPin}
hoveredHotel={hoveredHotelPin} hoveredHotel={hoveredHotelPin}
onClickHotel={onClickHotel} onClickHotel={onClickHotel}
pointsCurrency={pointsCurrency}
/> />
)} )}
{pointsOfInterest && markerInfo && ( {pointsOfInterest && markerInfo && (

View File

@@ -9,7 +9,8 @@ import { SEARCH_TYPE_REDEMPTION } from "../../../constants/booking"
import { AvailabilityEnum } from "../../../enums/selectHotel" import { AvailabilityEnum } from "../../../enums/selectHotel"
import { unauthorizedError } from "../../../errors" import { unauthorizedError } from "../../../errors"
import { safeProtectedServiceProcedure } from "../../../procedures" import { safeProtectedServiceProcedure } from "../../../procedures"
import { getVerifiedUser } from "../../user/utils/getVerifiedUser" import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
import { baseBookingSchema, baseRoomSchema, selectedRoomSchema } from "../input" import { baseBookingSchema, baseRoomSchema, selectedRoomSchema } from "../input"
import { getHotel } from "../services/getHotel" import { getHotel } from "../services/getHotel"
import { getRoomsAvailability } from "../services/getRoomsAvailability" import { getRoomsAvailability } from "../services/getRoomsAvailability"
@@ -37,13 +38,15 @@ export const enterDetails = safeProtectedServiceProcedure
.use(async ({ ctx, input, next }) => { .use(async ({ ctx, input, next }) => {
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) { if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
if (ctx.session?.token.access_token) { if (ctx.session?.token.access_token) {
const verifiedUser = await getVerifiedUser({ session: ctx.session }) const pointsValue = await getUserPointsBalance(ctx.session)
if (!verifiedUser?.error) { const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
if (pointsValue && token) {
return next({ return next({
ctx: { ctx: {
token: ctx.session.token.access_token, token: token,
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0, userPoints: pointsValue ?? 0,
}, },
input,
}) })
} }
} }

View File

@@ -7,7 +7,8 @@ import { env } from "../../../../env/server"
import { unauthorizedError } from "../../../errors" import { unauthorizedError } from "../../../errors"
import { safeProtectedServiceProcedure } from "../../../procedures" import { safeProtectedServiceProcedure } from "../../../procedures"
import { toApiLang } from "../../../utils" import { toApiLang } from "../../../utils"
import { getVerifiedUser } from "../../user/utils/getVerifiedUser" import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
import { getHotelsAvailabilityByCity } from "../services/getHotelsAvailabilityByCity" import { getHotelsAvailabilityByCity } from "../services/getHotelsAvailabilityByCity"
export type HotelsAvailabilityInputSchema = z.output< export type HotelsAvailabilityInputSchema = z.output<
@@ -68,12 +69,13 @@ export const hotelsByCity = safeProtectedServiceProcedure
.use(async ({ ctx, input, next }) => { .use(async ({ ctx, input, next }) => {
if (input.redemption) { if (input.redemption) {
if (ctx.session?.token.access_token) { if (ctx.session?.token.access_token) {
const verifiedUser = await getVerifiedUser({ session: ctx.session }) const pointsValue = await getUserPointsBalance(ctx.session)
if (!verifiedUser?.error) { const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
if (pointsValue && token) {
return next({ return next({
ctx: { ctx: {
token: ctx.session.token.access_token, token: token,
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0, userPoints: pointsValue ?? 0,
}, },
input, input,
}) })

View File

@@ -4,7 +4,8 @@ import { z } from "zod"
import { unauthorizedError } from "../../../errors" import { unauthorizedError } from "../../../errors"
import { safeProtectedServiceProcedure } from "../../../procedures" import { safeProtectedServiceProcedure } from "../../../procedures"
import { toApiLang } from "../../../utils" import { toApiLang } from "../../../utils"
import { getVerifiedUser } from "../../user/utils/getVerifiedUser" import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
import { getHotelsAvailabilityByHotelIds } from "../services/getHotelsAvailabilityByHotelIds" import { getHotelsAvailabilityByHotelIds } from "../services/getHotelsAvailabilityByHotelIds"
export type HotelsByHotelIdsAvailabilityInputSchema = z.output< export type HotelsByHotelIdsAvailabilityInputSchema = z.output<
@@ -65,12 +66,13 @@ export const hotelsByHotelIds = safeProtectedServiceProcedure
.use(async ({ ctx, input, next }) => { .use(async ({ ctx, input, next }) => {
if (input.redemption) { if (input.redemption) {
if (ctx.session?.token.access_token) { if (ctx.session?.token.access_token) {
const verifiedUser = await getVerifiedUser({ session: ctx.session }) const pointsValue = await getUserPointsBalance(ctx.session)
if (!verifiedUser?.error) { const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
if (pointsValue && token) {
return next({ return next({
ctx: { ctx: {
token: ctx.session.token.access_token, token: token,
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0, userPoints: pointsValue ?? 0,
}, },
input, input,
}) })

View File

@@ -6,7 +6,8 @@ import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { SEARCH_TYPE_REDEMPTION } from "../../../constants/booking" import { SEARCH_TYPE_REDEMPTION } from "../../../constants/booking"
import { unauthorizedError } from "../../../errors" import { unauthorizedError } from "../../../errors"
import { safeProtectedServiceProcedure } from "../../../procedures" import { safeProtectedServiceProcedure } from "../../../procedures"
import { getVerifiedUser } from "../../user/utils/getVerifiedUser" import { getRedemptionTokenSafely } from "../../../utils/getRedemptionTokenSafely"
import { getUserPointsBalance } from "../../../utils/getUserPointsBalance"
import { baseBookingSchema, baseRoomSchema, selectedRoomSchema } from "../input" import { baseBookingSchema, baseRoomSchema, selectedRoomSchema } from "../input"
import { getRoomsAvailability } from "../services/getRoomsAvailability" import { getRoomsAvailability } from "../services/getRoomsAvailability"
import { getSelectedRoomAvailability } from "../utils" import { getSelectedRoomAvailability } from "../utils"
@@ -24,13 +25,15 @@ export const myStay = safeProtectedServiceProcedure
.use(async ({ ctx, input, next }) => { .use(async ({ ctx, input, next }) => {
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) { if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
if (ctx.session?.token.access_token) { if (ctx.session?.token.access_token) {
const verifiedUser = await getVerifiedUser({ session: ctx.session }) const pointsValue = await getUserPointsBalance(ctx.session)
if (!verifiedUser?.error) { const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
if (pointsValue && token) {
return next({ return next({
ctx: { ctx: {
token: ctx.session.token.access_token, token: token,
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0, userPoints: pointsValue ?? 0,
}, },
input,
}) })
} }
} }

View File

@@ -5,7 +5,8 @@ import { Lang } from "@scandic-hotels/common/constants/language"
import { SEARCH_TYPE_REDEMPTION } from "../../../../constants/booking" import { SEARCH_TYPE_REDEMPTION } from "../../../../constants/booking"
import { unauthorizedError } from "../../../../errors" import { unauthorizedError } from "../../../../errors"
import { safeProtectedServiceProcedure } from "../../../../procedures" import { safeProtectedServiceProcedure } from "../../../../procedures"
import { getVerifiedUser } from "../../../user/utils/getVerifiedUser" import { getRedemptionTokenSafely } from "../../../../utils/getRedemptionTokenSafely"
import { getUserPointsBalance } from "../../../../utils/getUserPointsBalance"
import { baseBookingSchema, baseRoomSchema } from "../../input" import { baseBookingSchema, baseRoomSchema } from "../../input"
import { getRoomsAvailability } from "../../services/getRoomsAvailability" import { getRoomsAvailability } from "../../services/getRoomsAvailability"
import { mergeRoomTypes } from "../../utils" import { mergeRoomTypes } from "../../utils"
@@ -22,15 +23,15 @@ export const room = safeProtectedServiceProcedure
.use(async ({ ctx, input, next }) => { .use(async ({ ctx, input, next }) => {
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) { if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
if (ctx.session?.token.access_token) { if (ctx.session?.token.access_token) {
const verifiedUser = await getVerifiedUser({ const pointsValue = await getUserPointsBalance(ctx.session)
session: ctx.session, const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
}) if (pointsValue && token) {
if (!verifiedUser?.error) {
return next({ return next({
ctx: { ctx: {
token: ctx.session.token.access_token, token: token,
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0, userPoints: pointsValue ?? 0,
}, },
input,
}) })
} }
} }

View File

@@ -3,7 +3,8 @@ import "server-only"
import { SEARCH_TYPE_REDEMPTION } from "../../../../../constants/booking" import { SEARCH_TYPE_REDEMPTION } from "../../../../../constants/booking"
import { unauthorizedError } from "../../../../../errors" import { unauthorizedError } from "../../../../../errors"
import { safeProtectedServiceProcedure } from "../../../../../procedures" import { safeProtectedServiceProcedure } from "../../../../../procedures"
import { getVerifiedUser } from "../../../../user/utils/getVerifiedUser" import { getRedemptionTokenSafely } from "../../../../../utils/getRedemptionTokenSafely"
import { getUserPointsBalance } from "../../../../../utils/getUserPointsBalance"
import { getRoomsAvailability } from "../../../services/getRoomsAvailability" import { getRoomsAvailability } from "../../../services/getRoomsAvailability"
import { mergeRoomTypes } from "../../../utils" import { mergeRoomTypes } from "../../../utils"
import { selectRateRoomsAvailabilityInputSchema } from "./schema" import { selectRateRoomsAvailabilityInputSchema } from "./schema"
@@ -13,15 +14,15 @@ export const rooms = safeProtectedServiceProcedure
.use(async ({ ctx, input, next }) => { .use(async ({ ctx, input, next }) => {
if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) { if (input.booking.searchType === SEARCH_TYPE_REDEMPTION) {
if (ctx.session?.token.access_token) { if (ctx.session?.token.access_token) {
const verifiedUser = await getVerifiedUser({ const pointsValue = await getUserPointsBalance(ctx.session)
session: ctx.session, const token = getRedemptionTokenSafely(ctx.session, ctx.serviceToken)
}) if (pointsValue && token) {
if (!verifiedUser?.error) {
return next({ return next({
ctx: { ctx: {
token: ctx.session.token.access_token, token: token,
userPoints: verifiedUser?.data.membership?.currentPoints ?? 0, userPoints: pointsValue ?? 0,
}, },
input,
}) })
} }
} }

View File

@@ -5,6 +5,8 @@ import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { env } from "../../../../env/server" import { env } from "../../../../env/server"
import { protectedProcedure } from "../../../procedures" import { protectedProcedure } from "../../../procedures"
import type { Session } from "next-auth"
const outputSchema = z.object({ const outputSchema = z.object({
eurobonusNumber: z.string(), eurobonusNumber: z.string(),
firstName: z.string().optional(), firstName: z.string().optional(),
@@ -46,32 +48,67 @@ const outputSchema = z.object({
const sasLogger = createLogger("SAS") const sasLogger = createLogger("SAS")
const url = new URL("/api/scandic-partnership/v1/profile", env.SAS_API_ENDPOINT) const url = new URL("/api/scandic-partnership/v1/profile", env.SAS_API_ENDPOINT)
export const getEuroBonusProfile = protectedProcedure export async function getEuroBonusProfileData(session: Session) {
.output(outputSchema) if (session.token.loginType !== "sas") {
.query(async function ({ ctx }) { return {
if (ctx.session.token.loginType !== "sas") { error: {
throw new Error( message: `Failed to fetch EuroBonus profile, expected loginType to be "sas" but was ${session.token.loginType}`,
`Failed to fetch EuroBonus profile, expected loginType to be "sas" but was ${ctx.session.token.loginType}`
)
}
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": env.SAS_OCP_APIM,
Authorization: `Bearer ${ctx.session?.token?.access_token}`,
}, },
}) } as const
}
if (!response.ok) { if (!session.token.expires_at || session.token.expires_at < Date.now()) {
sasLogger.error( return {
`Failed to get EuroBonus profile, status: ${response.status}, statusText: ${response.statusText}` error: {
) message: "Token expired sas",
throw new Error("Failed to fetch EuroBonus profile", { },
cause: { status: response.status, statusText: response.statusText }, } as const
}) }
}
const data = await response.json() const response = await fetch(url, {
return data headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": env.SAS_OCP_APIM,
Authorization: `Bearer ${session?.token?.access_token}`,
},
}) })
if (!response.ok) {
sasLogger.error(
`Failed to get EuroBonus profile, status: ${response.status}, statusText: ${response.statusText}`
)
return {
error: {
message: "Failed to fetch EuroBonus profile",
cause: { status: response.status, statusText: response.statusText },
},
} as const
}
const responseJson = await response.json()
const data = outputSchema.safeParse(responseJson)
if (!data.success) {
sasLogger.error(
`Failed to parse EuroBonus profile, cause: ${data.error.cause}, message: ${data.error.message}`
)
return {
error: {
message: `Failed to parse EuroBonus profile: ${data.error.message}`,
cause: { status: response.status, statusText: response.statusText },
},
} as const
}
return data
}
export const getEuroBonusProfile = protectedProcedure.query(async function ({
ctx,
}) {
const verifiedSasUser = await getEuroBonusProfileData(ctx.session)
if ("error" in verifiedSasUser) {
throw new Error(verifiedSasUser.error?.message, {
cause: verifiedSasUser.error?.cause,
})
}
return verifiedSasUser.data
})

View File

@@ -9,7 +9,7 @@ import { creditCardsSchema } from "../../routers/user/output"
import { toApiLang } from "../../utils" import { toApiLang } from "../../utils"
import { encrypt } from "../../utils/encryption" import { encrypt } from "../../utils/encryption"
import { isValidSession } from "../../utils/session" import { isValidSession } from "../../utils/session"
import { getUserSchema } from "./output" import { getVerifiedUser } from "./utils/getVerifiedUser"
import { type FriendTransaction, getStaysSchema, type Stay } from "./output" import { type FriendTransaction, getStaysSchema, type Stay } from "./output"
import type { Lang } from "@scandic-hotels/common/constants/language" import type { Lang } from "@scandic-hotels/common/constants/language"
@@ -29,78 +29,6 @@ export async function getMembershipNumber(
return verifiedUser.data.membershipNumber return verifiedUser.data.membershipNumber
} }
export const getVerifiedUser = cache(
async ({
session,
includeExtendedPartnerData,
}: {
session: Session
includeExtendedPartnerData?: boolean
}) => {
const getVerifiedUserCounter = createCounter("user", "getVerifiedUser")
const metricsGetVerifiedUser = getVerifiedUserCounter.init()
metricsGetVerifiedUser.start()
const now = Date.now()
if (session.token.expires_at && session.token.expires_at < now) {
metricsGetVerifiedUser.dataError(`Token expired`)
return { error: true, cause: "token_expired" } as const
}
const apiResponse = await api.get(
api.endpoints.v2.Profile.profile,
{
headers: {
Authorization: `Bearer ${session.token.access_token}`,
},
},
includeExtendedPartnerData
? { includes: "extendedPartnerInformation" }
: {}
)
if (!apiResponse.ok) {
await metricsGetVerifiedUser.httpError(apiResponse)
if (apiResponse.status === 401) {
return { error: true, cause: "unauthorized" } as const
} else if (apiResponse.status === 403) {
return { error: true, cause: "forbidden" } as const
} else if (apiResponse.status === 404) {
return { error: true, cause: "notfound" } as const
}
return {
error: true,
cause: "unknown",
status: apiResponse.status,
} as const
}
const apiJson = await apiResponse.json()
if (!apiJson.data?.attributes) {
metricsGetVerifiedUser.dataError(
`Missing data attributes in API response`,
{
data: apiJson,
}
)
return null
}
const verifiedData = getUserSchema.safeParse(apiJson)
if (!verifiedData.success) {
metricsGetVerifiedUser.validationError(verifiedData.error)
return null
}
metricsGetVerifiedUser.success()
return verifiedData
}
)
export async function getPreviousStays( export async function getPreviousStays(
accessToken: string, accessToken: string,
limit: number = 10, limit: number = 10,

View File

@@ -0,0 +1,23 @@
import { isValidSession } from "./session"
import type { Session } from "next-auth"
export function getRedemptionTokenSafely(
session: Session,
serviceToken: string
): string | undefined {
if (!isValidSession(session)) return undefined
// ToDo- Get Curity based token when linked user is logged in
// const token =
// session.token.loginType === "sas"
// ? session.token.curity_access_token ?? serviceToken
// : session.token.access_token
const token =
session.token.loginType === "sas"
? serviceToken
: session.token.access_token
return token
}

View File

@@ -0,0 +1,27 @@
import { getEuroBonusProfileData } from "../routers/partners/sas/getEuroBonusProfile"
import { getVerifiedUser } from "../routers/user/utils/getVerifiedUser"
import { isValidSession } from "./session"
import type { Session } from "next-auth"
export async function getUserPointsBalance(
session: Session | null
): Promise<number | undefined> {
if (!isValidSession(session)) return undefined
const verifiedUser =
session.token.loginType === "sas"
? await getEuroBonusProfileData(session)
: await getVerifiedUser({ session })
if (!verifiedUser || "error" in verifiedUser) {
return undefined
}
const points =
"points" in verifiedUser.data
? verifiedUser.data.points.total
: verifiedUser.data.membership?.currentPoints
return points ?? 0
}