Dateien nach "backend/src/tools/bestellungen" hochladen
This commit is contained in:
@@ -4,7 +4,6 @@ const { authenticate } = require('../../middleware/auth');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const uid = req => req.user.id;
|
const uid = req => req.user.id;
|
||||||
|
|
||||||
// Hilfsfunktion: Bestellung mit Items laden
|
|
||||||
const loadOrder = (id, userId) => {
|
const loadOrder = (id, userId) => {
|
||||||
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(id, userId);
|
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(id, userId);
|
||||||
if (!order) return null;
|
if (!order) return null;
|
||||||
@@ -12,11 +11,13 @@ const loadOrder = (id, userId) => {
|
|||||||
return order;
|
return order;
|
||||||
};
|
};
|
||||||
|
|
||||||
// GET /api/tools/bestellungen – alle Bestellungen
|
// GET / – alle Bestellungen mit Zähler + Summen
|
||||||
router.get('/', authenticate, (req, res) => {
|
router.get('/', authenticate, (req, res) => {
|
||||||
const orders = db.prepare(`
|
const orders = db.prepare(`
|
||||||
SELECT o.*,
|
SELECT o.*,
|
||||||
COUNT(oi.id) as item_count
|
COUNT(oi.id) AS item_count,
|
||||||
|
SUM(CASE WHEN oi.status='fertig' THEN 1 ELSE 0 END) AS items_done,
|
||||||
|
SUM(COALESCE(oi.custom_price,0) * oi.stueckzahl) AS custom_price_sum
|
||||||
FROM orders o
|
FROM orders o
|
||||||
LEFT JOIN order_items oi ON oi.order_id = o.id
|
LEFT JOIN order_items oi ON oi.order_id = o.id
|
||||||
WHERE o.user_id = ?
|
WHERE o.user_id = ?
|
||||||
@@ -26,71 +27,72 @@ router.get('/', authenticate, (req, res) => {
|
|||||||
res.json(orders);
|
res.json(orders);
|
||||||
});
|
});
|
||||||
|
|
||||||
// GET /api/tools/bestellungen/:id – einzelne Bestellung mit Items
|
// GET /:id
|
||||||
router.get('/:id', authenticate, (req, res) => {
|
router.get('/:id', authenticate, (req, res) => {
|
||||||
const order = loadOrder(req.params.id, uid(req));
|
const order = loadOrder(req.params.id, uid(req));
|
||||||
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
res.json(order);
|
res.json(order);
|
||||||
});
|
});
|
||||||
|
|
||||||
// POST /api/tools/bestellungen – neue Bestellung
|
// POST / – neue Bestellung
|
||||||
router.post('/', authenticate, (req, res) => {
|
router.post('/', authenticate, (req, res) => {
|
||||||
const { name, bemerkung = '', status = 'warteliste', custom_price = null } = req.body;
|
const { name, bemerkung='', status='warteliste', custom_price=null } = req.body;
|
||||||
if (!name?.trim()) return res.status(400).json({ error: 'Name erforderlich' });
|
if (!name?.trim()) return res.status(400).json({ error: 'Name erforderlich' });
|
||||||
const r = db.prepare(`INSERT INTO orders (user_id,name,bemerkung,status,custom_price) VALUES (?,?,?,?,?)`)
|
const r = db.prepare('INSERT INTO orders (user_id,name,bemerkung,status,custom_price) VALUES (?,?,?,?,?)')
|
||||||
.run(uid(req), name.trim(), bemerkung, status, custom_price);
|
.run(uid(req), name.trim(), bemerkung, status, custom_price);
|
||||||
res.json(loadOrder(r.lastInsertRowid, uid(req)));
|
res.json(loadOrder(r.lastInsertRowid, uid(req)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// PUT /api/tools/bestellungen/:id – Bestellung aktualisieren
|
// PUT /:id
|
||||||
router.put('/:id', authenticate, (req, res) => {
|
router.put('/:id', authenticate, (req, res) => {
|
||||||
const ex = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
const ex = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
||||||
if (!ex) return res.status(404).json({ error: 'Nicht gefunden' });
|
if (!ex) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
const { name=ex.name, bemerkung=ex.bemerkung, status=ex.status, custom_price=ex.custom_price } = req.body;
|
const { name=ex.name, bemerkung=ex.bemerkung, status=ex.status, custom_price=ex.custom_price } = req.body;
|
||||||
db.prepare(`UPDATE orders SET name=?,bemerkung=?,status=?,custom_price=?,updated_at=CURRENT_TIMESTAMP WHERE id=? AND user_id=?`)
|
db.prepare('UPDATE orders SET name=?,bemerkung=?,status=?,custom_price=?,updated_at=CURRENT_TIMESTAMP WHERE id=? AND user_id=?')
|
||||||
.run(name, bemerkung, status, custom_price, req.params.id, uid(req));
|
.run(name, bemerkung, status, custom_price, req.params.id, uid(req));
|
||||||
res.json(loadOrder(req.params.id, uid(req)));
|
res.json(loadOrder(req.params.id, uid(req)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// DELETE /api/tools/bestellungen/:id
|
// DELETE /:id
|
||||||
router.delete('/:id', authenticate, (req, res) => {
|
router.delete('/:id', authenticate, (req, res) => {
|
||||||
const r = db.prepare('DELETE FROM orders WHERE id=? AND user_id=?').run(req.params.id, uid(req));
|
const r = db.prepare('DELETE FROM orders WHERE id=? AND user_id=?').run(req.params.id, uid(req));
|
||||||
if (!r.changes) return res.status(404).json({ error: 'Nicht gefunden' });
|
if (!r.changes) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
// POST /api/tools/bestellungen/:id/items – Archiv-Eintrag hinzufügen
|
// POST /:id/items
|
||||||
router.post('/:id/items', authenticate, (req, res) => {
|
router.post('/:id/items', authenticate, (req, res) => {
|
||||||
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
||||||
if (!order) return res.status(404).json({ error: 'Bestellung nicht gefunden' });
|
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
|
const { calculation_id, stueckzahl=1, custom_price=null, status='warteliste' } = req.body;
|
||||||
const { calculation_id, stueckzahl = 1 } = req.body;
|
|
||||||
const calc = db.prepare('SELECT * FROM calculations WHERE id=? AND user_id=?').get(calculation_id, uid(req));
|
const calc = db.prepare('SELECT * FROM calculations WHERE id=? AND user_id=?').get(calculation_id, uid(req));
|
||||||
if (!calc) return res.status(404).json({ error: 'Archiv-Eintrag nicht gefunden' });
|
if (!calc) return res.status(404).json({ error: 'Archiv-Eintrag nicht gefunden' });
|
||||||
|
|
||||||
// Bereits vorhanden? → Stückzahl erhöhen
|
|
||||||
const existing = db.prepare('SELECT * FROM order_items WHERE order_id=? AND calculation_id=?').get(req.params.id, calculation_id);
|
const existing = db.prepare('SELECT * FROM order_items WHERE order_id=? AND calculation_id=?').get(req.params.id, calculation_id);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
db.prepare('UPDATE order_items SET stueckzahl=? WHERE id=?').run(existing.stueckzahl + stueckzahl, existing.id);
|
db.prepare('UPDATE order_items SET stueckzahl=? WHERE id=?').run(existing.stueckzahl + stueckzahl, existing.id);
|
||||||
} else {
|
} else {
|
||||||
db.prepare(`INSERT INTO order_items (order_id,calculation_id,calc_name,preis_freundschaft,preis_normal,preis_auftrag,stueckzahl)
|
db.prepare(`INSERT INTO order_items (order_id,calculation_id,calc_name,preis_freundschaft,preis_normal,preis_auftrag,stueckzahl,custom_price,status)
|
||||||
VALUES (?,?,?,?,?,?,?)`)
|
VALUES (?,?,?,?,?,?,?,?,?)`)
|
||||||
.run(req.params.id, calc.id, calc.name, calc.preis_freundschaft, calc.preis_normal, calc.preis_auftrag, stueckzahl);
|
.run(req.params.id, calc.id, calc.name, calc.preis_freundschaft, calc.preis_normal, calc.preis_auftrag, stueckzahl, custom_price, status);
|
||||||
}
|
}
|
||||||
res.json(loadOrder(req.params.id, uid(req)));
|
res.json(loadOrder(req.params.id, uid(req)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// PUT /api/tools/bestellungen/:id/items/:itemId – Stückzahl ändern
|
// PUT /:id/items/:itemId – Stückzahl, Preis und Status
|
||||||
router.put('/:id/items/:itemId', authenticate, (req, res) => {
|
router.put('/:id/items/:itemId', authenticate, (req, res) => {
|
||||||
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
||||||
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
const { stueckzahl } = req.body;
|
const item = db.prepare('SELECT * FROM order_items WHERE id=? AND order_id=?').get(req.params.itemId, req.params.id);
|
||||||
if (!stueckzahl || stueckzahl < 1) return res.status(400).json({ error: 'Ungültige Stückzahl' });
|
if (!item) return res.status(404).json({ error: 'Position nicht gefunden' });
|
||||||
db.prepare('UPDATE order_items SET stueckzahl=? WHERE id=? AND order_id=?').run(stueckzahl, req.params.itemId, req.params.id);
|
const stueckzahl = req.body.stueckzahl ?? item.stueckzahl;
|
||||||
|
const custom_price = req.body.custom_price !== undefined ? req.body.custom_price : item.custom_price;
|
||||||
|
const status = req.body.status ?? item.status;
|
||||||
|
db.prepare('UPDATE order_items SET stueckzahl=?,custom_price=?,status=? WHERE id=?')
|
||||||
|
.run(stueckzahl, custom_price, status, req.params.itemId);
|
||||||
res.json(loadOrder(req.params.id, uid(req)));
|
res.json(loadOrder(req.params.id, uid(req)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// DELETE /api/tools/bestellungen/:id/items/:itemId
|
// DELETE /:id/items/:itemId
|
||||||
router.delete('/:id/items/:itemId', authenticate, (req, res) => {
|
router.delete('/:id/items/:itemId', authenticate, (req, res) => {
|
||||||
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
const order = db.prepare('SELECT * FROM orders WHERE id=? AND user_id=?').get(req.params.id, uid(req));
|
||||||
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
if (!order) return res.status(404).json({ error: 'Nicht gefunden' });
|
||||||
|
|||||||
Reference in New Issue
Block a user