From cfcbd7a68b73368a885c08d13f741b9e48c0b2e6 Mon Sep 17 00:00:00 2001 From: Martin Berka Date: Sun, 19 Jan 2025 23:32:37 +0100 Subject: [PATCH] =?UTF-8?q?Otypov=C3=A1n=C3=AD=20dn=C5=AF=20v=20t=C3=BDdnu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/mock.ts | 4 +--- server/src/restaurants.ts | 3 +-- server/src/service.ts | 12 ++++++++---- server/src/utils.ts | 6 +++--- types/Types.ts | 23 ++++++++++++++++------- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/server/src/mock.ts b/server/src/mock.ts index 961f724..d825553 100644 --- a/server/src/mock.ts +++ b/server/src/mock.ts @@ -1,5 +1,3 @@ -import { getDayOfWeekIndex } from "./utils"; - // Mockovací data pro podporované podniky, na jeden týden const MOCK_DATA = { 'sladovnicka': [ @@ -1274,7 +1272,7 @@ const MOCK_PIZZA_LIST = [ * Funkce vrací mock datu ve formátu YYYY-MM-DD */ export const getTodayMock = (): Date => { - return new Date('2025-01-10'); // pátek + return new Date('2025-01-08'); // pátek } export const getMenuSladovnickaMock = () => { diff --git a/server/src/restaurants.ts b/server/src/restaurants.ts index 66bdda9..7205035 100644 --- a/server/src/restaurants.ts +++ b/server/src/restaurants.ts @@ -1,8 +1,7 @@ import axios from "axios"; import { load } from 'cheerio'; import { Food } from "../../types"; -import {getMenuSladovnickaMock, getMenuTechTowerMock, getMenuUMotlikuMock, getMenuZastavkaUmichalaMock} from "./mock"; -import {formatDate, getDayOfWeekIndex, getIsWeekend} from "./utils"; +import { getMenuSladovnickaMock, getMenuTechTowerMock, getMenuUMotlikuMock, getMenuZastavkaUmichalaMock } from "./mock"; // Fráze v názvech jídel, které naznačují že se jedná o polévku const SOUP_NAMES = [ diff --git a/server/src/service.ts b/server/src/service.ts index 0daa62f..14a863b 100644 --- a/server/src/service.ts +++ b/server/src/service.ts @@ -1,5 +1,5 @@ import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getIsWeekend, getWeekNumber } from "./utils"; -import { ClientData, Restaurants, DayMenu, DepartureTime, DayData, WeekMenu, LocationKey } from "../../types"; +import { ClientData, Restaurants, RestaurantDailyMenu, DepartureTime, DayData, WeekMenu, LocationKey, DayOfWeekIndex, daysOfWeeksIndices } from "../../types"; import getStorage from "./storage"; import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku, getMenuZastavkaUmichala } from "./restaurants"; import { getTodayMock } from "./mock"; @@ -97,7 +97,7 @@ async function getMenu(date: Date): Promise { * @param date datum, ke kterému získat menu * @param mock příznak, zda chceme pouze mock data */ -export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): Promise { +export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): Promise { const usedDate = date ?? getToday(); const dayOfWeekIndex = getDayOfWeekIndex(usedDate); const now = new Date().getTime(); @@ -111,9 +111,9 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): P let menus = await getMenu(usedDate); if (menus == null) { - menus = []; + menus = {}; } - for (let i = 0; i < 5; i++) { + daysOfWeeksIndices.forEach(i => { if (menus[i] == null) { menus[i] = {}; } @@ -124,6 +124,9 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): P food: [], }; } + }) + if (!menus[dayOfWeekIndex]) { + menus[dayOfWeekIndex] = {}; } if (!menus[dayOfWeekIndex][restaurant]?.food?.length) { const firstDay = getFirstWorkDayOfWeek(usedDate); @@ -131,6 +134,7 @@ export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): P switch (restaurant) { case Restaurants.SLADOVNICKA: try { + // TODO tady jsme v háji, protože z následujících metod vracíme arbitrárně dlouhé pole (musíme vracet omezené na maximálně 0-7 prvků) const sladovnickaFood = await getMenuSladovnicka(firstDay, mock); for (let i = 0; i < sladovnickaFood.length; i++) { menus[i][restaurant]!.food = sladovnickaFood[i]; diff --git a/server/src/utils.ts b/server/src/utils.ts index e8e8e35..41ce790 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -1,4 +1,4 @@ -import { Choices, LocationKey } from "../../types"; +import { Choices, DayOfWeekIndex, LocationKey } from "../../types"; /** Vrátí datum v ISO formátu. */ export function formatDate(date: Date, format?: string) { @@ -32,9 +32,9 @@ export function getHumanTime(time: Date) { * @param date datum * @returns index dne v týdnu */ -export const getDayOfWeekIndex = (date: Date) => { +export const getDayOfWeekIndex = (date: Date): DayOfWeekIndex => { // https://stackoverflow.com/a/4467559 - return (((date.getDay() - 1) % 7) + 7) % 7; + return ((((date.getDay() - 1) % 7) + 7) % 7) as DayOfWeekIndex; } /** Vrátí true, pokud je předané datum o víkendu. */ diff --git a/types/Types.ts b/types/Types.ts index 7617518..bb76de7 100644 --- a/types/Types.ts +++ b/types/Types.ts @@ -70,20 +70,29 @@ interface PizzaDay { orders: Order[], // seznam objednávek jednotlivých lidí } +/** Index dne v týdnu (0 = pondělí, 6 = neděle) */ +// TODO začistit +// export type DayOfWeekIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6; +export const daysOfWeeksIndices = [0, 1, 2, 3, 4, 5, 6] as const; +export type DayOfWeekIndex = typeof daysOfWeeksIndices[number] + +/** Denní menu všech dostupných podniků. */ +export type DailyMenu = { + [restaurant in Restaurants]?: RestaurantDailyMenu +} + /** Týdenní menu jednotlivých restaurací. */ export type WeekMenu = { - [dayIndex: number]: { - [restaurant in Restaurants]?: DayMenu - } + [dayIndex in DayOfWeekIndex]?: DailyMenu } /** Data vztahující se k jednomu konkrétnímu dni. */ export type DayData = { date: string, // datum dne isWeekend: boolean, // příznak, zda je datum víkend - weekIndex: number, // index dne v týdnu (0-6) + weekIndex: DayOfWeekIndex, // index dne v týdnu (0-6) choices: Choices, // seznam voleb uživatelů - menus?: { [restaurant in Restaurants]?: DayMenu }, // menu jednotlivých restaurací + menus?: { [restaurant in Restaurants]?: RestaurantDailyMenu }, // menu jednotlivých restaurací pizzaDay?: PizzaDay, // pizza day pro dnešní den, pokud existuje pizzaList?: Pizza[], // seznam dostupných pizz pro dnešní den pizzaListLastUpdate?: Date, // datum a čas poslední aktualizace pizz @@ -91,11 +100,11 @@ export type DayData = { /** Veškerá data pro zobrazení na klientovi. */ export type ClientData = DayData & { - todayWeekIndex?: number, // index dnešního dne v týdnu (0-6) + todayWeekIndex?: DayOfWeekIndex, // index dnešního dne v týdnu (0-6) } /** Nabídka jídel jednoho podniku pro jeden konkrétní den. */ -export type DayMenu = { +export type RestaurantDailyMenu = { lastUpdate: number, // UNIX timestamp poslední aktualizace menu closed: boolean, // příznak, zda je daný podnik v tento den zavřený food: Food[], // seznam jídel v menu