38 lines
1.3 KiB
JavaScript
38 lines
1.3 KiB
JavaScript
import { ScoringRules } from '../config/ScoringConfig.js';
|
|
|
|
/**
|
|
* RuleMerger ensures that we apply sector-specific overrides
|
|
* to base asset rules without polluting the individual Asset or Scorer logic.
|
|
*/
|
|
export const RuleMerger = {
|
|
getRulesForAsset(type, metrics) {
|
|
// 1. Start with a deep clone of the base rules for this asset type (STOCK, ETF, etc.)
|
|
const baseRules = ScoringRules[type];
|
|
if (!baseRules) throw new Error(`No configuration found for type: ${type}`);
|
|
|
|
let finalRules = JSON.parse(JSON.stringify(baseRules));
|
|
|
|
// 2. If it's a stock and we have a sector, merge the overrides
|
|
if (type === 'STOCK' && metrics.sector) {
|
|
const sectorKey = metrics.sector.toUpperCase();
|
|
const overrides = baseRules.SECTOR_OVERRIDE?.[sectorKey];
|
|
|
|
if (overrides) {
|
|
// Merge gates, weights, and thresholds deeply
|
|
finalRules.gates = { ...finalRules.gates, ...overrides.gates };
|
|
finalRules.weights = { ...finalRules.weights, ...overrides.weights };
|
|
finalRules.thresholds = {
|
|
...finalRules.thresholds,
|
|
...overrides.thresholds,
|
|
};
|
|
}
|
|
}
|
|
|
|
// 3. Cleanup: Remove the override configuration from the final object
|
|
// so the Scorer works with a clean, flat rule set.
|
|
delete finalRules.SECTOR_OVERRIDE;
|
|
|
|
return finalRules;
|
|
},
|
|
};
|