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

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +46 -30
server.js CHANGED
@@ -294,54 +294,70 @@ app.post('/api/payments', (req, res) => {
294
  const insertPayment = 'INSERT INTO payments (cardholder_id, amount, payment_date) VALUES (?, ?, ?)';
295
  const updateCardholder = 'UPDATE cardholders SET installments_paid = installments_paid + 1 WHERE id = ?';
296
 
297
- console.log('[POST /api/payments] Starting transaction...');
298
 
299
- db.beginTransaction(err => {
 
300
  if (err) {
301
- console.error('[POST /api/payments] Transaction begin error:', err);
302
- return res.status(500).json({ error: 'Transaction error: ' + err.message });
303
  }
304
 
305
- console.log('[POST /api/payments] Inserting payment record...');
306
- console.log('[POST /api/payments] SQL:', insertPayment);
307
- console.log('[POST /api/payments] Values:', [cardholderId, amount, date]);
308
-
309
- db.query(insertPayment, [cardholderId, amount, date], (err, result) => {
310
  if (err) {
311
- console.error('[POST /api/payments] Error inserting payment:', err);
312
- return db.rollback(() => {
313
- console.log('[POST /api/payments] Transaction rolled back');
314
- res.status(500).json({ error: 'Database error inserting payment: ' + err.message });
315
- });
316
  }
317
 
318
- console.log('[POST /api/payments] Payment inserted successfully. ID:', result.insertId);
319
- console.log('[POST /api/payments] Updating cardholder installment count...');
320
- console.log('[POST /api/payments] SQL:', updateCardholder);
321
- console.log('[POST /api/payments] Cardholder ID:', cardholderId);
322
 
323
- db.query(updateCardholder, [cardholderId], (err, result) => {
324
  if (err) {
325
- console.error('[POST /api/payments] Error updating installment count:', err);
326
- return db.rollback(() => {
327
  console.log('[POST /api/payments] Transaction rolled back');
328
- res.status(500).json({ error: 'Database error updating installments: ' + err.message });
 
329
  });
330
  }
331
 
332
- console.log('[POST /api/payments] Installment count updated. Affected rows:', result.affectedRows);
333
- console.log('[POST /api/payments] Committing transaction...');
 
 
334
 
335
- db.commit(err => {
336
  if (err) {
337
- console.error('[POST /api/payments] Commit error:', err);
338
- return db.rollback(() => {
339
  console.log('[POST /api/payments] Transaction rolled back');
340
- res.status(500).json({ error: 'Commit error: ' + err.message });
 
341
  });
342
  }
343
- console.log('[POST /api/payments] Transaction committed successfully!');
344
- res.status(201).json({ message: 'Payment recorded and installment added' });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  });
346
  });
347
  });
 
294
  const insertPayment = 'INSERT INTO payments (cardholder_id, amount, payment_date) VALUES (?, ?, ?)';
295
  const updateCardholder = 'UPDATE cardholders SET installments_paid = installments_paid + 1 WHERE id = ?';
296
 
297
+ console.log('[POST /api/payments] Getting connection from pool...');
298
 
299
+ // Get a connection from the pool to use transactions
300
+ db.getConnection((err, connection) => {
301
  if (err) {
302
+ console.error('[POST /api/payments] Error getting connection:', err);
303
+ return res.status(500).json({ error: 'Connection error: ' + err.message });
304
  }
305
 
306
+ console.log('[POST /api/payments] Connection acquired. Starting transaction...');
307
+
308
+ connection.beginTransaction(err => {
 
 
309
  if (err) {
310
+ console.error('[POST /api/payments] Transaction begin error:', err);
311
+ connection.release();
312
+ return res.status(500).json({ error: 'Transaction error: ' + err.message });
 
 
313
  }
314
 
315
+ console.log('[POST /api/payments] Inserting payment record...');
316
+ console.log('[POST /api/payments] SQL:', insertPayment);
317
+ console.log('[POST /api/payments] Values:', [cardholderId, amount, date]);
 
318
 
319
+ connection.query(insertPayment, [cardholderId, amount, date], (err, result) => {
320
  if (err) {
321
+ console.error('[POST /api/payments] Error inserting payment:', err);
322
+ return connection.rollback(() => {
323
  console.log('[POST /api/payments] Transaction rolled back');
324
+ connection.release();
325
+ res.status(500).json({ error: 'Database error inserting payment: ' + err.message });
326
  });
327
  }
328
 
329
+ console.log('[POST /api/payments] Payment inserted successfully. ID:', result.insertId);
330
+ console.log('[POST /api/payments] Updating cardholder installment count...');
331
+ console.log('[POST /api/payments] SQL:', updateCardholder);
332
+ console.log('[POST /api/payments] Cardholder ID:', cardholderId);
333
 
334
+ connection.query(updateCardholder, [cardholderId], (err, result) => {
335
  if (err) {
336
+ console.error('[POST /api/payments] Error updating installment count:', err);
337
+ return connection.rollback(() => {
338
  console.log('[POST /api/payments] Transaction rolled back');
339
+ connection.release();
340
+ res.status(500).json({ error: 'Database error updating installments: ' + err.message });
341
  });
342
  }
343
+
344
+ console.log('[POST /api/payments] Installment count updated. Affected rows:', result.affectedRows);
345
+ console.log('[POST /api/payments] Committing transaction...');
346
+
347
+ connection.commit(err => {
348
+ if (err) {
349
+ console.error('[POST /api/payments] Commit error:', err);
350
+ return connection.rollback(() => {
351
+ console.log('[POST /api/payments] Transaction rolled back');
352
+ connection.release();
353
+ res.status(500).json({ error: 'Commit error: ' + err.message });
354
+ });
355
+ }
356
+ console.log('[POST /api/payments] Transaction committed successfully!');
357
+ connection.release();
358
+ console.log('[POST /api/payments] Connection released back to pool');
359
+ res.status(201).json({ message: 'Payment recorded and installment added' });
360
+ });
361
  });
362
  });
363
  });