Add sale management feature for admin panel
- Add database migration for sale fields (percentage, active, dates) - Update API to handle sale operations and bulk updates - Create SaleManager component for admin interface - Update FilamentTableV2 to display sale prices on frontend - Add sale column in admin dashboard - Implement sale price calculations with strikethrough styling
This commit is contained in:
@@ -153,7 +153,7 @@ 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 } = req.body;
|
||||
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
|
||||
@@ -165,9 +165,12 @@ app.put('/api/filaments/:id', authenticateToken, async (req, res) => {
|
||||
`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 = $9 RETURNING *`,
|
||||
[tip, finish, boja, boja_hex, refillNum, spulnaNum, kolicina, cena, id]
|
||||
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]
|
||||
);
|
||||
res.json(result.rows[0]);
|
||||
} catch (error) {
|
||||
@@ -188,6 +191,51 @@ app.delete('/api/filaments/:id', authenticateToken, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Bulk sale update endpoint
|
||||
app.post('/api/filaments/sale/bulk', authenticateToken, async (req, res) => {
|
||||
const { filamentIds, salePercentage, saleStartDate, saleEndDate, enableSale } = req.body;
|
||||
|
||||
try {
|
||||
let query;
|
||||
let params;
|
||||
|
||||
if (filamentIds && filamentIds.length > 0) {
|
||||
// Update specific filaments
|
||||
query = `
|
||||
UPDATE filaments
|
||||
SET sale_percentage = $1,
|
||||
sale_active = $2,
|
||||
sale_start_date = $3,
|
||||
sale_end_date = $4,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ANY($5)
|
||||
RETURNING *`;
|
||||
params = [salePercentage || 0, enableSale || false, saleStartDate, saleEndDate, filamentIds];
|
||||
} else {
|
||||
// Update all filaments
|
||||
query = `
|
||||
UPDATE filaments
|
||||
SET sale_percentage = $1,
|
||||
sale_active = $2,
|
||||
sale_start_date = $3,
|
||||
sale_end_date = $4,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
RETURNING *`;
|
||||
params = [salePercentage || 0, enableSale || false, saleStartDate, saleEndDate];
|
||||
}
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
res.json({
|
||||
success: true,
|
||||
updatedCount: result.rowCount,
|
||||
updatedFilaments: result.rows
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error updating sale:', error);
|
||||
res.status(500).json({ error: 'Failed to update sale' });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user