Spaces:
Paused
Paused
File size: 22,081 Bytes
49ee4c2 4ae2d3b f4e0b33 49ee4c2 cccf531 49ee4c2 c5fa0f1 cccf531 49ee4c2 c5fa0f1 49ee4c2 cccf531 49ee4c2 cccf531 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 c5fa0f1 cccf531 49ee4c2 c5fa0f1 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 c5fa0f1 cccf531 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 35e2d3b c5fa0f1 35e2d3b c5fa0f1 35e2d3b c5fa0f1 35e2d3b 49ee4c2 35e2d3b 49ee4c2 35e2d3b c5fa0f1 35e2d3b 49ee4c2 35e2d3b c5fa0f1 35e2d3b 49ee4c2 35e2d3b c5fa0f1 35e2d3b 49ee4c2 35e2d3b c5fa0f1 35e2d3b 49ee4c2 35e2d3b 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 cccf531 49ee4c2 cccf531 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 49ee4c2 c5fa0f1 f4e0b33 c5fa0f1 f4e0b33 cccf531 c5fa0f1 f4e0b33 | 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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | require('dotenv').config();
const express = require('express');
const mysql = require('mysql2');
const cors = require('cors');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const app = express();
const port = process.env.PORT || 7860;
const host = '0.0.0.0'; // Listen on all network interfaces
// Hardcoded accounts
const ACCOUNTS = {
master: {
username: 'master',
password: '9935156127',
role: 'master'
},
slave: {
username: 'sawan',
password: '8381934081',
role: 'slave'
}
};
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
app.use('/uploads', express.static('uploads'));
// Request logging middleware
app.use((req, res, next) => {
console.log(`\n[${new Date().toISOString()}] ${req.method} ${req.url}`);
console.log('Headers:', JSON.stringify(req.headers, null, 2));
if (req.body && Object.keys(req.body).length > 0) {
console.log('Body:', JSON.stringify(req.body, null, 2));
}
if (req.query && Object.keys(req.query).length > 0) {
console.log('Query:', JSON.stringify(req.query, null, 2));
}
next();
});
// Authentication middleware
function authenticate(req, res, next) {
const { username, password } = req.headers;
console.log(`[Auth] Checking credentials for: ${username}`);
if (!username || !password) {
console.log('[Auth] Missing credentials');
return res.status(401).json({ error: 'Missing authentication credentials' });
}
const account = Object.values(ACCOUNTS).find(
acc => acc.username === username && acc.password === password
);
if (!account) {
console.log('[Auth] Invalid credentials');
return res.status(401).json({ error: 'Invalid credentials' });
}
console.log(`[Auth] Authenticated as: ${account.role}`);
req.user = account;
next();
}
// Authorization middleware for master-only routes
function requireMaster(req, res, next) {
if (req.user.role !== 'master') {
console.log(`[Auth] Access denied. User role: ${req.user.role}`);
return res.status(403).json({ error: 'Access denied. Master account required.' });
}
next();
}
// Database Connection Pool
const db = mysql.createPool({
host: 'gateway01.eu-central-1.prod.aws.tidbcloud.com',
user: '2MchL9UQd5gPZWz.root',
password: '3N5atD6gfsRDZl2t',
database: 'test',
port: 4000,
ssl: {
rejectUnauthorized: true
},
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
function initDb() {
console.log('\n[DB Init] Starting database initialization...');
const createCardholdersTable = `
CREATE TABLE IF NOT EXISTS cardholders (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
phone VARCHAR(20),
aadhar_card VARCHAR(20),
aadhar_front_url TEXT,
aadhar_back_url TEXT,
nominee_card VARCHAR(20),
nominee_front_url TEXT,
nominee_back_url TEXT,
samriddhi_card VARCHAR(20),
account_no VARCHAR(50),
installments_paid INT DEFAULT 0,
area VARCHAR(100),
profile_image_url TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
const createAreasTable = `
CREATE TABLE IF NOT EXISTS areas (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
)
`;
const createPaymentsTable = `
CREATE TABLE IF NOT EXISTS payments (
id INT AUTO_INCREMENT PRIMARY KEY,
cardholder_id INT NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
payment_date DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (cardholder_id) REFERENCES cardholders(id) ON DELETE CASCADE
)
`;
db.query(createCardholdersTable, (err) => {
if (err) console.error('[DB Init] Error creating cardholders table:', err);
else console.log('[DB Init] Cardholders table ready');
});
db.query(createAreasTable, (err) => {
if (err) console.error('[DB Init] Error creating areas table:', err);
else console.log('[DB Init] Areas table ready');
});
db.query(createPaymentsTable, (err) => {
if (err) console.error('[DB Init] Error creating payments table:', err);
else console.log('[DB Init] Payments table ready');
});
}
// Check connection
db.getConnection((err, connection) => {
if (err) {
console.error('[DB Connection] Error connecting to database:', err);
return;
}
console.log('[DB Connection] Connected to TiDB MySQL database via Pool');
connection.release();
initDb();
});
// API Endpoints
// Login endpoint (no authentication required)
app.post('/api/login', (req, res) => {
console.log('\n[POST /api/login] Processing login request...');
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password required' });
}
const account = Object.values(ACCOUNTS).find(
acc => acc.username === username && acc.password === password
);
if (!account) {
console.log('[POST /api/login] Invalid credentials');
return res.status(401).json({ error: 'Invalid credentials' });
}
console.log(`[POST /api/login] Login successful for: ${account.role}`);
res.json({
message: 'Login successful',
role: account.role,
username: account.username
});
});
// Create Cardholder (Both master and slave can access)
app.post('/api/cardholders', authenticate, (req, res) => {
console.log('\n[POST /api/cardholders] Processing request...');
console.log(`[POST /api/cardholders] User role: ${req.user.role}`);
const {
name, phone, aadhar, nominee, samriddhi, account,
installments, area, profile_image_url,
aadhar_front_url, aadhar_back_url,
nominee_front_url, nominee_back_url
} = req.body;
// Validate required fields
if (!name) {
console.log('[POST /api/cardholders] Validation failed: Name is required');
return res.status(400).json({ error: 'Name is required' });
}
const sql = `INSERT INTO cardholders
(name, phone, aadhar_card, aadhar_front_url, aadhar_back_url,
nominee_card, nominee_front_url, nominee_back_url,
samriddhi_card, account_no, installments_paid, area, profile_image_url)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const values = [
name, phone, aadhar, aadhar_front_url, aadhar_back_url,
nominee, nominee_front_url, nominee_back_url,
samriddhi, account, installments || 0, area, profile_image_url
];
console.log('[POST /api/cardholders] Executing SQL:', sql);
console.log('[POST /api/cardholders] Values:', values);
db.query(sql, values, (err, result) => {
if (err) {
console.error('[POST /api/cardholders] Database error:', err);
res.status(500).json({ error: 'Database error: ' + err.message });
} else {
console.log('[POST /api/cardholders] Success! Inserted ID:', result.insertId);
res.status(201).json({ message: 'Cardholder added successfully', id: result.insertId });
}
});
});
// Get All Cardholders (Master only)
app.get('/api/cardholders', authenticate, requireMaster, (req, res) => {
console.log('\n[GET /api/cardholders] Processing request...');
const { area } = req.query;
let sql = 'SELECT * FROM cardholders';
let params = [];
if (area) {
sql += ' WHERE area = ?';
params.push(area);
console.log('[GET /api/cardholders] Filtering by area:', area);
}
sql += ' ORDER BY created_at DESC';
console.log('[GET /api/cardholders] Executing SQL:', sql);
db.query(sql, params, (err, results) => {
if (err) {
console.error('[GET /api/cardholders] Database error:', err);
res.status(500).json({ error: 'Database error' });
} else {
console.log('[GET /api/cardholders] Success! Returned', results.length, 'records');
res.json(results);
}
});
});
// Update Cardholder (Master only)
app.put('/api/cardholders/:id', authenticate, requireMaster, (req, res) => {
console.log('\n[PUT /api/cardholders/:id] Processing request...');
const { id } = req.params;
console.log('[PUT /api/cardholders/:id] Cardholder ID:', id);
const name = req.body.name;
const phone = req.body.phone;
const area = req.body.area;
const profile_image_url = req.body.profile_image_url;
const aadhar_front_url = req.body.aadhar_front_url;
const aadhar_back_url = req.body.aadhar_back_url;
const nominee_front_url = req.body.nominee_front_url;
const nominee_back_url = req.body.nominee_back_url;
const aadhar = req.body.aadhar || req.body.aadhar_card;
const nominee = req.body.nominee || req.body.nominee_card;
const samriddhi = req.body.samriddhi || req.body.samriddhi_card;
const account = req.body.account || req.body.account_no;
const installments = req.body.installments !== undefined ? req.body.installments : req.body.installments_paid;
const sql = `UPDATE cardholders SET
name=?, phone=?, aadhar_card=?, aadhar_front_url=?, aadhar_back_url=?,
nominee_card=?, nominee_front_url=?, nominee_back_url=?,
samriddhi_card=?, account_no=?, installments_paid=?, area=?, profile_image_url=?
WHERE id=?`;
const values = [
name, phone, aadhar, aadhar_front_url, aadhar_back_url,
nominee, nominee_front_url, nominee_back_url,
samriddhi, account, installments, area, profile_image_url, id
];
console.log('[PUT /api/cardholders/:id] Executing SQL:', sql);
console.log('[PUT /api/cardholders/:id] Values:', values);
db.query(sql, values, (err, result) => {
if (err) {
console.error('[PUT /api/cardholders/:id] Database error:', err);
res.status(500).json({ error: 'Database error: ' + err.message });
} else {
console.log('[PUT /api/cardholders/:id] Success! Affected rows:', result.affectedRows);
res.json({ message: 'User updated successfully' });
}
});
});
// Delete Cardholder (Master only)
app.delete('/api/cardholders/:id', authenticate, requireMaster, (req, res) => {
console.log('\n[DELETE /api/cardholders/:id] Processing request...');
const { id } = req.params;
console.log('[DELETE /api/cardholders/:id] Cardholder ID:', id);
const sql = 'DELETE FROM cardholders WHERE id = ?';
console.log('[DELETE /api/cardholders/:id] Executing SQL:', sql);
db.query(sql, [id], (err, result) => {
if (err) {
console.error('[DELETE /api/cardholders/:id] Database error:', err);
res.status(500).json({ error: 'Database error' });
} else {
console.log('[DELETE /api/cardholders/:id] Success! Affected rows:', result.affectedRows);
res.json({ message: 'User deleted successfully' });
}
});
});
// --- Areas Management ---
// Get Areas (Both master and slave can access)
app.get('/api/areas', authenticate, (req, res) => {
console.log('\n[GET /api/areas] Processing request...');
const sql = 'SELECT * FROM areas ORDER BY name ASC';
console.log('[GET /api/areas] Executing SQL:', sql);
db.query(sql, (err, results) => {
if (err) {
console.error('[GET /api/areas] Database error:', err);
res.status(500).json({ error: 'Database error' });
} else {
console.log('[GET /api/areas] Success! Returned', results.length, 'areas');
res.json(results.map(row => row.name));
}
});
});
// Add Area (Both master and slave can access)
app.post('/api/areas', authenticate, (req, res) => {
console.log('\n[POST /api/areas] Processing request...');
console.log(`[POST /api/areas] User role: ${req.user.role}`);
const { name } = req.body;
if (!name) {
console.log('[POST /api/areas] Validation failed: Area name is required');
return res.status(400).json({ error: 'Area name is required' });
}
const sql = 'INSERT INTO areas (name) VALUES (?)';
console.log('[POST /api/areas] Executing SQL:', sql);
console.log('[POST /api/areas] Area name:', name);
db.query(sql, [name], (err, result) => {
if (err) {
if (err.code === 'ER_DUP_ENTRY') {
console.log('[POST /api/areas] Duplicate entry:', name);
return res.status(409).json({ error: 'Area already exists' });
}
console.error('[POST /api/areas] Database error:', err);
res.status(500).json({ error: 'Database error' });
} else {
console.log('[POST /api/areas] Success! Inserted ID:', result.insertId);
res.status(201).json({ message: 'Area added', id: result.insertId });
}
});
});
// --- Payments (Master only) ---
app.post('/api/payments', authenticate, requireMaster, (req, res) => {
console.log('\n[POST /api/payments] Processing request...');
console.log('[POST /api/payments] Request body:', JSON.stringify(req.body, null, 2));
const { cardholderId, amount, date } = req.body;
// Detailed validation logging
if (!cardholderId) {
console.log('[POST /api/payments] Validation failed: cardholderId is missing');
return res.status(400).json({ error: 'Missing cardholderId field' });
}
if (!amount) {
console.log('[POST /api/payments] Validation failed: amount is missing');
return res.status(400).json({ error: 'Missing amount field' });
}
if (!date) {
console.log('[POST /api/payments] Validation failed: date is missing');
return res.status(400).json({ error: 'Missing date field' });
}
console.log('[POST /api/payments] Validated data:', { cardholderId, amount, date });
const insertPayment = 'INSERT INTO payments (cardholder_id, amount, payment_date) VALUES (?, ?, ?)';
const updateCardholder = 'UPDATE cardholders SET installments_paid = installments_paid + 1 WHERE id = ?';
console.log('[POST /api/payments] Getting connection from pool...');
// Get a connection from the pool to use transactions
db.getConnection((err, connection) => {
if (err) {
console.error('[POST /api/payments] Error getting connection:', err);
return res.status(500).json({ error: 'Connection error: ' + err.message });
}
console.log('[POST /api/payments] Connection acquired. Starting transaction...');
connection.beginTransaction(err => {
if (err) {
console.error('[POST /api/payments] Transaction begin error:', err);
connection.release();
return res.status(500).json({ error: 'Transaction error: ' + err.message });
}
console.log('[POST /api/payments] Inserting payment record...');
console.log('[POST /api/payments] SQL:', insertPayment);
console.log('[POST /api/payments] Values:', [cardholderId, amount, date]);
connection.query(insertPayment, [cardholderId, amount, date], (err, result) => {
if (err) {
console.error('[POST /api/payments] Error inserting payment:', err);
return connection.rollback(() => {
console.log('[POST /api/payments] Transaction rolled back');
connection.release();
res.status(500).json({ error: 'Database error inserting payment: ' + err.message });
});
}
console.log('[POST /api/payments] Payment inserted successfully. ID:', result.insertId);
console.log('[POST /api/payments] Updating cardholder installment count...');
console.log('[POST /api/payments] SQL:', updateCardholder);
console.log('[POST /api/payments] Cardholder ID:', cardholderId);
connection.query(updateCardholder, [cardholderId], (err, result) => {
if (err) {
console.error('[POST /api/payments] Error updating installment count:', err);
return connection.rollback(() => {
console.log('[POST /api/payments] Transaction rolled back');
connection.release();
res.status(500).json({ error: 'Database error updating installments: ' + err.message });
});
}
console.log('[POST /api/payments] Installment count updated. Affected rows:', result.affectedRows);
console.log('[POST /api/payments] Committing transaction...');
connection.commit(err => {
if (err) {
console.error('[POST /api/payments] Commit error:', err);
return connection.rollback(() => {
console.log('[POST /api/payments] Transaction rolled back');
connection.release();
res.status(500).json({ error: 'Commit error: ' + err.message });
});
}
console.log('[POST /api/payments] Transaction committed successfully!');
connection.release();
console.log('[POST /api/payments] Connection released back to pool');
res.status(201).json({ message: 'Payment recorded and installment added' });
});
});
});
});
});
});
// Get Payments for Cardholder (Master only)
app.get('/api/payments/:cardholderId', authenticate, requireMaster, (req, res) => {
console.log('\n[GET /api/payments/:cardholderId] Processing request...');
const { cardholderId } = req.params;
console.log('[GET /api/payments/:cardholderId] Cardholder ID:', cardholderId);
const sql = 'SELECT * FROM payments WHERE cardholder_id = ? ORDER BY payment_date DESC, created_at DESC';
console.log('[GET /api/payments/:cardholderId] Executing SQL:', sql);
db.query(sql, [cardholderId], (err, results) => {
if (err) {
console.error('[GET /api/payments/:cardholderId] Database error:', err);
res.status(500).json({ error: 'Database error' });
} else {
console.log('[GET /api/payments/:cardholderId] Success! Returned', results.length, 'payments');
res.json(results);
}
});
});
// --- System Reset (Master only) ---
app.post('/api/admin/flush', authenticate, requireMaster, (req, res) => {
console.log('\n[POST /api/admin/flush] โ ๏ธ WARNING: System flush requested!');
const dropPayments = 'DROP TABLE IF EXISTS payments';
const dropCardholders = 'DROP TABLE IF EXISTS cardholders';
const dropAreas = 'DROP TABLE IF EXISTS areas';
console.log('[POST /api/admin/flush] Dropping payments table...');
db.query(dropPayments, (err) => {
if (err) console.error('[POST /api/admin/flush] Error dropping payments:', err);
else console.log('[POST /api/admin/flush] Payments table dropped');
console.log('[POST /api/admin/flush] Dropping cardholders table...');
db.query(dropCardholders, (err) => {
if (err) {
console.error('[POST /api/admin/flush] Error dropping cardholders:', err);
return res.status(500).json({ error: 'Failed to drop cardholders table' });
}
console.log('[POST /api/admin/flush] Cardholders table dropped');
console.log('[POST /api/admin/flush] Dropping areas table...');
db.query(dropAreas, (err) => {
if (err) {
console.error('[POST /api/admin/flush] Error dropping areas:', err);
return res.status(500).json({ error: 'Failed to drop areas table' });
}
console.log('[POST /api/admin/flush] Areas table dropped');
console.log('[POST /api/admin/flush] Reinitializing database...');
initDb();
console.log('[POST /api/admin/flush] โ
System flush completed successfully');
res.json({ message: 'System flushed and reset successfully' });
});
});
});
});
// 404 handler
app.use((req, res) => {
console.log(`\n[404] Route not found: ${req.method} ${req.url}`);
res.status(404).json({ error: 'Route not found' });
});
// Error handler
app.use((err, req, res, next) => {
console.error('\n[Error Handler] Unhandled error:', err);
res.status(500).json({ error: 'Internal server error: ' + err.message });
});
// Start server on 0.0.0.0 to allow external connections
app.listen(port, host, () => {
console.log('\n========================================');
console.log('๐ Server Started Successfully!');
console.log('========================================');
console.log(`Server running at http://${host}:${port}`);
console.log(`Accessible locally at http://localhost:${port}`);
console.log(`Accessible on network at http://<your-ip>:${port}`);
console.log('========================================');
console.log('\n๐ Account Information:');
console.log('Master Account - Username: master, Password: 9935156127');
console.log('Slave Account - Username: sawan, Password: 8381934081');
console.log('========================================\n');
}); |