File size: 1,423 Bytes
ca1a2dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import os
import json
from pymongo import MongoClient
from dotenv import load_dotenv
from trip_utils import format_trip_summary
load_dotenv()
client = MongoClient(os.getenv("MONGODB_URI"))
db = client.get_database()
collection = db.get_collection("travelrecords")
def fetch_trips_by_location(city: str) -> str:
"""
Fetch all trip records to a given city across all users, using name fuzzy match
AND exact latitude/longitude match to ensure consistent location.
"""
reference = collection.find_one({
"$or": [
{"destinationName": {"$regex": city, "$options": "i"}},
{"locationName": {"$regex": city, "$options": "i"}}
],
"latitude": {"$exists": True},
"longitude": {"$exists": True}
})
if not reference:
return json.dumps({
"type": "text",
"message": f"No travel records found for {city}."
})
lat = reference.get("latitude")
lon = reference.get("longitude")
records = list(collection.find({
"latitude": lat,
"longitude": lon
}))
if not records:
return json.dumps({
"type": "text",
"message": f"No travel records found for {city}."
})
summaries = [format_trip_summary(rec) for rec in records]
result = "\n---\n".join(summaries)
return json.dumps({
"type": "text",
"message": result
}) |