pro refresh endpoint nevyzadovat authtoken
This commit is contained in:
parent
58bb5f4e7d
commit
cfffd2b31d
@ -9,7 +9,7 @@ import { generateToken, verify } from "./auth";
|
|||||||
import { InsufficientPermissions } from "./utils";
|
import { InsufficientPermissions } from "./utils";
|
||||||
import { initWebsocket } from "./websocket";
|
import { initWebsocket } from "./websocket";
|
||||||
import pizzaDayRoutes from "./routes/pizzaDayRoutes";
|
import pizzaDayRoutes from "./routes/pizzaDayRoutes";
|
||||||
import foodRoutes from "./routes/foodRoutes";
|
import foodRoutes, { refreshMetoda } from "./routes/foodRoutes";
|
||||||
import votingRoutes from "./routes/votingRoutes";
|
import votingRoutes from "./routes/votingRoutes";
|
||||||
import easterEggRoutes from "./routes/easterEggRoutes";
|
import easterEggRoutes from "./routes/easterEggRoutes";
|
||||||
import statsRoutes from "./routes/statsRoutes";
|
import statsRoutes from "./routes/statsRoutes";
|
||||||
@ -97,7 +97,7 @@ app.get("/api/qr", (req, res) => {
|
|||||||
// ----------------------------------------------------
|
// ----------------------------------------------------
|
||||||
|
|
||||||
// Přeskočení auth pro refresh dat xd
|
// Přeskočení auth pro refresh dat xd
|
||||||
app.use("/api/food/refresh", foodRoutes);
|
app.use("/api/food/refresh", refreshMetoda);
|
||||||
|
|
||||||
/** Middleware ověřující JWT token */
|
/** Middleware ověřující JWT token */
|
||||||
app.use("/api/", (req, res, next) => {
|
app.use("/api/", (req, res, next) => {
|
||||||
|
@ -183,7 +183,7 @@ router.post("/jdemeObed", async (req, res, next) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// /api/food/refresh?type=week&heslo=docasnyheslo
|
// /api/food/refresh?type=week&heslo=docasnyheslo
|
||||||
router.get("/refresh", async (req: Request, res: Response) => {
|
export const refreshMetoda = async (req: Request, res: Response) => {
|
||||||
const { type, heslo } = req.query as { type?: string; heslo?: string };
|
const { type, heslo } = req.query as { type?: string; heslo?: string };
|
||||||
if (heslo !== "docasnyheslo" && heslo !== "tohleheslopavelnesmizjistit123") {
|
if (heslo !== "docasnyheslo" && heslo !== "tohleheslopavelnesmizjistit123") {
|
||||||
return res.status(403).json({ error: "Neplatné heslo" });
|
return res.status(403).json({ error: "Neplatné heslo" });
|
||||||
@ -204,15 +204,15 @@ router.get("/refresh", async (req: Request, res: Response) => {
|
|||||||
const results: Record<string, any> = {};
|
const results: Record<string, any> = {};
|
||||||
const successfulRestaurants: string[] = [];
|
const successfulRestaurants: string[] = [];
|
||||||
const failedRestaurants: string[] = [];
|
const failedRestaurants: string[] = [];
|
||||||
|
|
||||||
// Nejdříve načíst všechna data bez ukládání
|
// Nejdříve načíst všechna data bez ukládání
|
||||||
for (const rest of restaurants) {
|
for (const rest of restaurants) {
|
||||||
try {
|
try {
|
||||||
const weekData = await fetchRestaurantWeekMenuData(rest, firstDay);
|
const weekData = await fetchRestaurantWeekMenuData(rest, firstDay);
|
||||||
results[rest] = weekData;
|
results[rest] = weekData;
|
||||||
|
|
||||||
// Kontrola validity dat
|
// Kontrola validity dat
|
||||||
if (weekData && weekData.length > 0 &&
|
if (weekData && weekData.length > 0 &&
|
||||||
weekData.some(dayMenu => dayMenu && dayMenu.length > 0)) {
|
weekData.some(dayMenu => dayMenu && dayMenu.length > 0)) {
|
||||||
successfulRestaurants.push(rest);
|
successfulRestaurants.push(rest);
|
||||||
} else {
|
} else {
|
||||||
@ -224,16 +224,16 @@ router.get("/refresh", async (req: Request, res: Response) => {
|
|||||||
results[rest] = { error: `Chyba při načítání: ${error}` };
|
results[rest] = { error: `Chyba při načítání: ${error}` };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pokud se nepodařilo načíst žádnou restauraci
|
// Pokud se nepodařilo načíst žádnou restauraci
|
||||||
if (successfulRestaurants.length === 0) {
|
if (successfulRestaurants.length === 0) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
error: "Nepodařilo se získat validní data z žádné restaurace",
|
error: "Nepodařilo se získat validní data z žádné restaurace",
|
||||||
failed: failedRestaurants,
|
failed: failedRestaurants,
|
||||||
results: results
|
results: results
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uložit pouze validní data
|
// Uložit pouze validní data
|
||||||
for (const rest of successfulRestaurants) {
|
for (const rest of successfulRestaurants) {
|
||||||
try {
|
try {
|
||||||
@ -242,23 +242,25 @@ router.get("/refresh", async (req: Request, res: Response) => {
|
|||||||
console.error(`Chyba při ukládání dat pro ${rest}:`, error);
|
console.error(`Chyba při ukládání dat pro ${rest}:`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Připravit odpověď
|
// Připravit odpověď
|
||||||
const response: any = {
|
const response: any = {
|
||||||
ok: true,
|
ok: true,
|
||||||
refreshed: results,
|
refreshed: results,
|
||||||
successful: successfulRestaurants
|
successful: successfulRestaurants
|
||||||
};
|
};
|
||||||
|
|
||||||
if (failedRestaurants.length > 0) {
|
if (failedRestaurants.length > 0) {
|
||||||
response.warning = `Nepodařilo se načíst: ${failedRestaurants.join(', ')}`;
|
response.warning = `Nepodařilo se načíst: ${failedRestaurants.join(', ')}`;
|
||||||
response.failed = failedRestaurants;
|
response.failed = failedRestaurants;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).json(response);
|
res.status(200).json(response);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
res.status(500).json({ error: e?.message || "Chyba při refreshi" });
|
res.status(500).json({ error: e?.message || "Chyba při refreshi" });
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
router.get("/refresh", refreshMetoda);
|
||||||
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
Loading…
x
Reference in New Issue
Block a user