feat: detekce starého menu TechTower, příznak isStale
Pokud TechTower vrátí menu z jiného týdne, uloží data s příznakem isStale a zobrazí varování "Data jsou z minulého týdne" místo chybové hlášky. Odstraněno staré varování o datech starších 24 hodin.
This commit is contained in:
+17
-8
@@ -1,6 +1,6 @@
|
||||
import { InsufficientPermissions, PizzaDayConflictError, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getIsWeekend, getWeekNumber } from "./utils";
|
||||
import getStorage from "./storage";
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuZastavkaUmichala, getMenuSenkSerikova } from "./restaurants";
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuZastavkaUmichala, getMenuSenkSerikova, StaleWeekError } from "./restaurants";
|
||||
import { getTodayMock } from "./mock";
|
||||
import { removeAllUserPizzas } from "./pizza";
|
||||
import { ClientData, DepartureTime, LunchChoice, PizzaDayState, Restaurant, RestaurantDayMenu, WeekMenu } from "../../types/gen/types.gen";
|
||||
@@ -216,6 +216,7 @@ export async function getRestaurantMenu(restaurant: Restaurant, date?: Date, for
|
||||
for (let i = 0; i < restaurantWeekFood.length && i < weekMenu.length; i++) {
|
||||
weekMenu[i][restaurant]!.food = restaurantWeekFood[i];
|
||||
weekMenu[i][restaurant]!.lastUpdate = now;
|
||||
weekMenu[i][restaurant]!.isStale = false;
|
||||
|
||||
// Detekce uzavření pro každou restauraci
|
||||
switch (restaurant) {
|
||||
@@ -245,22 +246,34 @@ export async function getRestaurantMenu(restaurant: Restaurant, date?: Date, for
|
||||
// Uložení do storage
|
||||
await storage.setData(getMenuKey(usedDate), weekMenu);
|
||||
} catch (e: any) {
|
||||
console.error(`Selhalo načtení jídel pro podnik ${restaurant}`, e);
|
||||
if (e instanceof StaleWeekError) {
|
||||
for (let i = 0; i < e.food.length && i < weekMenu.length; i++) {
|
||||
weekMenu[i][restaurant]!.food = e.food[i];
|
||||
weekMenu[i][restaurant]!.lastUpdate = now;
|
||||
weekMenu[i][restaurant]!.isStale = true;
|
||||
}
|
||||
await storage.setData(getMenuKey(usedDate), weekMenu);
|
||||
} else {
|
||||
console.error(`Selhalo načtení jídel pro podnik ${restaurant}`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
const result = weekMenu[dayOfWeekIndex][restaurant]!;
|
||||
result.warnings = generateMenuWarnings(result, now);
|
||||
result.warnings = generateMenuWarnings(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generuje varování o kvalitě/úplnosti dat menu restaurace.
|
||||
*/
|
||||
function generateMenuWarnings(menu: RestaurantDayMenu, now: number): string[] {
|
||||
function generateMenuWarnings(menu: RestaurantDayMenu): string[] {
|
||||
const warnings: string[] = [];
|
||||
if (!menu.food?.length || menu.closed) {
|
||||
return warnings;
|
||||
}
|
||||
if (menu.isStale) {
|
||||
warnings.push('Data jsou z minulého týdne');
|
||||
}
|
||||
const hasSoup = menu.food.some(f => f.isSoup);
|
||||
if (!hasSoup) {
|
||||
warnings.push('Chybí polévka');
|
||||
@@ -269,10 +282,6 @@ function generateMenuWarnings(menu: RestaurantDayMenu, now: number): string[] {
|
||||
if (missingPrice) {
|
||||
warnings.push('U některých jídel chybí cena');
|
||||
}
|
||||
const STALE_THRESHOLD_MS = 24 * 60 * 60 * 1000;
|
||||
if (menu.lastUpdate && (now - menu.lastUpdate) > STALE_THRESHOLD_MS) {
|
||||
warnings.push('Data jsou starší než 24 hodin');
|
||||
}
|
||||
return warnings;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user