Initial commit: ZiRA Classification Tool for Zuyderland CMDB
This commit is contained in:
121
backend/src/routes/configuration.ts
Normal file
121
backend/src/routes/configuration.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { readFile, writeFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { logger } from '../services/logger.js';
|
||||
import { clearEffortCalculationConfigCache, getEffortCalculationConfigV25 } from '../services/effortCalculation.js';
|
||||
import type { EffortCalculationConfig, EffortCalculationConfigV25 } from '../config/effortCalculation.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// Path to the configuration files
|
||||
const CONFIG_FILE_PATH = join(__dirname, '../../data/effort-calculation-config.json');
|
||||
const CONFIG_FILE_PATH_V25 = join(__dirname, '../../data/effort-calculation-config-v25.json');
|
||||
|
||||
/**
|
||||
* Get the current effort calculation configuration (legacy)
|
||||
*/
|
||||
router.get('/effort-calculation', async (req: Request, res: Response) => {
|
||||
try {
|
||||
// Try to read from JSON file, fallback to default config
|
||||
try {
|
||||
const fileContent = await readFile(CONFIG_FILE_PATH, 'utf-8');
|
||||
const config = JSON.parse(fileContent) as EffortCalculationConfig;
|
||||
res.json(config);
|
||||
} catch (fileError) {
|
||||
// If file doesn't exist, return default config from code
|
||||
const { EFFORT_CALCULATION_CONFIG } = await import('../config/effortCalculation.js');
|
||||
res.json(EFFORT_CALCULATION_CONFIG);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Failed to get effort calculation configuration', error);
|
||||
res.status(500).json({ error: 'Failed to get configuration' });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Update the effort calculation configuration (legacy)
|
||||
*/
|
||||
router.put('/effort-calculation', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const config = req.body as EffortCalculationConfig;
|
||||
|
||||
// Validate the configuration structure
|
||||
if (!config.governanceModelRules || !Array.isArray(config.governanceModelRules)) {
|
||||
res.status(400).json({ error: 'Invalid configuration: governanceModelRules must be an array' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.default || typeof config.default.result !== 'number') {
|
||||
res.status(400).json({ error: 'Invalid configuration: default.result must be a number' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Write to JSON file
|
||||
await writeFile(CONFIG_FILE_PATH, JSON.stringify(config, null, 2), 'utf-8');
|
||||
|
||||
// Clear the cache so the new config is loaded on next request
|
||||
clearEffortCalculationConfigCache();
|
||||
|
||||
logger.info('Effort calculation configuration updated');
|
||||
res.json({ success: true, message: 'Configuration saved successfully' });
|
||||
} catch (error) {
|
||||
logger.error('Failed to update effort calculation configuration', error);
|
||||
res.status(500).json({ error: 'Failed to save configuration' });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Get the v25 effort calculation configuration
|
||||
*/
|
||||
router.get('/effort-calculation-v25', async (req: Request, res: Response) => {
|
||||
try {
|
||||
// Try to read from JSON file, fallback to default config
|
||||
try {
|
||||
const fileContent = await readFile(CONFIG_FILE_PATH_V25, 'utf-8');
|
||||
const config = JSON.parse(fileContent) as EffortCalculationConfigV25;
|
||||
res.json(config);
|
||||
} catch (fileError) {
|
||||
// If file doesn't exist, return default config from code
|
||||
const config = getEffortCalculationConfigV25();
|
||||
res.json(config);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Failed to get effort calculation configuration v25', error);
|
||||
res.status(500).json({ error: 'Failed to get configuration' });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Update the v25 effort calculation configuration
|
||||
*/
|
||||
router.put('/effort-calculation-v25', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const config = req.body as EffortCalculationConfigV25;
|
||||
|
||||
// Validate the configuration structure
|
||||
if (!config.regiemodellen || typeof config.regiemodellen !== 'object') {
|
||||
res.status(400).json({ error: 'Invalid configuration: regiemodellen must be an object' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.validationRules || typeof config.validationRules !== 'object') {
|
||||
res.status(400).json({ error: 'Invalid configuration: validationRules must be an object' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Write to JSON file
|
||||
await writeFile(CONFIG_FILE_PATH_V25, JSON.stringify(config, null, 2), 'utf-8');
|
||||
|
||||
// Clear the cache so the new config is loaded on next request
|
||||
clearEffortCalculationConfigCache();
|
||||
|
||||
logger.info('Effort calculation configuration v25 updated');
|
||||
res.json({ success: true, message: 'Configuration v25 saved successfully' });
|
||||
} catch (error) {
|
||||
logger.error('Failed to update effort calculation configuration v25', error);
|
||||
res.status(500).json({ error: 'Failed to save configuration' });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user