All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
167 lines
6.4 KiB
TypeScript
167 lines
6.4 KiB
TypeScript
import express, { Request, Response } from "express";
|
|
import { getLogin, getTrusted } from "../auth";
|
|
import { addChoice, getDateForWeekIndex, getToday, removeChoice, removeChoices, updateDepartureTime, updateNote, getRestaurantMenu } from "../service";
|
|
import { getDayOfWeekIndex, parseToken, getFirstWorkDayOfWeek } from "../utils";
|
|
import { getWebsocket } from "../websocket";
|
|
import { callNotifikace } from "../notifikace";
|
|
import { AddChoiceData, ChangeDepartureTimeData, RemoveChoiceData, RemoveChoicesData, UdalostEnum, UpdateNoteData } from "../../../types/gen/types.gen";
|
|
|
|
/**
|
|
* Ověří a vrátí index dne v týdnu z požadavku, za předpokladu, že byl předán, a je zároveň
|
|
* roven nebo vyšší indexu dnešního dne.
|
|
*
|
|
* @param req request
|
|
* @returns index dne v týdnu
|
|
*/
|
|
const parseValidateFutureDayIndex = (req: Request<{}, any, AddChoiceData["body"] | UpdateNoteData["body"]>) => {
|
|
if (req.body.dayIndex == null) {
|
|
throw Error(`Nebyl předán index dne v týdnu.`);
|
|
}
|
|
const todayDayIndex = getDayOfWeekIndex(getToday());
|
|
const dayIndex = req.body.dayIndex;
|
|
if (isNaN(dayIndex)) {
|
|
throw Error(`Neplatný index dne v týdnu: ${req.body.dayIndex}`);
|
|
}
|
|
if (dayIndex < todayDayIndex) {
|
|
throw Error(`Předaný index dne v týdnu (${dayIndex}) nesmí být nižší než dnešní den (${todayDayIndex})`);
|
|
}
|
|
return dayIndex;
|
|
}
|
|
|
|
const router = express.Router();
|
|
|
|
router.post("/addChoice", async (req: Request<{}, any, AddChoiceData["body"]>, res, next) => {
|
|
const login = getLogin(parseToken(req));
|
|
const trusted = getTrusted(parseToken(req));
|
|
let date = undefined;
|
|
if (req.body.dayIndex != null) {
|
|
let dayIndex;
|
|
try {
|
|
dayIndex = parseValidateFutureDayIndex(req);
|
|
} catch (e: any) {
|
|
return res.status(400).json({ error: e.message });
|
|
}
|
|
date = getDateForWeekIndex(dayIndex);
|
|
}
|
|
try {
|
|
const data = await addChoice(login, trusted, req.body.locationKey, req.body.foodIndex, date);
|
|
getWebsocket().emit("message", data);
|
|
return res.status(200).json(data);
|
|
} catch (e: any) { next(e) }
|
|
});
|
|
|
|
router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesData["body"]>, res, next) => {
|
|
const login = getLogin(parseToken(req));
|
|
const trusted = getTrusted(parseToken(req));
|
|
let date = undefined;
|
|
if (req.body.dayIndex != null) {
|
|
let dayIndex;
|
|
try {
|
|
dayIndex = parseValidateFutureDayIndex(req);
|
|
} catch (e: any) {
|
|
return res.status(400).json({ error: e.message });
|
|
}
|
|
date = getDateForWeekIndex(dayIndex);
|
|
}
|
|
try {
|
|
const data = await removeChoices(login, trusted, req.body.locationKey, date);
|
|
getWebsocket().emit("message", data);
|
|
res.status(200).json(data);
|
|
} catch (e: any) { next(e) }
|
|
});
|
|
|
|
router.post("/removeChoice", async (req: Request<{}, any, RemoveChoiceData["body"]>, res, next) => {
|
|
const login = getLogin(parseToken(req));
|
|
const trusted = getTrusted(parseToken(req));
|
|
let date = undefined;
|
|
if (req.body.dayIndex != null) {
|
|
let dayIndex;
|
|
try {
|
|
dayIndex = parseValidateFutureDayIndex(req);
|
|
} catch (e: any) {
|
|
return res.status(400).json({ error: e.message });
|
|
}
|
|
date = getDateForWeekIndex(dayIndex);
|
|
}
|
|
try {
|
|
const data = await removeChoice(login, trusted, req.body.locationKey, req.body.foodIndex, date);
|
|
getWebsocket().emit("message", data);
|
|
res.status(200).json(data);
|
|
} catch (e: any) { next(e) }
|
|
});
|
|
|
|
router.post("/updateNote", async (req: Request<{}, any, UpdateNoteData["body"]>, res, next) => {
|
|
const login = getLogin(parseToken(req));
|
|
const trusted = getTrusted(parseToken(req));
|
|
const note = req.body.note;
|
|
try {
|
|
if (note && note.length > 70) {
|
|
throw Error("Poznámka může mít maximálně 70 znaků");
|
|
}
|
|
let date = undefined;
|
|
if (req.body.dayIndex != null) {
|
|
let dayIndex;
|
|
try {
|
|
dayIndex = parseValidateFutureDayIndex(req);
|
|
} catch (e: any) {
|
|
return res.status(400).json({ error: e.message });
|
|
}
|
|
date = getDateForWeekIndex(dayIndex);
|
|
}
|
|
const data = await updateNote(login, trusted, note, date);
|
|
getWebsocket().emit("message", data);
|
|
res.status(200).json(data);
|
|
} catch (e: any) { next(e) }
|
|
});
|
|
|
|
router.post("/changeDepartureTime", async (req: Request<{}, any, ChangeDepartureTimeData["body"]>, res, next) => {
|
|
const login = getLogin(parseToken(req));
|
|
let date = undefined;
|
|
if (req.body.dayIndex != null) {
|
|
let dayIndex;
|
|
try {
|
|
dayIndex = parseValidateFutureDayIndex(req);
|
|
} catch (e: any) {
|
|
return res.status(400).json({ error: e.message });
|
|
}
|
|
date = getDateForWeekIndex(dayIndex);
|
|
}
|
|
try {
|
|
const data = await updateDepartureTime(login, req.body?.time, date);
|
|
getWebsocket().emit("message", data);
|
|
res.status(200).json(data);
|
|
} catch (e: any) { next(e) }
|
|
});
|
|
|
|
router.post("/jdemeObed", async (req, res, next) => {
|
|
const login = getLogin(parseToken(req));
|
|
try {
|
|
await callNotifikace({ input: { user: login, udalost: UdalostEnum.JDEME_NA_OBED }, gotify: false })
|
|
res.status(200).json({});
|
|
} catch (e: any) { next(e) }
|
|
});
|
|
|
|
// /api/food/refresh?type=week&heslo=docasnyheslo
|
|
router.get("/refresh", async (req: Request, res: Response) => {
|
|
const { type, heslo } = req.query as { type?: string; heslo?: string };
|
|
if (heslo !== "docasnyheslo") {
|
|
return res.status(403).json({ error: "Neplatné heslo" });
|
|
}
|
|
if (type !== "week") {
|
|
return res.status(400).json({ error: "Neznámý typ refresh" });
|
|
}
|
|
try {
|
|
// Pro všechny restaurace refreshni menu na aktuální týden
|
|
const restaurants = ["SLADOVNICKA", "TECHTOWER", "ZASTAVKAUMICHALA", "SENKSERIKOVA"] as const;
|
|
const firstDay = getFirstWorkDayOfWeek(getToday());
|
|
const results: Record<string, any> = {};
|
|
for (const rest of restaurants) {
|
|
results[rest] = await getRestaurantMenu(rest, firstDay);
|
|
}
|
|
res.status(200).json({ ok: true, refreshed: results });
|
|
} catch (e: any) {
|
|
res.status(500).json({ error: e?.message || "Chyba při refreshi" });
|
|
}
|
|
});
|
|
|
|
export default router; |