From 8dda6b1014ac923fe5d2420c8319797ad9f4ac76 Mon Sep 17 00:00:00 2001 From: Martin Berka Date: Thu, 27 Feb 2025 21:19:38 +0100 Subject: [PATCH] Oprava #7 --- client/src/App.tsx | 5 +++-- server/src/service.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) 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 = - {menu.food.filter(f => (hideSoups ? !f.isSoup : true)).map((f: any, index: number) => - doAddClickFoodChoice(location, hideSoups ? index + 1 : index)}> + {menu.food.map((f: Food, index: number) => + (!hideSoups || !f.isSoup) && + doAddClickFoodChoice(location, index)}> 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. *
{f.amount} {f.name} {f.price}