fix attempt
This commit is contained in:
parent
d01d27e8a5
commit
da0c7b27d5
|
Before Width: | Height: | Size: 144 KiB After Width: | Height: | Size: 144 KiB |
20
server.js
20
server.js
@ -18,10 +18,6 @@ const app = express();
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// --- NEW: Serve static files from the 'public' directory ---
|
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
|
||||||
|
|
||||||
|
|
||||||
const dbPath = path.resolve(__dirname, 'data', 'timetracker.db');
|
const dbPath = path.resolve(__dirname, 'data', 'timetracker.db');
|
||||||
const db = new sqlite3.Database(dbPath, (err) => {
|
const db = new sqlite3.Database(dbPath, (err) => {
|
||||||
if (err) console.error("Error opening database", err.message);
|
if (err) console.error("Error opening database", err.message);
|
||||||
@ -46,6 +42,7 @@ function initializeDatabase() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- NEW: Clean up past time-off requests on server start ---
|
||||||
const today = new Date().toISOString().split('T')[0];
|
const today = new Date().toISOString().split('T')[0];
|
||||||
db.run(`DELETE FROM time_off_requests WHERE end_date < ?`, [today], function(err) {
|
db.run(`DELETE FROM time_off_requests WHERE end_date < ?`, [today], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -73,9 +70,7 @@ function authenticateToken(req, res, next) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- ALL /api/... ROUTES GO HERE (Code unchanged) ---
|
// --- API Routes ---
|
||||||
// (Your existing API routes for login, punch, admin, etc.)
|
|
||||||
|
|
||||||
app.post('/api/login', (req, res) => {
|
app.post('/api/login', (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password } = req.body;
|
||||||
db.get('SELECT * FROM users WHERE username = ?', [username], (err, user) => {
|
db.get('SELECT * FROM users WHERE username = ?', [username], (err, user) => {
|
||||||
@ -144,6 +139,8 @@ app.get('/api/user/time-off-requests', authenticateToken, (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// --- Admin Routes ---
|
||||||
app.post('/api/admin/create-user', authenticateToken, requireRole('admin'), (req, res) => {
|
app.post('/api/admin/create-user', authenticateToken, requireRole('admin'), (req, res) => {
|
||||||
const { username, password, role } = req.body;
|
const { username, password, role } = req.body;
|
||||||
const userRole = (role === 'admin' || role === 'employee') ? role : 'employee';
|
const userRole = (role === 'admin' || role === 'employee') ? role : 'employee';
|
||||||
@ -283,6 +280,7 @@ app.post('/api/admin/update-time-off-status', authenticateToken, requireRole('ad
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- NEW: Route to delete a time-off request ---
|
||||||
app.delete('/api/admin/time-off-requests/:id', authenticateToken, requireRole('admin'), (req, res) => {
|
app.delete('/api/admin/time-off-requests/:id', authenticateToken, requireRole('admin'), (req, res) => {
|
||||||
db.run('DELETE FROM time_off_requests WHERE id = ?', [req.params.id], function(err) {
|
db.run('DELETE FROM time_off_requests WHERE id = ?', [req.params.id], function(err) {
|
||||||
if (err) return res.status(500).json({ message: "Failed to delete request." });
|
if (err) return res.status(500).json({ message: "Failed to delete request." });
|
||||||
@ -291,12 +289,4 @@ app.delete('/api/admin/time-off-requests/:id', authenticateToken, requireRole('a
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// --- NEW: Add a catch-all route to serve the SPA ---
|
|
||||||
// This should come after all API routes
|
|
||||||
app.get('*', (req, res) => {
|
|
||||||
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
|
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
|
||||||
Loading…
x
Reference in New Issue
Block a user