diff --git a/client/src/App.tsx b/client/src/App.tsx index f80daea..2b3933a 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -348,8 +348,9 @@ function App() { const hideSoups = settings?.hideSoups; content =
{f.amount} | {f.name} | {f.price} | diff --git a/server/src/service.ts b/server/src/service.ts index f201446..c4b3a38 100644 --- a/server/src/service.ts +++ b/server/src/service.ts @@ -331,6 +331,7 @@ export async function addChoice(login: string, trusted: boolean, locationKey: Lo const selectedDate = formatDate(usedDate); let data: DayData = await storage.getData(selectedDate); validateTrusted(data, login, trusted); + await validateFoodIndex(locationKey, foodIndex, date); // Pokud měníme pouze lokaci, mažeme případné předchozí if (foodIndex == null) { data = await removeChoiceIfPresent(login, selectedDate); @@ -358,6 +359,24 @@ export async function addChoice(login: string, trusted: boolean, locationKey: Lo return data; } +/** + * Zvaliduje platnost indexu jídla pro vybranou lokalitu a datum. + * + * @param locationKey vybraná lokalita + * @param foodIndex index jídla pro danou lokalitu + * @param date datum, pro které je validace prováděna + */ +async function validateFoodIndex(locationKey: LocationKey, foodIndex?: number, date?: Date) { + if (locationKey in Restaurants && foodIndex != null) { + const usedDate = date ?? getToday(); + const restaurantKey = Restaurants[locationKey as keyof typeof Restaurants] + const menu = await getRestaurantMenu(restaurantKey, usedDate); + if (foodIndex > (menu.food.length - 1)) { + throw new Error(`Neplatný index ${foodIndex} pro lokalitu ${locationKey}`); + } + } +} + /** * Aktualizuje poznámku k aktuálně vybrané možnosti. *