diff --git a/server.js b/server.js index 6385067..0e3223f 100644 --- a/server.js +++ b/server.js @@ -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(`

ID not found

Back`); - // 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); diff --git a/views/admin.ejs b/views/admin.ejs index 6e0159b..b992df0 100644 --- a/views/admin.ejs +++ b/views/admin.ejs @@ -110,6 +110,7 @@ + @@ -119,11 +120,12 @@
<%= p.id %>
<% if (p.updates && p.updates.length > 0) { %> - <%= p.updates[p.updates.length - 1].status %> — - <%= p.updates[p.updates.length - 1].progress %>% + <%= p.updates[p.updates.length - 1].status %> — + <%= p.updates[p.updates.length - 1].progress %>%
<% } else { %> - No updates yet + No updates yet
<% } %> + ETA: <%= p.eta || 'Not set' %>
<% }) %> diff --git a/views/track.ejs b/views/track.ejs index d97cc94..a167224 100644 --- a/views/track.ejs +++ b/views/track.ejs @@ -1,9 +1,9 @@ - - - Track Your Package + + + Tracking Info

Metsamarja Vedu

+

Tracking ID: <%= id %>

+

<%= latest.status %> — <%= latest.progress %>%

- <% if (package) { %> -
-

Package ID: <%= package.id %>

-

Status: <%= latest.status %>

-

Progress: <%= latest.progress %>%

+
+
+
- -
-
-
-
+

Tracking History

+ - -
-

Estimated Delivery Time

-

<%= estimatedDeliveryTime %>

-
- - -
-

Tracking History:

- -
- -
-

Want to track another package? Go back to Home

-
- <% } else { %> -
-

Package not found. Try again

+ <% if (eta) { %> +
+ Estimated Delivery: <%= eta %>
<% } %> + + Back to Home