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 summarize_trip(person_name: str, city: str) -> str: | |
"""Summarize a user's trip to a given city using MongoDB data.""" | |
records = list(collection.find({ | |
"name": {"$regex": person_name, "$options": "i"}, | |
"$or": [ | |
{"destinationName": city}, | |
{"locationName": city} | |
] | |
})) | |
if not records: | |
return json.dumps({ | |
"type": "text", | |
"message": f"No travel records found for {person_name} in {city}." | |
}) | |
full_summaries = [format_trip_summary(rec) for rec in records] | |
final_summary = "\n---\n".join(full_summaries) | |
return json.dumps({ | |
"type": "text", | |
"message": final_summary | |
}) | |