From 6e45ce6cf9718b3c9819c77221015b85d3912da0 Mon Sep 17 00:00:00 2001 From: Andreas Knuth Date: Thu, 2 Apr 2026 12:07:39 -0500 Subject: [PATCH] better logging --- src/index.js | 8 ++++++++ src/routes/qbo.js | 20 +++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 965b4a8..bdadbe9 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,14 @@ * Quote & Invoice System - Main Entry Point * Modularized Backend */ +// ── Global timestamp logger – must be first line before any require ── +const _ts = () => new Date().toISOString().replace('T', ' ').substring(0, 19); +const _origLog = console.log.bind(console); +const _origWarn = console.warn.bind(console); +const _origError = console.error.bind(console); +console.log = (...a) => _origLog(`[${_ts()}]`, ...a); +console.warn = (...a) => _origWarn(`[${_ts()}]`, ...a); +console.error = (...a) => _origError(`[${_ts()}]`, ...a); const express = require('express'); const path = require('path'); const puppeteer = require('puppeteer'); diff --git a/src/routes/qbo.js b/src/routes/qbo.js index da6fe71..2a1e1f1 100644 --- a/src/routes/qbo.js +++ b/src/routes/qbo.js @@ -409,8 +409,6 @@ router.post('/record-payment', async (req, res) => { } }); -// Timestamp helper - add once at the top of qbo.js after the requires -const log = (msg) => console.log(`[${new Date().toISOString().replace('T',' ').substring(0,19)}] ${msg}`); // POST sync payments from QBO router.post('/sync-payments', async (req, res) => { @@ -449,7 +447,7 @@ router.post('/sync-payments', async (req, res) => { (data.QueryResponse?.Invoice || []).forEach(inv => qboInvoices.set(inv.Id, inv)); } - log(`🔍 QBO Sync: ${openInvoices.length} invoices checked, ${qboInvoices.size} loaded from QBO`); + console.log(`🔍 QBO Sync: ${openInvoices.length} invoices checked, ${qboInvoices.size} loaded from QBO`); // ── Collect all unique Payment IDs that need to be fetched ──────── // Instead of fetching each payment one-by-one, collect all IDs first @@ -470,7 +468,7 @@ router.post('/sync-payments', async (req, res) => { const paymentDepositMap = new Map(); // paymentId -> isDeposited (bool) if (paymentIdsToFetch.size > 0) { - log(`💳 Fetching ${paymentIdsToFetch.size} unique payment(s) from QBO...`); + console.log(`💳 Fetching ${paymentIdsToFetch.size} unique payment(s) from QBO...`); const pmIds = [...paymentIdsToFetch]; const pmBatchSize = 30; for (let i = 0; i < pmIds.length; i += pmBatchSize) { @@ -487,10 +485,10 @@ router.post('/sync-payments', async (req, res) => { paymentDepositMap.set(pm.Id, isDeposited); } } catch (e) { - log(`⚠️ Payment batch fetch error (non-fatal): ${e.message}`); + console.log(`⚠️ Payment batch fetch error (non-fatal): ${e.message}`); } } - log(`💳 Payment deposit status loaded for ${paymentDepositMap.size} payment(s)`); + console.log(`💳 Payment deposit status loaded for ${paymentDepositMap.size} payment(s)`); } // ── Process invoices ─────────────────────────────────────────────── @@ -529,7 +527,7 @@ router.post('/sync-payments', async (req, res) => { [status, localInv.id] ); updated++; - log(` ✅ #${localInv.invoice_number}: ${status}`); + console.log(` ✅ #${localInv.invoice_number}: ${status}`); } const diff = qboTotal - localPaid; @@ -547,7 +545,7 @@ router.post('/sync-payments', async (req, res) => { [payResult.rows[0].id, localInv.id, diff] ); newPayments++; - log(` 💰 #${localInv.invoice_number}: +$${diff.toFixed(2)} synced`); + console.log(` 💰 #${localInv.invoice_number}: +$${diff.toFixed(2)} synced`); } } else if (qboBalance > 0 && qboBalance < qboTotal) { @@ -576,7 +574,7 @@ router.post('/sync-payments', async (req, res) => { [payResult.rows[0].id, localInv.id, diff] ); newPayments++; - log(` 📎 #${localInv.invoice_number}: Partial +$${diff.toFixed(2)} ($${qboPaid.toFixed(2)} / $${qboTotal.toFixed(2)})`); + console.log(` 📎 #${localInv.invoice_number}: Partial +$${diff.toFixed(2)} ($${qboPaid.toFixed(2)} / $${qboTotal.toFixed(2)})`); } } } @@ -588,7 +586,7 @@ router.post('/sync-payments', async (req, res) => { await dbClient.query('COMMIT'); - log(`✅ Sync complete: ${updated} updated, ${newPayments} new payments`); + console.log(`✅ Sync complete: ${updated} updated, ${newPayments} new payments`); res.json({ synced: updated, new_payments: newPayments, @@ -598,7 +596,7 @@ router.post('/sync-payments', async (req, res) => { } catch (error) { await dbClient.query('ROLLBACK').catch(() => {}); - log(`❌ Sync Error: ${error.message}`); + console.log(`❌ Sync Error: ${error.message}`); res.status(500).json({ error: 'Sync failed: ' + error.message }); } finally { dbClient.release();