Migrace z pořadových indexů na unikátní klíče

This commit is contained in:
Martin Berka 2025-01-09 22:05:20 +01:00
parent 774cb4f9d2
commit 02de6691a8
7 changed files with 104 additions and 100 deletions

View File

@ -14,7 +14,7 @@ import './App.scss';
import { SelectSearchOption } from 'react-select-search'; import { SelectSearchOption } from 'react-select-search';
import { faCircleCheck, faNoteSticky, faTrashCan } from '@fortawesome/free-regular-svg-icons'; import { faCircleCheck, faNoteSticky, faTrashCan } from '@fortawesome/free-regular-svg-icons';
import { useSettings } from './context/settings'; import { useSettings } from './context/settings';
import { ClientData, Restaurants, Food, Order, Locations, PizzaOrder, PizzaDayState, FoodChoices, DayMenu, DepartureTime } from './types'; import { ClientData, Restaurants, Food, Order, Locations, PizzaOrder, PizzaDayState, FoodChoices, DayMenu, DepartureTime, LocationKey } from './types';
import Footer from './components/Footer'; import Footer from './components/Footer';
import { faChainBroken, faChevronLeft, faChevronRight, faGear, faSatelliteDish, faSearch } from '@fortawesome/free-solid-svg-icons'; import { faChainBroken, faChevronLeft, faChevronRight, faGear, faSatelliteDish, faSearch } from '@fortawesome/free-solid-svg-icons';
import Loader from './components/Loader'; import Loader from './components/Loader';
@ -141,11 +141,8 @@ function App() {
useEffect(() => { useEffect(() => {
if (choiceRef?.current?.value && choiceRef.current.value !== "") { if (choiceRef?.current?.value && choiceRef.current.value !== "") {
// TODO: wtf, cos pil, když jsi tohle psal? const locationKey = choiceRef.current.value as LocationKey;
const key = choiceRef?.current?.value; const restaurantKey = Object.keys(Restaurants).indexOf(locationKey);
const locationIndex = Object.keys(Locations).indexOf(key as unknown as Locations);
const locationsKey = Object.keys(Locations)[locationIndex];
const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey);
if (restaurantKey > -1 && food) { if (restaurantKey > -1 && food) {
const restaurant = Object.values(Restaurants)[restaurantKey]; const restaurant = Object.values(Restaurants)[restaurantKey];
setFoodChoiceList(food[restaurant]?.food); setFoodChoiceList(food[restaurant]?.food);
@ -194,9 +191,9 @@ function App() {
}, [auth?.login, easterEgg?.duration, easterEgg?.url, eggImage]); }, [auth?.login, easterEgg?.duration, easterEgg?.url, eggImage]);
const doAddChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => { const doAddChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => {
const index = Object.keys(Locations).indexOf(event.target.value as unknown as Locations); const locationKey = event.target.value as LocationKey;
if (auth?.login) { if (auth?.login) {
await errorHandler(() => addChoice(index, undefined, dayIndex)); await errorHandler(() => addChoice(locationKey, undefined, dayIndex));
if (foodChoiceRef.current?.value) { if (foodChoiceRef.current?.value) {
foodChoiceRef.current.value = ""; foodChoiceRef.current.value = "";
} }
@ -211,17 +208,16 @@ function App() {
const doAddFoodChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => { const doAddFoodChoice = async (event: React.ChangeEvent<HTMLSelectElement>) => {
if (event.target.value && foodChoiceList?.length && choiceRef.current?.value) { if (event.target.value && foodChoiceList?.length && choiceRef.current?.value) {
const restaurantKey = choiceRef.current.value; const locationKey = choiceRef.current.value as LocationKey;
if (auth?.login) { if (auth?.login) {
const locationIndex = Object.keys(Locations).indexOf(restaurantKey as unknown as Locations); await errorHandler(() => addChoice(locationKey, Number(event.target.value), dayIndex));
await errorHandler(() => addChoice(locationIndex, Number(event.target.value), dayIndex));
} }
} }
} }
const doRemoveChoices = async (locationKey: string) => { const doRemoveChoices = async (locationKey: LocationKey) => {
if (auth?.login) { if (auth?.login) {
await errorHandler(() => removeChoices(Number(locationKey), dayIndex)); await errorHandler(() => removeChoices(locationKey, dayIndex));
// Vyresetujeme výběr, aby bylo jasné pro který případně vybíráme jídlo // Vyresetujeme výběr, aby bylo jasné pro který případně vybíráme jídlo
if (choiceRef?.current?.value) { if (choiceRef?.current?.value) {
choiceRef.current.value = ""; choiceRef.current.value = "";
@ -232,9 +228,9 @@ function App() {
} }
} }
const doRemoveFoodChoice = async (locationKey: string, foodIndex: number) => { const doRemoveFoodChoice = async (locationKey: LocationKey, foodIndex: number) => {
if (auth?.login) { if (auth?.login) {
await errorHandler(() => removeChoice(Number(locationKey), foodIndex, dayIndex)); await errorHandler(() => removeChoice(locationKey, foodIndex, dayIndex));
if (choiceRef?.current?.value) { if (choiceRef?.current?.value) {
choiceRef.current.value = ""; choiceRef.current.value = "";
} }
@ -430,11 +426,8 @@ function App() {
<option></option> <option></option>
{Object.entries(Locations) {Object.entries(Locations)
.filter(entry => { .filter(entry => {
// TODO: wtf, cos pil, když jsi tohle psal? v2 const locationKey = entry[0] as LocationKey;
const key = entry[0]; const restaurantKey = Object.keys(Restaurants).indexOf(locationKey);
const locationIndex = Object.keys(Locations).indexOf(key as unknown as Locations);
const locationsKey = Object.keys(Locations)[locationIndex];
const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey);
const v = Object.values(Restaurants)[restaurantKey]; const v = Object.values(Restaurants)[restaurantKey];
return v == null || !food[v]?.closed; return v == null || !food[v]?.closed;
}) })
@ -459,11 +452,16 @@ function App() {
{Object.keys(data.choices).length > 0 ? {Object.keys(data.choices).length > 0 ?
<Table bordered className='mt-5'> <Table bordered className='mt-5'>
<tbody> <tbody>
{Object.keys(data.choices).map((locationKey: string) => { {Object.keys(data.choices).map(key => {
const locationName = Object.values(Locations)[Number(locationKey)]; const locationKey = key as LocationKey;
const locationLoginList = Object.entries(data.choices[Number(locationKey)]); const locationName = Locations[locationKey];
const loginObject = data.choices[locationKey];
if (!loginObject) {
return;
}
const locationLoginList = Object.entries(loginObject);
return ( return (
<tr key={locationKey}> <tr key={key}>
<td>{locationName}</td> <td>{locationName}</td>
<td className='p-0'> <td className='p-0'>
<Table> <Table>
@ -485,20 +483,20 @@ function App() {
setNoteModalOpen(true); setNoteModalOpen(true);
}} title='Upravit poznámku' className='action-icon' icon={faNoteSticky} />} }} title='Upravit poznámku' className='action-icon' icon={faNoteSticky} />}
{login === auth.login && canChangeChoice && <FontAwesomeIcon onClick={() => { {login === auth.login && canChangeChoice && <FontAwesomeIcon onClick={() => {
doRemoveChoices(locationKey); doRemoveChoices(key as LocationKey);
}} title={`Odstranit volbu ${locationName}, včetně případných zvolených jídel`} className='action-icon' icon={faTrashCan} />} }} title={`Odstranit volbu ${locationName}, včetně případných zvolených jídel`} className='action-icon' icon={faTrashCan} />}
</td> </td>
{userChoices?.length && food ? <td> {userChoices?.length && food ? <td>
<ul> <ul>
{userChoices?.map(foodIndex => { {userChoices?.map(foodIndex => {
const locationsKey = Object.keys(Locations)[Number(locationKey)] // TODO narovnat, tohle je zbytečně složité
const restaurantKey = Object.keys(Restaurants).indexOf(locationsKey); const restaurantKey = Object.keys(Restaurants).indexOf(key);
const restaurant = Object.values(Restaurants)[restaurantKey]; const restaurant = Object.values(Restaurants)[restaurantKey];
const foodName = food[restaurant]?.food[foodIndex].name; const foodName = food[restaurant]?.food[foodIndex].name;
return <li key={foodIndex}> return <li key={foodIndex}>
{foodName} {foodName}
{login === auth.login && canChangeChoice && <FontAwesomeIcon onClick={() => { {login === auth.login && canChangeChoice && <FontAwesomeIcon onClick={() => {
doRemoveFoodChoice(locationKey, foodIndex); doRemoveFoodChoice(key as LocationKey, foodIndex);
}} title={`Odstranit ${foodName}`} className='action-icon' icon={faTrashCan} />} }} title={`Odstranit ${foodName}`} className='action-icon' icon={faTrashCan} />}
</li> </li>
})} })}

View File

@ -1,18 +1,18 @@
import { AddChoiceRequest, ChangeDepartureTimeRequest, RemoveChoiceRequest, RemoveChoicesRequest, UpdateNoteRequest } from "../types"; import { AddChoiceRequest, ChangeDepartureTimeRequest, LocationKey, RemoveChoiceRequest, RemoveChoicesRequest, UpdateNoteRequest } from "../types";
import { api } from "./Api"; import { api } from "./Api";
const FOOD_API_PREFIX = '/api/food'; const FOOD_API_PREFIX = '/api/food';
export const addChoice = async (locationIndex: number, foodIndex?: number, dayIndex?: number) => { export const addChoice = async (locationKey: LocationKey, foodIndex?: number, dayIndex?: number) => {
return await api.post<AddChoiceRequest, void>(`${FOOD_API_PREFIX}/addChoice`, { locationIndex, foodIndex, dayIndex }); return await api.post<AddChoiceRequest, void>(`${FOOD_API_PREFIX}/addChoice`, { locationKey, foodIndex, dayIndex });
} }
export const removeChoices = async (locationIndex: number, dayIndex?: number) => { export const removeChoices = async (locationKey: LocationKey, dayIndex?: number) => {
return await api.post<RemoveChoicesRequest, void>(`${FOOD_API_PREFIX}/removeChoices`, { locationIndex, dayIndex }); return await api.post<RemoveChoicesRequest, void>(`${FOOD_API_PREFIX}/removeChoices`, { locationKey, dayIndex });
} }
export const removeChoice = async (locationIndex: number, foodIndex: number, dayIndex?: number) => { export const removeChoice = async (locationKey: LocationKey, foodIndex: number, dayIndex?: number) => {
return await api.post<RemoveChoiceRequest, void>(`${FOOD_API_PREFIX}/removeChoice`, { locationIndex, foodIndex, dayIndex }); return await api.post<RemoveChoiceRequest, void>(`${FOOD_API_PREFIX}/removeChoice`, { locationKey, foodIndex, dayIndex });
} }
export const updateNote = async (note?: string, dayIndex?: number) => { export const updateNote = async (note?: string, dayIndex?: number) => {

View File

@ -33,24 +33,21 @@ const router = express.Router();
router.post("/addChoice", async (req: Request<{}, any, AddChoiceRequest>, res, next) => { router.post("/addChoice", async (req: Request<{}, any, AddChoiceRequest>, res, next) => {
const login = getLogin(parseToken(req)); const login = getLogin(parseToken(req));
const trusted = getTrusted(parseToken(req)); const trusted = getTrusted(parseToken(req));
if (req.body.locationIndex > -1) { let date = undefined;
let date = undefined; if (req.body.dayIndex != null) {
if (req.body.dayIndex != null) { let dayIndex;
let dayIndex;
try {
dayIndex = parseValidateFutureDayIndex(req);
} catch (e: any) {
return res.status(400).json({ error: e.message });
}
date = getDateForWeekIndex(dayIndex);
}
try { try {
const data = await addChoice(login, trusted, req.body.locationIndex, req.body.foodIndex, date); dayIndex = parseValidateFutureDayIndex(req);
getWebsocket().emit("message", await addVolatileData(data)); } catch (e: any) {
return res.status(200).json(data); return res.status(400).json({ error: e.message });
} catch (e: any) { next(e) } }
date = getDateForWeekIndex(dayIndex);
} }
return res.status(400); // TODO přidat popis chyby try {
const data = await addChoice(login, trusted, req.body.locationKey, req.body.foodIndex, date);
getWebsocket().emit("message", await addVolatileData(data));
return res.status(200).json(data);
} catch (e: any) { next(e) }
}); });
router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesRequest>, res, next) => { router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesRequest>, res, next) => {
@ -67,7 +64,7 @@ router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesRequest>
date = getDateForWeekIndex(dayIndex); date = getDateForWeekIndex(dayIndex);
} }
try { try {
const data = await removeChoices(login, trusted, req.body.locationIndex, date); const data = await removeChoices(login, trusted, req.body.locationKey, date);
getWebsocket().emit("message", await addVolatileData(data)); getWebsocket().emit("message", await addVolatileData(data));
res.status(200).json(data); res.status(200).json(data);
} catch (e: any) { next(e) } } catch (e: any) { next(e) }
@ -87,7 +84,7 @@ router.post("/removeChoice", async (req: Request<{}, any, RemoveChoiceRequest>,
date = getDateForWeekIndex(dayIndex); date = getDateForWeekIndex(dayIndex);
} }
try { try {
const data = await removeChoice(login, trusted, req.body.locationIndex, req.body.foodIndex, date); const data = await removeChoice(login, trusted, req.body.locationKey, req.body.foodIndex, date);
getWebsocket().emit("message", await addVolatileData(data)); getWebsocket().emit("message", await addVolatileData(data));
res.status(200).json(data); res.status(200).json(data);
} catch (e: any) { next(e) } } catch (e: any) { next(e) }

View File

@ -1,5 +1,5 @@
import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getHumanTime, getIsWeekend, getLastWorkDayOfWeek, getWeekNumber } from "./utils"; import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getFirstWorkDayOfWeek, getHumanDate, getIsWeekend, getWeekNumber } from "./utils";
import { ClientData, Locations, Restaurants, DayMenu, DepartureTime, DayData, WeekMenu } from "../../types"; import { ClientData, Restaurants, DayMenu, DepartureTime, DayData, WeekMenu, LocationKey } from "../../types";
import getStorage from "./storage"; import getStorage from "./storage";
import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku } from "./restaurants"; import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku } from "./restaurants";
import { getTodayMock } from "./mock"; import { getTodayMock } from "./mock";
@ -192,19 +192,19 @@ export async function initIfNeeded(date?: Date) {
* *
* @param login login uživatele * @param login login uživatele
* @param trusted příznak, zda se jedná o ověřeného uživatele * @param trusted příznak, zda se jedná o ověřeného uživatele
* @param locationIndex vybrané "umístění" * @param locationKey vybrané "umístění"
* @param date datum, ke kterému se volba vztahuje * @param date datum, ke kterému se volba vztahuje
* @returns * @returns
*/ */
export async function removeChoices(login: string, trusted: boolean, locationIndex: number, date?: Date) { export async function removeChoices(login: string, trusted: boolean, locationKey: LocationKey, date?: Date) {
const selectedDay = formatDate(date ?? getToday()); const selectedDay = formatDate(date ?? getToday());
let data: DayData = await storage.getData(selectedDay); let data: DayData = await storage.getData(selectedDay);
validateTrusted(data, login, trusted); validateTrusted(data, login, trusted);
if (locationIndex in data.choices) { if (locationKey in data.choices) {
if (login in data.choices[locationIndex]) { if (data.choices[locationKey] && login in data.choices[locationKey]) {
delete data.choices[locationIndex][login] delete data.choices[locationKey][login]
if (Object.keys(data.choices[locationIndex]).length === 0) { if (Object.keys(data.choices[locationKey]).length === 0) {
delete data.choices[locationIndex] delete data.choices[locationKey]
} }
await storage.setData(selectedDay, data); await storage.setData(selectedDay, data);
} }
@ -218,20 +218,20 @@ export async function removeChoices(login: string, trusted: boolean, locationInd
* *
* @param login login uživatele * @param login login uživatele
* @param trusted příznak, zda se jedná o ověřeného uživatele * @param trusted příznak, zda se jedná o ověřeného uživatele
* @param locationIndex vybrané "umístění" * @param locationKey vybrané "umístění"
* @param foodIndex index jídla v jídelním lístku daného umístění, pokud existuje * @param foodIndex index jídla v jídelním lístku daného umístění, pokud existuje
* @param date datum, ke kterému se volba vztahuje * @param date datum, ke kterému se volba vztahuje
* @returns * @returns
*/ */
export async function removeChoice(login: string, trusted: boolean, locationIndex: number, foodIndex: number, date?: Date) { export async function removeChoice(login: string, trusted: boolean, locationKey: LocationKey, foodIndex: number, date?: Date) {
const selectedDay = formatDate(date ?? getToday()); const selectedDay = formatDate(date ?? getToday());
let data: DayData = await storage.getData(selectedDay); let data: DayData = await storage.getData(selectedDay);
validateTrusted(data, login, trusted); validateTrusted(data, login, trusted);
if (locationIndex in data.choices) { if (locationKey in data.choices) {
if (login in data.choices[locationIndex]) { if (data.choices[locationKey] && login in data.choices[locationKey]) {
const index = data.choices[locationIndex][login].options.indexOf(foodIndex); const index = data.choices[locationKey][login].options.indexOf(foodIndex);
if (index > -1) { if (index > -1) {
data.choices[locationIndex][login].options.splice(index, 1) data.choices[locationKey][login].options.splice(index, 1)
await storage.setData(selectedDay, data); await storage.setData(selectedDay, data);
} }
} }
@ -247,10 +247,11 @@ export async function removeChoice(login: string, trusted: boolean, locationInde
async function removeChoiceIfPresent(login: string, date: string) { async function removeChoiceIfPresent(login: string, date: string) {
let data: DayData = await storage.getData(date); let data: DayData = await storage.getData(date);
for (const key of Object.keys(data.choices)) { for (const key of Object.keys(data.choices)) {
if (login in data.choices[key]) { const locationKey = key as LocationKey;
delete data.choices[key][login]; if (data.choices[locationKey] && login in data.choices[locationKey]) {
if (Object.keys(data.choices[key]).length === 0) { delete data.choices[locationKey][login];
delete data.choices[key]; if (Object.keys(data.choices[locationKey]).length === 0) {
delete data.choices[locationKey];
} }
await storage.setData(date, data); await storage.setData(date, data);
} }
@ -285,13 +286,13 @@ function validateTrusted(data: ClientData, login: string, trusted: boolean) {
* *
* @param login login uživatele * @param login login uživatele
* @param trusted příznak, zda se jedná o ověřeného uživatele * @param trusted příznak, zda se jedná o ověřeného uživatele
* @param locationIndex vybrané "umístění" * @param locationKey vybrané "umístění"
* @param foodIndex volitelný index jídla v daném umístění * @param foodIndex volitelný index jídla v daném umístění
* @param trusted příznak, zda se jedná o ověřeného uživatele * @param trusted příznak, zda se jedná o ověřeného uživatele
* @param date datum, ke kterému se volba vztahuje * @param date datum, ke kterému se volba vztahuje
* @returns aktuální data * @returns aktuální data
*/ */
export async function addChoice(login: string, trusted: boolean, locationIndex: number, foodIndex?: number, date?: Date) { export async function addChoice(login: string, trusted: boolean, locationKey: LocationKey, foodIndex?: number, date?: Date) {
const usedDate = date ?? getToday(); const usedDate = date ?? getToday();
await initIfNeeded(usedDate); await initIfNeeded(usedDate);
const selectedDate = formatDate(usedDate); const selectedDate = formatDate(usedDate);
@ -301,17 +302,21 @@ export async function addChoice(login: string, trusted: boolean, locationIndex:
if (foodIndex == null) { if (foodIndex == null) {
data = await removeChoiceIfPresent(login, selectedDate); data = await removeChoiceIfPresent(login, selectedDate);
} }
if (!(locationIndex in data.choices)) { // TODO vytáhnout inicializaci "prázdné struktury" do vlastní funkce
data.choices[locationIndex] = {}; if (!(data.choices[locationKey])) {
data.choices[locationKey] = {}
} }
if (!(login in data.choices[locationIndex])) { if (!(login in data.choices[locationKey])) {
data.choices[locationIndex][login] = { if (!data.choices[locationKey]) {
data.choices[locationKey] = {}
}
data.choices[locationKey][login] = {
trusted, trusted,
options: [] options: []
}; };
} }
if (foodIndex != null && !data.choices[locationIndex][login].options.includes(foodIndex)) { if (foodIndex != null && !data.choices[locationKey][login].options.includes(foodIndex)) {
data.choices[locationIndex][login].options.push(foodIndex); data.choices[locationKey][login].options.push(foodIndex);
} }
await storage.setData(selectedDate, data); await storage.setData(selectedDate, data);
return data; return data;

View File

@ -1,4 +1,4 @@
import { Choices } from "../../types"; import { Choices, LocationKey } from "../../types";
/** Vrátí datum v ISO formátu. */ /** Vrátí datum v ISO formátu. */
export function formatDate(date: Date) { export function formatDate(date: Date) {
@ -110,19 +110,19 @@ export const checkBodyParams = (req: any, paramNames: string[]) => {
// TODO umístit do samostatného souboru // TODO umístit do samostatného souboru
export class InsufficientPermissions extends Error { } export class InsufficientPermissions extends Error { }
export const getUsersByLocation = (data: Choices, login: string): string[] => { export const getUsersByLocation = (choices: Choices, login: string): string[] => {
const result: string[] = []; const result: string[] = [];
for (const location in data) { for (const location of Object.entries(choices)) {
if (data.hasOwnProperty(location)) { const locationKey = location[0] as LocationKey;
if (data[location][login]) { const locationValue = location[1];
for (const username in data[location]) { if (locationValue[login]) {
if (data[location].hasOwnProperty(username)) { for (const username in choices[locationKey]) {
result.push(username); if (choices[locationKey].hasOwnProperty(username)) {
} result.push(username);
} }
break;
} }
break;
} }
} }

View File

@ -1,20 +1,20 @@
import { FeatureRequest, PizzaOrder } from "./Types"; import { FeatureRequest, LocationKey, PizzaOrder } from "./Types";
export type ILocationKey = {
locationKey: LocationKey,
}
export type IDayIndex = { export type IDayIndex = {
dayIndex?: number, dayIndex?: number,
} }
export type AddChoiceRequest = IDayIndex & { export type AddChoiceRequest = IDayIndex & ILocationKey & {
locationIndex: number,
foodIndex?: number, foodIndex?: number,
} }
export type RemoveChoicesRequest = IDayIndex & { export type RemoveChoicesRequest = IDayIndex & ILocationKey;
locationIndex: number,
}
export type RemoveChoiceRequest = IDayIndex & { export type RemoveChoiceRequest = IDayIndex & ILocationKey & {
locationIndex: number,
foodIndex: number, foodIndex: number,
} }

View File

@ -12,10 +12,11 @@ export type FoodChoices = {
note?: string, note?: string,
} }
// TODO okomentovat / rozdělit
export type Choices = { export type Choices = {
[location: string]: { [location in LocationKey]?: {
[login: string]: FoodChoices [login: string]: FoodChoices
}, }
} }
/** Velikost konkrétní pizzy */ /** Velikost konkrétní pizzy */
@ -119,6 +120,9 @@ export enum Locations {
ROZHODUJI = 'Rozhoduji se', ROZHODUJI = 'Rozhoduji se',
} }
// TODO totéž
export type LocationKey = keyof typeof Locations;
export enum UdalostEnum { export enum UdalostEnum {
ZAHAJENA_PIZZA = "Zahájen pizza day", ZAHAJENA_PIZZA = "Zahájen pizza day",
OBJEDNANA_PIZZA = "Objednána pizza", OBJEDNANA_PIZZA = "Objednána pizza",