1
0
Files
jecnarozvrh/server.js
jzitnik-dev 5fe6ac4a29
All checks were successful
Remote Deploy / deploy (push) Successful in 3s
fix: Maybe this
2025-10-24 08:21:24 +02:00

61 lines
1.6 KiB
JavaScript

import express from "express";
import path from "path";
const app = express();
import fs from "fs/promises";
import { getCurrentInterval } from "./scheduleRules.js";
const PORT = process.env.PORT || 3000;
globalThis.File = class File {};
app.get('/', async (_, res) => {
const dataStr = await fs.readFile(path.join(process.cwd(), "db", "current.json"), "utf8");
const data = JSON.parse(dataStr);
data["status"]["currentUpdateSchedule"] = getCurrentInterval();
res.json(data);
});
app.get("/status", async (_, res) => {
const dataStr = await fs.readFile(path.resolve("./volume/customState.json"), {encoding: "utf8"});
const data = JSON.parse(dataStr);
if (data.working) {
res.json({ working: true })
} else {
res.json({ working: data.working, message: data.message })
}
})
app.post("/report", async (req, res) => {
const { class: className, location, content } = req.body;
if (!className || !location || !content) {
return res.status(400).json({ error: "Missing required fields." });
}
if (!["TIMETABLE", "ABSENCES", "OTHER"].includes(location)) {
return res.status(400).json({ error: "Invalid location value." });
}
const url = `https://n8n.local.jzitnik.dev/webhook/${process.env.WEBHOOK_UUID}`;
const resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "text/plain",
},
body: `${content}\n\nClass: ${className}\nLocation: ${location}`,
});
if (!resp.ok) {
throw new Error(`Request failed`);
}
res.status(200).json({ message: "Report received successfully." });
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});