96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
|
const express = require("express");
|
||
|
const bodyParser = require("body-parser");
|
||
|
const fs = require("fs");
|
||
|
const path = require("path");
|
||
|
require("dotenv").config();
|
||
|
|
||
|
const app = express();
|
||
|
const PORT = 3000;
|
||
|
const DATA_PATH = path.join(__dirname, "data", "packages.json");
|
||
|
const PASSWORD = process.env.ADMIN_PASSWORD;
|
||
|
|
||
|
app.set("view engine", "ejs");
|
||
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||
|
app.use(express.static("public"));
|
||
|
|
||
|
function loadPackages() {
|
||
|
return JSON.parse(fs.readFileSync(DATA_PATH));
|
||
|
}
|
||
|
|
||
|
function savePackages(data) {
|
||
|
fs.writeFileSync(DATA_PATH, JSON.stringify(data, null, 2));
|
||
|
}
|
||
|
|
||
|
// Homepage - tracking form
|
||
|
app.get("/", (req, res) => {
|
||
|
res.sendFile(path.join(__dirname, "public", "index.html"));
|
||
|
});
|
||
|
|
||
|
// Handle tracking request (POST)
|
||
|
app.post("/track", (req, res) => {
|
||
|
const id = req.body.id?.trim(); // Get package ID from the form submission
|
||
|
if (!id) return res.send("Invalid tracking ID");
|
||
|
|
||
|
const packages = loadPackages();
|
||
|
const pack = packages.find(p => p.id === id);
|
||
|
|
||
|
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 });
|
||
|
});
|
||
|
|
||
|
// Admin login form
|
||
|
app.get("/admin", (req, res) => {
|
||
|
res.render("admin", { packages: null, error: null });
|
||
|
});
|
||
|
|
||
|
// Admin password check
|
||
|
app.post("/admin", (req, res) => {
|
||
|
const { password } = req.body;
|
||
|
if (password !== PASSWORD) {
|
||
|
return res.render("admin", { packages: null, error: "Wrong password" });
|
||
|
}
|
||
|
const packages = loadPackages();
|
||
|
res.render("admin", { packages, error: null });
|
||
|
});
|
||
|
|
||
|
// Handle updates
|
||
|
app.post("/update", (req, res) => {
|
||
|
const { id, progress, status, 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
|
||
|
};
|
||
|
|
||
|
const packages = loadPackages();
|
||
|
const pack = packages.find(p => p.id === id);
|
||
|
|
||
|
if (pack) {
|
||
|
pack.updates.push(update);
|
||
|
} else {
|
||
|
packages.push({ id, updates: [update] });
|
||
|
}
|
||
|
|
||
|
savePackages(packages);
|
||
|
res.redirect("/admin");
|
||
|
});
|
||
|
|
||
|
app.listen(PORT, () => {
|
||
|
console.log(`🚀 Courier tracker running at http://localhost:${PORT}`);
|
||
|
});
|