This commit is contained in:
Andreas Knuth 2026-01-21 20:55:29 -06:00
parent 711ed1e3cf
commit c8522af331
1 changed files with 26 additions and 7 deletions

View File

@ -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) {