This commit is contained in:
Kevin Kaldoja 2025-04-23 23:17:30 +03:00
commit 9866d8951b
7 changed files with 2027 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
data/
node_modules/
.env

1558
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "tracking",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"body-parser": "^2.2.0",
"dotenv": "^16.5.0",
"ejs": "^3.1.10",
"express": "^5.1.0"
}
}

71
public/index.html Normal file
View File

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Track Your Package</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #1e1e1e;
color: #f1f1f1;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #2c2c2c;
border-radius: 15px;
padding: 40px 60px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 600px;
text-align: center;
}
h1 {
font-size: 36px;
color: #f1f1f1;
margin-bottom: 30px;
}
input {
width: 100%;
padding: 12px;
margin: 10px 0;
border-radius: 8px;
border: none;
font-size: 16px;
}
button {
padding: 12px;
width: 100%;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 10px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h1>Metsamarja Vedu</h1>
<form action="/track" method="POST">
<input type="text" name="id" placeholder="Enter Package ID" required>
<button type="submit">Track Package</button>
</form>
</div>
</body>
</html>

95
server.js Normal file
View File

@ -0,0 +1,95 @@
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}`);
});

133
views/admin.ejs Normal file
View File

@ -0,0 +1,133 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Panel</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #1e1e1e;
color: #f1f1f1;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #2c2c2c;
border-radius: 15px;
padding: 40px 60px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 500px;
text-align: center;
}
h1 {
font-size: 36px;
color: #f1f1f1;
margin-bottom: 30px;
}
input {
width: 100%;
padding: 12px;
margin: 10px 0;
border-radius: 10px;
border: 1px solid #444;
background-color: #444;
color: #fff;
font-size: 16px;
}
input:focus {
outline: none;
border-color: #777;
}
button {
padding: 12px;
width: 100%;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 10px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
.message {
font-size: 18px;
color: #ddd;
margin-top: 20px;
}
.message a {
color: #4caf50;
text-decoration: none;
}
.package-list {
margin-top: 20px;
text-align: left;
}
.package-item {
background-color: #333;
padding: 10px;
margin: 5px 0;
border-radius: 8px;
color: #fff;
}
.package-item strong {
color: #4caf50;
}
</style>
</head>
<body>
<div class="container">
<h1>Admin Panel</h1>
<% if (!packages) { %>
<% if (error) { %><p style="color:red;"><%= error %></p><% } %>
<form method="POST" action="/admin">
<input name="password" type="password" placeholder="Password" />
<button type="submit">Login</button>
</form>
<% } else { %>
<form method="POST" action="/update">
<input name="password" type="hidden" value="<%= process.env.ADMIN_PASSWORD %>">
<input name="id" placeholder="Tracking ID" required />
<input name="progress" type="number" placeholder="Progress %" required />
<input name="status" placeholder="Status" required />
<button type="submit">Update</button>
</form>
<h3>Packages</h3>
<div class="package-list">
<% packages.forEach(p => { %>
<div class="package-item">
<strong><%= p.id %></strong><br>
<% if (p.updates && p.updates.length > 0) { %>
<%= p.updates[p.updates.length - 1].status %> —
<%= p.updates[p.updates.length - 1].progress %>%
<% } else { %>
<em>No updates yet</em>
<% } %>
</div>
<% }) %>
</div>
<% } %>
</div>
</body>
</html>

148
views/track.ejs Normal file
View File

@ -0,0 +1,148 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Track Your Package</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #1e1e1e;
color: #f1f1f1;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #2c2c2c;
border-radius: 15px;
padding: 40px 60px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 600px;
text-align: center;
}
h1 {
font-size: 36px;
color: #f1f1f1;
margin-bottom: 30px;
}
.status {
background-color: #333;
padding: 20px;
margin: 20px 0;
border-radius: 8px;
}
.status h3 {
margin: 0;
color: #4caf50;
}
.status p {
margin: 5px 0;
color: #ddd;
}
.status .progress-bar {
width: 100%;
height: 10px;
background-color: #444;
border-radius: 5px;
}
.progress {
height: 100%;
background-color: #4caf50;
border-radius: 5px;
}
.history {
text-align: left;
padding: 20px;
margin-top: 20px;
background-color: #444;
border-radius: 8px;
}
.history li {
margin-bottom: 10px;
}
.message {
font-size: 18px;
color: #ddd;
margin-top: 20px;
}
.message a {
color: #4caf50;
text-decoration: none;
}
button {
padding: 12px;
width: 100%;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 10px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h1>Metsamarja Vedu</h1>
<% if (package) { %>
<div class="status">
<h3>Package ID: <%= package.id %></h3>
<p>Status: <%= latest.status %></p>
<p>Progress: <%= latest.progress %>%</p>
<!-- Progress Bar -->
<div class="progress-bar">
<div class="progress" style="width: <%= latest.progress + '%' %>"></div>
</div>
</div>
<!-- Estimated Delivery Time -->
<div class="status">
<h3>Estimated Delivery Time</h3>
<p><%= estimatedDeliveryTime %></p>
</div>
<!-- Tracking History -->
<div class="history">
<h4>Tracking History:</h4>
<ul>
<% history.forEach(u => { %>
<li><strong><%= u.time %>:</strong> <%= u.status %> <%= u.progress %>%</li>
<% }) %>
</ul>
</div>
<div class="message">
<p>Want to track another package? <a href="/">Go back to Home</a></p>
</div>
<% } else { %>
<div class="message">
<p>Package not found. <a href="/">Try again</a></p>
</div>
<% } %>
</div>
</body>
</html>