66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
import express from "express";
|
|
import path from "path";
|
|
const app = express();
|
|
import fs from "fs/promises";
|
|
import { getCurrentInterval } from "./scheduleRules.js";
|
|
import bodyParser from "body-parser";
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
globalThis.File = class File {};
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
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}`;
|
|
|
|
console.log(url)
|
|
|
|
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}`);
|
|
});
|