fix and stuff

This commit is contained in:
2025-04-24 18:21:28 +03:00
parent 9866d8951b
commit f03cc3e203
3 changed files with 98 additions and 118 deletions

View File

@ -12,23 +12,41 @@ const PASSWORD = process.env.ADMIN_PASSWORD;
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use((req, res, next) => {
console.log(`[${new Date().toLocaleString()}] ${req.method} ${req.url}`);
next();
});
// Load packages from JSON
function loadPackages() {
if (!fs.existsSync(DATA_PATH)) return [];
return JSON.parse(fs.readFileSync(DATA_PATH));
}
// Save packages to JSON
function savePackages(data) {
fs.writeFileSync(DATA_PATH, JSON.stringify(data, null, 2));
}
// Homepage - tracking form
// Format local date/time to "YYYY-MM-DD HH:mm"
function getLocalTimeString() {
const now = new Date();
return now.getFullYear() + "-" +
String(now.getMonth() + 1).padStart(2, '0') + "-" +
String(now.getDate()).padStart(2, '0') + " " +
String(now.getHours()).padStart(2, '0') + ":" +
String(now.getMinutes()).padStart(2, '0');
}
// Home page
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Handle tracking request (POST)
// Track package
app.post("/track", (req, res) => {
const id = req.body.id?.trim(); // Get package ID from the form submission
const id = req.body.id?.trim();
if (!id) return res.send("Invalid tracking ID");
const packages = loadPackages();
@ -36,26 +54,23 @@ app.post("/track", (req, res) => {
if (!pack) return res.send(`<p>ID not found</p><a href="/">Back</a>`);
// Prepare data to be passed to the view
const latest = pack.updates[pack.updates.length - 1];
const history = [...pack.updates].reverse();
// Estimate delivery time (3 days from last update)
const estimatedDeliveryDate = new Date();
estimatedDeliveryDate.setDate(estimatedDeliveryDate.getDate() + 3); // Add 3 days
const estimatedDeliveryTime = `Estimated Delivery Date: ${estimatedDeliveryDate.toLocaleDateString()}`;
// Render track.ejs with the package data
res.render("track", { package: pack, latest, history, estimatedDeliveryTime });
res.render("track", {
id: pack.id,
latest,
history,
eta: pack.eta || "Not set"
});
});
// Admin login form
// Admin panel
app.get("/admin", (req, res) => {
res.render("admin", { packages: null, error: null });
});
// Admin password check
// Admin login
app.post("/admin", (req, res) => {
const { password } = req.body;
if (password !== PASSWORD) {
@ -65,16 +80,15 @@ app.post("/admin", (req, res) => {
res.render("admin", { packages, error: null });
});
// Handle updates
// Handle update
app.post("/update", (req, res) => {
const { id, progress, status, password } = req.body;
const { id, progress, status, eta, password } = req.body;
if (password !== PASSWORD) return res.send("Invalid password");
const now = new Date().toISOString().replace("T", " ").slice(0, 16);
const update = {
progress: parseInt(progress),
status,
time: now
time: getLocalTimeString()
};
const packages = loadPackages();
@ -82,8 +96,14 @@ app.post("/update", (req, res) => {
if (pack) {
pack.updates.push(update);
if (eta) pack.eta = eta;
} else {
packages.push({ id, updates: [update] });
const newPackage = {
id,
updates: [update],
eta: eta || ""
};
packages.push(newPackage);
}
savePackages(packages);