This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
171 lines (145 loc) · 5.62 KB
/
Copy pathapp.js
File metadata and controls
171 lines (145 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const User = require('./models/user')
const Medicine = require('./models/medicine')
const doctorRoutes = require('./routes/doctor');
const Doctor = require('./models/doctor');
require('dotenv').config();
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
mongoose.connect(process.env.MONGODB_URI)
.then(async () => {
console.log('Connected to database.')
// const allUsers = await User.find({});
// const allDoctors = await Doctor.find({});
// console.log("Fetching doctors: ", allDoctors);
})
.catch((err) => console.error('MongoDB connection error:', err));
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/api/doctor', doctorRoutes);
app.post('/api/logout', (req, res) => {
req.session.destroy(err => {
if (err) {
return res.status(500).json({ error: 'Failed to log out' });
}
res.status(200).json({ message: 'Logged out successfully' });
});
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'login.html'));
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'login.html'));
});
app.get('/about', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'about.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'contact.html'));
})
app.post('/login', async (req, res) => {
const { username, password, role } = req.body;
try {
const user = await User.findOne({ username });
console.log('User found:', user ? 'Yes' : 'No');
if (!user) {
return res.status(401).json({ message: 'Invalid username or password' });
}
const isMatch = await bcrypt.compare(password, user.password);
console.log('Password match:', isMatch);
if (!isMatch) {
return res.status(401).json({ message: 'Invalid username or password' });
}
// Check if role matches
if (user.role !== role) {
return res.status(401).json({ message: 'Invalid role for this user' });
}
// Successful login - redirect based on role
switch (user.role) {
case 'admin':
res.redirect('/admin');
break;
case 'doctor':
res.redirect('/doctor');
break;
case 'pharmacist':
res.redirect('/pharmacist/inventory');
break;
default:
return res.status(400).json({ message: 'Invalid role' });
}
} catch (error) {
console.error('Error during login:', error);
res.status(500).json({ message: 'Internal server error' });
}
});
app.get('/admin', (req, res) => {
res.sendFile(path.join(__dirname, 'pages', 'admin.html'));
});
app.get('/doctor', (req, res) => {
res.sendFile(path.join(__dirname, 'pages', 'doctor.html'));
});
app.get('/api/doctor', async (req, res) => {
try {
const doctors = await Doctor.find(); // Fetch doctors from MongoDB
res.json(doctors); // Send the data as JSON
} catch (error) {
console.error(error);
res.status(500).send('Server Error');
}
});
// Route to get all inventory items
app.get('/pharmacist/inventory', async (req, res) => {
res.sendFile(path.join(__dirname, 'pages', 'inventory.html'));
});
// Route to add a new inventory item
app.post('/pharmacist/inventory', async (req, res) => {
const { name, dosage, description, quantity, expiryDate } = req.body;
const newItem = new Medicine({ name, dosage, description, quantity, expiryDate });
try {
const savedItem = await newItem.save();
console.log('Saved item:', savedItem); // Log the saved item
res.status(201).json(savedItem); // Send the saved item back
} catch (error) {
console.error('Error adding inventory:', error);
res.status(500).json({ message: 'Failed to add inventory' });
}
});
app.put('/pharmacist/inventory/:id', async (req, res) => {
try {
const inventoryItem = await Medicine.findByIdAndUpdate(req.params.id, req.body, { new: true });
if (!inventoryItem) return res.status(404).json({ message: 'Inventory item not found' });
res.json(inventoryItem);
} catch (error) {
res.status(400).json({ message: 'Error updating inventory item', error });
}
});
app.delete('/pharmacist/inventory/:id', async (req, res) => {
try {
const inventoryItem = await Medicine.findByIdAndDelete(req.params.id);
if (!inventoryItem) return res.status(404).json({ message: 'Inventory item not found' });
res.json({ message: 'Inventory item deleted successfully' });
} catch (error) {
res.status(400).json({ message: 'Error deleting inventory item', error });
}
});
// Route to get all inventory items
app.get('/pharmacist/inventory/items', async (req, res) => {
try {
const items = await Medicine.find(); // Fetch all items from the Medicine collection
res.json(items); // Send the items as JSON
} catch (error) {
console.error('Error fetching inventory items:', error);
res.status(500).json({ message: 'Failed to fetch inventory items' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});