Files
web/packages/trpc/lib/routers/hotels/schemas/hotel/poi.ts
Erik Tiekstra 8180271b0f fix(SW-3203): Added property id to pointOfInterestSchema to handle identical names
Approved-by: Matilda Landström
Approved-by: Chuma Mcphoy (We Ahead)
2025-08-26 05:58:25 +00:00

65 lines
1.9 KiB
TypeScript

import { z } from "zod"
import { nullableNumberValidator } from "@scandic-hotels/common/utils/zod/numberValidator"
import { nullableStringValidator } from "@scandic-hotels/common/utils/zod/stringValidator"
import { PointOfInterestGroupEnum } from "../../../../enums/pointOfInterest"
import { locationSchema } from "./location"
export const pointOfInterestSchema = z
.object({
category: z.object({
name: nullableStringValidator,
}),
distance: nullableNumberValidator,
location: locationSchema,
name: nullableStringValidator,
})
.transform((poi) => ({
id: `${poi.name}-${poi.location.latitude}-${poi.location.longitude}`,
categoryName: poi.category.name,
coordinates: {
lat: poi.location.latitude,
lng: poi.location.longitude,
},
distance: poi.distance,
group: getPoiGroupByCategoryName(poi.category.name),
name: poi.name,
}))
export const pointOfInterestsSchema = z
.array(pointOfInterestSchema)
.nullish()
.transform((arr) => (arr ? arr.filter(Boolean) : []))
.transform((pois) =>
pois.sort((a, b) => (a.distance ?? 0) - (b.distance ?? 0))
)
function getPoiGroupByCategoryName(category: string | undefined) {
if (!category) return PointOfInterestGroupEnum.LOCATION
switch (category) {
case "Airport":
case "Bus terminal":
case "Transportations":
return PointOfInterestGroupEnum.PUBLIC_TRANSPORT
case "Amusement park":
case "Museum":
case "Sports":
case "Theatre":
case "Tourist":
case "Zoo":
return PointOfInterestGroupEnum.ATTRACTIONS
case "Nearby companies":
case "Fair":
return PointOfInterestGroupEnum.BUSINESS
case "Parking / Garage":
return PointOfInterestGroupEnum.PARKING
case "Shopping":
case "Restaurant":
return PointOfInterestGroupEnum.SHOPPING_DINING
case "Hospital":
default:
return PointOfInterestGroupEnum.LOCATION
}
}