fix(SW-1014): Fixed issue where google didn't found city.

This commit is contained in:
Pontus Dreij
2024-11-25 15:38:08 +01:00
parent 0a35243d88
commit 0ef12d4a29
7 changed files with 50 additions and 9 deletions

View File

@@ -76,4 +76,7 @@ export const getRoomPackagesInputSchema = z.object({
})
export const getCityCoordinatesInputSchema = z.object({
city: z.string(),
hotel: z.object({
address: z.string(),
}),
})

View File

@@ -1087,14 +1087,37 @@ export const hotelQueryRouter = router({
.input(getCityCoordinatesInputSchema)
.query(async function ({ input }) {
const apiKey = process.env.GOOGLE_STATIC_MAP_KEY
const { city } = input
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(city)}&key=${apiKey}`
const { city, hotel } = input
const response = await fetch(url)
const data = await response.json()
const { lat, lng } = data.results[0].geometry.location
async function fetchCoordinates(address: string) {
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${apiKey}`
const response = await fetch(url)
const data = await response.json()
return { lat, lng }
if (data.status !== "OK") {
console.error(`Geocode error: ${data.status}`)
return null
}
const location = data.results[0]?.geometry?.location
if (!location) {
console.error("No location found in geocode response")
return null
}
return location
}
let location = await fetchCoordinates(city)
if (!location) {
location = await fetchCoordinates(`${city}, ${hotel.address}`)
}
if (!location) {
throw new Error("Unable to fetch coordinates")
}
return location
}),
}),
})