const express = require('express'); const { authenticateAPIKey, verifyToken } = require('../auth'); const { createProductionOrders, importTinyProductCompositionExport, listProductionOrders, updateProductionOrderStatus, upsertTinyProductionOrderDetail } = require('../services/productionOrderService'); const router = express.Router(); router.get('/production-orders', verifyToken, async (req, res) => { try { res.json(await listProductionOrders(req.query || {})); } catch (error) { console.error('Error fetching production orders:', error); res.status(500).json({ error: 'Internal Server Error' }); } }); router.post('/production-orders', verifyToken, async (req, res) => { try { const orders = Array.isArray(req.body?.orders) ? req.body.orders : []; res.status(201).json(await createProductionOrders(orders)); } catch (error) { console.error('Error creating production orders:', error); res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); router.post('/production-orders/tiny-sync', authenticateAPIKey, async (req, res) => { try { res.status(201).json(await upsertTinyProductionOrderDetail(req.body || {})); } catch (error) { console.error('Error syncing Tiny production order:', error); res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); router.post('/production-orders/tiny-compositions/import', authenticateAPIKey, async (req, res) => { try { res.status(201).json(await importTinyProductCompositionExport(req.body || {})); } catch (error) { console.error('Error importing Tiny product compositions:', error); res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); router.patch('/production-orders/:id/status', verifyToken, async (req, res) => { try { res.json(await updateProductionOrderStatus(req.params.id, req.body?.status)); } catch (error) { console.error('Error updating production order status:', error); res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); module.exports = router;