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 { initWebsocket } from "./websocket";
|
||||
import pizzaDayRoutes from "./routes/pizzaDayRoutes";
|
||||
import foodRoutes from "./routes/foodRoutes";
|
||||
import foodRoutes, { refreshMetoda } from "./routes/foodRoutes";
|
||||
import votingRoutes from "./routes/votingRoutes";
|
||||
import easterEggRoutes from "./routes/easterEggRoutes";
|
||||
import statsRoutes from "./routes/statsRoutes";
|
||||
@ -97,7 +97,7 @@ app.get("/api/qr", (req, res) => {
|
||||
// ----------------------------------------------------
|
||||
|
||||
// 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 */
|
||||
app.use("/api/", (req, res, next) => {
|
||||
|
@ -183,7 +183,7 @@ router.post("/jdemeObed", async (req, res, next) => {
|
||||
});
|
||||
|
||||
// /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 };
|
||||
if (heslo !== "docasnyheslo" && heslo !== "tohleheslopavelnesmizjistit123") {
|
||||
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 successfulRestaurants: string[] = [];
|
||||
const failedRestaurants: string[] = [];
|
||||
|
||||
|
||||
// Nejdříve načíst všechna data bez ukládání
|
||||
for (const rest of restaurants) {
|
||||
try {
|
||||
const weekData = await fetchRestaurantWeekMenuData(rest, firstDay);
|
||||
results[rest] = weekData;
|
||||
|
||||
|
||||
// Kontrola validity dat
|
||||
if (weekData && weekData.length > 0 &&
|
||||
if (weekData && weekData.length > 0 &&
|
||||
weekData.some(dayMenu => dayMenu && dayMenu.length > 0)) {
|
||||
successfulRestaurants.push(rest);
|
||||
} else {
|
||||
@ -224,16 +224,16 @@ router.get("/refresh", async (req: Request, res: Response) => {
|
||||
results[rest] = { error: `Chyba při načítání: ${error}` };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Pokud se nepodařilo načíst žádnou restauraci
|
||||
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",
|
||||
failed: failedRestaurants,
|
||||
results: results
|
||||
results: results
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Uložit pouze validní data
|
||||
for (const rest of successfulRestaurants) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Připravit odpověď
|
||||
const response: any = {
|
||||
ok: true,
|
||||
refreshed: results,
|
||||
successful: successfulRestaurants
|
||||
};
|
||||
|
||||
|
||||
if (failedRestaurants.length > 0) {
|
||||
response.warning = `Nepodařilo se načíst: ${failedRestaurants.join(', ')}`;
|
||||
response.failed = failedRestaurants;
|
||||
}
|
||||
|
||||
|
||||
res.status(200).json(response);
|
||||
} catch (e: any) {
|
||||
res.status(500).json({ error: e?.message || "Chyba při refreshi" });
|
||||
}
|
||||
});
|
||||
}
|
||||
router.get("/refresh", refreshMetoda);
|
||||
|
||||
|
||||
export default router;
|
Loading…
x
Reference in New Issue
Block a user