feat: Add INCLUDE_CATEGORY_IDS environment variable and logic to promote specified categories to root level.
This commit is contained in:
@@ -13,4 +13,5 @@ JTL_STEUERZONE_ID=1
|
||||
SERVER_PORT=3991
|
||||
SERVER_HOST=127.0.0.1
|
||||
SYNC_INTERVAL_MS=600000
|
||||
EXCLUDE_CATEGORY_IDS=
|
||||
EXCLUDE_CATEGORY_IDS=
|
||||
INCLUDE_CATEGORY_IDS=
|
||||
@@ -15,6 +15,19 @@ export function parseExcludedIds() {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse included category IDs from environment variable
|
||||
* @returns {Set<number>} - Set of included category IDs
|
||||
*/
|
||||
export function parseIncludedIds() {
|
||||
return new Set(
|
||||
(process.env.INCLUDE_CATEGORY_IDS || '')
|
||||
.split(',')
|
||||
.map(id => parseInt(id.trim()))
|
||||
.filter(id => !isNaN(id))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hierarchical category tree from flat data
|
||||
* @param {Array} categories - Category records with kKategorie, kOberKategorie, nSort
|
||||
@@ -51,6 +64,7 @@ export function buildTree(categories, names, articleCounts, images, applyRootFil
|
||||
|
||||
const rootNodes = [];
|
||||
const excludedIds = parseExcludedIds();
|
||||
const includedIds = parseIncludedIds();
|
||||
|
||||
// Build hierarchy
|
||||
categories.forEach(cat => {
|
||||
@@ -81,6 +95,23 @@ export function buildTree(categories, names, articleCounts, images, applyRootFil
|
||||
}
|
||||
}
|
||||
|
||||
// Add included categories to the result nodes (promoting them to root level)
|
||||
includedIds.forEach(id => {
|
||||
// Skip if excluded (exclusion takes precedence)
|
||||
if (excludedIds.has(id)) return;
|
||||
|
||||
const node = categoryMap.get(id);
|
||||
if (node) {
|
||||
// Remove subcategories for included categories as requested
|
||||
node.children = [];
|
||||
|
||||
// Avoid duplicates if the node is already in the result list
|
||||
if (!resultNodes.includes(node)) {
|
||||
resultNodes.push(node);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Sort children and remove nSort
|
||||
for (const node of categoryMap.values()) {
|
||||
node.children.sort((a, b) => a.nSort - b.nSort || a.kKategorie - b.kKategorie);
|
||||
|
||||
Reference in New Issue
Block a user