veltrixcode commited on
Commit
cccf531
·
verified ·
1 Parent(s): 35e2d3b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +136 -23
server.js CHANGED
@@ -10,6 +10,20 @@ const app = express();
10
  const port = process.env.PORT || 7860;
11
  const host = '0.0.0.0'; // Listen on all network interfaces
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  // Middleware
14
  app.use(cors());
15
  app.use(express.json());
@@ -29,6 +43,40 @@ app.use((req, res, next) => {
29
  next();
30
  });
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  // Database Connection Pool
33
  const db = mysql.createPool({
34
  host: 'gateway01.eu-central-1.prod.aws.tidbcloud.com',
@@ -53,7 +101,11 @@ function initDb() {
53
  name VARCHAR(255) NOT NULL,
54
  phone VARCHAR(20),
55
  aadhar_card VARCHAR(20),
 
 
56
  nominee_card VARCHAR(20),
 
 
57
  samriddhi_card VARCHAR(20),
58
  account_no VARCHAR(50),
59
  installments_paid INT DEFAULT 0,
@@ -110,10 +162,43 @@ db.getConnection((err, connection) => {
110
 
111
  // API Endpoints
112
 
113
- // Create Cardholder
114
- app.post('/api/cardholders', (req, res) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  console.log('\n[POST /api/cardholders] Processing request...');
116
- const { name, phone, aadhar, nominee, samriddhi, account, installments, area, profile_image_url } = req.body;
 
 
 
 
 
 
 
117
 
118
  // Validate required fields
119
  if (!name) {
@@ -121,8 +206,17 @@ app.post('/api/cardholders', (req, res) => {
121
  return res.status(400).json({ error: 'Name is required' });
122
  }
123
 
124
- const sql = `INSERT INTO cardholders (name, phone, aadhar_card, nominee_card, samriddhi_card, account_no, installments_paid, area, profile_image_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
125
- const values = [name, phone, aadhar, nominee, samriddhi, account, installments || 0, area, profile_image_url];
 
 
 
 
 
 
 
 
 
126
 
127
  console.log('[POST /api/cardholders] Executing SQL:', sql);
128
  console.log('[POST /api/cardholders] Values:', values);
@@ -138,8 +232,8 @@ app.post('/api/cardholders', (req, res) => {
138
  });
139
  });
140
 
141
- // Get All Cardholders
142
- app.get('/api/cardholders', (req, res) => {
143
  console.log('\n[GET /api/cardholders] Processing request...');
144
  const { area } = req.query;
145
  let sql = 'SELECT * FROM cardholders';
@@ -165,8 +259,8 @@ app.get('/api/cardholders', (req, res) => {
165
  });
166
  });
167
 
168
- // Update Cardholder
169
- app.put('/api/cardholders/:id', (req, res) => {
170
  console.log('\n[PUT /api/cardholders/:id] Processing request...');
171
  const { id } = req.params;
172
  console.log('[PUT /api/cardholders/:id] Cardholder ID:', id);
@@ -175,6 +269,10 @@ app.put('/api/cardholders/:id', (req, res) => {
175
  const phone = req.body.phone;
176
  const area = req.body.area;
177
  const profile_image_url = req.body.profile_image_url;
 
 
 
 
178
 
179
  const aadhar = req.body.aadhar || req.body.aadhar_card;
180
  const nominee = req.body.nominee || req.body.nominee_card;
@@ -182,8 +280,17 @@ app.put('/api/cardholders/:id', (req, res) => {
182
  const account = req.body.account || req.body.account_no;
183
  const installments = req.body.installments !== undefined ? req.body.installments : req.body.installments_paid;
184
 
185
- const sql = `UPDATE cardholders SET name=?, phone=?, aadhar_card=?, nominee_card=?, samriddhi_card=?, account_no=?, installments_paid=?, area=?, profile_image_url=? WHERE id=?`;
186
- const values = [name, phone, aadhar, nominee, samriddhi, account, installments, area, profile_image_url, id];
 
 
 
 
 
 
 
 
 
187
 
188
  console.log('[PUT /api/cardholders/:id] Executing SQL:', sql);
189
  console.log('[PUT /api/cardholders/:id] Values:', values);
@@ -199,8 +306,8 @@ app.put('/api/cardholders/:id', (req, res) => {
199
  });
200
  });
201
 
202
- // Delete Cardholder
203
- app.delete('/api/cardholders/:id', (req, res) => {
204
  console.log('\n[DELETE /api/cardholders/:id] Processing request...');
205
  const { id } = req.params;
206
  console.log('[DELETE /api/cardholders/:id] Cardholder ID:', id);
@@ -221,8 +328,8 @@ app.delete('/api/cardholders/:id', (req, res) => {
221
 
222
  // --- Areas Management ---
223
 
224
- // Get Areas
225
- app.get('/api/areas', (req, res) => {
226
  console.log('\n[GET /api/areas] Processing request...');
227
  const sql = 'SELECT * FROM areas ORDER BY name ASC';
228
  console.log('[GET /api/areas] Executing SQL:', sql);
@@ -238,9 +345,11 @@ app.get('/api/areas', (req, res) => {
238
  });
239
  });
240
 
241
- // Add Area
242
- app.post('/api/areas', (req, res) => {
243
  console.log('\n[POST /api/areas] Processing request...');
 
 
244
  const { name } = req.body;
245
 
246
  if (!name) {
@@ -267,9 +376,9 @@ app.post('/api/areas', (req, res) => {
267
  });
268
  });
269
 
270
- // --- Payments ---
271
 
272
- app.post('/api/payments', (req, res) => {
273
  console.log('\n[POST /api/payments] Processing request...');
274
  console.log('[POST /api/payments] Request body:', JSON.stringify(req.body, null, 2));
275
 
@@ -364,8 +473,8 @@ app.post('/api/payments', (req, res) => {
364
  });
365
  });
366
 
367
- // Get Payments for Cardholder
368
- app.get('/api/payments/:cardholderId', (req, res) => {
369
  console.log('\n[GET /api/payments/:cardholderId] Processing request...');
370
  const { cardholderId } = req.params;
371
  console.log('[GET /api/payments/:cardholderId] Cardholder ID:', cardholderId);
@@ -384,9 +493,9 @@ app.get('/api/payments/:cardholderId', (req, res) => {
384
  });
385
  });
386
 
387
- // --- System Reset ---
388
 
389
- app.post('/api/admin/flush', (req, res) => {
390
  console.log('\n[POST /api/admin/flush] ⚠️ WARNING: System flush requested!');
391
 
392
  const dropPayments = 'DROP TABLE IF EXISTS payments';
@@ -443,5 +552,9 @@ app.listen(port, host, () => {
443
  console.log(`Server running at http://${host}:${port}`);
444
  console.log(`Accessible locally at http://localhost:${port}`);
445
  console.log(`Accessible on network at http://<your-ip>:${port}`);
 
 
 
 
446
  console.log('========================================\n');
447
  });
 
10
  const port = process.env.PORT || 7860;
11
  const host = '0.0.0.0'; // Listen on all network interfaces
12
 
13
+ // Hardcoded accounts
14
+ const ACCOUNTS = {
15
+ master: {
16
+ username: 'master',
17
+ password: '9935156127',
18
+ role: 'master'
19
+ },
20
+ slave: {
21
+ username: 'sawan',
22
+ password: '8381934081',
23
+ role: 'slave'
24
+ }
25
+ };
26
+
27
  // Middleware
28
  app.use(cors());
29
  app.use(express.json());
 
43
  next();
44
  });
45
 
46
+ // Authentication middleware
47
+ function authenticate(req, res, next) {
48
+ const { username, password } = req.headers;
49
+
50
+ console.log(`[Auth] Checking credentials for: ${username}`);
51
+
52
+ if (!username || !password) {
53
+ console.log('[Auth] Missing credentials');
54
+ return res.status(401).json({ error: 'Missing authentication credentials' });
55
+ }
56
+
57
+ const account = Object.values(ACCOUNTS).find(
58
+ acc => acc.username === username && acc.password === password
59
+ );
60
+
61
+ if (!account) {
62
+ console.log('[Auth] Invalid credentials');
63
+ return res.status(401).json({ error: 'Invalid credentials' });
64
+ }
65
+
66
+ console.log(`[Auth] Authenticated as: ${account.role}`);
67
+ req.user = account;
68
+ next();
69
+ }
70
+
71
+ // Authorization middleware for master-only routes
72
+ function requireMaster(req, res, next) {
73
+ if (req.user.role !== 'master') {
74
+ console.log(`[Auth] Access denied. User role: ${req.user.role}`);
75
+ return res.status(403).json({ error: 'Access denied. Master account required.' });
76
+ }
77
+ next();
78
+ }
79
+
80
  // Database Connection Pool
81
  const db = mysql.createPool({
82
  host: 'gateway01.eu-central-1.prod.aws.tidbcloud.com',
 
101
  name VARCHAR(255) NOT NULL,
102
  phone VARCHAR(20),
103
  aadhar_card VARCHAR(20),
104
+ aadhar_front_url TEXT,
105
+ aadhar_back_url TEXT,
106
  nominee_card VARCHAR(20),
107
+ nominee_front_url TEXT,
108
+ nominee_back_url TEXT,
109
  samriddhi_card VARCHAR(20),
110
  account_no VARCHAR(50),
111
  installments_paid INT DEFAULT 0,
 
162
 
163
  // API Endpoints
164
 
165
+ // Login endpoint (no authentication required)
166
+ app.post('/api/login', (req, res) => {
167
+ console.log('\n[POST /api/login] Processing login request...');
168
+ const { username, password } = req.body;
169
+
170
+ if (!username || !password) {
171
+ return res.status(400).json({ error: 'Username and password required' });
172
+ }
173
+
174
+ const account = Object.values(ACCOUNTS).find(
175
+ acc => acc.username === username && acc.password === password
176
+ );
177
+
178
+ if (!account) {
179
+ console.log('[POST /api/login] Invalid credentials');
180
+ return res.status(401).json({ error: 'Invalid credentials' });
181
+ }
182
+
183
+ console.log(`[POST /api/login] Login successful for: ${account.role}`);
184
+ res.json({
185
+ message: 'Login successful',
186
+ role: account.role,
187
+ username: account.username
188
+ });
189
+ });
190
+
191
+ // Create Cardholder (Both master and slave can access)
192
+ app.post('/api/cardholders', authenticate, (req, res) => {
193
  console.log('\n[POST /api/cardholders] Processing request...');
194
+ console.log(`[POST /api/cardholders] User role: ${req.user.role}`);
195
+
196
+ const {
197
+ name, phone, aadhar, nominee, samriddhi, account,
198
+ installments, area, profile_image_url,
199
+ aadhar_front_url, aadhar_back_url,
200
+ nominee_front_url, nominee_back_url
201
+ } = req.body;
202
 
203
  // Validate required fields
204
  if (!name) {
 
206
  return res.status(400).json({ error: 'Name is required' });
207
  }
208
 
209
+ const sql = `INSERT INTO cardholders
210
+ (name, phone, aadhar_card, aadhar_front_url, aadhar_back_url,
211
+ nominee_card, nominee_front_url, nominee_back_url,
212
+ samriddhi_card, account_no, installments_paid, area, profile_image_url)
213
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
214
+
215
+ const values = [
216
+ name, phone, aadhar, aadhar_front_url, aadhar_back_url,
217
+ nominee, nominee_front_url, nominee_back_url,
218
+ samriddhi, account, installments || 0, area, profile_image_url
219
+ ];
220
 
221
  console.log('[POST /api/cardholders] Executing SQL:', sql);
222
  console.log('[POST /api/cardholders] Values:', values);
 
232
  });
233
  });
234
 
235
+ // Get All Cardholders (Master only)
236
+ app.get('/api/cardholders', authenticate, requireMaster, (req, res) => {
237
  console.log('\n[GET /api/cardholders] Processing request...');
238
  const { area } = req.query;
239
  let sql = 'SELECT * FROM cardholders';
 
259
  });
260
  });
261
 
262
+ // Update Cardholder (Master only)
263
+ app.put('/api/cardholders/:id', authenticate, requireMaster, (req, res) => {
264
  console.log('\n[PUT /api/cardholders/:id] Processing request...');
265
  const { id } = req.params;
266
  console.log('[PUT /api/cardholders/:id] Cardholder ID:', id);
 
269
  const phone = req.body.phone;
270
  const area = req.body.area;
271
  const profile_image_url = req.body.profile_image_url;
272
+ const aadhar_front_url = req.body.aadhar_front_url;
273
+ const aadhar_back_url = req.body.aadhar_back_url;
274
+ const nominee_front_url = req.body.nominee_front_url;
275
+ const nominee_back_url = req.body.nominee_back_url;
276
 
277
  const aadhar = req.body.aadhar || req.body.aadhar_card;
278
  const nominee = req.body.nominee || req.body.nominee_card;
 
280
  const account = req.body.account || req.body.account_no;
281
  const installments = req.body.installments !== undefined ? req.body.installments : req.body.installments_paid;
282
 
283
+ const sql = `UPDATE cardholders SET
284
+ name=?, phone=?, aadhar_card=?, aadhar_front_url=?, aadhar_back_url=?,
285
+ nominee_card=?, nominee_front_url=?, nominee_back_url=?,
286
+ samriddhi_card=?, account_no=?, installments_paid=?, area=?, profile_image_url=?
287
+ WHERE id=?`;
288
+
289
+ const values = [
290
+ name, phone, aadhar, aadhar_front_url, aadhar_back_url,
291
+ nominee, nominee_front_url, nominee_back_url,
292
+ samriddhi, account, installments, area, profile_image_url, id
293
+ ];
294
 
295
  console.log('[PUT /api/cardholders/:id] Executing SQL:', sql);
296
  console.log('[PUT /api/cardholders/:id] Values:', values);
 
306
  });
307
  });
308
 
309
+ // Delete Cardholder (Master only)
310
+ app.delete('/api/cardholders/:id', authenticate, requireMaster, (req, res) => {
311
  console.log('\n[DELETE /api/cardholders/:id] Processing request...');
312
  const { id } = req.params;
313
  console.log('[DELETE /api/cardholders/:id] Cardholder ID:', id);
 
328
 
329
  // --- Areas Management ---
330
 
331
+ // Get Areas (Both master and slave can access)
332
+ app.get('/api/areas', authenticate, (req, res) => {
333
  console.log('\n[GET /api/areas] Processing request...');
334
  const sql = 'SELECT * FROM areas ORDER BY name ASC';
335
  console.log('[GET /api/areas] Executing SQL:', sql);
 
345
  });
346
  });
347
 
348
+ // Add Area (Both master and slave can access)
349
+ app.post('/api/areas', authenticate, (req, res) => {
350
  console.log('\n[POST /api/areas] Processing request...');
351
+ console.log(`[POST /api/areas] User role: ${req.user.role}`);
352
+
353
  const { name } = req.body;
354
 
355
  if (!name) {
 
376
  });
377
  });
378
 
379
+ // --- Payments (Master only) ---
380
 
381
+ app.post('/api/payments', authenticate, requireMaster, (req, res) => {
382
  console.log('\n[POST /api/payments] Processing request...');
383
  console.log('[POST /api/payments] Request body:', JSON.stringify(req.body, null, 2));
384
 
 
473
  });
474
  });
475
 
476
+ // Get Payments for Cardholder (Master only)
477
+ app.get('/api/payments/:cardholderId', authenticate, requireMaster, (req, res) => {
478
  console.log('\n[GET /api/payments/:cardholderId] Processing request...');
479
  const { cardholderId } = req.params;
480
  console.log('[GET /api/payments/:cardholderId] Cardholder ID:', cardholderId);
 
493
  });
494
  });
495
 
496
+ // --- System Reset (Master only) ---
497
 
498
+ app.post('/api/admin/flush', authenticate, requireMaster, (req, res) => {
499
  console.log('\n[POST /api/admin/flush] ⚠️ WARNING: System flush requested!');
500
 
501
  const dropPayments = 'DROP TABLE IF EXISTS payments';
 
552
  console.log(`Server running at http://${host}:${port}`);
553
  console.log(`Accessible locally at http://localhost:${port}`);
554
  console.log(`Accessible on network at http://<your-ip>:${port}`);
555
+ console.log('========================================');
556
+ console.log('\n📋 Account Information:');
557
+ console.log('Master Account - Username: master, Password: 9935156127');
558
+ console.log('Slave Account - Username: sawan, Password: 8381934081');
559
  console.log('========================================\n');
560
  });