65 lines
1.9 KiB
TypeScript
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
|
|
}
|
|
}
|