Ukládání dat výhradně do DB
This commit is contained in:
@@ -2,13 +2,13 @@ import express from "express";
|
||||
import { Server } from "socket.io";
|
||||
import bodyParser from "body-parser";
|
||||
import cors from 'cors';
|
||||
import { addChoice, addPizzaOrder, createPizzaDay, deletePizzaDay, finishPizzaDelivery, finishPizzaOrder, getData, getPizzaList, lockPizzaDay, removeChoice, removeChoices, removePizzaOrder, savePizzaList, unlockPizzaDay, updateDepartureTime, updateNote } from "./service";
|
||||
import { addChoice, addPizzaOrder, createPizzaDay, deletePizzaDay, finishPizzaDelivery, finishPizzaOrder, getData, getPizzaList, getRestaurantMenu, lockPizzaDay, removeChoice, removeChoices, removePizzaOrder, savePizzaList, unlockPizzaDay, updateDepartureTime, updateNote } from "./service";
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku } from "./restaurants";
|
||||
import { getQr } from "./qr";
|
||||
import { generateToken, getLogin, getTrusted, verify } from "./auth";
|
||||
import { Locations, Restaurants } from "../../types";
|
||||
import { Food, Locations, Restaurants } from "../../types";
|
||||
import { downloadPizzy } from "./chefie";
|
||||
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production';
|
||||
@@ -114,9 +114,9 @@ app.get("/api/food", async (req, res) => {
|
||||
const mock = !!process.env.MOCK_DATA;
|
||||
const date = new Date();
|
||||
const data = {
|
||||
[Restaurants.SLADOVNICKA]: await getMenuSladovnicka(date, mock),
|
||||
[Restaurants.UMOTLIKU]: await getMenuUMotliku(date, mock),
|
||||
[Restaurants.TECHTOWER]: await getMenuTechTower(date, mock),
|
||||
[Restaurants.SLADOVNICKA]: await getRestaurantMenu(Restaurants.SLADOVNICKA, date, mock),
|
||||
[Restaurants.UMOTLIKU]: await getRestaurantMenu(Restaurants.UMOTLIKU, date, mock),
|
||||
[Restaurants.TECHTOWER]: await getRestaurantMenu(Restaurants.TECHTOWER, date, mock),
|
||||
}
|
||||
res.status(200).json(data);
|
||||
});
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import axios from "axios";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { load } from 'cheerio';
|
||||
import { formatDate } from "./utils";
|
||||
import { Food } from "../../types";
|
||||
|
||||
// Fráze v názvech jídel, které naznačují že se jedná o polévku
|
||||
@@ -51,22 +47,15 @@ const getDayOfWeekIndex = (date: Date) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Stáhne (v případě potřeby) a vrátí HTML z dané URL pro předané datum.
|
||||
* Pokud je pro dané datum již staženo, vrátí jeho obsah ze souboru.
|
||||
* Stáhne a vrátí aktuální HTML z dané URL.
|
||||
*
|
||||
* @param url URL pro stažení
|
||||
* @param prefix prefix pro uložení do souboru
|
||||
* @param date datum ke kterému stáhnout HTML
|
||||
* @returns stažené HTML, nebo HTML ze souborové cache
|
||||
* @returns stažené HTML
|
||||
*/
|
||||
const getHtml = async (url: string, prefix: string, date: Date): Promise<Buffer> => {
|
||||
const fileName = path.join(os.tmpdir(), `${prefix}_${formatDate(date)}.html`);
|
||||
if (!fs.existsSync(fileName)) {
|
||||
await axios.get(url).then(res => res.data).then(content => {
|
||||
fs.writeFileSync(fileName, content);
|
||||
});
|
||||
}
|
||||
return fs.readFileSync(fileName);
|
||||
const getHtml = async (url: string): Promise<any> => {
|
||||
await axios.get(url).then(res => res.data).then(content => {
|
||||
return content
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +98,7 @@ export const getMenuSladovnicka = async (date: Date = new Date(), mock: boolean
|
||||
if (todayDayIndex == 5 || todayDayIndex == 6) { // Víkend
|
||||
return [];
|
||||
}
|
||||
const html = await getHtml(SLADOVNICKA_URL, 'sladovnicka', date);
|
||||
const html = await getHtml(SLADOVNICKA_URL);
|
||||
const $ = load(html);
|
||||
// Najdeme index pro vstupní datum (např. při svátcích bude posunutý)
|
||||
// TODO validovat, že vstupní datum je v aktuálním týdnu
|
||||
@@ -230,7 +219,7 @@ export const getMenuUMotliku = async (date: Date = new Date(), mock: boolean = f
|
||||
if (todayDayIndex == 5 || todayDayIndex == 6) { // Víkend
|
||||
return [];
|
||||
}
|
||||
const html = await getHtml(U_MOTLIKU_URL, 'umotliku', date);
|
||||
const html = await getHtml(U_MOTLIKU_URL);
|
||||
const $ = load(html);
|
||||
const table = $('table.table.table-hover.Xtable-striped').first();
|
||||
const body = table.children().first();
|
||||
@@ -313,7 +302,7 @@ export const getMenuTechTower = async (date: Date = new Date(), mock: boolean =
|
||||
if (todayDayIndex == 5 || todayDayIndex == 6) { // Víkend
|
||||
return [];
|
||||
}
|
||||
const html = await getHtml(TECHTOWER_URL, 'techtower', date);
|
||||
const html = await getHtml(TECHTOWER_URL);
|
||||
const $ = load(html);
|
||||
const fonts = $('font.wsw-41');
|
||||
let font = undefined;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { formatDate, getHumanDate, getIsWeekend } from "./utils";
|
||||
import { formatDate, getHumanDate, getHumanTime, getIsWeekend } from "./utils";
|
||||
import { callNotifikace } from "./notifikace";
|
||||
import { generateQr } from "./qr";
|
||||
import { ClientData, PizzaDayState, UdalostEnum, Pizza, PizzaSize, Order, PizzaOrder, Locations } from "../../types";
|
||||
import { ClientData, PizzaDayState, UdalostEnum, Pizza, PizzaSize, Order, PizzaOrder, Locations, Restaurants, Food, Menu } from "../../types";
|
||||
import getStorage from "./storage";
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku } from "./restaurants";
|
||||
|
||||
const storage = getStorage();
|
||||
|
||||
@@ -51,6 +52,44 @@ export async function savePizzaList(pizzaList: Pizza[]): Promise<ClientData> {
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vrátí menu dané restaurace pro předaný den. Pokud neexistuje, provede jeho stažení a uložení do DB.
|
||||
*
|
||||
* @param restaurant restaurace
|
||||
* @param date datum
|
||||
* @param mock příznak, zda chceme pouze mock data
|
||||
*/
|
||||
export async function getRestaurantMenu(restaurant: Restaurants, date?: Date, mock?: boolean): Promise<Menu> {
|
||||
await initIfNeeded();
|
||||
const today = formatDate(getToday());
|
||||
const clientData: ClientData = await storage.getData(today);
|
||||
if (!clientData.menus) {
|
||||
clientData.menus = {};
|
||||
storage.setData(today, clientData);
|
||||
}
|
||||
if (!clientData?.menus?.[restaurant]) {
|
||||
if (!clientData.menus[restaurant]) {
|
||||
clientData.menus[restaurant] = {
|
||||
lastUpdate: getHumanTime(new Date()),
|
||||
food: [],
|
||||
};
|
||||
}
|
||||
switch (restaurant) {
|
||||
case Restaurants.SLADOVNICKA:
|
||||
clientData.menus[restaurant].food = await getMenuSladovnicka(date, mock);
|
||||
break;
|
||||
case Restaurants.UMOTLIKU:
|
||||
clientData.menus[restaurant].food = await getMenuUMotliku(date, mock);
|
||||
break;
|
||||
case Restaurants.TECHTOWER:
|
||||
clientData.menus[restaurant].food = await getMenuTechTower(date, mock);
|
||||
break;
|
||||
}
|
||||
storage.setData(today, clientData);
|
||||
}
|
||||
return clientData?.menus?.[restaurant];
|
||||
}
|
||||
|
||||
/**
|
||||
* Vytvoří pizza day pro aktuální den a vrátí data pro klienta.
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,13 @@ export function getHumanDate(date: Date) {
|
||||
return `${currentDay}.${currentMonth}.${currentYear} (${currentDayOfWeek})`;
|
||||
}
|
||||
|
||||
/** Vrátí human-readable reprezentaci předaného času pro zobrazení. */
|
||||
export function getHumanTime(time: Date) {
|
||||
let currentHours = String(time.getHours()).padStart(2, '0');
|
||||
let currentMinutes = String(time.getMinutes()).padStart(2, "0");
|
||||
return `${currentHours}:${currentMinutes}`;
|
||||
}
|
||||
|
||||
/** Vrátí true, pokud je předané datum o víkendu. */
|
||||
export function getIsWeekend(date: Date) {
|
||||
const dayName = date.toLocaleDateString("CZ-cs", { weekday: 'long' }).toLowerCase()
|
||||
|
||||
Reference in New Issue
Block a user