diff --git a/inventory-server/scripts/full-reset.js b/inventory-server/scripts/full-reset.js new file mode 100644 index 0000000..96d0439 --- /dev/null +++ b/inventory-server/scripts/full-reset.js @@ -0,0 +1,96 @@ +const path = require('path'); +const { spawn } = require('child_process'); + +function outputProgress(data) { + if (!data.status) { + data = { + status: 'running', + ...data + }; + } + console.log(JSON.stringify(data)); +} + +function runScript(scriptPath) { + return new Promise((resolve, reject) => { + const child = spawn('node', [scriptPath], { + stdio: ['inherit', 'pipe', 'pipe'] + }); + + let output = ''; + + child.stdout.on('data', (data) => { + const lines = data.toString().split('\n'); + lines.filter(line => line.trim()).forEach(line => { + try { + console.log(line); // Pass through the JSON output + output += line + '\n'; + } catch (e) { + console.log(line); // If not JSON, just log it directly + } + }); + }); + + child.stderr.on('data', (data) => { + console.error(data.toString()); + }); + + child.on('close', (code) => { + if (code !== 0) { + reject(new Error(`Script ${scriptPath} exited with code ${code}`)); + } else { + resolve(output); + } + }); + + child.on('error', (err) => { + reject(err); + }); + }); +} + +async function fullReset() { + try { + // Step 1: Reset Database + outputProgress({ + operation: 'Starting full reset', + message: 'Step 1/3: Resetting database...' + }); + await runScript(path.join(__dirname, 'reset-db.js')); + + // Step 2: Import from Production + outputProgress({ + operation: 'Database reset complete', + message: 'Step 2/3: Importing from production...' + }); + await runScript(path.join(__dirname, 'import-from-prod.js')); + + // Step 3: Calculate Metrics + outputProgress({ + operation: 'Import complete', + message: 'Step 3/3: Calculating metrics...' + }); + await runScript(path.join(__dirname, 'calculate-metrics.js')); + + outputProgress({ + status: 'complete', + operation: 'Full reset complete', + message: 'Successfully completed database reset, import, and metrics calculation' + }); + } catch (error) { + outputProgress({ + status: 'error', + operation: 'Full reset failed', + error: error.message, + stack: error.stack + }); + process.exit(1); + } +} + +// Run if called directly +if (require.main === module) { + fullReset(); +} + +module.exports = fullReset; \ No newline at end of file diff --git a/inventory-server/scripts/full-update.js b/inventory-server/scripts/full-update.js new file mode 100644 index 0000000..1707deb --- /dev/null +++ b/inventory-server/scripts/full-update.js @@ -0,0 +1,89 @@ +const path = require('path'); +const { spawn } = require('child_process'); + +function outputProgress(data) { + if (!data.status) { + data = { + status: 'running', + ...data + }; + } + console.log(JSON.stringify(data)); +} + +function runScript(scriptPath) { + return new Promise((resolve, reject) => { + const child = spawn('node', [scriptPath], { + stdio: ['inherit', 'pipe', 'pipe'] + }); + + let output = ''; + + child.stdout.on('data', (data) => { + const lines = data.toString().split('\n'); + lines.filter(line => line.trim()).forEach(line => { + try { + console.log(line); // Pass through the JSON output + output += line + '\n'; + } catch (e) { + console.log(line); // If not JSON, just log it directly + } + }); + }); + + child.stderr.on('data', (data) => { + console.error(data.toString()); + }); + + child.on('close', (code) => { + if (code !== 0) { + reject(new Error(`Script ${scriptPath} exited with code ${code}`)); + } else { + resolve(output); + } + }); + + child.on('error', (err) => { + reject(err); + }); + }); +} + +async function fullUpdate() { + try { + // Step 1: Import from Production + outputProgress({ + operation: 'Starting full update', + message: 'Step 1/2: Importing from production...' + }); + await runScript(path.join(__dirname, 'import-from-prod.js')); + + // Step 2: Calculate Metrics + outputProgress({ + operation: 'Import complete', + message: 'Step 2/2: Calculating metrics...' + }); + await runScript(path.join(__dirname, 'calculate-metrics.js')); + + outputProgress({ + status: 'complete', + operation: 'Full update complete', + message: 'Successfully completed import and metrics calculation' + }); + } catch (error) { + outputProgress({ + status: 'error', + operation: 'Full update failed', + error: error.message, + stack: error.stack + }); + process.exit(1); + } +} + +// Run if called directly +if (require.main === module) { + fullUpdate(); +} + +module.exports = fullUpdate; \ No newline at end of file diff --git a/inventory-server/scripts/reset-db.js b/inventory-server/scripts/reset-db.js index 0e79013..b7bea1a 100644 --- a/inventory-server/scripts/reset-db.js +++ b/inventory-server/scripts/reset-db.js @@ -543,5 +543,15 @@ async function resetDatabase() { } } -// Run the reset -resetDatabase(); \ No newline at end of file +// Export if required as a module +if (typeof module !== 'undefined' && module.exports) { + module.exports = resetDatabase; +} + +// Run if called directly +if (require.main === module) { + resetDatabase().catch(error => { + console.error('Error:', error); + process.exit(1); + }); +} \ No newline at end of file