Otypování requestů na API

This commit is contained in:
2025-01-08 17:58:49 +01:00
parent 414664b2d7
commit e611d36995
10 changed files with 126 additions and 66 deletions

View File

@@ -1,10 +1,10 @@
import express from "express";
import express, { Request } from "express";
import { getLogin, getTrusted } from "../auth";
import { addChoice, addVolatileData, getDateForWeekIndex, getToday, removeChoice, removeChoices, updateDepartureTime, updateNote } from "../service";
import { getDayOfWeekIndex, parseToken } from "../utils";
import { getWebsocket } from "../websocket";
import { callNotifikace } from "../notifikace";
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ň
@@ -13,12 +13,12 @@ import { UdalostEnum } from "../../../types";
* @param req request
* @returns index dne v týdnu
*/
const parseValidateFutureDayIndex = (req: any) => {
const parseValidateFutureDayIndex = (req: Request<{}, any, IDayIndex>) => {
if (req.body.dayIndex == null) {
throw Error(`Nebyl předán index dne v týdnu.`);
}
const todayDayIndex = getDayOfWeekIndex(getToday());
const dayIndex = parseInt(req.body.dayIndex);
const dayIndex = req.body.dayIndex;
if (isNaN(dayIndex)) {
throw Error(`Neplatný index dne v týdnu: ${req.body.dayIndex}`);
}
@@ -30,7 +30,7 @@ const parseValidateFutureDayIndex = (req: any) => {
const router = express.Router();
router.post("/addChoice", async (req, res, next) => {
router.post("/addChoice", async (req: Request<{}, any, AddChoiceRequest>, res, next) => {
const login = getLogin(parseToken(req));
const trusted = getTrusted(parseToken(req));
if (req.body.locationIndex > -1) {
@@ -53,7 +53,7 @@ router.post("/addChoice", async (req, res, next) => {
return res.status(400); // TODO přidat popis chyby
});
router.post("/removeChoices", async (req, res, next) => {
router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesRequest>, res, next) => {
const login = getLogin(parseToken(req));
const trusted = getTrusted(parseToken(req));
let date = undefined;
@@ -73,7 +73,7 @@ router.post("/removeChoices", async (req, res, next) => {
} catch (e: any) { next(e) }
});
router.post("/removeChoice", async (req, res, next) => {
router.post("/removeChoice", async (req: Request<{}, any, RemoveChoiceRequest>, res, next) => {
const login = getLogin(parseToken(req));
const trusted = getTrusted(parseToken(req));
let date = undefined;
@@ -93,7 +93,7 @@ router.post("/removeChoice", async (req, res, next) => {
} catch (e: any) { next(e) }
});
router.post("/updateNote", async (req, res, next) => {
router.post("/updateNote", async (req: Request<{}, any, UpdateNoteRequest>, res, next) => {
const login = getLogin(parseToken(req));
const trusted = getTrusted(parseToken(req));
const note = req.body.note;
@@ -117,7 +117,7 @@ router.post("/updateNote", async (req, res, next) => {
} catch (e: any) { next(e) }
});
router.post("/changeDepartureTime", async (req, res, next) => {
router.post("/changeDepartureTime", async (req: Request<{}, any, ChangeDepartureTimeRequest>, res, next) => {
const login = getLogin(parseToken(req));
let date = undefined;
if (req.body.dayIndex != null) {

View File

@@ -1,14 +1,15 @@
import express from "express";
import express, { Request } from "express";
import { getLogin } from "../auth";
import { createPizzaDay, deletePizzaDay, getPizzaList, addPizzaOrder, removePizzaOrder, lockPizzaDay, unlockPizzaDay, finishPizzaOrder, finishPizzaDelivery, updatePizzaDayNote, updatePizzaFee } from "../pizza";
import { parseToken } from "../utils";
import { getWebsocket } from "../websocket";
import { addVolatileData } from "../service";
import { AddPizzaRequest, FinishDeliveryRequest, RemovePizzaRequest, UpdatePizzaDayNoteRequest, UpdatePizzaFeeRequest } from "../../../types";
const router = express.Router();
/** Založí pizza day pro aktuální den, za předpokladu že dosud neexistuje. */
router.post("/create", async (req, res) => {
router.post("/create", async (req: Request<{}, any, undefined>, res) => {
const login = getLogin(parseToken(req));
const data = await createPizzaDay(login);
res.status(200).json(data);
@@ -16,13 +17,13 @@ router.post("/create", async (req, res) => {
});
/** Smaže pizza day pro aktuální den, za předpokladu že existuje. */
router.post("/delete", async (req, res) => {
router.post("/delete", async (req: Request<{}, any, undefined>, res) => {
const login = getLogin(parseToken(req));
const data = await deletePizzaDay(login);
getWebsocket().emit("message", await addVolatileData(data));
});
router.post("/add", async (req, res) => {
router.post("/add", async (req: Request<{}, any, AddPizzaRequest>, res) => {
const login = getLogin(parseToken(req));
if (isNaN(req.body?.pizzaIndex)) {
throw Error("Nebyl předán index pizzy");
@@ -47,7 +48,7 @@ router.post("/add", async (req, res) => {
res.status(200).json({});
});
router.post("/remove", async (req, res) => {
router.post("/remove", async (req: Request<{}, any, RemovePizzaRequest>, res) => {
const login = getLogin(parseToken(req));
if (!req.body?.pizzaOrder) {
throw Error("Nebyla předána objednávka");
@@ -57,35 +58,35 @@ router.post("/remove", async (req, res) => {
res.status(200).json({});
});
router.post("/lock", async (req, res) => {
router.post("/lock", async (req: Request<{}, any, undefined>, res) => {
const login = getLogin(parseToken(req));
const data = await lockPizzaDay(login);
getWebsocket().emit("message", await addVolatileData(data));
res.status(200).json({});
});
router.post("/unlock", async (req, res) => {
router.post("/unlock", async (req: Request<{}, any, undefined>, res) => {
const login = getLogin(parseToken(req));
const data = await unlockPizzaDay(login);
getWebsocket().emit("message", await addVolatileData(data));
res.status(200).json({});
});
router.post("/finishOrder", async (req, res) => {
router.post("/finishOrder", async (req: Request<{}, any, undefined>, res) => {
const login = getLogin(parseToken(req));
const data = await finishPizzaOrder(login);
getWebsocket().emit("message", await addVolatileData(data));
res.status(200).json({});
});
router.post("/finishDelivery", async (req, res) => {
router.post("/finishDelivery", async (req: Request<{}, any, FinishDeliveryRequest>, res) => {
const login = getLogin(parseToken(req));
const data = await finishPizzaDelivery(login, req.body.bankAccount, req.body.bankAccountHolder);
getWebsocket().emit("message", await addVolatileData(data));
res.status(200).json({});
});
router.post("/updatePizzaDayNote", async (req, res, next) => {
router.post("/updatePizzaDayNote", async (req: Request<{}, any, UpdatePizzaDayNoteRequest>, res, next) => {
const login = getLogin(parseToken(req));
try {
if (req.body.note && req.body.note.length > 70) {
@@ -97,7 +98,7 @@ router.post("/updatePizzaDayNote", async (req, res, next) => {
} catch (e: any) { next(e) }
});
router.post("/updatePizzaFee", async (req, res, next) => {
router.post("/updatePizzaFee", async (req: Request<{}, any, UpdatePizzaFeeRequest>, res, next) => {
const login = getLogin(parseToken(req));
if (!req.body.login) {
return res.status(400).json({ error: "Nebyl předán login cílového uživatele" });

View File

@@ -1,17 +1,18 @@
import express from "express";
import express, { Request, Response } from "express";
import { getLogin } from "../auth";
import { parseToken } from "../utils";
import { getUserVotes, updateFeatureVote } from "../voting";
import { FeatureRequest, UpdateFeatureVoteRequest } from "../../../types";
const router = express.Router();
router.get("/getVotes", async (req, res) => {
router.get("/getVotes", async (req: Request<{}, any, undefined>, res: Response<FeatureRequest[]>) => {
const login = getLogin(parseToken(req));
const data = await getUserVotes(login);
res.status(200).json(data);
});
router.post("/updateVote", async (req, res, next) => {
router.post("/updateVote", async (req: Request<{}, any, UpdateFeatureVoteRequest>, res, next) => {
const login = getLogin(parseToken(req));
if (req.body?.option == null || req.body?.active == null) {
res.status(400).json({ error: "Chybné parametry volání" });

View File

@@ -192,19 +192,19 @@ export async function initIfNeeded(date?: Date) {
*
* @param login login uživatele
* @param trusted příznak, zda se jedná o ověřeného uživatele
* @param location vybrané "umístění"
* @param locationIndex vybrané "umístění"
* @param date datum, ke kterému se volba vztahuje
* @returns
*/
export async function removeChoices(login: string, trusted: boolean, location: Locations, date?: Date) {
export async function removeChoices(login: string, trusted: boolean, locationIndex: number, date?: Date) {
const selectedDay = formatDate(date ?? getToday());
let data: DayData = await storage.getData(selectedDay);
validateTrusted(data, login, trusted);
if (location in data.choices) {
if (login in data.choices[location]) {
delete data.choices[location][login]
if (Object.keys(data.choices[location]).length === 0) {
delete data.choices[location]
if (locationIndex in data.choices) {
if (login in data.choices[locationIndex]) {
delete data.choices[locationIndex][login]
if (Object.keys(data.choices[locationIndex]).length === 0) {
delete data.choices[locationIndex]
}
await storage.setData(selectedDay, data);
}
@@ -218,20 +218,20 @@ export async function removeChoices(login: string, trusted: boolean, location: L
*
* @param login login uživatele
* @param trusted příznak, zda se jedná o ověřeného uživatele
* @param location vybrané "umístění"
* @param locationIndex vybrané "umístění"
* @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
* @returns
*/
export async function removeChoice(login: string, trusted: boolean, location: Locations, foodIndex: number, date?: Date) {
export async function removeChoice(login: string, trusted: boolean, locationIndex: number, foodIndex: number, date?: Date) {
const selectedDay = formatDate(date ?? getToday());
let data: DayData = await storage.getData(selectedDay);
validateTrusted(data, login, trusted);
if (location in data.choices) {
if (login in data.choices[location]) {
const index = data.choices[location][login].options.indexOf(foodIndex);
if (locationIndex in data.choices) {
if (login in data.choices[locationIndex]) {
const index = data.choices[locationIndex][login].options.indexOf(foodIndex);
if (index > -1) {
data.choices[location][login].options.splice(index, 1)
data.choices[locationIndex][login].options.splice(index, 1)
await storage.setData(selectedDay, data);
}
}
@@ -285,13 +285,13 @@ function validateTrusted(data: ClientData, login: string, trusted: boolean) {
*
* @param login login uživatele
* @param trusted příznak, zda se jedná o ověřeného uživatele
* @param location vybrané "umístění"
* @param locationIndex vybrané "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 date datum, ke kterému se volba vztahuje
* @returns aktuální data
*/
export async function addChoice(login: string, trusted: boolean, location: Locations, foodIndex?: number, date?: Date) {
export async function addChoice(login: string, trusted: boolean, locationIndex: number, foodIndex?: number, date?: Date) {
const usedDate = date ?? getToday();
await initIfNeeded(usedDate);
const selectedDate = formatDate(usedDate);
@@ -301,17 +301,17 @@ export async function addChoice(login: string, trusted: boolean, location: Locat
if (foodIndex == null) {
data = await removeChoiceIfPresent(login, selectedDate);
}
if (!(location in data.choices)) {
data.choices[location] = {};
if (!(locationIndex in data.choices)) {
data.choices[locationIndex] = {};
}
if (!(login in data.choices[location])) {
data.choices[location][login] = {
if (!(login in data.choices[locationIndex])) {
data.choices[locationIndex][login] = {
trusted,
options: []
};
}
if (foodIndex != null && !data.choices[location][login].options.includes(foodIndex)) {
data.choices[location][login].options.push(foodIndex);
if (foodIndex != null && !data.choices[locationIndex][login].options.includes(foodIndex)) {
data.choices[locationIndex][login].options.push(foodIndex);
}
await storage.setData(selectedDate, data);
return data;