fix(SW-1014): Fixed issue where google didn't found city.
This commit is contained in:
@@ -76,4 +76,7 @@ export const getRoomPackagesInputSchema = z.object({
|
||||
})
|
||||
export const getCityCoordinatesInputSchema = z.object({
|
||||
city: z.string(),
|
||||
hotel: z.object({
|
||||
address: z.string(),
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user