This commit is contained in:
@@ -49,16 +49,16 @@ export async function downloadPizzy(mock: boolean): Promise<Pizza[]> {
|
||||
const $ = load(html);
|
||||
const links = $('.vypisproduktu > div > h4 > a');
|
||||
const urls = [];
|
||||
for (let i = 0; i < links.length; i++) {
|
||||
if (links[i].name === 'a' && links[i].attribs?.href) {
|
||||
const pizzaUrl = links[i].attribs?.href;
|
||||
for (const element of links) {
|
||||
if (element.name === 'a' && element.attribs?.href) {
|
||||
const pizzaUrl = element.attribs?.href;
|
||||
urls.push(buildPizzaUrl(pizzaUrl));
|
||||
}
|
||||
}
|
||||
// Scrapneme jednotlivé pizzy
|
||||
const result: Pizza[] = [];
|
||||
for (let i = 0; i < urls.length; i++) {
|
||||
const pizzaUrl = urls[i];
|
||||
for (const element of urls) {
|
||||
const pizzaUrl = element;
|
||||
const pizzaHtml = await axios.get(pizzaUrl).then(res => res.data);
|
||||
// Název
|
||||
const name = $('.produkt > h2', pizzaHtml).first().text()
|
||||
|
||||
@@ -14,7 +14,7 @@ import votingRoutes from "./routes/votingRoutes";
|
||||
import easterEggRoutes from "./routes/easterEggRoutes";
|
||||
import statsRoutes from "./routes/statsRoutes";
|
||||
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production';
|
||||
const ENVIRONMENT = process.env.NODE_ENV ?? 'production';
|
||||
dotenv.config({ path: path.resolve(__dirname, `./.env.${ENVIRONMENT}`) });
|
||||
|
||||
// Validace nastavení JWT tokenu - nemá bez něj smysl vůbec povolit server spustit
|
||||
@@ -35,7 +35,7 @@ app.use(cors({
|
||||
|
||||
// Zapínatelný login přes hlavičky - pokud je zapnutý nepovolí "basicauth"
|
||||
const HTTP_REMOTE_USER_ENABLED = process.env.HTTP_REMOTE_USER_ENABLED === 'true' || false;
|
||||
const HTTP_REMOTE_USER_HEADER_NAME = process.env.HTTP_REMOTE_USER_HEADER_NAME || 'remote-user';
|
||||
const HTTP_REMOTE_USER_HEADER_NAME = process.env.HTTP_REMOTE_USER_HEADER_NAME ?? 'remote-user';
|
||||
if (HTTP_REMOTE_USER_ENABLED) {
|
||||
if (!process.env.HTTP_REMOTE_TRUSTED_IPS) {
|
||||
throw new Error('Je zapnutý login z hlaviček, ale není nastaven rozsah adres ze kterých hlavička může přijít.');
|
||||
@@ -79,7 +79,6 @@ app.post("/api/login", (req, res) => {
|
||||
|
||||
// TODO dočasné řešení - QR se zobrazuje přes <img>, nemáme sem jak dostat token
|
||||
app.get("/api/qr", (req, res) => {
|
||||
// const login = getLogin(parseToken(req));
|
||||
if (!req.query?.login) {
|
||||
throw Error("Nebyl předán login");
|
||||
}
|
||||
@@ -148,8 +147,8 @@ app.use((err: any, req: any, res: any, next: any) => {
|
||||
next();
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const HOST = process.env.HOST || '0.0.0.0';
|
||||
const PORT = process.env.PORT ?? 3001;
|
||||
const HOST = process.env.HOST ?? '0.0.0.0';
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Server listening on ${HOST}, port ${PORT}`);
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
/** Notifikace */
|
||||
import axios from 'axios';
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import { getClientData, getToday } from "./service";
|
||||
import { getUsersByLocation, getHumanTime } from "./utils";
|
||||
import getStorage from "./storage";
|
||||
import { NotifikaceData, NotifikaceInput } from '../../types';
|
||||
|
||||
const storage = getStorage();
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production'
|
||||
const ENVIRONMENT = process.env.NODE_ENV ?? 'production';
|
||||
dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) });
|
||||
|
||||
// const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}";
|
||||
|
||||
@@ -128,11 +128,11 @@ export async function removePizzaOrder(login: string, pizzaOrder: PizzaVariant)
|
||||
if (!clientData.pizzaDay) {
|
||||
throw Error("Pizza day pro dnešní den neexistuje");
|
||||
}
|
||||
const orderIndex = clientData.pizzaDay!.orders!.findIndex(o => o.customer === login);
|
||||
const orderIndex = clientData.pizzaDay.orders!.findIndex(o => o.customer === login);
|
||||
if (orderIndex < 0) {
|
||||
throw Error("Nebyly nalezeny žádné objednávky pro uživatele " + login);
|
||||
}
|
||||
const order = clientData.pizzaDay!.orders![orderIndex];
|
||||
const order = clientData.pizzaDay.orders![orderIndex];
|
||||
const index = order.pizzaList!.findIndex(o => o.name === pizzaOrder.name && o.size === pizzaOrder.size);
|
||||
if (index < 0) {
|
||||
throw Error("Objednávka s danými parametry nebyla nalezena");
|
||||
@@ -308,7 +308,7 @@ export async function updatePizzaFee(login: string, targetLogin: string, text?:
|
||||
targetOrder.fee = { text, price };
|
||||
}
|
||||
// Přepočet ceny
|
||||
targetOrder.totalPrice = targetOrder.pizzaList.reduce((price, pizzaOrder) => price + pizzaOrder.price, 0) + (targetOrder.fee?.price || 0);
|
||||
targetOrder.totalPrice = targetOrder.pizzaList.reduce((price, pizzaOrder) => price + pizzaOrder.price, 0) + (targetOrder.fee?.price ?? 0);
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
@@ -92,7 +92,6 @@ export const getMenuSladovnicka = async (firstDayOfWeek: Date, mock: boolean = f
|
||||
const rowText = $(dayRow).first().text().trim();
|
||||
if (rowText === searchedDayText) {
|
||||
index = i;
|
||||
return;
|
||||
}
|
||||
})
|
||||
if (index === undefined) {
|
||||
@@ -360,7 +359,6 @@ export const getMenuZastavkaUmichala = async (firstDayOfWeek: Date, mock: boolea
|
||||
price: "",
|
||||
isSoup: false,
|
||||
}];
|
||||
continue;
|
||||
} else {
|
||||
const url = (currentDate.getDate() === nowDate) ?
|
||||
ZASTAVKAUMICHALA_URL : ZASTAVKAUMICHALA_URL + '/?do=dailyMenu-changeDate&dailyMenu-dateString=' + formatDate(currentDate, 'DD.MM.YYYY');
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import express, { NextFunction } from "express";
|
||||
import { getLogin, getTrusted } from "../auth";
|
||||
import { getLogin } from "../auth";
|
||||
import { parseToken } from "../utils";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
@@ -34,16 +34,11 @@ function generateUrl() {
|
||||
*/
|
||||
function getEasterEggImage(req: any, res: any, next: NextFunction) {
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
try {
|
||||
// TODO vrátit!
|
||||
// if (trusted) {
|
||||
if (true) {
|
||||
if (login in easterEggs) {
|
||||
const imagePath = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)].path;
|
||||
res.sendFile(path.join(__dirname, IMAGES_PATH, imagePath));
|
||||
return;
|
||||
}
|
||||
if (login in easterEggs) {
|
||||
const imagePath = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)].path;
|
||||
res.sendFile(path.join(__dirname, IMAGES_PATH, imagePath));
|
||||
return;
|
||||
}
|
||||
res.sendStatus(404);
|
||||
} catch (e: any) { next(e) }
|
||||
@@ -124,7 +119,7 @@ let easterEggs: EasterEggsJson;
|
||||
if (fs.existsSync(EASTER_EGGS_JSON_PATH)) {
|
||||
const content = fs.readFileSync(EASTER_EGGS_JSON_PATH, 'utf-8');
|
||||
easterEggs = JSON.parse(content);
|
||||
for (const [key, eggs] of Object.entries(easterEggs)) {
|
||||
for (const [_, eggs] of Object.entries(easterEggs)) {
|
||||
for (const easterEgg of eggs) {
|
||||
const url = generateUrl();
|
||||
easterEgg.url = url;
|
||||
@@ -138,16 +133,11 @@ if (fs.existsSync(EASTER_EGGS_JSON_PATH)) {
|
||||
// Získání náhodného easter eggu pro přihlášeného uživatele
|
||||
router.get("/", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
try {
|
||||
// TODO vrátit!
|
||||
// if (trusted) {
|
||||
if (true) {
|
||||
if (easterEggs && login in easterEggs) {
|
||||
const randomEasterEgg = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)];
|
||||
const { path, startOffset, endOffset, ...strippedEasterEgg } = randomEasterEgg; // Path klient k ničemu nepotřebuje a nemá ho znát
|
||||
return res.status(200).json({ ...strippedEasterEgg, ...getRandomPosition(startOffset, endOffset) });
|
||||
}
|
||||
if (easterEggs && login in easterEggs) {
|
||||
const randomEasterEgg = easterEggs[login][Math.floor(Math.random() * easterEggs[login].length)];
|
||||
const { path, startOffset, endOffset, ...strippedEasterEgg } = randomEasterEgg; // Path klient k ničemu nepotřebuje a nemá ho znát
|
||||
return res.status(200).json({ ...strippedEasterEgg, ...getRandomPosition(startOffset, endOffset) });
|
||||
}
|
||||
return res.status(200).send();
|
||||
} catch (e: any) { next(e) }
|
||||
|
||||
@@ -4,8 +4,7 @@ import { addChoice, getDateForWeekIndex, getToday, removeChoice, removeChoices,
|
||||
import { getDayOfWeekIndex, parseToken } from "../utils";
|
||||
import { getWebsocket } from "../websocket";
|
||||
import { callNotifikace } from "../notifikace";
|
||||
import { AddChoiceRequest, ChangeDepartureTimeRequest, IDayIndex, RemoveChoiceRequest, RemoveChoicesRequest, UpdateNoteRequest } from "../../../types";
|
||||
import { UdalostEnum } from "../../../types";
|
||||
import { AddChoiceRequest, ChangeDepartureTimeRequest, IDayIndex, RemoveChoiceRequest, RemoveChoicesRequest, UdalostEnum, UpdateNoteRequest } from "../../../types";
|
||||
|
||||
/**
|
||||
* Ověří a vrátí index dne v týdnu z požadavku, za předpokladu, že byl předán, a je zároveň
|
||||
|
||||
@@ -2,8 +2,7 @@ import express, { Request, Response } from "express";
|
||||
import { getLogin } from "../auth";
|
||||
import { parseToken } from "../utils";
|
||||
import { getUserVotes, updateFeatureVote } from "../voting";
|
||||
import { UpdateFeatureVoteRequest } from "../../../types";
|
||||
import { FeatureRequest } from "../../../types";
|
||||
import { FeatureRequest, UpdateFeatureVoteRequest } from "../../../types";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getIsWeekend, getWeekNumber } from "./utils";
|
||||
import getStorage from "./storage";
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku, getMenuZastavkaUmichala, getMenuSenkSerikova } from "./restaurants";
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuZastavkaUmichala, getMenuSenkSerikova } from "./restaurants";
|
||||
import { getTodayMock } from "./mock";
|
||||
import { ClientData, DepartureTime, LunchChoice, Restaurant, RestaurantDayMenu, WeekMenu } from "../../types";
|
||||
|
||||
@@ -152,10 +152,10 @@ export async function getRestaurantMenu(restaurant: keyof typeof Restaurant, dat
|
||||
weekMenu[i][restaurant]!.closed = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} catch (e: any) {
|
||||
console.error("Selhalo načtení jídel pro podnik TechTower", e);
|
||||
}
|
||||
break;
|
||||
case 'ZASTAVKAUMICHALA':
|
||||
try {
|
||||
const zastavkaUmichalaFood = await getMenuZastavkaUmichala(firstDay, mock);
|
||||
@@ -165,10 +165,10 @@ export async function getRestaurantMenu(restaurant: keyof typeof Restaurant, dat
|
||||
weekMenu[i][restaurant]!.closed = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} catch (e: any) {
|
||||
console.error("Selhalo načtení jídel pro podnik Zastávka u Michala", e);
|
||||
}
|
||||
break;
|
||||
case 'SENKSERIKOVA':
|
||||
try {
|
||||
const senkSerikovaFood = await getMenuSenkSerikova(firstDay, mock);
|
||||
@@ -178,10 +178,10 @@ export async function getRestaurantMenu(restaurant: keyof typeof Restaurant, dat
|
||||
weekMenu[i][restaurant]!.closed = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} catch (e: any) {
|
||||
console.error("Selhalo načtení jídel pro podnik Pivovarský šenk Šeříková", e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
await storage.setData(getMenuKey(usedDate), weekMenu);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import { DailyStats, LunchChoice, WeeklyStats } from "../../types";
|
||||
import { getStatsMock } from "./mock";
|
||||
import { getClientData } from "./service";
|
||||
import getStorage from "./storage";
|
||||
import { formatDate } from "./utils";
|
||||
|
||||
const storage = getStorage();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { StorageInterface } from "./StorageInterface";
|
||||
import JsonStorage from "./json";
|
||||
import RedisStorage from "./redis";
|
||||
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production';
|
||||
const ENVIRONMENT = process.env.NODE_ENV ?? 'production';
|
||||
dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) });
|
||||
|
||||
const JSON_KEY = 'json';
|
||||
|
||||
@@ -8,15 +8,15 @@ let client: RedisClientType;
|
||||
*/
|
||||
export default class RedisStorage implements StorageInterface {
|
||||
constructor() {
|
||||
const HOST = process.env.REDIS_HOST || 'localhost';
|
||||
const PORT = process.env.REDIS_PORT || 6379;
|
||||
const HOST = process.env.REDIS_HOST ?? 'localhost';
|
||||
const PORT = process.env.REDIS_PORT ?? 6379;
|
||||
client = createClient({ url: `redis://${HOST}:${PORT}` });
|
||||
client.connect();
|
||||
}
|
||||
|
||||
async hasData(key: string) {
|
||||
const data = await client.json.get(key);
|
||||
return (data ? true : false);
|
||||
return (!!data);
|
||||
}
|
||||
|
||||
async getData<Type>(key: string) {
|
||||
|
||||
@@ -8,7 +8,7 @@ export function formatDate(date: Date, format?: string) {
|
||||
let month = String(date.getMonth() + 1).padStart(2, "0");
|
||||
let year = String(date.getFullYear());
|
||||
|
||||
const f = (format === undefined) ? 'YYYY-MM-DD' : format;
|
||||
const f = format ?? 'YYYY-MM-DD';
|
||||
return f.replace('DD', day).replace('MM', month).replace('YYYY', year);
|
||||
}
|
||||
|
||||
@@ -61,10 +61,10 @@ export function getLastWorkDayOfWeek(date: Date) {
|
||||
|
||||
/** Vrátí pořadové číslo týdne předaného data v roce dle ISO 8601. */
|
||||
export function getWeekNumber(inputDate: Date) {
|
||||
var date = new Date(inputDate.getTime());
|
||||
const date = new Date(inputDate.getTime());
|
||||
date.setHours(0, 0, 0, 0);
|
||||
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
|
||||
var week1 = new Date(date.getFullYear(), 0, 4);
|
||||
const week1 = new Date(date.getFullYear(), 0, 4);
|
||||
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user