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,17 +161,36 @@ 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
`UPDATE filaments const hasSaleFields = 'sale_percentage' in req.body || 'sale_active' in req.body ||
SET tip = $1, finish = $2, boja = $3, boja_hex = $4, 'sale_start_date' in req.body || 'sale_end_date' in req.body;
refill = $5, spulna = $6, kolicina = $7, cena = $8,
sale_percentage = $9, sale_active = $10, let result;
sale_start_date = $11, sale_end_date = $12, if (hasSaleFields) {
updated_at = CURRENT_TIMESTAMP // Update with sale fields if they are provided
WHERE id = $13 RETURNING *`, result = await pool.query(
[tip, finish, boja, boja_hex, refillNum, spulnaNum, kolicina, cena, `UPDATE filaments
sale_percentage || 0, sale_active || false, sale_start_date, sale_end_date, id] SET tip = $1, finish = $2, boja = $3, boja_hex = $4,
); refill = $5, spulna = $6, kolicina = $7, cena = $8,
sale_percentage = $9, sale_active = $10,
sale_start_date = $11, sale_end_date = $12,
updated_at = CURRENT_TIMESTAMP
WHERE id = $13 RETURNING *`,
[tip, finish, boja, boja_hex, refillNum, spulnaNum, kolicina, cena,
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);