Add ui for csv update and import (broken) + fix build issues
This commit is contained in:
96
inventory-server/src/routes/csv.js
Normal file
96
inventory-server/src/routes/csv.js
Normal file
@@ -0,0 +1,96 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { spawn } = require('child_process');
|
||||
const path = require('path');
|
||||
|
||||
// Debug middleware MUST be first
|
||||
router.use((req, res, next) => {
|
||||
console.log(`[CSV Route Debug] ${req.method} ${req.path}`);
|
||||
next();
|
||||
});
|
||||
|
||||
// Store active import process and its progress
|
||||
let activeImport = null;
|
||||
let importProgress = null;
|
||||
|
||||
// SSE clients for progress updates
|
||||
const clients = new Set();
|
||||
|
||||
// Helper to send progress to all connected clients
|
||||
function sendProgressToClients(progress) {
|
||||
clients.forEach(client => {
|
||||
client.write(`data: ${JSON.stringify(progress)}\n\n`);
|
||||
});
|
||||
}
|
||||
|
||||
// Debug endpoint to verify route registration
|
||||
router.get('/test', (req, res) => {
|
||||
console.log('CSV test endpoint hit');
|
||||
res.json({ message: 'CSV routes are working' });
|
||||
});
|
||||
|
||||
// Route to check import status
|
||||
router.get('/status', (req, res) => {
|
||||
console.log('CSV status endpoint hit');
|
||||
res.json({
|
||||
active: !!activeImport,
|
||||
progress: importProgress
|
||||
});
|
||||
});
|
||||
|
||||
// Route to update CSV files
|
||||
router.post('/update', async (req, res) => {
|
||||
console.log('CSV update endpoint hit');
|
||||
|
||||
if (activeImport) {
|
||||
console.log('Import already in progress');
|
||||
return res.status(409).json({ error: 'Import already in progress' });
|
||||
}
|
||||
|
||||
try {
|
||||
const scriptPath = path.join(__dirname, '..', '..', 'scripts', 'update-csv.js');
|
||||
console.log('Running script:', scriptPath);
|
||||
|
||||
if (!require('fs').existsSync(scriptPath)) {
|
||||
console.error('Script not found:', scriptPath);
|
||||
return res.status(500).json({ error: 'Update script not found' });
|
||||
}
|
||||
|
||||
activeImport = spawn('node', [scriptPath]);
|
||||
|
||||
activeImport.stdout.on('data', (data) => {
|
||||
console.log(`CSV Update: ${data}`);
|
||||
importProgress = data.toString();
|
||||
sendProgressToClients({ status: 'running', progress: importProgress });
|
||||
});
|
||||
|
||||
activeImport.stderr.on('data', (data) => {
|
||||
console.error(`CSV Update Error: ${data}`);
|
||||
sendProgressToClients({ status: 'error', error: data.toString() });
|
||||
});
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
activeImport.on('close', (code) => {
|
||||
console.log(`CSV update process exited with code ${code}`);
|
||||
if (code === 0) {
|
||||
sendProgressToClients({ status: 'complete' });
|
||||
resolve();
|
||||
} else {
|
||||
sendProgressToClients({ status: 'error', error: `Process exited with code ${code}` });
|
||||
reject(new Error(`Update process exited with code ${code}`));
|
||||
}
|
||||
activeImport = null;
|
||||
importProgress = null;
|
||||
});
|
||||
});
|
||||
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error updating CSV files:', error);
|
||||
activeImport = null;
|
||||
importProgress = null;
|
||||
res.status(500).json({ error: 'Failed to update CSV files', details: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user