-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver2.js
More file actions
335 lines (289 loc) · 9.71 KB
/
server2.js
File metadata and controls
335 lines (289 loc) · 9.71 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const session = require('express-session');
const crypto = require('crypto');
const nodemailer = require('nodemailer');
const app = express();
const port = 3000;
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(session({
secret: 'your-secret-key',
resave: false,
saveUninitialized: true
}));
app.use(express.static(path.join(__dirname, 'public')));
// MongoDB connection
mongoose.connect('mongodb://localhost:27017/cable_operator', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Connected to MongoDB');
}).catch((err) => {
console.log('MongoDB connection error:', err);
});
// Schemas
const adminSchema = new mongoose.Schema({
username: String,
password: String
});
const Admin = mongoose.model('Admin', adminSchema);
const userSchema = new mongoose.Schema({
customerId: String,
name: String,
email: String,
phone: String,
password: String,
address: String,
city: String,
zip: String,
package: String,
connectionDate: Date,
status: { type: String, default: 'Active' },
createdAt: { type: Date, default: Date.now },
resetOTP: String,
otpExpiresAt: Date
});
const User = mongoose.model('User', userSchema);
const complaintSchema = new mongoose.Schema({
customerId: String,
customerName: String,
complaintText: String,
complaintCategory: String,
employeeName: String,
status: { type: String, default: 'Pending' },
createdAt: { type: Date, default: Date.now }
});
const Complaint = mongoose.model('Complaint', complaintSchema);
// Auto-create admin
async function checkAdminExists() {
const exists = await Admin.findOne();
if (!exists) {
const hash = await bcrypt.hash('adminPassword', 10);
await new Admin({ username: 'admin', password: hash }).save();
console.log('Default admin created: admin/adminPassword');
}
}
checkAdminExists();
// Helper: Customer ID generator
async function generateCustomerId() {
const lastUser = await User.findOne().sort({ _id: -1 });
let lastId = 1000;
if (lastUser && lastUser.customerId) {
const match = lastUser.customerId.match(/CUST(\d+)/);
if (match) lastId = parseInt(match[1], 10);
}
return `CUST${lastId + 1}`;
}
// Nodemailer setup
const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'your.email@gmail.com',
pass: 'your-email-password' // Use App Password if 2FA is on
}
});
// Routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Admin login
app.post('/adminlogin', async (req, res) => {
const { username, password } = req.body;
const admin = await Admin.findOne({ username });
if (!admin || !(await bcrypt.compare(password, admin.password))) {
return res.status(400).json({ message: 'Invalid credentials' });
}
req.session.admin = { username };
res.redirect('/admindashboard');
});
app.get('/admindashboard', (req, res) => {
if (!req.session.admin) return res.redirect('/');
res.sendFile(path.join(__dirname, 'public', 'admindashboard.html'));
});
// User registration
app.post('/register', async (req, res) => {
const { name, email, phone, password, address, city, zip, package: pkg, connectionDate } = req.body;
if (!name || !email || !phone || !password || !address || !city || !zip || !pkg || !connectionDate) {
return res.status(400).json({ message: 'All fields required' });
}
const exists = await User.findOne({ email });
if (exists) return res.status(400).json({ message: 'Email already registered' });
const customerId = await generateCustomerId();
const hash = await bcrypt.hash(password, 10);
await new User({
customerId,
name,
email,
phone,
password: hash,
address,
city,
zip,
package: pkg,
connectionDate,
status: 'Active'
}).save();
res.redirect('/userlogin.html');
});
// User login
app.post('/userlogin', async (req, res) => {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user || !(await bcrypt.compare(password, user.password))) {
return res.status(400).json({ message: 'Invalid credentials' });
}
req.session.user = { id: user._id.toString(), name: user.name, email: user.email };
res.redirect('/userdashboard');
});
// Middleware to protect user routes
function isAuthenticated(req, res, next) {
if (req.session.user) return next();
res.redirect('/userlogin.html');
}
// User dashboard
app.get('/userdashboard', isAuthenticated, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'userdashboard.html'));
});
// Complaint submission
const employeeMapping = {
"No Signal / Channel Not Working": "Amit Sharma",
"Poor Picture or Sound Quality": "Priya Gupta",
"Set-Top Box Issues": "Ravi Kumar",
"Billing or Payment Issues": "Neha Yadav",
"Package Subscription Issues": "Arvind Patel",
"Late or Missed Service": "Suresh Reddy",
"Remote Control Not Working": "Deepika Joshi",
"Customer Service Complaints": "Vikram Singh",
"Internet Connectivity": "Sita Mehta",
"Request for Disconnection or Suspension": "Rajesh Nair",
"Others": "Kavita Desai"
};
app.post('/submit-complaint', async (req, res) => {
const { customerId, customerName, complaintText, complaintCategory } = req.body;
const trimmedCategory = complaintCategory?.trim();
const employeeName = employeeMapping[trimmedCategory] || "Amit Sharma";
try {
await new Complaint({
customerId,
customerName,
complaintText,
complaintCategory: trimmedCategory,
employeeName
}).save();
res.status(200).json({ message: 'Complaint submitted successfully' });
} catch (err) {
res.status(500).json({ message: 'Error saving complaint', error: err.message });
}
});
// Admin: Get all complaints
app.get('/get-complaints', async (req, res) => {
try {
const complaints = await Complaint.find().sort({ createdAt: -1 });
res.json(complaints);
} catch (err) {
res.status(500).json({ message: 'Error fetching complaints' });
}
});
// Admin: Update complaint
app.put('/update-complaint/:id', async (req, res) => {
const { status, employeeName } = req.body;
try {
await Complaint.findByIdAndUpdate(req.params.id, { status, employeeName });
res.status(200).json({ message: 'Updated successfully' });
} catch (err) {
res.status(500).json({ message: 'Update failed' });
}
});
// Admin: Get all users
app.get('/get-users', async (req, res) => {
try {
const users = await User.find({}, '-password').sort({ createdAt: -1 });
res.json(users);
} catch (err) {
res.status(500).json({ message: 'Error fetching users' });
}
});
// User: Get details
app.get('/get-user-details', async (req, res) => {
if (!req.session.user) return res.status(401).json({ message: 'Not logged in' });
try {
const user = await User.findById(req.session.user.id).select('-password');
if (!user) return res.status(404).json({ message: 'User not found' });
res.json(user);
} catch (err) {
res.status(500).json({ message: 'Error fetching user data' });
}
});
// Update user profile
app.put('/update-user', async (req, res) => {
if (!req.session.user) return res.status(401).json({ message: 'Not logged in' });
const { name, email, phone, address, city, zip, package, connectionDate } = req.body;
try {
const user = await User.findById(req.session.user.id);
if (!user) return res.status(404).json({ message: 'User not found' });
user.name = name || user.name;
user.email = email || user.email;
user.phone = phone || user.phone;
user.address = address || user.address;
user.city = city || user.city;
user.zip = zip || user.zip;
user.package = package || user.package;
user.connectionDate = connectionDate || user.connectionDate;
await user.save();
res.json(user);
} catch (err) {
console.error('Error updating user:', err);
res.status(500).json({ message: 'Error updating user data' });
}
});
// 🔐 Forgot Password Flow
app.post('/user/send-otp', async (req, res) => {
const { email } = req.body;
const user = await User.findOne({ email });
if (!user) return res.status(400).json({ message: 'User not found' });
const otp = Math.floor(100000 + Math.random() * 900000).toString();
const expiry = new Date(Date.now() + 10 * 60000); // 10 minutes
user.resetOTP = otp;
user.otpExpiresAt = expiry;
await user.save();
await transporter.sendMail({
to: user.email,
subject: 'Password Reset OTP',
html: `<p>Your OTP is <strong>${otp}</strong>. It expires in 10 minutes.</p>`
});
res.json({ message: 'OTP sent to your email.' });
});
app.post('/user/verify-otp', async (req, res) => {
const { email, otp } = req.body;
const user = await User.findOne({ email, resetOTP: otp });
if (!user || new Date() > user.otpExpiresAt) {
return res.status(400).json({ message: 'Invalid or expired OTP.' });
}
res.json({ message: 'OTP verified. Proceed to reset password.' });
});
app.post('/user/reset-password', async (req, res) => {
const { email, otp, newPassword } = req.body;
const user = await User.findOne({ email, resetOTP: otp });
if (!user || new Date() > user.otpExpiresAt) {
return res.status(400).json({ message: 'Invalid or expired OTP.' });
}
user.password = await bcrypt.hash(newPassword, 10);
user.resetOTP = null;
user.otpExpiresAt = null;
await user.save();
res.json({ message: 'Password successfully reset. You can now log in.' });
});
// Logout
app.get('/logout', (req, res) => {
req.session.destroy();
res.redirect('/');
});
// Start server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});