Možnost zvolit pouze jednu variantu obědu

This commit is contained in:
2023-07-30 21:24:15 +02:00
parent c0efb01803
commit 028186c8ea
2 changed files with 25 additions and 1 deletions

View File

@@ -285,6 +285,24 @@ export function removeChoice(login: string, location: Locations, foodIndex: numb
return data;
}
/**
* Odstraní kompletně volbu uživatele.
*
* @param login login uživatele
*/
function removeChoiceIfPresent(login: string) {
const today = formatDate(getToday());
let data: ClientData = db.get(today);
for (const key of Object.keys(data.choices)) {
if (login in data.choices[key]) {
delete data.choices[key][login];
if (Object.keys(data.choices[key]).length === 0) {
delete data.choices[key];
}
}
}
}
/**
* Přidá volbu uživatele.
*
@@ -295,6 +313,10 @@ export function removeChoice(login: string, location: Locations, foodIndex: numb
*/
export function addChoice(login: string, location: Locations, foodIndex?: number) {
initIfNeeded();
// Pokud měníme pouze lokaci, mažeme případné předchozí
if (foodIndex == null) {
removeChoiceIfPresent(login);
}
const today = formatDate(getToday());
let data: ClientData = db.get(today);
if (!(location in data.choices)) {