This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user