File size: 973 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
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
    })