Spaces:
Sleeping

edi.chat.test / backend /src /models /DiningMenu.js
Claude Code Bot
Claude
Deploy edi.chat UGA AI Assistant to Hugging Face Spaces
0b355ec
Raw History Blame Contribute Delete
6.63 kB
const mongoose = require('mongoose');
const menuItemSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true,
index: true
},
description: {
type: String,
trim: true
},
station: {
type: String,
required: true,
trim: true,
index: true
},
dietaryInfo: [{
type: String,
enum: ['vegan', 'vegetarian', 'gluten-free', 'dairy-free', 'nut-free', 'halal', 'kosher', 'heart-healthy']
}],
nutritionInfo: {
calories: Number,
protein: Number,
carbs: Number,
fat: Number,
sodium: Number,
fiber: Number
},
allergens: [{
type: String,
enum: ['milk', 'eggs', 'fish', 'shellfish', 'tree-nuts', 'peanuts', 'wheat', 'soybeans', 'sesame']
}],
price: {
type: Number,
min: 0
},
availability: {
breakfast: { type: Boolean, default: false },
lunch: { type: Boolean, default: false },
dinner: { type: Boolean, default: false },
lateNight: { type: Boolean, default: false }
}
}, { _id: false });
const diningMenuSchema = new mongoose.Schema({
location: {
name: {
type: String,
required: true,
trim: true
},
slug: {
type: String,
required: true,
trim: true,
lowercase: true
},
id: {
type: String,
required: true,
trim: true
}
},
date: {
type: Date,
required: true,
index: true
},
dateString: {
type: String,
required: true,
index: true
},
mealTypes: [{
type: String,
enum: ['breakfast', 'lunch', 'dinner', 'brunch', 'lateNight'],
required: true
}],
items: [menuItemSchema],
metadata: {
lastUpdated: {
type: Date,
default: Date.now
},
source: {
type: String,
enum: ['nutrislice-api', 'web-scraping', 'manual', 'fallback'],
default: 'nutrislice-api'
},
itemCount: {
type: Number,
default: 0
},
stationCount: {
type: Number,
default: 0
}
},
isActive: {
type: Boolean,
default: true
}
}, {
timestamps: true
});
// Compound indexes for efficient querying
diningMenuSchema.index({ 'location.slug': 1, dateString: 1 }, { unique: true });
diningMenuSchema.index({ date: 1, 'location.slug': 1 });
diningMenuSchema.index({ dateString: 1, isActive: 1 });
diningMenuSchema.index({ 'items.name': 'text', 'items.description': 'text', 'items.station': 'text' });
// Pre-save middleware to set computed fields
diningMenuSchema.pre('save', function(next) {
if (this.isModified('date')) {
this.dateString = this.date.toISOString().split('T')[0];
}
if (this.isModified('items')) {
this.metadata.itemCount = this.items.length;
this.metadata.stationCount = [...new Set(this.items.map(item => item.station))].length;
}
this.metadata.lastUpdated = new Date();
next();
});
// Static methods
diningMenuSchema.statics.findByLocationAndDate = function(locationSlug, date) {
const dateString = new Date(date).toISOString().split('T')[0];
return this.findOne({
'location.slug': locationSlug,
dateString: dateString,
isActive: true
});
};
diningMenuSchema.statics.findByDate = function(date) {
const dateString = new Date(date).toISOString().split('T')[0];
return this.find({
dateString: dateString,
isActive: true
}).sort({ 'location.name': 1 });
};
diningMenuSchema.statics.searchItems = function(query, date = new Date()) {
const dateString = new Date(date).toISOString().split('T')[0];
return this.aggregate([
{
$match: {
dateString: dateString,
isActive: true,
$text: { $search: query }
}
},
{
$unwind: '$items'
},
{
$match: {
$or: [
{ 'items.name': { $regex: query, $options: 'i' } },
{ 'items.description': { $regex: query, $options: 'i' } },
{ 'items.station': { $regex: query, $options: 'i' } }
]
}
},
{
$project: {
location: '$location',
date: '$date',
item: '$items'
}
}
]);
};
diningMenuSchema.statics.getLocationSummary = function(locationSlug, date = new Date()) {
const dateString = new Date(date).toISOString().split('T')[0];
return this.aggregate([
{
$match: {
'location.slug': locationSlug,
dateString: dateString,
isActive: true
}
},
{
$project: {
location: 1,
date: 1,
mealTypes: 1,
itemCount: '$metadata.itemCount',
stationCount: '$metadata.stationCount',
stations: {
$reduce: {
input: '$items',
initialValue: [],
in: {
$setUnion: ['$$value', ['$$this.station']]
}
}
},
dietaryOptions: {
$reduce: {
input: '$items.dietaryInfo',
initialValue: [],
in: {
$setUnion: ['$$value', '$$this']
}
}
}
}
}
]);
};
// Instance methods
diningMenuSchema.methods.getItemsByStation = function() {
const stationMap = new Map();
this.items.forEach(item => {
if (!stationMap.has(item.station)) {
stationMap.set(item.station, []);
}
stationMap.get(item.station).push(item);
});
return Object.fromEntries(stationMap);
};
diningMenuSchema.methods.getItemsByMealType = function() {
const mealMap = {
breakfast: [],
lunch: [],
dinner: [],
lateNight: []
};
this.items.forEach(item => {
Object.keys(item.availability).forEach(meal => {
if (item.availability[meal] && mealMap[meal]) {
mealMap[meal].push(item);
}
});
});
return mealMap;
};
diningMenuSchema.methods.getFilteredItems = function(filters = {}) {
let filteredItems = [...this.items];
if (filters.dietaryInfo && filters.dietaryInfo.length > 0) {
filteredItems = filteredItems.filter(item =>
filters.dietaryInfo.some(diet => item.dietaryInfo.includes(diet))
);
}
if (filters.excludeAllergens && filters.excludeAllergens.length > 0) {
filteredItems = filteredItems.filter(item =>
!filters.excludeAllergens.some(allergen => item.allergens.includes(allergen))
);
}
if (filters.station) {
filteredItems = filteredItems.filter(item =>
item.station.toLowerCase().includes(filters.station.toLowerCase())
);
}
if (filters.mealType) {
filteredItems = filteredItems.filter(item =>
item.availability[filters.mealType]
);
}
return filteredItems;
};
module.exports = mongoose.model('DiningMenu', diningMenuSchema);