Spaces:
Sleeping
Sleeping
File size: 8,245 Bytes
0b355ec | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | const diningService = require('../services/diningService');
const logger = require('../utils/logger');
class DiningController {
async getMenus(req, res) {
try {
const { location, date } = req.query;
const targetDate = date ? new Date(date) : new Date();
let result;
if (location) {
result = await diningService.getMenuForLocation(location, targetDate);
result = this.organizeMenuByStation(result);
} else {
result = await diningService.getAllMenus(targetDate);
// Organize each location's menu by station
Object.keys(result).forEach(locationSlug => {
result[locationSlug] = this.organizeMenuByStation(result[locationSlug]);
});
}
res.json({
success: true,
data: result
});
} catch (error) {
logger.error('Get menus error:', error);
res.status(500).json({
success: false,
error: error.message || 'Failed to fetch dining menus'
});
}
}
async searchMenuItems(req, res) {
try {
const { q: query, date } = req.query;
if (!query || query.trim().length === 0) {
return res.status(400).json({
success: false,
error: 'Search query is required'
});
}
const targetDate = date ? new Date(date) : new Date();
const results = await diningService.searchMenuItems(query.trim(), targetDate);
res.json({
success: true,
data: {
query: query.trim(),
date: targetDate.toISOString().split('T')[0],
results,
count: results.length
}
});
} catch (error) {
logger.error('Search menu items error:', error);
res.status(500).json({
success: false,
error: 'Failed to search menu items'
});
}
}
async getDiningHours(req, res) {
try {
const hours = diningService.getDiningHours();
res.json({
success: true,
data: hours
});
} catch (error) {
logger.error('Get dining hours error:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch dining hours'
});
}
}
async getDiningLocations(req, res) {
try {
const locations = diningService.diningLocations.map(location => ({
name: location.name,
slug: location.slug,
id: location.id
}));
res.json({
success: true,
data: locations
});
} catch (error) {
logger.error('Get dining locations error:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch dining locations'
});
}
}
async getMenuForDate(req, res) {
try {
const { location } = req.params;
const { date } = req.params;
const targetDate = new Date(date);
if (isNaN(targetDate.getTime())) {
return res.status(400).json({
success: false,
error: 'Invalid date format. Use YYYY-MM-DD'
});
}
const menu = await diningService.getMenuForLocation(location, targetDate);
const organizedMenu = this.organizeMenuByStation(menu);
res.json({
success: true,
data: organizedMenu
});
} catch (error) {
logger.error('Get menu for date error:', error);
res.status(500).json({
success: false,
error: error.message || 'Failed to fetch menu for specified date'
});
}
}
async getFilteredMenus(req, res) {
try {
const {
dietary = '',
exclude = '',
station = '',
mealType = '',
date,
location
} = req.query;
const targetDate = date ? new Date(date) : new Date();
const filters = {
dietaryInfo: dietary ? dietary.split(',').map(d => d.trim()) : [],
excludeAllergens: exclude ? exclude.split(',').map(a => a.trim()) : [],
station: station || null,
mealType: mealType || null
};
let menus;
if (location) {
const menu = await diningService.getMenuForLocation(location, targetDate);
menus = { [location]: menu };
} else {
menus = await diningService.getAllMenus(targetDate);
}
// Apply filters to each menu
const filteredMenus = {};
for (const [locationSlug, menu] of Object.entries(menus)) {
if (menu && menu.items) {
const filteredItems = diningService.applyDietaryFilters(menu.items, filters);
const filteredMenu = {
...menu,
items: filteredItems,
originalItemCount: menu.items.length,
filteredItemCount: filteredItems.length,
filters: filters
};
// Organize filtered menu by station
filteredMenus[locationSlug] = this.organizeMenuByStation(filteredMenu);
}
}
res.json({
success: true,
data: filteredMenus,
appliedFilters: filters,
totalLocations: Object.keys(filteredMenus).length
});
} catch (error) {
logger.error('Get filtered menus error:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch filtered menus'
});
}
}
async getDietarySummary(req, res) {
try {
const { date } = req.query;
const targetDate = date ? new Date(date) : new Date();
const summary = await diningService.getDietarySummaryByLocation(targetDate);
res.json({
success: true,
data: summary
});
} catch (error) {
logger.error('Get dietary summary error:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch dietary summary'
});
}
}
organizeMenuByStation(menuData) {
if (!menuData || !menuData.items || menuData.items.length === 0) {
return menuData;
}
// Group items by station
const stationGroups = {};
menuData.items.forEach(item => {
const station = item.station || 'Main';
if (!stationGroups[station]) {
stationGroups[station] = [];
}
stationGroups[station].push(item);
});
// Create organized structure with station names and their items
const organizedStations = {};
Object.keys(stationGroups).forEach(stationName => {
// Format station names for better display (e.g., "buon-appetito" -> "Buon Appetito Specials")
const formattedStationName = this.formatStationName(stationName);
organizedStations[formattedStationName] = {
name: formattedStationName,
items: stationGroups[stationName],
itemCount: stationGroups[stationName].length
};
});
// Return the menu data with organized stations
return {
...menuData,
stations: organizedStations,
stationCount: Object.keys(organizedStations).length,
items: menuData.items // Keep original items array for backward compatibility
};
}
formatStationName(stationName) {
// Handle common UGA dining station names
const stationMap = {
'buon appetito': 'Buon Appetito Specials',
'the wok': 'The Wok Specials',
'grill': 'Grill Specials',
'pizza': 'Pizza Station',
'salad bar': 'Salad Bar',
'deli': 'Deli Station',
'main': 'Main Entrees',
'soup': 'Soup Station',
'dessert': 'Desserts',
'beverages': 'Beverages',
'breakfast': 'Breakfast Items'
};
const lowerStation = stationName.toLowerCase().trim();
// Check if we have a specific mapping
if (stationMap[lowerStation]) {
return stationMap[lowerStation];
}
// If no mapping, format by capitalizing each word and adding "Specials" if needed
const formatted = stationName
.split(/[\s-_]+/)
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
// Add "Specials" suffix if it doesn't already end with common station suffixes
if (!formatted.match(/(Specials|Station|Bar|Items|Entrees|Desserts|Beverages)$/i)) {
return formatted + ' Specials';
}
return formatted;
}
}
module.exports = new DiningController(); |