feat: vylepšení Pizza day
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getIsWeekend, getWeekNumber } from "./utils";
|
||||
import { InsufficientPermissions, PizzaDayConflictError, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getIsWeekend, getWeekNumber } from "./utils";
|
||||
import getStorage from "./storage";
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuZastavkaUmichala, getMenuSenkSerikova } from "./restaurants";
|
||||
import { getTodayMock } from "./mock";
|
||||
import { ClientData, DepartureTime, LunchChoice, Restaurant, RestaurantDayMenu, WeekMenu } from "../../types/gen/types.gen";
|
||||
import { removeAllUserPizzas } from "./pizza";
|
||||
import { ClientData, DepartureTime, LunchChoice, PizzaDayState, Restaurant, RestaurantDayMenu, WeekMenu } from "../../types/gen/types.gen";
|
||||
|
||||
const storage = getStorage();
|
||||
const MENU_PREFIX = 'menu';
|
||||
@@ -99,7 +100,7 @@ export async function saveRestaurantWeekMenu(restaurant: Restaurant, date: Date,
|
||||
const now = new Date().getTime();
|
||||
let weekMenu = await getMenu(date);
|
||||
weekMenu ??= [{}, {}, {}, {}, {}];
|
||||
|
||||
|
||||
// Inicializace struktury pro restauraci
|
||||
for (let i = 0; i < 5; i++) {
|
||||
weekMenu[i] ??= {};
|
||||
@@ -109,12 +110,12 @@ export async function saveRestaurantWeekMenu(restaurant: Restaurant, date: Date,
|
||||
food: [],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Uložení dat pro všechny dny
|
||||
for (let i = 0; i < weekData.length && i < weekMenu.length; i++) {
|
||||
weekMenu[i][restaurant]!.food = weekData[i];
|
||||
weekMenu[i][restaurant]!.lastUpdate = now;
|
||||
|
||||
|
||||
// Detekce uzavření pro každou restauraci
|
||||
switch (restaurant) {
|
||||
case 'SLADOVNICKA':
|
||||
@@ -139,7 +140,7 @@ export async function saveRestaurantWeekMenu(restaurant: Restaurant, date: Date,
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Uložení do storage
|
||||
await storage.setData(getMenuKey(date), weekMenu);
|
||||
}
|
||||
@@ -153,7 +154,7 @@ export async function saveRestaurantWeekMenu(restaurant: Restaurant, date: Date,
|
||||
*/
|
||||
async function fetchRestaurantWeekMenu(restaurant: Restaurant, firstDay: Date): Promise<any[]> {
|
||||
const mock = process.env.MOCK_DATA === 'true';
|
||||
|
||||
|
||||
switch (restaurant) {
|
||||
case 'SLADOVNICKA':
|
||||
return await getMenuSladovnicka(firstDay, mock);
|
||||
@@ -207,15 +208,15 @@ export async function getRestaurantMenu(restaurant: Restaurant, date?: Date, for
|
||||
(!existingMenu?.food?.length && !existingMenu?.closed && lastFetchExpired);
|
||||
if (shouldFetch) {
|
||||
const firstDay = getFirstWorkDayOfWeek(usedDate);
|
||||
|
||||
|
||||
try {
|
||||
const restaurantWeekFood = await fetchRestaurantWeekMenu(restaurant, firstDay);
|
||||
|
||||
|
||||
// Aktualizace menu pro všechny dny
|
||||
for (let i = 0; i < restaurantWeekFood.length && i < weekMenu.length; i++) {
|
||||
weekMenu[i][restaurant]!.food = restaurantWeekFood[i];
|
||||
weekMenu[i][restaurant]!.lastUpdate = now;
|
||||
|
||||
|
||||
// Detekce uzavření pro každou restauraci
|
||||
switch (restaurant) {
|
||||
case 'SLADOVNICKA':
|
||||
@@ -240,7 +241,7 @@ export async function getRestaurantMenu(restaurant: Restaurant, date?: Date, for
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Uložení do storage
|
||||
await storage.setData(getMenuKey(usedDate), weekMenu);
|
||||
} catch (e: any) {
|
||||
@@ -405,6 +406,29 @@ export async function addChoice(login: string, trusted: boolean, locationKey: Lu
|
||||
let data = await getClientData(usedDate);
|
||||
validateTrusted(data, login, trusted);
|
||||
await validateFoodIndex(locationKey, foodIndex, date);
|
||||
|
||||
// Pokud uživatel měl vybranou PIZZA a mění na něco jiného
|
||||
const hadPizzaChoice = data.choices.PIZZA && login in data.choices.PIZZA;
|
||||
if (hadPizzaChoice && locationKey !== LunchChoice.PIZZA && foodIndex == null) {
|
||||
// Kontrola, zda existuje Pizza day a uživatel je jeho zakladatel
|
||||
if (data.pizzaDay && data.pizzaDay.creator === login) {
|
||||
// Pokud Pizza day není ve stavu CREATED, nelze změnit volbu
|
||||
if (data.pizzaDay.state !== PizzaDayState.CREATED) {
|
||||
throw new PizzaDayConflictError(
|
||||
`Nelze změnit volbu. Pizza day je ve stavu "${data.pizzaDay.state}" a musí být nejprve dokončen nebo smazán.`
|
||||
);
|
||||
}
|
||||
// Pizza day je ve stavu CREATED - bude smazán frontendem po potvrzení uživatelem
|
||||
// (frontend volá nejprve deletePizzaDay, pak teprve addChoice)
|
||||
}
|
||||
|
||||
// Smažeme pizzy uživatele (pokud Pizza day nebyl založen tímto uživatelem,
|
||||
// nebo byl již smazán frontendem)
|
||||
await removeAllUserPizzas(login, usedDate);
|
||||
// Znovu načteme data, protože removeAllUserPizzas je upravila
|
||||
data = await getClientData(usedDate);
|
||||
}
|
||||
|
||||
// Pokud měníme pouze lokaci, mažeme případné předchozí
|
||||
if (foodIndex == null) {
|
||||
data = await removeChoiceIfPresent(login, usedDate);
|
||||
|
||||
Reference in New Issue
Block a user