Fix sale discount reset when updating filament quantities
Preserve sale fields when updating filament basic data
This commit is contained in:
@@ -154,24 +154,43 @@ app.post('/api/filaments', authenticateToken, async (req, res) => {
|
||||
app.put('/api/filaments/:id', authenticateToken, async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { tip, finish, boja, boja_hex, refill, spulna, cena, sale_percentage, sale_active, sale_start_date, sale_end_date } = req.body;
|
||||
|
||||
|
||||
try {
|
||||
// Ensure refill and spulna are numbers
|
||||
const refillNum = parseInt(refill) || 0;
|
||||
const spulnaNum = parseInt(spulna) || 0;
|
||||
const kolicina = refillNum + spulnaNum;
|
||||
|
||||
const result = await pool.query(
|
||||
`UPDATE filaments
|
||||
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]
|
||||
);
|
||||
|
||||
// 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
|
||||
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]);
|
||||
} catch (error) {
|
||||
console.error('Error updating filament:', error);
|
||||
|
||||
Reference in New Issue
Block a user