Fix sale discount reset when updating filament quantities

Preserve sale fields when updating filament basic data
This commit is contained in:
DaX
2025-09-30 11:58:45 +02:00
parent 92b186b6bc
commit 01a24e781b

View File

@@ -161,7 +161,14 @@ app.put('/api/filaments/:id', authenticateToken, async (req, res) => {
const spulnaNum = parseInt(spulna) || 0; const spulnaNum = parseInt(spulna) || 0;
const kolicina = refillNum + spulnaNum; const kolicina = refillNum + spulnaNum;
const result = await pool.query( // Check if sale fields are provided in the request
const hasSaleFields = 'sale_percentage' in req.body || 'sale_active' in req.body ||
'sale_start_date' in req.body || 'sale_end_date' in req.body;
let result;
if (hasSaleFields) {
// Update with sale fields if they are provided
result = await pool.query(
`UPDATE filaments `UPDATE filaments
SET tip = $1, finish = $2, boja = $3, boja_hex = $4, SET tip = $1, finish = $2, boja = $3, boja_hex = $4,
refill = $5, spulna = $6, kolicina = $7, cena = $8, refill = $5, spulna = $6, kolicina = $7, cena = $8,
@@ -172,6 +179,18 @@ app.put('/api/filaments/:id', authenticateToken, async (req, res) => {
[tip, finish, boja, boja_hex, refillNum, spulnaNum, kolicina, cena, [tip, finish, boja, boja_hex, refillNum, spulnaNum, kolicina, cena,
sale_percentage || 0, sale_active || false, sale_start_date, sale_end_date, id] sale_percentage || 0, sale_active || false, sale_start_date, sale_end_date, id]
); );
} else {
// Update without touching sale fields if they are not provided
result = await pool.query(
`UPDATE filaments
SET tip = $1, finish = $2, boja = $3, boja_hex = $4,
refill = $5, spulna = $6, kolicina = $7, cena = $8,
updated_at = CURRENT_TIMESTAMP
WHERE id = $9 RETURNING *`,
[tip, finish, boja, boja_hex, refillNum, spulnaNum, kolicina, cena, id]
);
}
res.json(result.rows[0]); res.json(result.rows[0]);
} catch (error) { } catch (error) {
console.error('Error updating filament:', error); console.error('Error updating filament:', error);