56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
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: <string> }
|
|
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,
|
|
};
|
|
|