chore(SW-3321): Moved Select rate context to booking-flow package * chore(SW-3321): Moved Select rate context to booking-flow package * chore(SW-3321): Optimised code Approved-by: Joakim Jäderberg
110 lines
3.2 KiB
TypeScript
110 lines
3.2 KiB
TypeScript
import { differenceInCalendarDays, format, isWeekend } from "date-fns"
|
|
|
|
import {
|
|
TrackingChannelEnum,
|
|
type TrackingSDKHotelInfo,
|
|
type TrackingSDKPageData,
|
|
} from "@scandic-hotels/common/tracking/types"
|
|
import { ChildBedMapEnum } from "@scandic-hotels/trpc/enums/childBedMapEnum"
|
|
import { RoomPackageCodeEnum } from "@scandic-hotels/trpc/enums/roomFilter"
|
|
|
|
import type { Room } from "@scandic-hotels/booking-flow/types/components/selectRate/selectRate"
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
import type { Child } from "@scandic-hotels/trpc/types/child"
|
|
|
|
|
|
type ChildrenInRoom = (Child[] | null)[] | null
|
|
|
|
type SelectRateTrackingInput = {
|
|
lang: Lang
|
|
arrivalDate: Date
|
|
departureDate: Date
|
|
hotelId: string
|
|
hotelName: string
|
|
country: string | undefined
|
|
hotelCity: string | undefined
|
|
paramCity: string | undefined
|
|
bookingCode?: string
|
|
isRedemption?: boolean
|
|
rooms?: Room[]
|
|
}
|
|
|
|
export function getSelectRateTracking({
|
|
lang,
|
|
arrivalDate,
|
|
departureDate,
|
|
hotelId,
|
|
hotelName,
|
|
country,
|
|
hotelCity,
|
|
paramCity,
|
|
bookingCode,
|
|
isRedemption = false,
|
|
rooms = [],
|
|
}: SelectRateTrackingInput) {
|
|
const pageTrackingData: TrackingSDKPageData = {
|
|
channel: TrackingChannelEnum.hotelreservation,
|
|
domainLanguage: lang,
|
|
pageId: "select-rate",
|
|
pageName: "hotelreservation|select-rate",
|
|
pageType: "bookingroomsandratespage",
|
|
siteSections: "hotelreservation|select-rate",
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
let adultsInRoom: number[] = []
|
|
let childrenInRoom: ChildrenInRoom = null
|
|
if (rooms?.length) {
|
|
adultsInRoom = rooms.map((room) => room.adults ?? 0)
|
|
childrenInRoom = rooms.map((room) => room.childrenInRoom ?? null)
|
|
}
|
|
|
|
const hotelsTrackingData: TrackingSDKHotelInfo = {
|
|
ageOfChildren: childrenInRoom
|
|
?.map((c) => c?.map((k) => k.age).join(",") ?? "")
|
|
.join("|"),
|
|
arrivalDate: format(arrivalDate, "yyyy-MM-dd"),
|
|
bookingTypeofDay: isWeekend(arrivalDate) ? "weekend" : "weekday",
|
|
childBedPreference: childrenInRoom
|
|
?.map((c) => c?.map((k) => ChildBedMapEnum[k.bed]).join(",") ?? "")
|
|
.join("|"),
|
|
country,
|
|
departureDate: format(departureDate, "yyyy-MM-dd"),
|
|
duration: differenceInCalendarDays(departureDate, arrivalDate),
|
|
hotelID: hotelId,
|
|
leadTime: differenceInCalendarDays(arrivalDate, new Date()),
|
|
noOfAdults: adultsInRoom.join(","),
|
|
noOfChildren: childrenInRoom?.map((kids) => kids?.length ?? 0).join(","),
|
|
noOfRooms: rooms?.length ?? 0,
|
|
region: hotelCity,
|
|
searchTerm: paramCity ?? hotelName,
|
|
searchType: "hotel",
|
|
bookingCode: bookingCode ?? "n/a",
|
|
rewardNight: isRedemption ? "yes" : "no",
|
|
specialRoomType: rooms
|
|
?.map((room) => {
|
|
const packages = room.packages
|
|
?.map((pkg) => {
|
|
if (pkg === RoomPackageCodeEnum.ACCESSIBILITY_ROOM) {
|
|
return "accessibility"
|
|
} else if (pkg === RoomPackageCodeEnum.ALLERGY_ROOM) {
|
|
return "allergy friendly"
|
|
} else if (pkg === RoomPackageCodeEnum.PET_ROOM) {
|
|
return "pet room"
|
|
} else {
|
|
return ""
|
|
}
|
|
})
|
|
.join(",")
|
|
|
|
return packages ?? ""
|
|
})
|
|
.join("|"),
|
|
}
|
|
|
|
return {
|
|
hotelsTrackingData,
|
|
pageTrackingData,
|
|
}
|
|
}
|