Spaces:
Sleeping

edi.chat.test / backend /src /controllers /diningController.js
Claude Code Bot
Claude
Deploy edi.chat UGA AI Assistant to Hugging Face Spaces
0b355ec
Raw History Blame Contribute Delete
8.25 kB
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();