Otypování dnů v týdnu

This commit is contained in:
Martin Berka 2025-01-19 23:32:37 +01:00
parent fe9cee3a80
commit 71542a3f34
5 changed files with 29 additions and 19 deletions

View File

@ -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 = () => {

View File

@ -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 = [

View File

@ -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<WeekMenu | undefined> {
* @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<DayMenu> {
export async function getRestaurantMenu(restaurant: Restaurants, date?: Date): Promise<RestaurantDailyMenu> {
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];

View File

@ -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. */

View File

@ -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