const { MongoClient } = require('mongodb'); const DEFAULT_EXTERNAL_DB_NAME = 'reallives_serverless_eb'; const DEFAULT_EXTERNAL_COLLECTION = 'Food'; function getExternalMongoUri() { return process.env.MONGODB_URI || ''; } function getCollection() { return process.env.FOOD_EXTERNAL_COLLECTION || DEFAULT_EXTERNAL_COLLECTION; } function getDbName() { return process.env.FOOD_EXTERNAL_DB || DEFAULT_EXTERNAL_DB_NAME; } /** * Fetch existing food record for an event from external DB. * * @param {string} eventId * @returns {Promise<{ location?: string, recommendations?: string, restaurants?: any[], raw?: any } | null>} */ async function fetchFoodRecordForEvent(eventId) { const eventIdStr = eventId != null ? String(eventId) : ''; if (!eventIdStr) return null; const mongoUri = getExternalMongoUri(); if (!mongoUri) return null; const client = new MongoClient(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true }); await client.connect(); try { const db = client.db(getDbName()); const coll = db.collection(getCollection()); // The external doc created in foodRecommendationService uses { eventId: } const doc = await coll.findOne({ eventId: eventIdStr }); if (!doc) return null; return { location: doc.location, recommendations: doc.recommendations, restaurants: doc.restaurants, raw: doc, }; } finally { await client.close(); } } module.exports = { fetchFoodRecordForEvent, };