Serverová validace času odchodu

This commit is contained in:
2023-09-24 08:38:40 +02:00
parent c286bd1778
commit e4451e299a
4 changed files with 38 additions and 24 deletions

View File

@@ -293,7 +293,7 @@ app.post("/api/updatePizzaDayNote", async (req, res) => {
res.status(200).json(data);
});
app.post("/api/changeDepartureTime", async (req, res) => {
app.post("/api/changeDepartureTime", async (req, res, next) => {
const login = getLogin(parseToken(req));
let date = undefined;
if (req.body.dayIndex != null) {
@@ -305,9 +305,11 @@ app.post("/api/changeDepartureTime", async (req, res) => {
}
date = getDateForWeekIndex(dayIndex);
}
const data = await updateDepartureTime(login, req.body?.time, date);
io.emit("message", data);
res.status(200).json(data);
try {
const data = await updateDepartureTime(login, req.body?.time, date);
io.emit("message", data);
res.status(200).json(data);
} catch (e: any) { next(e) }
});
// Middleware pro zpracování chyb

View File

@@ -1,5 +1,5 @@
import { InsufficientPermissions, formatDate, getDayOfWeekIndex, getHumanDate, getHumanTime, getIsWeekend } from "./utils";
import { ClientData, Locations, Restaurants, Menu } from "../../types";
import { ClientData, Locations, Restaurants, Menu, DepartureTime } from "../../types";
import getStorage from "./storage";
import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku } from "./restaurants";
import { getTodayMock } from "./mock";
@@ -29,7 +29,14 @@ export const getDateForWeekIndex = (index: number) => {
/** Vrátí "prázdná" (implicitní) data pro předaný den. */
function getEmptyData(date?: Date): ClientData {
const usedDate = date || getToday();
return { date: getHumanDate(usedDate), isWeekend: getIsWeekend(usedDate), weekIndex: getDayOfWeekIndex(usedDate), todayWeekIndex: getDayOfWeekIndex(getToday()), choices: {} };
return {
date: getHumanDate(usedDate),
isWeekend: getIsWeekend(usedDate),
weekIndex: getDayOfWeekIndex(usedDate),
todayWeekIndex: getDayOfWeekIndex(getToday()),
choices: {},
departureTimes: Object.values(DepartureTime),
};
}
/**
@@ -246,6 +253,9 @@ export async function updateDepartureTime(login: string, time?: string, date?: D
if (!time?.length) {
delete found[login].departureTime;
} else {
if (!Object.values<string>(DepartureTime).includes(time)) {
throw Error(`Neplatný čas odchodu ${time}`);
}
found[login].departureTime = time;
}
await storage.setData(selectedDate, clientData);