diff --git a/client/src/App.tsx b/client/src/App.tsx index 1eed593..eb66400 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -18,8 +18,6 @@ import Loader from './components/Loader'; import { getHumanDateTime, isInTheFuture } from './Utils'; import NoteModal from './components/modals/NoteModal'; import { useEasterEgg } from './context/eggs'; -import { Link } from 'react-router'; -import { STATS_URL } from './AppRoutes'; import { ClientData, Food, PizzaOrder, DepartureTime, PizzaDayState, Restaurant, RestaurantDayMenu, RestaurantDayMenuMap, LunchChoice, UserLunchChoice, PizzaVariant, getData, getEasterEggImage, addPizza, removePizza, updatePizzaDayNote, createPizzaDay, deletePizzaDay, lockPizzaDay, unlockPizzaDay, finishOrder, finishDelivery, addChoice, jdemeObed, removeChoices, removeChoice, updateNote, changeDepartureTime } from '../../types'; import { getLunchChoiceName } from './enums'; diff --git a/client/src/components/modals/PizzaCalculatorModal.tsx b/client/src/components/modals/PizzaCalculatorModal.tsx index 774426b..e199a7a 100644 --- a/client/src/components/modals/PizzaCalculatorModal.tsx +++ b/client/src/components/modals/PizzaCalculatorModal.tsx @@ -37,9 +37,7 @@ export default function PizzaCalculatorModal({ isOpen, onClose }: Readonly 0) { r.pizza1.diameter = diameter1; r.pizza1.area = Math.PI * Math.pow(diameter1 / 2, 2); @@ -59,9 +57,7 @@ export default function PizzaCalculatorModal({ isOpen, onClose }: Readonly 0) { r.pizza2.diameter = diameter2; r.pizza2.area = Math.PI * Math.pow(diameter2 / 2, 2); diff --git a/server/src/notifikace.ts b/server/src/notifikace.ts index 8e6ef83..9663181 100644 --- a/server/src/notifikace.ts +++ b/server/src/notifikace.ts @@ -8,49 +8,6 @@ import { NotifikaceData, NotifikaceInput } from '../../types'; const ENVIRONMENT = process.env.NODE_ENV ?? 'production'; dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) }); -// const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}"; -// const gotifyData: GotifyServer[] = JSON.parse(gotifyDataRaw); -// export const gotifyCall = async (data: NotififaceInput, gotifyServers?: GotifyServer[]): Promise => { -// if (!Array.isArray(gotifyServers)) { -// return [] -// } -// const urls = gotifyServers.flatMap(gotifyServer => -// gotifyServer.api_keys.map(apiKey => `${gotifyServer.server}/message?token=${apiKey}`)); -// -// const dataPayload = { -// title: "Luncher", -// message: `${data.udalost} - spustil:${data.user}`, -// priority: 7, -// }; -// -// const headers = { "Content-Type": "application/json" }; -// -// const promises = urls.map(url => -// axios.post(url, dataPayload, { headers }).then(response => { -// response.data = { -// success: true, -// message: "Notifikace doručena", -// }; -// return response; -// }).catch(error => { -// if (axios.isAxiosError(error)) { -// const axiosError = error as AxiosError; -// if (axiosError.response) { -// axiosError.response.data = { -// success: false, -// message: "fail", -// }; -// console.log(error) -// return axiosError.response; -// } -// } -// // Handle unknown error without a response -// console.log(error, "unknown error"); -// }) -// ); -// return promises; -// }; - export const ntfyCall = async (data: NotifikaceInput) => { const url = process.env.NTFY_HOST const username = process.env.NTFY_USERNAME; diff --git a/server/src/pizza.ts b/server/src/pizza.ts index ccf74db..f1c7d48 100644 --- a/server/src/pizza.ts +++ b/server/src/pizza.ts @@ -96,9 +96,7 @@ export async function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize totalPrice: 0, hasQr: false, } - if (!clientData.pizzaDay.orders) { - clientData.pizzaDay.orders = []; - } + clientData.pizzaDay.orders ??= []; clientData.pizzaDay.orders.push(order); } const pizzaOrder: PizzaVariant = { @@ -107,9 +105,7 @@ export async function addPizzaOrder(login: string, pizza: Pizza, size: PizzaSize size: size.size, price: size.price, } - if (!order.pizzaList) { - order.pizzaList = []; - } + order.pizzaList ??= []; order.pizzaList.push(pizzaOrder); order.totalPrice += pizzaOrder.price; await storage.setData(today, clientData); diff --git a/server/src/restaurants.ts b/server/src/restaurants.ts index 9d6f681..ca502fd 100644 --- a/server/src/restaurants.ts +++ b/server/src/restaurants.ts @@ -300,9 +300,7 @@ export const getMenuTechTower = async (firstDayOfWeek: Date, mock: boolean = fal price = `${split.slice(1)[0]}\xA0Kč` name = split[0].replace('•', ''); } - if (result[currentDayIndex] == null) { - result[currentDayIndex] = []; - } + result[currentDayIndex] ??= []; result[currentDayIndex].push({ amount: '-', name, diff --git a/server/src/routes/foodRoutes.ts b/server/src/routes/foodRoutes.ts index 0f2236c..8817ded 100644 --- a/server/src/routes/foodRoutes.ts +++ b/server/src/routes/foodRoutes.ts @@ -1,6 +1,6 @@ import express, { Request, Response } from "express"; import { getLogin, getTrusted } from "../auth"; -import { addChoice, getDateForWeekIndex, getToday, removeChoice, removeChoices, updateDepartureTime, updateNote, getRestaurantMenu, fetchRestaurantWeekMenuData, saveRestaurantWeekMenu } from "../service"; +import { addChoice, getDateForWeekIndex, getToday, removeChoice, removeChoices, updateDepartureTime, updateNote, fetchRestaurantWeekMenuData, saveRestaurantWeekMenu } from "../service"; import { getDayOfWeekIndex, parseToken, getFirstWorkDayOfWeek } from "../utils"; import { getWebsocket } from "../websocket"; import { callNotifikace } from "../notifikace"; diff --git a/server/src/service.ts b/server/src/service.ts index 4de8c60..01ae6fa 100644 --- a/server/src/service.ts +++ b/server/src/service.ts @@ -381,9 +381,7 @@ export async function addChoice(login: string, trusted: boolean, locationKey: Lu removeChoiceIfPresent(login, usedDate, locationKey); } // TODO vytáhnout inicializaci "prázdné struktury" do vlastní funkce - if (!(data.choices[locationKey])) { - data.choices[locationKey] = {} - } + data.choices[locationKey] ??= {}; if (!(login in data.choices[locationKey])) { if (!data.choices[locationKey]) { data.choices[locationKey] = {} diff --git a/server/src/storage/StorageInterface.ts b/server/src/storage/StorageInterface.ts index ba034d5..8956456 100644 --- a/server/src/storage/StorageInterface.ts +++ b/server/src/storage/StorageInterface.ts @@ -5,6 +5,12 @@ * Postupem času lze předělat pro efektivnější využití Redis. */ export interface StorageInterface { + + /** + * Inicializuje úložiště, pokud je potřeba (např. připojení k databázi). + */ + initialize?(): Promise; + /** * Vrátí příznak, zda existují data pro předaný klíč. * @param key klíč, pro který zjišťujeme data (typicky datum) diff --git a/server/src/storage/index.ts b/server/src/storage/index.ts index 9841a12..3ac8483 100644 --- a/server/src/storage/index.ts +++ b/server/src/storage/index.ts @@ -19,6 +19,13 @@ if (!process.env.STORAGE || process.env.STORAGE?.toLowerCase() === JSON_KEY) { throw Error("Nepodporovaná hodnota proměnné STORAGE: " + process.env.STORAGE + ", podporované jsou 'json' nebo 'redis'"); } +(async () => { + if (storage.initialize) { + await storage.initialize(); + } +})(); + + export default function getStorage(): StorageInterface { return storage; } diff --git a/server/src/storage/redis.ts b/server/src/storage/redis.ts index 966236a..f92720e 100644 --- a/server/src/storage/redis.ts +++ b/server/src/storage/redis.ts @@ -11,6 +11,9 @@ export default class RedisStorage implements StorageInterface { const HOST = process.env.REDIS_HOST ?? 'localhost'; const PORT = process.env.REDIS_PORT ?? 6379; client = createClient({ url: `redis://${HOST}:${PORT}` }); + } + + async initialize() { client.connect(); } diff --git a/server/src/voting.ts b/server/src/voting.ts index a0426ce..db0d147 100644 --- a/server/src/voting.ts +++ b/server/src/voting.ts @@ -29,9 +29,7 @@ export async function getUserVotes(login: string) { */ export async function updateFeatureVote(login: string, option: FeatureRequest, active: boolean): Promise { let data = await storage.getData(STORAGE_KEY); - if (data == null) { - data = {}; - } + data ??= {}; if (!(login in data)) { data[login] = []; }