From c8522af33160520decc8fbb97862dabd3f9c9214 Mon Sep 17 00:00:00 2001 From: Andreas Knuth Date: Wed, 21 Jan 2026 20:55:29 -0600 Subject: [PATCH] sdf --- server.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index b2d6552..1cc0782 100644 --- a/server.js +++ b/server.js @@ -283,6 +283,7 @@ app.put('/api/quotes/:id', async (req, res) => { const client = await pool.connect(); try { await client.query('BEGIN'); + console.log('Transaction started'); const { customer_id, quote_date, tax_exempt, items, tbd_note } = req.body; @@ -303,7 +304,10 @@ app.put('/api/quotes/:id', async (req, res) => { const tax_amount = tax_exempt ? 0 : (subtotal * tax_rate / 100); const total = subtotal + tax_amount; + console.log('Calculated totals:', { subtotal, tax_amount, total, has_tbd }); + // Update quote + console.log('Updating quote with id:', req.params.id); const quoteResult = await client.query( `UPDATE quotes SET customer_id = $1, quote_date = $2, tax_exempt = $3, @@ -315,15 +319,20 @@ app.put('/api/quotes/:id', async (req, res) => { subtotal, tax_amount, total, has_tbd, tbd_note, req.params.id] ); + console.log('Quote updated, rows affected:', quoteResult.rows.length); + if (quoteResult.rows.length === 0) { await client.query('ROLLBACK'); + console.log('Quote not found, rolling back'); return res.status(404).json({ error: 'Quote not found' }); } // Delete old items + console.log('Deleting old items'); await client.query('DELETE FROM quote_items WHERE quote_id = $1', [req.params.id]); // Insert new items + console.log('Inserting', items.length, 'new items'); for (let i = 0; i < items.length; i++) { const item = items[i]; await client.query( @@ -334,12 +343,14 @@ app.put('/api/quotes/:id', async (req, res) => { ); } + console.log('Committing transaction'); await client.query('COMMIT'); + console.log('PUT request successful, sending response'); res.json(quoteResult.rows[0]); } catch (err) { await client.query('ROLLBACK'); - console.error(err); - res.status(500).json({ error: 'Database error' }); + console.error('PUT Error:', err); + res.status(500).json({ error: 'Database error: ' + err.message }); } finally { client.release(); } @@ -454,11 +465,12 @@ app.post('/api/quotes/:id/pdf', async (req, res) => { format: 'Letter', printBackground: true, preferCSSPageSize: false, + displayHeaderFooter: false, margin: { - top: '0.4in', - right: '0.4in', - bottom: '0.4in', - left: '0.4in' + top: '0.5in', + right: '0.5in', + bottom: '0.5in', + left: '0.5in' } }); @@ -466,11 +478,18 @@ app.post('/api/quotes/:id/pdf', async (req, res) => { browser = null; console.log('PDF generated successfully, size:', pdf.length, 'bytes'); + console.log('PDF first bytes:', pdf.slice(0, 20).toString('hex')); + + // Verify PDF header + const pdfHeader = pdf.slice(0, 4).toString(); + if (pdfHeader !== '%PDF') { + throw new Error('Generated file is not a valid PDF (missing %PDF header)'); + } res.setHeader('Content-Type', 'application/pdf'); res.setHeader('Content-Length', pdf.length); res.setHeader('Content-Disposition', `attachment; filename="Quote_${quote.quote_number}.pdf"`); - res.send(pdf); + res.end(pdf, 'binary'); } catch (err) { console.error('PDF Generation Error:', err); if (browser) {