const express = require('express'); const { authenticateAPIKey, verifyToken } = require('../auth'); const { addProductionOrderDefect, createProductionOrders, importTinyProductCompositionExport, listProductionOrders, listProductionPlans, listProductionWorkstations, updateProductionOrderExecution, updateProductionOrderStatus, updateProductionPlan, updateProductionPlanStatus, 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 : []; const lotReservations = Array.isArray(req.body?.lotReservations) ? req.body.lotReservations : []; res.status(201).json(await createProductionOrders(orders, lotReservations, req.body?.plan || null)); } catch (error) { console.error('Error creating production orders:', error); res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); router.get('/production-plans', verifyToken, async (req, res) => { try { res.json(await listProductionPlans()); } catch (error) { console.error('Error fetching production plans:', error); res.status(500).json({ error: 'Internal Server Error' }); } }); router.get('/production-workstations', verifyToken, async (req, res) => { try { res.json(await listProductionWorkstations()); } catch (error) { res.status(500).json({ error: 'Internal Server Error' }); } }); router.put('/production-plans/:id', verifyToken, async (req, res) => { try { res.json(await updateProductionPlan(req.params.id, req.body || {})); } catch (error) { console.error('Error updating production plan:', error); res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); router.patch('/production-plans/:id/status', verifyToken, async (req, res) => { try { res.json(await updateProductionPlanStatus(req.params.id, req.body?.status)); } catch (error) { console.error('Error updating production plan status:', 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' }); } }); router.patch('/production-orders/:id/execution', verifyToken, async (req, res) => { try { res.json(await updateProductionOrderExecution(req.params.id, req.body || {})); } catch (error) { console.error('Error updating production order execution:', error); res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); router.post('/production-orders/:id/defects', verifyToken, async (req, res) => { try { res.status(201).json(await addProductionOrderDefect(req.params.id, req.body || {})); } catch (error) { res.status(error.statusCode || 500).json({ error: error.message || 'Internal Server Error' }); } }); module.exports = router;